• Automated Repository Exporting

  • OpenKM has many interesting features, but requires some configuration process to show its full potential.
OpenKM has many interesting features, but requires some configuration process to show its full potential.
Forum rules: Please, before asking something see the documentation wiki or use the search feature of the forum. And remember we don't have a crystal ball or mental readers, so if you post about an issue tell us which OpenKM are you using and also the browser and operating system version. For more info read How to Report Bugs Effectively.
 #21922  by rodrigm21
 
Hi everyone,

I installed OpenKM last week and so far it seems to be the best solution for the project I'm working on. I needed a server side application that could let users modify a file structure (in this case the repository) which can then be simply exported to the server's file system for file backups and use with a website. On the website, I have a button that will need to export the files from the repository out to the file system as these files are used in the dynamic generation of the site's content. Is there a way that I can export the repository contents to the server's file system without needing to log in? I currently modified the repository_export.jsp file and brought it up one level hoping that it would bypass the need to log in, but it didn't. The source code is:
Code: Select all
// Automated Repository Export
<%@ page import="java.io.FileNotFoundException"%>
<%@ page import="java.io.IOException"%>
<%@ page import="java.io.File" %>
<%@ page import="com.openkm.core.Config" %>
<%@ page import="com.openkm.servlet.admin.BaseServlet" %>
<%@ page import="com.openkm.bean.ContentInfo" %>
<%@ page import="com.openkm.api.OKMFolder" %>
<%@ page import="com.openkm.util.WebUtils"%>
<%@ page import="com.openkm.util.FormatUtil" %>
<%@ page import="com.openkm.util.impexp.RepositoryExporter" %>
<%@ page import="com.openkm.util.impexp.HTMLInfoDecorator" %>
<%@ page import="com.openkm.util.impexp.ImpExpStats"%>
<%@ page import="com.openkm.bean.Repository"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link rel="Shortcut icon" href="favicon.ico" />
  <link rel="stylesheet" type="text/css" href="css/style.css" />
  <script type="text/javascript" src="../js/jquery-1.7.1.min.js"></script>
  <script type="text/javascript" src="js/jquery.DOMWindow.js"></script>
  <script type="text/javascript">
    $(document).ready(function() {
		$dm = $('.ds').openDOMWindow({
			height:200, width:300,
			eventType:'click',
			overlayOpacity:'57',
			windowSource:'iframe', windowPadding:0
		});
	});
    
    function dialogClose() {
		$dm.closeDOMWindow();
    }
  </script>
  <title>Repository Export</title>
</head>
<body>
<%
		request.setCharacterEncoding("UTF-8");
		String repoPath = "/okm:root";
		String fsPath = "/openkm";
		boolean metadata = false;
		boolean history = false;
		
		out.println("<ul id=\"breadcrumb\">");
		out.println("  <li class=\"path\"><a href=\"repo_export.jsp\">Repository export</a></li>");
		out.println("</ul>");
		out.println("<br/>");

		try {
			if (repoPath != null && !repoPath.equals("") && fsPath != null && !fsPath.equals("")) {
				out.println("<hr/>");
				
				if (fsPath.startsWith(Config.INSTANCE_CHROOT_PATH)) {
					File dir = new File(fsPath);
					ContentInfo cInfo = OKMFolder.getInstance().getContentInfo(null, repoPath);
					out.println("<b>Files & directories to export:</b> "+(cInfo.getDocuments() + cInfo.getFolders())+"<br/>");
					long begin = System.currentTimeMillis();
					ImpExpStats stats = RepositoryExporter.exportDocuments(null, repoPath, dir, metadata, history, out,
						new HTMLInfoDecorator((int) cInfo.getDocuments() + (int) cInfo.getFolders()));
					long end = System.currentTimeMillis();
					out.println("<hr/>");
					out.println("<div class=\"ok\">Folder '"+repoPath+"' exported to '"+new File(fsPath).getAbsolutePath()+"'</div>");
					out.println("<br/>");
					out.println("<b>Documents:</b> "+stats.getDocuments()+"<br/>");
					out.println("<b>Folders:</b> "+stats.getFolders()+"<br/>");
					out.println("<b>Size:</b> "+FormatUtil.formatSize(stats.getSize())+"<br/>");
					out.println("<b>Time:</b> "+FormatUtil.formatSeconds(end - begin)+"<br/>");
				} else {
					out.println("<div class=\"error\">Path out of root: "+Config.INSTANCE_CHROOT_PATH+"<div>");
				}
			}
		} catch (FileNotFoundException e) {
			out.println("<div class=\"error\">File Not Found: "+e.getMessage()+"<div>");
		} catch (IOException e) {
			out.println("<div class=\"error\">IO Error: "+e.getMessage()+"<div>");
		} catch (Exception e) {
			out.println("<div class=\"error\">Error: "+e.getMessage()+"<div>");
		}
%>
</body>
</html>
When I try to access it through the web browser when I am not logged in, I get: "Error: 872a4a15-be33-4455-bcbb-f9b53d5ff630 : /okm:root", but if I log in as any user (admin privileges or not) it works. I think it has something to do with a Session ID or cookie that is generated when you log in. Is there any way to fix this or any other means by which I can export the repository without having to open up the website?

EDIT: I'm using OpenKM 6.2.1 Build 7659 hosted on Ubuntu 12.04 LTS Desktop. I have tried Chrome, Firefox and IE8.

Thanks,
Mike
 #21925  by rodrigm21
 
Hi again,

Sorry for another post. I just found another post that appears to solve some of my questions:
http://forum.openkm.com/viewtopic.php?f=5&t=9334
I modified the script to export only the Root repository and no Categories. I can successfully execute it using the Script page on the Administrator panel in OpenKM. If I try to execute it using BSH in my Ubuntu 12.04 LTS Terminal though, I get the following error:
Code: Select all
Evaluation Error: Sourced file: export.bsh : Typed variable declaration : Attempt to resolve method: getInstance() on undefined variable or class name: DbSessionManager : at Line: 11 : in file: export.bsh : DbSessionManager .getInstance ( )
The script source code is:
Code: Select all
import com.openkm.module.db.stuff.DbSessionManager;
import java.io.StringWriter;
import com.openkm.util.impexp.*;
import java.io.File;
import java.util.Date;
import java.text.SimpleDateFormat;
import org.apache.commons.io.FileUtils;
import java.util.ArrayList;
import java.util.List;

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

Date now = new Date();
SimpleDateFormat simpleDateformat = new SimpleDateFormat("EEEE"); 

String okmPath = "/okm:root";
String exportPath = "/openkm/";

File backupDir = new File(exportPath);

//Wipe out old backup if it does not work
if (backupDir.exists()) {
    FileUtils.deleteDirectory(backupDir);
}

File rootBackupDir = new File(exportPath);

//Make Directories for Backup
FileUtils.forceMkdir(rootBackupDir);

boolean metadata = false;
boolean history = false;

StringWriter out = new StringWriter();
InfoDecorator deco = new DummyInfoDecorator();
ImpExpStats statsRoot, statsCat;

//Export Root
print("Root Export\n");
//print("systemToken: " + systemToken.toString() + "\n");
print("okmPath: " + okmPath.toString() + "\n");
print("rootBackupDir: " + rootBackupDir.toString() + "\n");
print("metadata: " + metadata.toString() + "\n");
print("history: " + history.toString() + "\n");
statsRoot = RepositoryExporter.exportDocuments(systemToken, okmPath, rootBackupDir, metadata, history, out, deco);
print(statsRoot.toString() + "\n");

print("Export Finished!");
It appears as though it can't find the DbSessionManager module. Any thoughts on why I'm seeing the error or any ways to fix it?

EDIT: Occasionally this script does not work throwing a NullPointer exception... any thoughts on why that is? All of the fields are defined before used as I can print them out prior to calling the RepositoryExporter.exportDocuments method.

Thanks in advance,
Mike
 #21932  by rodrigm21
 
Although I wasn't able to figure out why I can't run the script via the Terminal, I was able to modify the code so that I can load a webpage (logged in or not) and it will export the repository. I would prefer being able to run a command in the terminal though since as the repository gets larger the site may timeout? I'm also getting a "Unable to communicate with the server" error on my account (when logged in) while I load the page to export the repository. Any thoughts on how to avoid this besides removing the code for it?
 #22278  by pavila
 
If you want to export the repository a solution is create a BeanShell script which call the RepositoryExporter.exportDocuments method. The token maybe null (and the will use the principal of the authenticated user) or a give token (for example the system token, and in this case will use the internal system credentials)
 #22287  by rodrigm21
 
I occasionally get a NullPointer Exception when I use
Code: Select all
RepositoryExporter.exportDocuments(systemToken, okmPath, rootBackupDir, metadata, history, new StringWriter(), new DummyInfoDecorator());
but if I change that to:
Code: Select all
RepositoryExporter.exportDocuments(null, okmPath, rootBackupDir, metadata, history, new StringWriter(), new DummyInfoDecorator());
then the code works fine as long as a user is logged in. Is this a known bug? It seems to happen only after the server has been running for a day or so. I'm retrieving the systemToken via this code:
Code: Select all
String systemToken = DbSessionManager.getInstance().getSystemToken();
I can out.print(systemToken); and it prints out what appears to be a valid token too. I'm confused as to why a NullPointer exception is suddenly being thrown after this code worked fine all day yesterday...

UPDATE: Just updated to 6.2.3 Build 7945. Hopefully this will fix the issue.

About Us

OpenKM is part of the management software. A management software is a program that facilitates the accomplishment of administrative tasks. OpenKM is a document management system that allows you to manage business content and workflow in a more efficient way. Document managers guarantee data protection by establishing information security for business content.