Crontab importer with metadata
From OpenKM Documentation
Import files with metadata values in the filename from serverfilesystem. 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 )
<property-group label="Contrato" name="okg:contract">
<input label="Cups" type="text" name="okp:contrato.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.mont" 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>
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;
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 grpName = "okg:contract";
String contractUUID = "56aabd01-eeeb-47b3-bb10-86ee609541cf";
String systemFolder = "/home/openkm/pending_to_import_folder";
String basePath = OKMRepository.getInstance().getNodePath(null, 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 forma -> 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(null, path)) {
Folder folder = new Folder();
folder.setPath(path);
OKMFolder.getInstance().create(null, folder);
}
// test if folder month exists otherside create it
path = path + "/" + status.month;
if (!OKMRepository.getInstance().hasNode(null, path)) {
Folder folder = new Folder();
folder.setPath(path);
OKMFolder.getInstance().create(null, 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 = new DbDocumentModule().create(null, doc, fis);
status.dstPath = path;
// Create new metadata
NodeBaseDAO.getInstance().addPropertyGroup(doc.getUuid(), 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.mont",gson.toJson(new String[] {status.month.toLowerCase()}));
NodeBaseDAO.getInstance().setProperties(doc.getUuid(), 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>Nombre</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>Destino</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());