• Automatic import

  • We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
We tried to make OpenKM as intuitive as possible, but an advice is always welcome.
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.
 #18306  by dsc68
 
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"));
 #19455  by fergalom
 
I have tried the script above in 6.2 CE running on Ubuntu 12.04 but to no avail.

Tried 2 methods:
Using CRON in openkm admin panel
Code: Select all
Name	                Expression  	Mime	             File Name	        Mail 	Last Begin	                          Last End
Scan Import to PDF	*/5 * * * * 	   application/x-bsh	scan-import.bsh 		Nov 21, 2012 12:15:00 PM 	Nov 21, 2012 12:15:00 PM
scan-import.bsh is saved in /home/fom/ and has correct permissions (even tried 777)
Scanner scans to /home/fom/scanner

Paths are set correctly in the script and Scans folder created in okm:root

Also tried the script panel in admin and when click evaluate, get the following as a script error:
Code: Select all
Sourced file: inline evaluation of: ``import javax.jcr.*; import com.openkm.core.*; import com.openkm.api.*; . . . '' : Typed variable declaration : Attempt to resolve method: getInstance() on undefined variable or class name: JcrSessionManager : at Line: 6 : in file: inline evaluation of: ``import javax.jcr.*; import com.openkm.core.*; import com.openkm.api.*; . . . '' : JcrSessionManager .getInstance ( ) 
Any ideas where I'm going wrong?
 #19480  by jllort
 
What do you have in line 6 ? if you're using version 6 you should not use JCR classes this script seem to be prepared for version 5.x you need 6.x equivalent classes take a look at http://doxygen.openkm.com/6.2.x/
 #19491  by fergalom
 
same as line 6 in the script above
Code: Select all
String token = JcrSessionManager.getInstance().getSystemToken();
Will have a look through the provided link, any guidance on what JcrSessionManager has been replaced with in 6.2?
 #19505  by jllort
 
Basically from 6.x we're using own database not jackrabbit. We have split two packages one for jackrabbit and other for our own database. By default configuration should be used the second.
 #19515  by pavila
 
Change:
Code: Select all
String token = JcrSessionManager.getInstance().getSystemToken();
to
Code: Select all
String token = DbSessionManager.getInstance().getSystemToken();
 #20992  by 1ststoppcshop
 
i having trouble with this for some reason. I've tried it with both String token = JcrSessionManager & String token = DbSessionManager and both yeld results of error in line 6
Im running OpenKM v6.2
 #20993  by jllort
 
In version 6.x you should only use DBManager not JCRManager, make simply script test to print systemTokem and you'll see all it's right on it.
 #20999  by 1ststoppcshop
 
Code: Select all
    import javax.jcr.*;
    import com.openkm.core.*;
    import com.openkm.api.*;
    import java.io.*;
	import com.openkm.module.db

    String token = DbSessionManager.getInstance().getSystemToken();
    Session session = DbSessionManager.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"));
still the results are error out on the DbSessionManager
 #21009  by jllort
 
Sometimes scripting messages are not clear, like this case, you forget the ; at ends of import com.openkm.module.db ,add it.
Code: Select all
   import com.openkm.module.db;
Anyway you should not use session etc.. is valid for 5.x but not for 6.x I suggest work step by step with this script to transform in 6.x compatible. When you get finished, if you want we can share in our online documentation.
 #21077  by fergalom
 
ok so that line should actually be
Code: Select all
import com.openkm.module.db.stuff.DbSessionManager;
I then get a script output of
Code: Select all
Scanning scanner
Importing img-121120185236.pdf
Exception:java.lang.NullPointerException
So I have narrowed it down to being this line at issue
Code: Select all
document.createSimple(token, okmPath + file.getName(), new FileInputStream(file));
Any pointers?
I will of course be referring to the documentation now to have a look for my problem.
 #21079  by fergalom
 
Ok so finally have this working! Had to remove the line
Code: Select all
Session session = DbSessionManager.getInstance().get(token);
Here is the full script for 6.2
Code: Select all
import javax.jcr.*;
    import com.openkm.core.*;
    import com.openkm.api.*;
    import java.io.*;
import com.openkm.module.db.stuff.DbSessionManager;

    String token = DbSessionManager.getInstance().getSystemToken();
    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) {
			  print("folder already exists<br>");
                // 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/fom/scanner"));
Thanks for the help and of course to the person who put this script together
 #21199  by 1ststoppcshop
 
you beat me to it my script still had
Code: Select all
Session session = DbSessionManager.getInstance().get(token);
seen your post took it out and magic thank you
  • 1
  • 3
  • 4
  • 5
  • 6
  • 7

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.