History of metadata changes sample
Description
This sample is based on Automation feature. In deep there's a plugin named SaveMetadataHistory what is listening "set metadata group" events ( what are raised each time any node into the whole repository is changed ). The SaveMetadataHistory plugin retrieves the metadata values and save them into another table to preseve them on time.
Installation steps
- Download the file SaveMetadataHistory.jar and copy into your $TOMCAT_HOME/plugins
- Restart OpenKM application.
- Go to Administration > Tools > Plugins and check if the plugin com.openkm.automation.action.SaveMetadataHistory is listed under com.openkm.automation.Action plugins.
- Go to Administration > Automation and create a new rule:
- Choose event "set metadata group".
- Choose at "post".
- Set the name for example "Save metadata history".
- Set the order value to 0.
- Set checkbox exclusive disabled.
- Enable checkbox active.
- Click on "Create button".
- Click on the "Definition" ( the filter icon ) to set the rule definition.
- Choose "SaveMetadataHistory" from add Action list of values.
- Click on "Add" icon on the right. Will be shown a table with SaveMetadataHistory fields.
- Enable checkbox active.
- Click on "Create button".
- Go to Administration > Tools > Database Query and execute the query to create the table:
SQL Script below is for MySQL database engine might be in another database engine should be modified.
CREATE TABLE OKM_NODE_PROPERTY_VERSION (
NPG_ID bigint(20) NOT NULL AUTO_INCREMENT,
NPG_UUID varchar(64) COLLATE utf8_bin NOT NULL,
NPG_GROUP varchar(64) COLLATE utf8_bin NOT NULL,
NPG_NAME varchar(64) COLLATE utf8_bin NOT NULL,
NPG_VALUE varchar(20000) COLLATE utf8_bin DEFAULT NULL,
NPG_VERSION varchar(64) COLLATE utf8_bin NOT NULL,
NPG_AUTHOR varchar(64) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (NPG_ID),
UNIQUE KEY NPG_NODE (NPG_UUID,NPG_GROUP,NPG_NAME, NPG_VERSION)
);
To ensure everything is working, change some metadata and check if have been added some rows into the "OKM_NODE_PROPERTY_VERSION" table.
JAVA code
In the Creating your own Automation Action section you will find more information about how you can extend the Automation plugins features.
package com.openkm.automation.action;
import java.text.SimpleDateFormat;
import java.util.Map;
import java.util.Map.Entry;
import net.xeoh.plugins.base.annotations.PluginImplementation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import com.ibm.icu.util.Calendar;
import com.openkm.automation.Action;
import com.openkm.automation.AutomationUtils;
import com.openkm.dao.LegacyDAO;
import com.openkm.dao.bean.Automation;
import com.openkm.spring.PrincipalUtils;
/**
* SaveMetadataHistory
*/
@PluginImplementation
public class SaveMetadataHistory implements Action {
private static Logger log = LoggerFactory.getLogger(SaveMetadataHistory.class);
@Override
public void executePre(Map<String, Object> env, Object... params) throws Exception {
}
@Override
@SuppressWarnings("unchecked")
public void executePost(Map<String, Object> env, Object... params) throws Exception {
String uuid = AutomationUtils.getUuid(env);
if (env.containsKey(AutomationUtils.PROPERTY_GROUP_NAME) && env.containsKey(AutomationUtils.PROPERTY_GROUP_PROPERTIES)) {
String userId = PrincipalUtils.getUser();
String grpName = (String) env.get(AutomationUtils.PROPERTY_GROUP_NAME);
Map<String, String> nodProps = (Map<String, String>) env.get(AutomationUtils.PROPERTY_GROUP_PROPERTIES);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SS");
String version = sdf.format(Calendar.getInstance().getTime());
String sql = "INSERT INTO OKM_NODE_PROPERTY_VERSION (NPG_UUID, NPG_GROUP, NPG_NAME, NPG_VALUE, NPG_VERSION, NPG_AUTHOR) VALUES (?, ?, ?, ?, ?, ?)";
for (Entry<String, String> prop : nodProps.entrySet()) {
//long npgId = DatabaseMetadataDAO.getNextSequenceValue("OKM_NODE_PROPERTY_VERSION", "NPG_ID");
//LegacyDAO.executeSQL(sql, npgId, uuid, grpName, prop.getKey(), prop.getValue(), version, userId);
LegacyDAO.executeSQL(sql, uuid, grpName, prop.getKey(), prop.getValue(), version, userId);
}
}
}
@Override
public boolean hasPost() {
return true;
}
@Override
public boolean hasPre() {
return false;
}
@Override
public String getName() {
return "SaveMetadataHistory";
}
@Override
public String getParamType00() {
return Automation.PARAM_TYPE_EMPTY;
}
@Override
public String getParamSrc00() {
return Automation.PARAM_SOURCE_EMPTY;
}
@Override
public String getParamDesc00() {
return "";
}
@Override
public String getParamType01() {
return Automation.PARAM_TYPE_EMPTY;
}
@Override
public String getParamSrc01() {
return Automation.PARAM_SOURCE_EMPTY;
}
@Override
public String getParamDesc01() {
return "";
}
@Override
public String getParamType02() {
return Automation.PARAM_TYPE_EMPTY;
}
@Override
public String getParamSrc02() {
return Automation.PARAM_SOURCE_EMPTY;
}
@Override
public String getParamDesc02() {
return "";
}
}