2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.export.nuxeo;
26 import java.io.InputStream;
27 import java.util.Arrays;
28 import java.util.Iterator;
29 import java.util.List;
31 import org.collectionspace.services.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.common.NuxeoBasedResource;
34 import org.collectionspace.services.common.ResourceMap;
35 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
36 import org.collectionspace.services.common.context.ServiceContext;
37 import org.collectionspace.services.common.invocable.Invocable;
38 import org.collectionspace.services.common.invocable.InvocationContext;
39 import org.collectionspace.services.config.service.ServiceBindingType;
40 import org.collectionspace.services.export.ExportsCommon;
41 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
42 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
43 import org.collectionspace.services.nuxeo.client.java.NuxeoRepositoryClientImpl;
45 import org.nuxeo.ecm.core.api.DocumentModel;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * ExportDocumentModelHandler
53 * $LastChangedRevision: $
56 public class ExportDocumentModelHandler extends NuxeoDocumentModelHandler<ExportsCommon> {
57 private final Logger logger = LoggerFactory.getLogger(ExportDocumentModelHandler.class);
59 public InputStream invokeExport(
60 TenantBindingConfigReaderImpl tenantBindingReader,
61 ResourceMap resourceMap,
62 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
63 InvocationContext invocationContext,
64 StringBuffer outputMimeType,
65 StringBuffer outputFileName) throws Exception {
67 NuxeoRepositoryClientImpl repoClient = (NuxeoRepositoryClientImpl) this.getRepositoryClient(serviceContext);
68 boolean releaseRepoSession = false;
69 CoreSessionInterface repoSession = this.getRepositorySession();
71 if (repoSession == null) {
72 repoSession = repoClient.getRepositorySession(serviceContext);
73 releaseRepoSession = true;
77 Iterator<DocumentModel> documents = findDocuments(tenantBindingReader, resourceMap, serviceContext, repoSession, invocationContext);
81 if (releaseRepoSession && repoSession != null) {
82 repoClient.releaseRepositorySession(serviceContext, repoSession);
87 // return buildExportResult(csid, params, exportFileNameProperty, outMimeType.toString(), outExportFileName);
90 private Iterator<DocumentModel> findDocuments(
91 TenantBindingConfigReaderImpl tenantBindingReader,
92 ResourceMap resourceMap,
93 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
94 CoreSessionInterface repoSession,
95 InvocationContext invocationContext) throws Exception {
97 String docType = invocationContext.getDocType();
98 ServiceBindingType binding = tenantBindingReader.getServiceBindingForDocType(serviceContext.getTenantId(), docType);
99 String serviceName = binding.getName();
101 switch (invocationContext.getMode().toLowerCase()) {
102 case Invocable.INVOCATION_MODE_SINGLE:
103 return findDocumentByCsid(resourceMap, serviceContext, repoSession, serviceName, invocationContext.getSingleCSID());
104 case Invocable.INVOCATION_MODE_LIST:
105 return findDocumentsByCsid(resourceMap, serviceContext, repoSession, serviceName, invocationContext.getListCSIDs().getCsid());
106 case Invocable.INVOCATION_MODE_GROUP:
107 return findDocumentsByGroup(resourceMap, serviceContext, repoSession, invocationContext.getGroupCSID());
108 case Invocable.INVOCATION_MODE_NO_CONTEXT:
109 return findDocumentsByType(resourceMap, serviceContext, repoSession, invocationContext.getDocType());
115 private Iterator<DocumentModel> findDocumentByCsid(
116 ResourceMap resourceMap,
117 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
118 CoreSessionInterface repoSession,
120 String csid) throws Exception {
122 return findDocumentsByCsid(resourceMap, serviceContext, repoSession, serviceName, Arrays.asList(csid));
125 private Iterator<DocumentModel> findDocumentsByCsid(
126 ResourceMap resourceMap,
127 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
128 CoreSessionInterface repoSession,
130 List<String> csids) throws Exception {
132 return new DocumentsByCsidIterator(resourceMap, serviceContext, repoSession, serviceName, csids);
135 private Iterator<DocumentModel> findDocumentsByType(
136 ResourceMap resourceMap,
137 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
138 CoreSessionInterface repoSession,
144 private Iterator<DocumentModel> findDocumentsByGroup(
145 ResourceMap resourceMap,
146 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
147 CoreSessionInterface repoSession,
153 // private InputStream buildExportResult(String exportCSID,
154 // HashMap<String, Object> params,
155 // String exportFileName,
156 // String outputMimeType,
157 // StringBuffer outExportFileName) throws Exception {
158 // Connection conn = null;
159 // InputStream result = null;
164 private class DocumentsByCsidIterator implements Iterator<DocumentModel> {
165 private NuxeoBasedResource resource;
166 private ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext;
167 private CoreSessionInterface repoSession;
168 private Iterator<String> csidIterator;
170 DocumentsByCsidIterator(
171 ResourceMap resourceMap,
172 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
173 CoreSessionInterface repoSession,
175 List<String> csids) throws Exception {
177 NuxeoBasedResource resource = (NuxeoBasedResource) resourceMap.get(serviceName.toLowerCase());
179 if (resource == null) {
180 throw new Exception("Resource not found for service name " + serviceName);
183 this.resource = resource;
184 this.serviceContext = serviceContext;
185 this.repoSession = repoSession;
186 this.csidIterator = csids.iterator();
190 public boolean hasNext() {
191 return csidIterator.hasNext();
195 public DocumentModel next() {
196 String csid = csidIterator.next();
199 // PoxPayloadOut payload = resource.getWithParentCtx(serviceContext, csid);
202 catch (Exception e) {
203 logger.warn("Could not get document with csid " + csid, e);