Difference between revisions of "Crontab importer with metadata"
From OpenKM Documentation
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
− | Import files with metadata values in the filename from | + | Import files with metadata values in the filename from server file system. This is a contrab task based on scripting ( bsh ). |
Description: | Description: | ||
Line 12: | Line 12: | ||
== Metadata ( property groups ) == | == Metadata ( property groups ) == | ||
<source lang="xml"> | <source lang="xml"> | ||
− | + | <?xml version="1.0" encoding="UTF-8"?> | |
+ | <!DOCTYPE property-groups PUBLIC "-//OpenKM//DTD Property Groups 2.1//EN" | ||
+ | "http://www.openkm.com/dtd/property-groups-2.1.dtd"> | ||
+ | <property-groups> | ||
+ | <property-group label="Contract" name="okg:contract"> | ||
<input label="Cups" type="text" name="okp:contract.cups" width="200px"/> | <input label="Cups" type="text" name="okp:contract.cups" width="200px"/> | ||
<select label="Año" name="okp:contract.year" type="simple"> | <select label="Año" name="okp:contract.year" type="simple"> | ||
Line 36: | Line 40: | ||
</select> | </select> | ||
</property-group> | </property-group> | ||
+ | </property-groups> | ||
</source> | </source> | ||
Line 57: | Line 62: | ||
import com.openkm.util.MailUtils; | import com.openkm.util.MailUtils; | ||
import java.util.Date; | import java.util.Date; | ||
+ | import com.openkm.module.db.stuff.DbSessionManager; | ||
+ | import com.openkm.extension.core.*; | ||
+ | import com.openkm.api.OKMDocument; | ||
+ | import com.openkm.api.OKMPropertyGroup; | ||
class Status { | class Status { | ||
Line 82: | Line 91: | ||
month.add("DICIEMBRE"); | month.add("DICIEMBRE"); | ||
+ | String token = DbSessionManager.getInstance().getSystemToken(); | ||
String grpName = "okg:contract"; | String grpName = "okg:contract"; | ||
String contractUUID = "56aabd01-eeeb-47b3-bb10-86ee609541cf"; | String contractUUID = "56aabd01-eeeb-47b3-bb10-86ee609541cf"; | ||
String systemFolder = "/home/openkm/pending_to_import_folder"; | String systemFolder = "/home/openkm/pending_to_import_folder"; | ||
− | String basePath = OKMRepository.getInstance().getNodePath( | + | String basePath = OKMRepository.getInstance().getNodePath(token, contractUUID); |
Gson gson = new Gson(); | Gson gson = new Gson(); | ||
List bad = new ArrayList(); | List bad = new ArrayList(); | ||
Line 118: | Line 128: | ||
// test if folder year exists otherside create it | // test if folder year exists otherside create it | ||
path = basePath + "/" + status.year; | path = basePath + "/" + status.year; | ||
− | if (!OKMRepository.getInstance().hasNode( | + | if (!OKMRepository.getInstance().hasNode(token, path)) { |
Folder folder = new Folder(); | Folder folder = new Folder(); | ||
folder.setPath(path); | folder.setPath(path); | ||
− | OKMFolder.getInstance().create( | + | OKMFolder.getInstance().create(token, folder); |
} | } | ||
// test if folder month exists otherside create it | // test if folder month exists otherside create it | ||
path = path + "/" + status.month; | path = path + "/" + status.month; | ||
− | if (!OKMRepository.getInstance().hasNode( | + | if (!OKMRepository.getInstance().hasNode(token, path)) { |
Folder folder = new Folder(); | Folder folder = new Folder(); | ||
folder.setPath(path); | folder.setPath(path); | ||
− | OKMFolder.getInstance().create( | + | OKMFolder.getInstance().create(token, folder); |
} | } | ||
// Create document and adding metadata | // Create document and adding metadata | ||
Line 143: | Line 153: | ||
doc.setPath(path); | doc.setPath(path); | ||
FileInputStream fis = new FileInputStream(file); | FileInputStream fis = new FileInputStream(file); | ||
− | doc = | + | doc = OKMDocument.getInstance().create(token, doc, fis); |
status.dstPath = path; | status.dstPath = path; | ||
// Create new metadata | // Create new metadata | ||
− | + | OKMPropertyGroup.getInstance().addGroup(token, doc.getPath(), grpName); | |
Map properties = new HashMap(); | Map properties = new HashMap(); | ||
properties.put("okp:contract.cups",status.cups); | properties.put("okp:contract.cups",status.cups); | ||
properties.put("okp:contract.year",gson.toJson(new String[] {status.year})); | properties.put("okp:contract.year",gson.toJson(new String[] {status.year})); | ||
properties.put("okp:contract.month",gson.toJson(new String[] {status.month.toLowerCase()})); | properties.put("okp:contract.month",gson.toJson(new String[] {status.month.toLowerCase()})); | ||
− | + | OKMPropertyGroup.getInstance().setPropertiesSimple(token, doc.getPath(), grpName, properties); | |
// Delete file | // Delete file | ||
Line 179: | Line 189: | ||
status.error += "IOException"; | status.error += "IOException"; | ||
} catch (Exception e) { | } catch (Exception e) { | ||
− | status.error += | + | status.error += e.getMessage(); |
} | } | ||
} | } | ||
Line 236: | Line 246: | ||
result.append("<td>"+status.year+"</td>"); | result.append("<td>"+status.year+"</td>"); | ||
result.append("<td>"+status.month+"</td>"); | result.append("<td>"+status.month+"</td>"); | ||
− | result.append("<td><a href=\""+Config.APPLICATION_URL+"?docPath"+URLEncoder.encode(status.dstPath, "UTF-8")+"\">"+status.dstPath+"</font></td>"); | + | result.append("<td><a href=\""+Config.APPLICATION_URL+"?docPath="+URLEncoder.encode(status.dstPath, "UTF-8")+"\">"+status.dstPath+"</font></td>"); |
result.append("</tr>"); | result.append("</tr>"); | ||
} | } | ||
Line 248: | Line 258: | ||
</source> | </source> | ||
− | == | + | == Example == |
− | + | '''Register metadata:''' | |
+ | [[File:Okm_user_guide_334.png|center]] | ||
− | |||
− | + | '''Create crontab task:''' | |
− | + | [[File:Okm_user_guide_333.png|center]] | |
− | [[File:Okm_user_guide_315.png]] | + | |
+ | '''Files in OpenKM server:''' | ||
+ | [[File:Okm_user_guide_316.png|center]] | ||
+ | |||
+ | |||
+ | '''Mail notification format:''' | ||
+ | [[File:Okm_user_guide_314.png|center]] | ||
+ | |||
+ | |||
+ | '''Imported documents view from OpenKM desktop:''' | ||
+ | [[File:Okm_user_guide_315.png|center]] | ||
[[Category: Utilities]] | [[Category: Utilities]] |
Latest revision as of 07:30, 13 June 2013
Import files with metadata values in the filename from server file system. This is a contrab task based on scripting ( bsh ).
Description:
- Files stored in OpenKM server have a filename format like some_text - (CUPS,REC).pdf
- grpName indicates the metadata group to be inserted.
- contractUUID indicates the UUID of the OpenKM where will be imported the files.
- systemFolder indicates from where OpenKM will try exporting files.
- Verifies paths and extracts metadata from text between ();
- Files are stored at basePath/year/month ( is year or month are not present are automatically created )
- Is sended a mail with importation results
Metadata ( property groups )
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE property-groups PUBLIC "-//OpenKM//DTD Property Groups 2.1//EN"
"http://www.openkm.com/dtd/property-groups-2.1.dtd">
<property-groups>
<property-group label="Contract" name="okg:contract">
<input label="Cups" type="text" name="okp:contract.cups" width="200px"/>
<select label="Año" name="okp:contract.year" type="simple">
<option label="2012" value="2012"/>
<option label="2011" value="2011"/>
<option label="2010" value="2010"/>
<option label="2009" value="2009"/>
<option label="2008" value="2008"/>
</select>
<select label="Mes" name="okp:contract.month" type="simple">
<option label="Enero" value="enero"/>
<option label="Febrero" value="febrero"/>
<option label="Marzo" value="marzo"/>
<option label="Abril" value="abril"/>
<option label="Mayo" value="mayo"/>
<option label="Junio" value="junio"/>
<option label="Julio" value="julio"/>
<option label="Agosto" value="agosto"/>
<option label="Septiembre" value="septiembre"/>
<option label="Octubre" value="octubre"/>
<option label="Noviembre" value="noviembre"/>
<option label="Diciembre" value="diciembre"/>
</select>
</property-group>
</property-groups>
Scripting file
import java.io.*;
import java.util.*;
import com.openkm.bean.Document;
import com.openkm.module.db.DbDocumentModule;
import com.openkm.dao.DatabaseMetadataDAO;
import com.openkm.util.DatabaseMetadataUtils;
import com.openkm.dao.bean.DatabaseMetadataValue;
import com.openkm.api.OKMRepository;
import com.openkm.api.OKMFolder;
import com.openkm.bean.Folder;
import com.google.gson.Gson;
import com.openkm.dao.NodeBaseDAO;
import java.lang.StringBuffer;
import com.openkm.core.*;
import java.net.URLEncoder;
import com.openkm.util.MailUtils;
import java.util.Date;
import com.openkm.module.db.stuff.DbSessionManager;
import com.openkm.extension.core.*;
import com.openkm.api.OKMDocument;
import com.openkm.api.OKMPropertyGroup;
class Status {
public String fileName = "";
public String dstPath = "";
public String cups = "";
public String month = "";
public String year = "";
public String error = "";
}
// Months
List month = new ArrayList();
month.add("ENERO");
month.add("FEBRERO");
month.add("MARZO");
month.add("ABRIL");
month.add("MAYO");
month.add("JUNIO");
month.add("JULIO");
month.add("AGOSTO");
month.add("SEPTIEMBRE");
month.add("OCTUBRE");
month.add("NOVIEMBRE");
month.add("DICIEMBRE");
String token = DbSessionManager.getInstance().getSystemToken();
String grpName = "okg:contract";
String contractUUID = "56aabd01-eeeb-47b3-bb10-86ee609541cf";
String systemFolder = "/home/openkm/pending_to_import_folder";
String basePath = OKMRepository.getInstance().getNodePath(token, contractUUID);
Gson gson = new Gson();
List bad = new ArrayList();
List good = new ArrayList();
List toAddress = new ArrayList();
// Loading files
File folder = new File(systemFolder);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
File file = listOfFiles[i];
if (file.isFile() && file.getName().toLowerCase().endsWith(".pdf")) {
Status status = new Status();
fileName = file.getName();
status.fileName = fileName;
if (fileName.indexOf("(")>0 && fileName.indexOf("(")<fileName.indexOf(")")) {
// Get metadata
String metadata = fileName.substring((fileName.indexOf("(")+1), fileName.lastIndexOf(")"));
// get data
String[] data = metadata.split(",");
if (data.length!=2) {
status.error = "Incorrect format -> name (cups,REN)<br>";
} else {
status.cups = data[0].toUpperCase().replaceAll(" ","");
Date date = new Date(file.lastModified());
status.year = String.valueOf(1900 + date.getYear());
status.month = month.get(date.getMonth());
if (!data[1].toUpperCase().equals("REN")) {
status.error = "Error type REN not found<br>";
}
if (status.error.equals("")) {
// test if folder year exists otherside create it
path = basePath + "/" + status.year;
if (!OKMRepository.getInstance().hasNode(token, path)) {
Folder folder = new Folder();
folder.setPath(path);
OKMFolder.getInstance().create(token, folder);
}
// test if folder month exists otherside create it
path = path + "/" + status.month;
if (!OKMRepository.getInstance().hasNode(token, path)) {
Folder folder = new Folder();
folder.setPath(path);
OKMFolder.getInstance().create(token, folder);
}
// Create document and adding metadata
try {
// Removing extra ( contents )
fileName = fileName.substring(0,fileName.indexOf("("));
// Removing - and spaces at ends
while (fileName.substring(fileName.length()-1).equals(" ") || fileName.substring(fileName.length()-1).equals("-")) {
fileName = fileName.substring(0,fileName.length()-1);
}
fileName = fileName + ".pdf";
Document doc = new Document();
path = path + "/" + fileName;
doc.setPath(path);
FileInputStream fis = new FileInputStream(file);
doc = OKMDocument.getInstance().create(token, doc, fis);
status.dstPath = path;
// Create new metadata
OKMPropertyGroup.getInstance().addGroup(token, doc.getPath(), grpName);
Map properties = new HashMap();
properties.put("okp:contract.cups",status.cups);
properties.put("okp:contract.year",gson.toJson(new String[] {status.year}));
properties.put("okp:contract.month",gson.toJson(new String[] {status.month.toLowerCase()}));
OKMPropertyGroup.getInstance().setPropertiesSimple(token, doc.getPath(), grpName, properties);
// Delete file
boolean success = file.delete();
if (!success) {
status.error += "File can not been deleted";
}
} catch (PathNotFoundException e) {
status.error += "PathNotFoundException";
} catch (ItemExistsException e) {
status.error += "ItemExistsException";
} catch (UnsupportedMimeTypeException e) {
status.error += "UnsupportedMimeTypeException";
} catch (FileSizeExceededException e) {
status.error += "FileSizeExceededException";
} catch (VirusDetectedException e) {
status.error += "VirusDetectedException";
} catch (RepositoryException e) {
status.error += "RepositoryException";
} catch (DatabaseException e) {
status.error += "DatabaseException";
} catch (ExtensionException e) {
status.error += "ExtensionException";
} catch (IOException e) {
status.error += "IOException";
} catch (Exception e) {
status.error += e.getMessage();
}
}
}
} else {
status.error = "Document format incorrect -> nombre - (cups,REN)<br>";
}
if (!status.error.equals("")) {
bad.add(status);
} else {
good.add(status);
}
}
}
StringBuffer result = new StringBuffer();
result.append("<h1>Import report</h1>");
result.append("</br></br>");
result.append("<table boder=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">");
result.append("<tr>");
result.append("<td bgcolor=\"silver\"><b>Name</b></td>");
result.append("<td bgcolor=\"silver\"><b>CUPS</b></td>");
result.append("<td bgcolor=\"silver\"><b>Año</b></td>");
result.append("<td bgcolor=\"silver\"><b>Mes</b></td>");
result.append("<td bgcolor=\"silver\"><b>Error</b></td>");
result.append("</tr>");
result.append("<tr>");
result.append("<td colspan=\"6\" bgcolor=\"silver\"><b>Errors:"+bad.size()+"</b></td>");
result.append("</tr>");
for (Status status : bad) {
result.append("<tr>");
result.append("<td>"+status.fileName+"</td>");
result.append("<td>"+status.cups+"</td>");
result.append("<td>"+status.year+"</td>");
result.append("<td>"+status.month+"</td>");
result.append("<td><font color=\"red\">"+status.error+"</font></td>");
result.append("</tr>");
}
result.append("</table>");
result.append("</br></br>");
result.append("<table boder=\"0\" cellpadding=\"2\" cellspacing=\"0\" width=\"100%\">");
result.append("<tr>");
result.append("<td bgcolor=\"silver\"><b>Name</b></td>");
result.append("<td bgcolor=\"silver\"><b>CUPS</b></td>");
result.append("<td bgcolor=\"silver\"><b>Year</b></td>");
result.append("<td bgcolor=\"silver\"><b>Month</b></td>");
result.append("<td bgcolor=\"silver\"><b>Destination</b></td>");
result.append("</tr>");
result.append("<tr>");
result.append("<td colspan=\"6\" bgcolor=\"silver\"><b>Imported correctly:"+good.size()+"</b></td>");
result.append("</tr>");
for (Status status : good) {
result.append("<tr>");
result.append("<td>"+status.fileName+"</td>");
result.append("<td>"+status.cups+"</td>");
result.append("<td>"+status.year+"</td>");
result.append("<td>"+status.month+"</td>");
result.append("<td><a href=\""+Config.APPLICATION_URL+"?docPath="+URLEncoder.encode(status.dstPath, "UTF-8")+"\">"+status.dstPath+"</font></td>");
result.append("</tr>");
}
result.append("</table>");
// Sending mails
toAddress.add("some@mail.com");
MailUtils.sendMessage(toAddress, "Importing report",result.toString());
print(result.toString());
Example
Register metadata:
Create crontab task:
Files in OpenKM server:
Mail notification format:
Imported documents view from OpenKM desktop: