Script to get document name

He we will discuss about how to make customization and improvement to the OpenKM source code.
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.
Post Reply
jorseng
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: Tue Jan 16, 2018 2:22 pm

Script to get document name

Post by jorseng » Wed May 23, 2018 2:03 pm

Hi,

I am trying to append the file name based on the metadata value.
The code below is meant to be run in the automation action "execute script"

Code: Select all

import com.openkm.api.OKMPropertyGroup;
import com.openkm.api.OKMRepository;
import com.openkm.api.OKMDocument;
import java.util.*;
import com.openkm.bean.form.*;
import com.openkm.bean.Document;

String grpName = "okg:medicalrecord";
String path = OKMRepository.getInstance().getNodePath(null,uuid);

boolean merge;

for (FormElement formElement : OKMPropertyGroup.getInstance().getProperties(null,path,grpName)) {
	if (formElement.getName().equals("okp:medicalrecord.merge")) {
		merge = formElement.getValue();
			
			if (merge==true) {
				docName = OKMDocument.getInstance().getProperties(null,path).getTitle();
				String newName = docName + " Merge!";
				OKMDocument.getInstance().rename(null,path,newName);
				
				//search for file with the same name, mrn,
				//then merge pdf, inherit metadata
				//condition to check - both have no metadata, one has, both have
			}
			
	}
}

However looking through the API i cannot seem to find the right method to get the file name and append the name with a "Merge!" at the end of the file name.

lnovoa
Senior Boarder
Senior Boarder
Posts: 59
Joined: Tue Mar 27, 2018 8:00 am

Re: Script to get document name

Post by lnovoa » Wed May 23, 2018 3:03 pm

Hi!,
you have to add this line to get the name of the path

String name = path.substring(path.lastIndexOf('/') + 1);

For example:
path: /okm:root/documentName.pdf
The result be: documentName.pdf

jllort
Moderator
Moderator
Posts: 10037
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: Script to get document name

Post by jllort » Thu May 24, 2018 7:34 am

Also exists a class PathUtils with method getName.

jorseng
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: Tue Jan 16, 2018 2:22 pm

Re: Script to get document name

Post by jorseng » Fri May 25, 2018 3:28 am

Dear jllort,

Thanks for your prompt reply! I mange to get it working, however each time the script renames it, i get the error message below.
error message.PNG
Here is my workflow configuration:
automation prompt.PNG
automation prompt.PNG (6.73 KiB) Viewed 343 times
Here is the action:
automation action.png
Anyway i can avoid getting the error message each time the script runs?

lnovoa
Senior Boarder
Senior Boarder
Posts: 59
Joined: Tue Mar 27, 2018 8:00 am

Re: Script to get document name

Post by lnovoa » Fri May 25, 2018 8:56 am

The file is renaming it without the extension.
The PathUtils and FileUtils classes contain methods to obtain the extension.

jllort
Moderator
Moderator
Posts: 10037
Joined: Fri Dec 21, 2007 11:23 am
Location: Sineu - ( Illes Balears ) - Spain
Contact:

Re: Script to get document name

Post by jllort » Sat May 26, 2018 7:34 am

I think it is caused by OpenKM bug, basically you apply metadata change to document XXXX.pdf and come back as YYYY.pdf, but the UI is trying to refresh YYYY.pdf. In the UI should evaluate if the document name has been changed before trying to refresh previous path ( what has been changed ). This was an old bug from professional version what I suspect has not been corrected in community.

Add an issue here https://github.com/openkm/document-mana ... tem/issues

jorseng
Fresh Boarder
Fresh Boarder
Posts: 15
Joined: Tue Jan 16, 2018 2:22 pm

Re: Script to get document name

Post by jorseng » Tue Jun 05, 2018 2:17 am

Dear Inovoa,
I have tried adding extension to the renaming, the error remained.

Dear jllort,
I will post the issue at the given link.

Thank you all!

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests