Script for automation - Set unique document name based on metadata
Each time a new document is uploaded, the script sets a unique document name based in metadata values.
- Property okp:data.id stores a unique auto incremental value.
- Property okp:data.project.code stores a project code value.
- Property okp:data.customer stores a customer code.
- Property okp:data.description stores a document description.
- When a new document is uploaded the user must fill all the fields except okp:data.id which is automatically set by OpenKM.
- When Metadata group is changed - event triggered- is executed automation code which generates okp:data.id and renames the document based on mask projectCode-autonumericId-clientCode-description.documentExtension.
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="Data" name="okg:data">
<input label="Id" type="text" name="okp:data.id" readonly="true"/>
<input label="Project code" type="text" name="okp:data.project_code">
<validator type="req"/>
<validator type="num"/>
<validator type="maxlen" parameter="6"/>
<validator type="minlen" parameter="6"/>
</input>
<select label="Customer" name="okp:data.customer" type="simple"
optionsQuery="select CUS_ID, CUS_NAME from MY_CUSTUMER">
<validator type="req"/>
</select>
<input label="Description" type="text" name="okp:data.description">
<validator type="req"/>
<validator type="maxlen" parameter="150"/>
</input>
</property-group>
</property-groups>
Database table definition:
create table MY_CUSTUMER (
CUS_ID int not null auto_increment,
CUS_NAME varchar(128),
primary key(CUS_ID)
);
insert into MY_CUSTUMER(CUS_ID, CUS_NAME) values (1, 'Customer 1');
insert into MY_CUSTUMER(CUS_ID, CUS_NAME) values (2, 'Customer 2');
The script:
import com.openkm.db.service.*;
import com.openkm.db.bean.*;
import com.google.gson.*;
import com.openkm.api.*;
import com.openkm.util.*;
getNextValue() {
ConfigSrv cfgSrv = ContextWrapper.getContext().getBean(ConfigSrv.class);
Config cfg = cfgSrv.findByPk("autoincrement.data.id");
if (cfg == null) {
cfg = new Config();
cfg.setType(Config.INTEGER);
cfg.setKey("autoincrement.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 {
if ("okg:data".equals(env.get("propGroupName"))) {
// Get metadata
Map props = env.get("propGroupProperties");
String prjCode = props.get("okp:data.project_code");
String desc = props.get("okp:data.description");
String cust = props.get("okp:data.customer");
String id = props.get("okp:data.id");
String[] opts = new Gson().fromJson(cust, String[].class);
String clientCode = opts[0];
// Set metadata
if (id == null || id.isEmpty()) {
id = getNextValue();
props.put("okp:data.id", id);
env.put("propGroupProperties", props);
}
// Rename document
OKMRepository okmRepository = ContextWrapper.getContext().getBean(OKMRepository.class);
String path = okmRepository.getNodePath(null, uuid);
String docExt = FileUtils.getFileExtension(PathUtils.getName(path));
String newName = prjCode + "-" + id + "-" + clientCode + "-" + desc + "." + docExt;
print("New doc name: " + newName);
OKMDocument okmDocument = ContextWrapper.getContext().getBean(OKMDocument.class);
okmDocument.rename(null, uuid, newName);
}
} catch (Exception e) {
e.printStackTrace();
}
Register automation rule
Create automation task based on metadata group creation event:
- Name: AutoRename
- Event: Add metadata group
- At: Pre
Use the previous scripting in ExecuteScripting action.