Difference between revisions of "Crontab simple importer"

From OpenKM Documentation
Jump to: navigation, search
(Created page with ' <source lang="java"> import com.openkm.core.*; import com.openkm.api.*; import java.io.*; import com.openkm.module.db.stuff.DbSessionManager; String token = DbSessionManager.ge…')
 
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
Imports files fromserver file system. This is a crontab task based on scripting ( bsh ).
 +
Description:
 +
* Files stored in OpenKM ( can be directly stored in openkm or be a network folder connected to OpenKM server or shared by OpenKM server into the network )
 +
* Files are periodically imported by crontab task to some OpenKM folder ( /okm:root/Scans )
  
 +
== Script file ==
 
<source lang="java">
 
<source lang="java">
 
import com.openkm.core.*;
 
import com.openkm.core.*;
Line 5: Line 10:
 
import java.io.*;
 
import java.io.*;
 
import com.openkm.module.db.stuff.DbSessionManager;
 
import com.openkm.module.db.stuff.DbSessionManager;
 
+
 
String token = DbSessionManager.getInstance().getSystemToken();
 
String token = DbSessionManager.getInstance().getSystemToken();
 
OKMDocument document = OKMDocument.getInstance();
 
OKMDocument document = OKMDocument.getInstance();
 
OKMFolder folder = OKMFolder.getInstance();
 
OKMFolder folder = OKMFolder.getInstance();
 
+
 
public void autoImport(String okmPath, File fldpath){
 
public void autoImport(String okmPath, File fldpath){
   try{
+
   try {
print("Scanning " + fldpath.getName() + "<br>");
+
    print("Scanning " + fldpath.getName() + "<br>");
 
+
    for (File file : fldpath.listFiles()) {
for (File file : fldpath.listFiles()) {
+
      print("Importing " + file.getName() + "<br>");
  print("Importing " + file.getName() + "<br>");
+
try {
 
+
  if (file.isDirectory()) {
  try {
+
    try {
if (file.isDirectory()){
+
      folder.createSimple(token, okmPath + file.getName());
  try {
+
    } catch (ItemExistsException ie) {
folder.createSimple(token, okmPath + file.getName());
+
      print("folder already exists<br>");
  } catch (ItemExistsException ie) {
+
      // Folder already exists - just ignore exception
  print("folder already exists<br>");
+
    }
// Folder already exists - just ignore exception
+
    autoImport( okmPath + file.getName() + "/", file);
  }
 
  autoImport( okmPath + file.getName() + "/", file);
 
 
  } else {
 
  } else {
  // Check if file is still being written to
+
    // Check if file is still being written to
  long length = file.length();
+
    long length = file.length();
  Thread.sleep(1000);
+
    Thread.sleep(1000);
+
    if (file.length() > length) continue;  // Skip file this time
  if (file.length() > length) continue;  // Skip file this time
+
    document.createSimple(token, okmPath + file.getName(), new FileInputStream(file));
  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;
 
 
  }
 
  }
 
+
  print("Created " + okmPath + file.getName() + "<br>");
  file.delete();
+
} catch (Exception e) {
 +
  print ("Exception:" + e + "<br>");
 +
  // Something bad happened to prevent import. Skip to next file.
 +
continue;
 
}
 
}
 +
      file.delete();
 +
    }
 
   } catch (Exception e) {
 
   } catch (Exception e) {
print("Exception: " + e + "<br>");
+
    print("Exception: " + e + "<br>");
 
   }
 
   }
 
}
 
}
Line 51: Line 51:
 
autoImport("/okm:root/Scans/", new File("/home/fom/scanner"));
 
autoImport("/okm:root/Scans/", new File("/home/fom/scanner"));
 
</source>
 
</source>
 +
 +
== Example ==
 +
'''Create crontab task:'''
 +
[[File:Okm_user_guide_355.png|center]]
 +
 +
'''Files in OpenKM server:'''
 +
[[File:Okm_user_guide_356.png|center]]
 +
 +
'''Imported documents view from OpenKM desktop:'''
 +
[[File:Okm_user_guide_357.png]]

Latest revision as of 10:34, 25 January 2013

Imports files fromserver file system. This is a crontab task based on scripting ( bsh ). Description:

  • Files stored in OpenKM ( can be directly stored in openkm or be a network folder connected to OpenKM server or shared by OpenKM server into the network )
  • Files are periodically imported by crontab task to some OpenKM folder ( /okm:root/Scans )

Script file

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"));

Example

Create crontab task:

Okm user guide 355.png

Files in OpenKM server:

Okm user guide 356.png

Imported documents view from OpenKM desktop: Okm user guide 357.png