Page 1 of 1

Troubles renaming document whit autoincrement id and metadata

Posted: Thu Mar 16, 2017 4:57 am
by really.neo
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

Re: Troubles renaming document whit autoincrement id and metadata

Posted: Fri Mar 17, 2017 8:30 am
by jllort
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

Re: Troubles renaming document whit autoincrement id and metadata

Posted: Mon Mar 20, 2017 4:56 pm
by really.neo
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.

Re: Troubles renaming document whit autoincrement id and metadata

Posted: Tue Mar 21, 2017 7:17 pm
by jllort
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") {

Re: Troubles renaming document whit autoincrement id and metadata

Posted: Mon May 15, 2017 3:33 am
by jujustine21
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?

Re: Troubles renaming document whit autoincrement id and metadata

Posted: Mon May 15, 2017 7:50 pm
by jllort
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.