Crontab importer with metadata

From OpenKM Documentation
Revision as of 12:49, 8 November 2012 by Jllort (talk | contribs) (Created page with 'Import files from OpenKM server to OpenKM with metadata. Description: * Files stored in OpenKM server have a filename format like some_text - (CUPS,REC).pdf * '''grpName''' indi…')

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Import files from OpenKM server to OpenKM with metadata.

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
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 = "Formato de documento incorrecto -> nombre (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 tipo REN no localizado<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:contrato.cups",status.cups);
						properties.put("okp:contrato.ano",gson.toJson(new String[] {status.year}));
						properties.put("okp:contrato.mes",gson.toJson(new String[] {status.month.toLowerCase()}));
						NodeBaseDAO.getInstance().setProperties(doc.getUuid(), grpName, properties);
						
						// Delete file
						boolean success = file.delete();
						if (!success) {
							status.error += "El fichero no se ha podido borrar";
						}
						
					} 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 = "Formato de documento incorrecto -> nombre - (cups,REN)<br>";
		}
		if (!status.error.equals("")) {
			bad.add(status);
		} else {
			good.add(status);
		}
   	}
}

StringBuffer result = new StringBuffer();
result.append("<h1>Informe de importación de reactivados</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>Nombre</b></td>");
result.append("<td bgcolor=\"silver\"><b>CUPS</b></td>");
result.append("<td bgcolor=\"silver\"><b>Agente</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>Errores:"+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>Agente</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>Importados correctamente:"+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("informatica@factorenergia.com");
MailUtils.sendMessage(toAddress, "Informe de importación de reactivados",result.toString());

print(result.toString());