Page 6 of 7

Re: Automatic import

PostPosted:Thu Aug 30, 2012 12:56 am
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"));

Re: Automatic import

PostPosted:Fri Aug 31, 2012 10:30 am
by jllort

Re: Automatic import

PostPosted:Wed Nov 21, 2012 12:23 pm
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?

Re: Automatic import

PostPosted:Thu Nov 22, 2012 6:33 pm
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/

Re: Automatic import

PostPosted:Fri Nov 23, 2012 10:28 am
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?

Re: Automatic import

PostPosted:Sat Nov 24, 2012 11:33 am
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.

Re: Automatic import

PostPosted:Sun Nov 25, 2012 7:41 pm
by pavila
Change:
Code: Select all
String token = JcrSessionManager.getInstance().getSystemToken();
to
Code: Select all
String token = DbSessionManager.getInstance().getSystemToken();

Re: Automatic import

PostPosted:Sun Jan 20, 2013 2:19 pm
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

Re: Automatic import

PostPosted:Sun Jan 20, 2013 5:31 pm
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.

Re: Automatic import

PostPosted:Mon Jan 21, 2013 2:36 am
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

Re: Automatic import

PostPosted:Mon Jan 21, 2013 6:50 pm
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.

Re: Automatic import

PostPosted:Thu Jan 24, 2013 5:46 pm
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.

Re: Automatic import

PostPosted:Thu Jan 24, 2013 10:57 pm
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

Re: Automatic import

PostPosted:Fri Jan 25, 2013 8:49 am
by jllort
I have added here http://wiki.openkm.com/index.php/Utilities with name Crontab simple importer

Re: Automatic import

PostPosted:Thu Jan 31, 2013 6:52 am
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