Script para automation - Establecer un nombre de documento único basado en metadatos
Cada vez que se sube un nuevo documento, el script establece un nombre de documento único basado en valores de metadatos.
Definición del grupo de metadatos:
<?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>Definición de la tabla de base de datos:
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');El 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();}Registrar la regla de automation
Sección titulada «Registrar la regla de automation»Cree una tarea de automation basada en el evento de creación del grupo de metadatos:
- Name: AutoRename
- Event: Add metadata group
- At: Pre
Use el script anterior en la acción ExecuteScripting.