Script for automation - Autonumeric

Creates an unique number for each new document.  

  • There's a metadata with tree fields which stores data.
  • Each time a new document is uploaded, automation is executed. Automation script creates a new unique number identifier using metadata database sequence feature.
  • The document id is a 6 digits incremental numeric
  • The revision number starts with 1
  • The code is a unique id + "-" + revision

To get it running it's needed to register a metadata group and then create and automation task based on scripting.

Metadata group definition:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE property-groups PUBLIC "-//OpenKM//DTD Property Groups 2.1//EN"
"http://www.openkm.com/dtd/property-groups-2.1.dtd">
<property-groups>
    <property-group label="Autonumeric" name="okg:autonumber">
        <input label="Document ID" name="okp:autonumber.id" type="text" readonly="true"/>
        <input label="Revision" name="okp:autonumber.revision" type="text" readonly="true"/>
        <input label="Code" name="okp:autonumber.code" type="text" readonly="true"/>
    </property-group>
</property-groups>

The automation script:

import com.openkm.api.OKMPropertyGroup;
import com.openkm.api.OKMRepository;
import java.util.*;
import com.openkm.dao.DatabaseMetadataDAO;
 
String grpName = "okg:autonumber";
String table = "autonumber";
String sequenceName = "doc_id";
String path = OKMRepository.getInstance().getNodePath(null,uuid);
 
// Add Group
OKMPropertyGroup.getInstance().addGroup(null, path, grpName);
 
// Setting properties
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;
}
 
String revision = "1";
String code = id + "-" +revision;
 
Map map = new HashMap();
map.put("okp:autonumber.id",id);
map.put("okp:autonumber.revision",revision);
map.put("okp:autonumber.code",code);
OKMPropertyGroup.getInstance().setPropertiesSimple(null, path, grpName, map);

Images

Register metadata group

Register automation rule

Result after uploading a new document