Troubles renaming document whit autoincrement id and metadata

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.
Post Reply
really.neo
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: Thu Mar 16, 2017 4:32 am

Troubles renaming document whit autoincrement id and metadata

Post by really.neo » Thu Mar 16, 2017 4:57 am

Hi, I am user of OPEN KM Community Version 6.3.3 build: 8289.

I have troubles renaming the document, I am using this automate rules and script, based on your excellent wiki information, and It's actually working adding metadata whit form the wizard, and id autoincrement, but the rename action is not working :(.

property group registration:

Code: Select all

</property-group>
    
     <property-group label="Datos" name="okg:data">
		<input label="Id" type="text" name="okp:data.id" width="200px" readonly="true"/>
		<input label="Oficio No" type="text" name="okp:data.project.code" width="200px">
			<validator type="req"/>
			<validator type="maxlen" parameter="10"/>
			<validator type="minlen" parameter="3"/>
		</input>
		<input label="Asunto" type="text" name="okp:data.description" width="200px">
			<validator type="req"/>
			<validator type="maxlen" parameter="150"/>
		</input>
  </property-group>

Screenshot:
imagegroup.jpg
Automation information:

document creation and set property group
documentcreation,setpropertygroup.jpg
Document creation, validation and actions

ExecuteScripting info:

Code: Select all

import com.openkm.api.OKMPropertyGroup;
import com.openkm.api.OKMRepository;
import java.util.*;
import com.openkm.dao.DatabaseMetadataDAO;
 
String grpName = "okg:data";
String table = "autonumber";
String sequenceName = "doc_id";
String path = OKMRepository.getInstance().getNodePath(null,uuid);
 
// Add Group
OKMPropertyGroup.getInstance().addGroup(null, path, grpName);

String id = String.valueOf(DatabaseMetadataDAO.getNextSequenceValue(table, sequenceName));
 
switch (id.length()) {
    case 1:
    	id = "00000" + id;
    	break;
    case 2:
    	id = "0000" + id;
    	break;
    case 3:
    	id = "000" + id;
    	break;
    case 4:
    	id = "00" + id;
    	break;
    case 5:
    	id = "0" + id;
    	break;
    }

    Map map = new HashMap();
    map.put("okp:data.id",id);
    OKMPropertyGroup.getInstance().setPropertiesSimple(null, path, grpName, map);
}
documentcreationvalidationactions.jpg

Set property group, validation and actions
setpropertygroupvalidationactions.jpg
ExecuteScripting info:

Code: Select all

import com.openkm.api.OKMPropertyGroup;
import com.openkm.api.OKMRepository;
import java.util.*;
import com.openkm.dao.DatabaseMetadataDAO;
import com.openkm.api.OKMPropertyGroup;
import com.openkm.bean.form.FormElement;
import com.openkm.bean.form.Input;
import com.openkm.bean.form.Select;
import com.openkm.bean.form.Option;
import com.openkm.util.PathUtils;
import com.openkm.util.FileUtils;
import com.openkm.api.OKMDocument;
 
String grpName = "okg:data";
String table = "autonumber";
String sequenceName = "doc_id";
String path = OKMRepository.getInstance().getNodePath(null,uuid);
 
// Evaluate if already has property group
boolean add = true;
String prjCode = "";
String id = "";
String desc = "";
for (FormElement formElement : OKMPropertyGroup.getInstance().getProperties(null, path, grpName)) {
    if (formElement.getName().equals("okp:data.id")) {
        docId = ((Input) formElement).getValue();
        add = id.equals("");
    } else if (formElement.getName().equals("okp:data.project.code")) {
        prjCode = ((Input) formElement).getValue();
    } else if (formElement.getName().equals("okp:data.description")) {
        desc = ((Input) formElement).getValue();
            }
        }  
 
// Setting properties
if (add) {
    OKMPropertyGroup.getInstance().setPropertiesSimple(null, path, grpName, map);
}
 
// rename document
String newName = id + "-" + prjCode + "-" + desc + "." + FileUtils.getFileExtension(PathUtils.getName(path));
OKMDocument.getInstance().rename(null, path, newName);

Then, here is the example working, auto increment Id, and setting metadata whit wizard form. but the rename script is not working, thank you for your help.
workingmetadataidautonotrenaming.jpg

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

Re: Troubles renaming document whit autoincrement id and metadata

Post by jllort » Fri Mar 17, 2017 8:30 am

The second script I think is wrong.

You shold validate if the document what has fired the event setProperties has the property group:

Code: Select all

if (OKMProperty.getInstance().hasGroup(null, "okg:XXXX") {
  // Here execute the rename
}
I suggest using classes rather scripting, because you are not able to debug scripting but is quite easy debug action classes. I suggest take a look here: https://docs.openkm.com/kcenter/view/ok ... ction.html

really.neo
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: Thu Mar 16, 2017 4:32 am

Re: Troubles renaming document whit autoincrement id and metadata

Post by really.neo » Mon Mar 20, 2017 4:56 pm

jllort wrote:The second script I think is wrong.

You shold validate if the document what has fired the event setProperties has the property group:

Code: Select all

if (OKMProperty.getInstance().hasGroup(null, "okg:XXXX") {
  // Here execute the rename
}
I suggest using classes rather scripting, because you are not able to debug scripting but is quite easy debug action classes. I suggest take a look here: https://docs.openkm.com/kcenter/view/ok ... ction.html
Thank you very much for your answer, I modified the metadata wizard form and the script like this, the metadata set is working right, but the rename is not working yet. It's like this:

Code: Select all

import com.openkm.api.OKMPropertyGroup;
import com.openkm.api.OKMRepository;
import java.util.*;
import com.openkm.dao.DatabaseMetadataDAO;
import com.openkm.api.OKMPropertyGroup;
import com.openkm.bean.form.FormElement;
import com.openkm.bean.form.Input;
import com.openkm.bean.form.Select;
import com.openkm.bean.form.Option;
import com.openkm.util.PathUtils;
import com.openkm.util.FileUtils;
import com.openkm.api.OKMDocument;
 
String grpName = "okg:data";
String table = "autonumber";
String sequenceName = "doc_id";
String path = OKMRepository.getInstance().getNodePath(null,uuid);
 
// Evaluate if already has property group
boolean add = true;
String prjCode = "";
String id = "";
String desc = "";
String LetraID = "";
String year = "";
for (FormElement formElement : OKMPropertyGroup.getInstance().getProperties(null, path, grpName)) {
    if (formElement.getName().equals("okp:data.id")) {
        docId = ((Input) formElement).getValue();
        add = id.equals("");
    } else if (formElement.getName().equals("okp:data.project.code")) {
        prjCode = ((Input) formElement).getValue();
    } else if (formElement.getName().equals("okp:data.description")) {
        desc = ((Input) formElement).getValue();
    } else if (formElement.getName().equals("okp:data.letra")) {
        LetraID = ((Input) formElement).getValue();
    }  else if (formElement.getName().equals("okp:data.year")) {
        year = ((Input) formElement).getValue();
    }
}
 
// Setting properties
if (add) {
    OKMPropertyGroup.getInstance().setPropertiesSimple(null, path, grpName, map);
}
 
// rename document
if (OKMProperty.getInstance().hasGroup(null, "okg:data") {
String newName = id + "-" + LetraID + "-" + year + "-" + prjCode + "-" + desc + "." + FileUtils.getFileExtension(PathUtils.getName(path));
OKMDocument.getInstance().rename(null, path, newName);
}
Adding at the end:

Code: Select all

if (OKMProperty.getInstance().hasGroup(null, "okg:data") {
String newName = id + "-" + LetraID + "-" + year + "-" + prjCode + "-" + desc + "." + FileUtils.getFileExtension(PathUtils.getName(path));
OKMDocument.getInstance().rename(null, path, newName);
}
foroejemplorenme.png
The file upload right but still dont rename, I dont know anything else to try, Thank your for your help.

OPEN KM Community Version 6.3.3 build: 8289.

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

Re: Troubles renaming document whit autoincrement id and metadata

Post by jllort » Tue Mar 21, 2017 7:17 pm

This method is wrong

Code: Select all

if (OKMProperty.getInstance().hasGroup(null, "okg:data") {
There's missing the uuid of the document

Code: Select all

if (OKMProperty.getInstance().hasGroup(null, uuid, "okg:data") {

jujustine21
Fresh Boarder
Fresh Boarder
Posts: 5
Joined: Mon May 15, 2017 3:21 am

Re: Troubles renaming document whit autoincrement id and metadata

Post by jujustine21 » Mon May 15, 2017 3:33 am

Hi sir, i'm newbie here, i just want to know how to get the group value of the doc.
Ex.
if I upload a file, then i set the metadata (okg.data = TRUE)
how to know what is the value of okg.data?
Is there method to do that?

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

Re: Troubles renaming document whit autoincrement id and metadata

Post by jllort » Mon May 15, 2017 7:50 pm

Please jujustine21, do not merge other questions rather than what initated this topic, it creates a lot of confusion in other users. Please add a new post for it. Thanks.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests