Ejemplo de crontab - Importador de documentos con valores de metadatos
El script importa archivos con valores de metadatos desde el sistema de archivos del servidor.
Definición del grupo de metadatos:
<?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>El script:
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 = "";}
// MonthsList 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 filesFile 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 end 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 mailstoAddress.add("some@mail.com");MailUtils.sendMessage(toAddress, "Importing report",result.toString());
print(result.toString());Imágenes
Sección titulada «Imágenes»Registrar la definición de metadatos
Sección titulada «Registrar la definición de metadatos»
Crear la tarea de crontab
Sección titulada «Crear la tarea de crontab»
Archivos en el servidor de aplicaciones
Sección titulada «Archivos en el servidor de aplicaciones»
Resultado
Sección titulada «Resultado»Notificación por correo
Sección titulada «Notificación por correo»
Archivos importados en el repositorio
Sección titulada «Archivos importados en el repositorio»