Scripting - OpenKM 6.2

From OpenKM Documentation
Revision as of 09:54, 5 September 2014 by Jllort (talk | contribs)

Jump to: navigation, search

Scripting was an advanced feature introduced in OpenKM 5.0 that enables administrators to execute some BeanShell scripts in folders, fired on every notified event ( for example uploading documents ). This feature has been overseed by Automation, which help you to do the same things with a few mouse clicks. For compatibility reasons there is also an Automation task where you can put a BeanShell code. This replaces the old way of enabling scripting.



Nota clasica.png OpenKM uses BeanShell. For more information point your browser to http://www.beanshell.org/intro.html.


BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.

You can also execute a custom script when OpenKM starts or stops. To do so, just create a start.bsh (and / or stop.bsh) file in $TOMCAT_HOME. For example, we use this feature to create a complete environment (create custom users, register property groups, register workflow) each time the OpenKM demo is restarted.

Scripting in automation view

In Automation Scripting execution, there're several variables what are directly injected in scripting environment what can be use.

List of variables what can be used directly

Variable Status Description
systemToken The system token
node Involved node (NodeDocument, NodeFolder, etc.. )
uuid Object unique identify
file Only available when uploading or updating a new document content.
userId The user id
nodeUuid deprecated Object unique identify
nodePath deprecated Object path

To test these variables simply can execute the script:

print(uuid);


Examples

Purge all users trash

import com.openkm.api.*;
import com.openkm.core.*;
import com.openkm.bean.*;
import com.openkm.module.db.stuff.*;

String token = DbSessionManager.getInstance().getSystemToken();

for (Folder trash : OKMFolder.getInstance().getChilds(token, "/okm:trash")) {
    print("Trash: " + trash.getPath()+"<br/>");
    
    for (Folder fld : OKMFolder.getInstance().getChilds(token, trash.getPath())) {
        print("About to delete folder: " + fld.getPath() + "<br/>");
        OKMFolder.getInstance().purge(token, fld.getPath());
    }
    
    for (Document doc : OKMDocument.getInstance().getChilds(token, trash.getPath())) {
        print("About to delete document: " + doc.getPath() + "<br/>");
        OKMDocument.getInstance().purge(token, doc.getPath());
    }
    
    for (Mail mail : OKMMail.getInstance().getChilds(token, trash.getPath())) {
        print("About to delete mail: " + mail.getPath() + "<br/>");
        OKMMail.getInstance().purge(token, mail.getPath());
    }
}

Update repository stats

import com.openkm.core.*;

new RepositoryInfo().run();

Import reports from a folder

Import reports from $TOMCAT_HOME/reports and also enable them in the default profile.

import java.io.*;
import java.sql.*;
import org.hibernate.*;
import com.openkm.core.*;
import com.openkm.util.*;
import com.openkm.dao.*;

Session hbmSession = HibernateUtil.getSessionFactory().openSession();
Connection con = hbmSession.connection();
Statement st = con.createStatement();
File reports = new File(Config.HOME_DIR + "/reports");

try {
    if (reports.isDirectory()) {
        for (File rep : reports.listFiles()) {
            int id = ReportDAO.createFromFile(rep, FileUtils.getFileName(rep.getName()), true);
            String sql = "insert into OKM_PROFILE_MSC_REPORT (PRP_ID, PRP_REPORT) values (1, " + id + ")";
            LegacyDAO.execute(con, sql);
        }
    }
} catch (Exception e) {
    e.printStackTrace();
} finally {
    LegacyDAO.close(con);
    HibernateUtil.close(hbmSession);
}

Show jBPM mail configuration

import org.jbpm.*;

print("Templates: " + JbpmConfiguration.Configs.getString("resource.mail.templates") + "<br/>");
print("From: " + JbpmConfiguration.Configs.getString("jbpm.mail.from.address") + "<br/>");
print("Host: " + JbpmConfiguration.Configs.getString("jbpm.mail.smtp.host") + "<br/>");

Reset user document size

import com.openkm.dao.bean.cache.*;
import com.openkm.cache.*;

UserItems ui = UserItemsManager.get("okmAdmin");
ui.setSize(0);

Refresh user items

import com.openkm.cache.*;

UserItemsManager.refreshDbUserItems();

Create some folders and set property group

import com.openkm.api.*;

for (int i=0; i < 10; i++) {
  String path = "/okm:root/fld_" + i;
  OKMFolder.getInstance().createSimple(null, path);
  OKMPropertyGroup.getInstance().addGroup(null, path, "okg:technology");
}

Show number of documents, folders and size from a given path

import com.openkm.bean.*;
import com.openkm.util.*;
import com.openkm.api.*;

ContentInfo ci = OKMFolder.getInstance().getContentInfo(null, "/okm:root/path/to/folder");
print("Folders: " + ci.getFolders() + "<br/>");
print("Documents: " + ci.getDocuments() + "<br/>");
print("Size: " + FormatUtil.formatSize(ci.getSize()) + "<br/>");

Get path by UUID

import com.openkm.api.OKMRepository;
String path = OKMRepository.getInstance().getNodePath(null, "b99f7973-4b90-457a-a2d0-cf2090ba995d");
print(path);

Compact documents with size=0

Find all documents with size=0 and compact history of versions to latest

import java.util.*;
import com.openkm.api.OKMDocument;
import com.openkm.api.OKMRepository;
import com.openkm.dao.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.Query;
import com.openkm.dao.bean.NodeDocumentVersion;

String qs = "from NodeDocumentVersion ndv where ndv.size=0";
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Query q = session.createQuery(qs);
List docVersionList = q.list();
HibernateUtil.commit(tx);

print("Number of nodes"+docVersionList.size()+"<br/>");

for (NodeDocumentVersion ndv : docVersionList) {
    String path = OKMRepository.getInstance().getNodePath(null,ndv.getParent());
    OKMDocument.getInstance().purgeVersionHistory(null, path);
}

HibernateUtil.close(session);
print("done");

Recursive repository traversal

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.openkm.api.*;
import com.openkm.bean.*;

Logger log = LoggerFactory.getLogger("com.openkm.scripting");
int MAX_DEPTH = Integer.MAX_VALUE;

void nodeTask(String path, int depth) throws Exception {
    for (Document doc : OKMDocument.getInstance().getChildren(null, path)) {
        log.info("Document: {}", doc.getPath());
    }
 
    for (Folder fld : OKMFolder.getInstance().getChildren(null, path)) {
        log.info("Folder: {}", fld.getPath());
        
        if (depth < MAX_DEPTH) {
            nodeTask(fld.getPath(), depth + 1);
        }
    }
}
 
log.info("***** Process BEGIN *****");
nodeTask("/okm:root", 0);
log.info("***** Process END *****");

Show all documents used in every running workflow

import com.openkm.module.common.CommonWorkflowModule;
import com.openkm.bean.workflow.ProcessDefinition;
import com.openkm.bean.workflow.ProcessInstance;
import com.openkm.core.Config;

for (ProcessDefinition procDef : CommonWorkflowModule.findAllProcessDefinitions()) {
    for (ProcessInstance procIns : CommonWorkflowModule.findProcessInstances(procDef.getId())) {
        if (procIns.getEnd() == null) {
            String uuid = (String) procIns.getVariables().get(Config.WORKFLOW_PROCESS_INSTANCE_VARIABLE_UUID);
            print("Doc UUID: " + uuid + ", Process Definition: " + procDef.getId() + ", Process Instance: " + procIns.getId());
        }
    }
}