• Using 'file' variable in automation scripting

  • OpenKM has many interesting features, but requires some configuration process to show its full potential.
OpenKM has many interesting features, but requires some configuration process to show its full potential.
Forum rules: Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
 #48328  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 all
import 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();
}
 #48347  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 )
 #48352  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)
 #48353  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
 #48408  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 )

About Us

OpenKM is part of the management software. A management software is a program that facilitates the accomplishment of administrative tasks. OpenKM is a document management system that allows you to manage business content and workflow in a more efficient way. Document managers guarantee data protection by establishing information security for business content.