]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b22e5cf097e2845ec915ecbed4285c5a6847bf01
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.imports.nuxeo;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.Calendar;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.TreeSet;
9
10 import org.collectionspace.services.nuxeo.client.java.NuxeoClientEmbedded;
11 import org.collectionspace.services.nuxeo.client.java.NuxeoConnectorEmbedded;
12 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
13 import org.nuxeo.common.utils.FileUtils;
14 import org.nuxeo.ecm.core.api.DocumentModel;
15 import org.nuxeo.ecm.core.api.DocumentRef;
16 import org.nuxeo.ecm.core.api.IdRef;
17 import org.nuxeo.ecm.core.api.PathRef;
18 import org.nuxeo.ecm.core.io.DocumentPipe;
19 import org.nuxeo.ecm.core.io.DocumentReader;
20 import org.nuxeo.ecm.core.io.DocumentTranslationMap;
21 import org.nuxeo.ecm.core.io.DocumentWriter;
22 import org.nuxeo.ecm.core.io.impl.DocumentPipeImpl;
23 import org.nuxeo.ecm.core.io.impl.plugins.DocumentModelWriter;
24 import org.nuxeo.ecm.platform.importer.service.DefaultImporterService;
25 import org.nuxeo.ecm.platform.importer.service.DefaultImporterServiceImpl;
26 import org.nuxeo.ecm.platform.importer.xml.parser.XMLImporterService;
27 import org.nuxeo.runtime.api.Framework;
28 // we use our own override of this: import org.nuxeo.ecm.core.io.impl.plugins.XMLDirectoryReader;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 // based loosely on package org.nuxeo.ecm.shell.commands.io.ImportCommand;
33 public class ImportCommand {
34
35     private final Logger logger = LoggerFactory.getLogger(ImportCommand.class);
36
37     public String run(String src, String repoName, String workspacesPath, int timeout) throws Exception {
38         File file = new File(src);
39         ///cspace way of configuring client and auth:
40         NuxeoClientEmbedded client = NuxeoConnectorEmbedded.getInstance().getClient();
41         CoreSessionInterface repoSession = null;
42         try {
43             repoSession = client.openRepository(repoName, timeout);
44             if (logger.isDebugEnabled()) {
45                 String msg = String.format("Start of import is Local time: %tT", Calendar.getInstance());
46                 logger.debug(msg);
47             }
48             return newImportTree(repoSession, file, workspacesPath, timeout);
49         } catch (Exception e) {
50             throw e;
51         } finally {
52             if (logger.isDebugEnabled()) {
53                 String msg = String.format("End of import is Local time: %tT", Calendar.getInstance());
54                 logger.debug(msg);
55             }
56             client.releaseRepository(repoSession);
57         }
58     }
59     
60 //    DocumentModel root = session.getRootDocument();
61 //    File xml = File("/where/is/my/umbrella");
62 //    XMLImporterService importer = Framework.getLocalService(XMLImporterService.class);
63 //    importer.importDocuments(root, xml);
64     
65     private String newImportTree(CoreSessionInterface repoSession, File file, String toPath, int timeout) throws IOException {
66         File source = new File("C:/dev/tools/apache-tomcat-7.0.64/temp/imports-2014210770025398386/Personauthorities.zip");
67         if (source.exists()) {
68                 String targetPath = "/core-domain/Workspaces/Personauthorities/"; ///core-domain/Workspaces/Personauthorities
69                 DocumentRef docRef = new PathRef(targetPath);
70                 DocumentModel root = repoSession.getDocument(docRef);
71                 XMLImporterService importerService = Framework.getLocalService(XMLImporterService.class);
72             importerService.importDocuments(root, source);
73         } else {
74                 System.err.println("Could not find file: " + source.getAbsolutePath());
75         }
76         
77         return "New Import Service Run";
78     }
79
80     /*
81      * If the import exceeds the number of seconds in 'timeout', we'll thrown an exception and rollback all import work
82      */
83     String importTree(CoreSessionInterface repoSession, File file, String toPath, int timeout) throws Exception {
84         Exception failed = null;
85         DocumentReader reader = null;
86         DocumentWriter writer = null;
87         DocumentModel docModel = null;
88         DocumentRef keyDocRef, valueDocRef;
89         String docType;
90         StringBuffer dump = new StringBuffer();
91         Map<String, Integer> recordsImportedForDocType = new HashMap<String, Integer>();
92         Integer numRecordsImportedForDocType = new Integer(0);
93         int totalRecordsImported = 0;
94         try {
95             if (logger.isInfoEnabled()) {
96                 logger.info("ImportCommand.importTree() method reading file: " + file + (file != null ? " exists? " + file.exists() : " file param is null"));
97                 logger.info(String.format("ImportCommand.importTree() will timeout if import does not complete in %d seconds.", timeout));
98             }
99             reader = new LoggedXMLDirectoryReader(file, timeout);  //our overload of XMLDirectoryReader.
100             writer = new DocumentModelWriter(repoSession.getCoreSession(), toPath, 10);
101             DocumentPipe pipe = new DocumentPipeImpl(10);
102             // pipe.addTransformer(transformer);
103             pipe.setReader(reader);
104             pipe.setWriter(writer);
105             DocumentTranslationMap dtm = pipe.run();
106             if (dtm == null) {
107                 throw new Exception("Could not process import payload. Check XML markup for not-well-formed errors, elements not matching import schema, etc.");
108             }
109             Map<DocumentRef, DocumentRef> documentRefs = dtm.getDocRefMap();
110             if (documentRefs != null && documentRefs.isEmpty()) {
111                 throw new Exception("No valid records found in import payload. Check XML markup for elements not matching import or document-specific schema, etc.");
112             }
113             dump.append("<importedRecords>");
114             for (Map.Entry<DocumentRef, DocumentRef> entry : documentRefs.entrySet()) {
115                 keyDocRef = (DocumentRef) entry.getKey();
116                 valueDocRef = (DocumentRef) entry.getValue();
117                 if (keyDocRef == null || valueDocRef == null) {
118                     continue;
119                 }
120                 dump.append("<importedRecord>");
121                 docModel = repoSession.getDocument(valueDocRef);
122                 docType = docModel.getDocumentType().getName();
123                 dump.append("<doctype>" + docType + "</doctype>");
124                 dump.append("<csid>" + keyDocRef.toString() + "</csid>");
125                 dump.append("</importedRecord>");
126                 if (recordsImportedForDocType.containsKey(docType)) {
127                     numRecordsImportedForDocType = (Integer) recordsImportedForDocType.get(docType);
128                     numRecordsImportedForDocType = Integer.valueOf(numRecordsImportedForDocType.intValue() + 1);
129                     recordsImportedForDocType.put(docType, numRecordsImportedForDocType);
130                 } else {
131                     recordsImportedForDocType.put(docType, 1);
132                 }
133                 totalRecordsImported++;
134             }
135             dump.append("</importedRecords>");
136         } catch (Exception e) {
137             failed = e;
138             throw failed;
139         } finally {
140             String status = failed == null ? "Success" : "Failed";
141             dump.append("<status>" + status + "</status>");
142             dump.append("<totalRecordsImported>" + totalRecordsImported + "</totalRecordsImported>");
143             dump.append("<numRecordsImportedByDocType>");
144             TreeSet<String> keys = new TreeSet<String>(recordsImportedForDocType.keySet());
145             for (String key : keys) {
146                 dump.append("<numRecordsImported>");
147                 dump.append("<docType>" + key + "</docType>");
148                 dump.append("<numRecords>" + recordsImportedForDocType.get(key).intValue() + "</numRecords>");
149                 dump.append("</numRecordsImported>");
150             }
151             dump.append("</numRecordsImportedByDocType>");
152             if (reader != null) {
153                 dump.append("<report>" + (((LoggedXMLDirectoryReader) reader).report()) + "</report>");
154                 reader.close();
155             }
156             if (writer != null) {
157                 writer.close();
158             }
159
160             if (failed != null) {
161                 String msg = "The Import service encountered an exception: " + failed.getLocalizedMessage();
162                 logger.error(msg, failed);
163             }
164         }
165         return dump.toString();
166     }
167 }