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.

This is only a sample implementation and not intended to be used in production.

Metadata group definition:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE property-groups PUBLIC "-//OpenKM//DTD Property Groups 3.10//EN"
"http://www.openkm.com/dtd/property-groups-3.10.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.db.service.*;
import com.openkm.db.bean.*;
import com.openkm.api.*;
import com.openkm.util.*;

getNextValue() {
  ConfigSrv cfgSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
  Config cfg = cfgSrv.findByPk("autonumber.data.id");
  
  if (cfg == null) {
    cfg = new Config();
    cfg.setType(Config.INTEGER);
    cfg.setKey("autonumber.data.id");
    cfg.setValue("0");
  }
 
  int value = Integer.parseInt(cfg.getValue()) + 1;
  cfg.setValue(String.valueOf(value));
  cfgSrv.save(cfg);
  
  return String.format("%04d", value);
}

try {
  // Set metadata
  String id = getNextValue();
  String rev = "1";
  String code = id + "-" + rev;

  // Add metadata
  Map props = new HashMap();
  props.put("okp:autonumber.id",id);
  props.put("okp:autonumber.revision",rev);
  props.put("okp:autonumber.code",code);

  OKMPropertyGroup okmPropertyGroup = ContextWrapper.getContext().getBean(OKMPropertyGroup.class);
  okmPropertyGroup.addGroup(null, uuid, "okg:autonumber", props);
} catch (Exception e) {
  e.printStackTrace();
}

Register metadata group

Create automation task based on metadata group creation event:

  • Name: Autonumeric
  • Event: Document create
  • At: Post

 Use the previous scripting in ExecuteScripting action.

Table of contents [ Hide Show ]