Page 1 of 1
Using 'file' variable in automation scripting
PostPosted:Wed Jul 03, 2019 9:48 am
by zaans2
I am trying to use the 'file' variable that should be available according to
https://docs.openkm.com/kcenter/view/ok ... Automation
However whenever I try to do anything with it I get null pointer exceptions. What am I doing wrong?
The following script is run on document creation (I have tried both pre and post and I get the same result)
Code: Select allimport java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
File file2 = (File) file;//cast from Object to File so I can call toPath method
BasicFileAttributes attr = null;
try {
attr = Files.readAttributes(file2.toPath(), BasicFileAttributes.class);//line where null pointer exception occurs on file2 variable
System.out.println("creationTime: " + attr.creationTime());
} catch (IOException e) {
e.printStackTrace();
}
Re: Using 'file' variable in automation scripting
PostPosted:Wed Jul 03, 2019 12:35 pm
by zaans2
Even when I have a really simple script it keeps saying `file` is null.
The following script also gives me an exception.
Code: Select allSystem.out.println(file.toString());
Re: Using 'file' variable in automation scripting
PostPosted:Sun Jul 07, 2019 11:03 am
by jllort
The file object is not added into all the events. Open the AutomationUtils.java class and find the usage of the variable DOCUMENT_FILE ( and methods related with it )
Re: Using 'file' variable in automation scripting
PostPosted:Mon Jul 08, 2019 9:07 am
by zaans2
I know it isn't added into all events. According to the webpage I linked to in the original post, it should be available upon document creation or update (at least that is what I make of it).
When looking at the AutomationUtils.java class the only method that uses DOCUMENT_FILE is getFile(Map<String, Object> env). When I look at the ExecuteScripting.java class (which I assume is what is used when executing a script, which is what I am trying to do) the getFile method mentioned earlier is used in the execute(Map<String, Object> env, Object... params) method. I assume this method is called whenever the script is executed. So looking at all of that I would assume it should be added into the script I am trying to execute from automation at a document creation event (tried both pre and post)
Re: Using 'file' variable in automation scripting
PostPosted:Mon Jul 08, 2019 10:09 am
by zaans2
I looked even deeper and eventually found where the event is fired. If you look at BaseDocumentModule.java lines 83-92 and lines 171-173 you can see the firing of the document creation events, both pre and post. In both cases the `file` variable isn't put into the `env` variable. The `env` variable is what is put through all the way and is the variable used by the getFile method to get the actual file. Since it is never set it will always be empty
Re: Using 'file' variable in automation scripting
PostPosted:Sat Jul 13, 2019 9:02 am
by jllort
In the document creation is not available:
https://github.com/openkm/document-mana ... e.java#L84
Either in the checkin
https://github.com/openkm/document-mana ... .java#L888
I think in community edition the file is only set in conversion stage ( to PDF ). However you can modify the source code and add the parameter ( might be you are interested in set inputstream and recover then inputstream either a file in these scenario )
Re: Using 'file' variable in automation scripting
PostPosted:Mon Jul 15, 2019 8:38 am
by zaans2
Understood. Was this done on purpose in the CE or is it something that slipped by?
In both cases the page on Automation Scripting in the OpenKM 6.3 - CE knowledge center is incorrect, see
https://docs.openkm.com/kcenter/view/ok ... Automation
Re: Using 'file' variable in automation scripting
PostPosted:Fri Jul 19, 2019 6:16 pm
by jllort
I have corrected the documentation, apologies for the inconvenience.