1 package org.collectionspace.services.imports.nuxeo;
4 import java.util.Calendar;
5 import java.util.HashMap;
7 import java.util.TreeSet;
9 import org.collectionspace.services.nuxeo.client.java.NuxeoClientEmbedded;
10 import org.collectionspace.services.nuxeo.client.java.NuxeoConnectorEmbedded;
11 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
13 import org.nuxeo.ecm.core.api.DocumentModel;
14 import org.nuxeo.ecm.core.api.DocumentRef;
15 import org.nuxeo.ecm.core.io.DocumentPipe;
16 import org.nuxeo.ecm.core.io.DocumentReader;
17 import org.nuxeo.ecm.core.io.DocumentTranslationMap;
18 import org.nuxeo.ecm.core.io.DocumentWriter;
19 import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl;
20 import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter;
21 // we use our own override of this: import org.nuxeo.ecm.core.io.impl.plugins.XMLDirectoryReader;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 // based loosely on package org.nuxeo.ecm.shell.commands.io.ImportCommand;
26 public class ImportCommand {
28 private final Logger logger = LoggerFactory.getLogger(ImportCommand.class);
30 public String run(String src, String repoName, String workspacesPath, int timeout) throws Exception {
31 File file = new File(src);
32 ///cspace way of configuring client and auth:
33 NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
34 CoreSessionInterface repoSession = null;
36 repoSession = client.openRepository(repoName, timeout);
37 if (logger.isDebugEnabled()) {
38 String msg = String.format("Start of import is Local time: %tT", Calendar.getInstance());
41 return importTree(repoSession, file, workspacesPath, timeout);
42 } catch (Exception e) {
45 if (logger.isDebugEnabled()) {
46 String msg = String.format("End of import is Local time: %tT", Calendar.getInstance());
49 client.releaseRepository(repoSession);
54 * If the import exceeds the number of seconds in 'timeout', we'll thrown an exception and rollback all import work
56 String importTree(CoreSessionInterface repoSession, File file, String toPath, int timeout) throws Exception {
57 Exception failed = null;
58 DocumentReader reader = null;
59 DocumentWriter writer = null;
60 DocumentModel docModel = null;
61 DocumentRef keyDocRef, valueDocRef;
63 StringBuffer dump = new StringBuffer();
64 Map<String, Integer> recordsImportedForDocType = new HashMap<String, Integer>();
65 Integer numRecordsImportedForDocType = new Integer(0);
66 int totalRecordsImported = 0;
68 if (logger.isInfoEnabled()) {
69 logger.info("ImportCommand.importTree() method reading file: " + file + (file != null ? " exists? " + file.exists() : " file param is null"));
70 logger.info(String.format("ImportCommand.importTree() will timeout if import does not complete in %d seconds.", timeout));
72 reader = new LoggedXMLDirectoryReader(file, timeout); //our overload of XMLDirectoryReader.
73 writer = new DocumentModelWriter(repoSession.getCoreSession(), toPath, 10);
74 DocumentPipe pipe = new DocumentPipeImpl(10);
75 // pipe.addTransformer(transformer);
76 pipe.setReader(reader);
77 pipe.setWriter(writer);
78 DocumentTranslationMap dtm = pipe.run();
80 throw new Exception("Could not process import payload. Check XML markup for not-well-formed errors, elements not matching import schema, etc.");
82 Map<DocumentRef, DocumentRef> documentRefs = dtm.getDocRefMap();
83 if (documentRefs != null && documentRefs.isEmpty()) {
84 throw new Exception("No valid records found in import payload. Check XML markup for elements not matching import or document-specific schema, etc.");
86 dump.append("<importedRecords>");
87 for (Map.Entry<DocumentRef, DocumentRef> entry : documentRefs.entrySet()) {
88 keyDocRef = (DocumentRef) entry.getKey();
89 valueDocRef = (DocumentRef) entry.getValue();
90 if (keyDocRef == null || valueDocRef == null) {
93 dump.append("<importedRecord>");
94 docModel = repoSession.getDocument(valueDocRef);
95 docType = docModel.getDocumentType().getName();
96 dump.append("<doctype>" + docType + "</doctype>");
97 dump.append("<csid>" + keyDocRef.toString() + "</csid>");
98 dump.append("</importedRecord>");
99 if (recordsImportedForDocType.containsKey(docType)) {
100 numRecordsImportedForDocType = (Integer) recordsImportedForDocType.get(docType);
101 numRecordsImportedForDocType = Integer.valueOf(numRecordsImportedForDocType.intValue() + 1);
102 recordsImportedForDocType.put(docType, numRecordsImportedForDocType);
104 recordsImportedForDocType.put(docType, 1);
106 totalRecordsImported++;
108 dump.append("</importedRecords>");
109 } catch (Exception e) {
113 String status = failed == null ? "Success" : "Failed";
114 dump.append("<status>" + status + "</status>");
115 dump.append("<totalRecordsImported>" + totalRecordsImported + "</totalRecordsImported>");
116 dump.append("<numRecordsImportedByDocType>");
117 TreeSet<String> keys = new TreeSet<String>(recordsImportedForDocType.keySet());
118 for (String key : keys) {
119 dump.append("<numRecordsImported>");
120 dump.append("<docType>" + key + "</docType>");
121 dump.append("<numRecords>" + recordsImportedForDocType.get(key).intValue() + "</numRecords>");
122 dump.append("</numRecordsImported>");
124 dump.append("</numRecordsImportedByDocType>");
125 if (reader != null) {
126 dump.append("<report>" + (((LoggedXMLDirectoryReader) reader).report()) + "</report>");
129 if (writer != null) {
133 if (failed != null) {
134 String msg = "The Import service encountered an exception: " + failed.getLocalizedMessage();
135 logger.error(msg, failed);
138 return dump.toString();