Here is a more advanced version of the auto-import script. This version will import all files and subdirectories below a given staring point ("/home/scanner/scans" in this example) and recreate the directory structure in OpenKM from the specified location ("/okm:root/Scans" in this example).
Code: Select all
import javax.jcr.*;
import com.openkm.core.*;
import com.openkm.api.*;
import java.io.*;
String token = JcrSessionManager.getInstance().getSystemToken();
Session session = JcrSessionManager.getInstance().get(token);
OKMDocument document = OKMDocument.getInstance();
OKMFolder folder = OKMFolder.getInstance();
public void autoImport(String okmPath, File fldpath){
try{
print("Scanning " + fldpath.getName() + "<br>");
for (File file : fldpath.listFiles()) {
print("Importing " + file.getName() + "<br>");
try {
if (file.isDirectory()){
try {
folder.createSimple(token, okmPath + file.getName());
} catch (ItemExistsException ie) {
// Folder already exists - just ignore exception
}
autoImport( okmPath + file.getName() + "/", file);
} else {
// Check if file is still being written to
long length = file.length();
Thread.sleep(1000);
if (file.length() > length) continue; // Skip file this time
document.createSimple(token, okmPath + file.getName(), new FileInputStream(file));
}
print("Created " + okmPath + file.getName() + "<br>");
} catch (Exception e) {
print ("Exception:" + e + "<br>");
// Something bad happened to prevent import. Skip to next file.
continue;
}
file.delete();
}
} catch (Exception e) {
print("Exception: " + e + "<br>");
}
}
autoImport("/okm:root/Scans/", new File("/home/scanner/scans"));