Scripting - OpenKM 6.2

From OpenKM Documentation
Revision as of 00:05, 21 February 2015 by Jllort (talk | contribs) (Cron tab exporter)

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().getChildren(token, "/okm:trash")) {
    print("Trash: " + trash.getPath()+"<br/>");
 
    for (Folder fld : OKMFolder.getInstance().getChildren(token, trash.getPath())) {
        print("About to delete folder: " + fld.getPath() + "<br/>");
        OKMFolder.getInstance().purge(token, fld.getPath());
    }
 
    for (Document doc : OKMDocument.getInstance().getChildren(token, trash.getPath())) {
        print("About to delete document: " + doc.getPath() + "<br/>");
        
        if (OKMDocument.getInstance().isLocked(token, doc.getPath())) {
            OKMDocument.getInstance().forceUnlock(token, doc.getPath());
        }
        
        OKMDocument.getInstance().purge(token, doc.getPath());
    }
 
    for (Mail mail : OKMMail.getInstance().getChildren(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());
        }
    }
}

Purge all folders into some parent

That script purge all folder into some parent, without previously moving files to trash. The purge action is a non recovery action what remove definitivelly the objets from OpenKM repository. Use it with care.

import org.hibernate.*;
import com.openkm.dao.*;
import com.openkm.dao.bean.NodeFolder;
import java.util.*;
import com.openkm.api.OKMFolder;

Session session = null;
found = true;	
try {
    String sql = "from NodeFolder nf where nf.parent='aa08b392-0117-4b29-af9a-c4419ba6cab6'";
    while (found) {
        session = HibernateUtil.getSessionFactory().openSession();
        Query q = session.createQuery(sql);
        q.setMaxResults(100);
        found=false;
        List fldList = new ArrayList();
        for (NodeFolder fld : q.list()) {
            found= true;
            fldList.add(fld.getUuid());
        }
        for (String uuid : fldList) {
            try {
	        OKMFolder.getInstance().purge(null, uuid);
            } catch (Exception e) {
                print(e.getMessage()+"<br/>");
            }
        }
    }    
} catch (Exception e) {
    print(e.getMessage());
} finally {
	HibernateUtil.close(session); 
}

Count all children subfolder

Count all children folders - and subfolders into, recursion - into first level folder ( okm:root children folders ).

import com.openkm.api.*;
import com.openkm.bean.*;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.openkm.dao.HibernateUtil;
import org.hibernate.HibernateException;
import org.hibernate.Query;

class Util {
    public int childCount(String uuid)  {
        String qsUUIDList = "select uuid from NodeFolder nf where nf.parent=:parent";
        Session session = null;
	 	Transaction tx = null;
        int value = 0;
        List uuidList = new ArrayList();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();
            Query q = session.createQuery(qsUUIDList).setCacheable(true);
            q.setString("parent", uuid);
            uuidList = q.list();
            HibernateUtil.commit(tx);
            value = uuidList.size();
            print(value);
        } catch (HibernateException e) {
            HibernateUtil.rollback(tx);
        } finally {
            HibernateUtil.close(session);
        }
        
        for (String childUuid : uuidList) {
            value = value + childCount(childUuid);
        }
        return value;
    }
}

Util util = new Util();
for (Folder fld : OKMFolder.getInstance().getChildren(null, "/okm:root")) {
    print(fld.getPath()+"->"+util.childCount(fld.getUuid())+"<br/>");
}

Recursive character renaming

Rename a character in all folders or documents from some initial path.

// Recursive renaming & to -
import com.openkm.api.*;
import com.openkm.bean.*;

void findNodePathHelper(String path) {
    for (Folder fld : OKMFolder.getInstance().getChilds(null,path)) {
	//print(fld.getPath().substring(fld.getPath().lastIndexOf("/")).replaceAll("&","-"));
	if(fld.getPath().contains("&")) {
		OKMFolder.getInstance().rename(null, fld.getPath(), fld.getPath().substring(fld.getPath().lastIndexOf("/")).replaceAll("&","-"));
	}
	for (Document doc : OKMDocument.getInstance().getChilds(null, fld.getPath())) {
		//print(doc.getPath().substring(doc.getPath().lastIndexOf("/")).replaceAll("&","-"));
		if(doc.getPath().contains("&")) {
			OKMDocument.getInstance().rename(null, doc.getPath(), doc.getPath().substring(doc.getPath().lastIndexOf("/")).replaceAll("&","-"));
		}
	}
	findNodePathHelper(fld.getPath());
    }
}

findNodePathHelper("/okm:root");

Cron tab exporter

Export execution based on crontab task

import com.openkm.util.FileLogger;
import com.openkm.bean.ContentInfo;
import com.openkm.api.*;
import com.openkm.util.impexp.*;
import java.io.*;
import java.text.*;
import com.openkm.core.Config;
import java.util.*;

// Configuration parameters
String LOG_FILE_NAME = "CrontabRepositoryExporter";
String repoPath = "/okm:root";
String fsPath = "F:\\Restore_OKM";
boolean metadata = false;
boolean history = false;

// Start
FileWriter fw = null;
try {
    FileLogger.info(LOG_FILE_NAME, "Started");
    // Open outputstream
    String fileDate = new SimpleDateFormat("yyyyMMdd").format(new Date());
    String fileName = Config.LOG_DIR + File.separator + LOG_FILE_NAME + "_" + fileDate + ".log";
    File file = new File(fileName);
    
    fw = new FileWriter(file);
    // Starting the process
    ContentInfo cInfo = OKMFolder.getInstance().getContentInfo(null, repoPath);
    FileLogger.info(LOG_FILE_NAME, "Files & directories to export:"+(cInfo.getDocuments() + cInfo.getFolders()));
    File dir = new File(fsPath);
    ImpExpStats stats = RepositoryExporter.exportFolder(null, repoPath, dir, metadata, history, fw, new HTMLInfoDecorator((int) cInfo.getDocuments() + (int) cInfo.getFolders()));
    FileLogger.info("CrontabRepositoryExporter", "Finished");
} catch (Exception e) {
    if (fw!=null) {
	try {
		fw.close();
	} catch (IOException e1) {
    }
}
}