Difference between revisions of "Scripting - OpenKM 6.2"

From OpenKM Documentation
Jump to: navigation, search
(Created page with '{{TOCright}} __TOC__ Scripting was an advanced feature introduced in OpenKM 5.0 that enables administrators to execute some BeanShell scripts in folders, fired on every notified…')
m (Purge all users trash)
Line 17: Line 17:
import com.openkm.core.*;
import com.openkm.core.*;
import com.openkm.bean.*;
import com.openkm.bean.*;
import com.openkm.module.db.stuff.*;
String token = DbSessionManager.getInstance().getSystemToken();
String token = DbSessionManager.getInstance().getSystemToken();

Revision as of 17:28, 13 March 2013

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.


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) {
} finally {

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");

Refresh user items

import com.openkm.cache.*;


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");

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();

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

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


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 *****");