]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
719734c6c8764fe6c0fd324039937293f3e9d5a8
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.export.nuxeo;
25
26 import java.io.InputStream;
27 import java.util.Arrays;
28 import java.util.Iterator;
29 import java.util.List;
30
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;
44
45 import org.nuxeo.ecm.core.api.DocumentModel;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * ExportDocumentModelHandler
52  *
53  * $LastChangedRevision: $
54  * $LastChangedDate: $
55  */
56 public class ExportDocumentModelHandler extends NuxeoDocumentModelHandler<ExportsCommon> {
57         private final Logger logger = LoggerFactory.getLogger(ExportDocumentModelHandler.class);
58
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 {
66
67                 NuxeoRepositoryClientImpl repoClient = (NuxeoRepositoryClientImpl) this.getRepositoryClient(serviceContext);
68                 boolean releaseRepoSession = false;
69                 CoreSessionInterface repoSession = this.getRepositorySession();
70
71                 if (repoSession == null) {
72                         repoSession = repoClient.getRepositorySession(serviceContext);
73                         releaseRepoSession = true;
74                 }
75
76                 try {
77                         Iterator<DocumentModel> documents = findDocuments(tenantBindingReader, resourceMap, serviceContext, repoSession, invocationContext);
78
79                 }
80                 finally {
81                         if (releaseRepoSession && repoSession != null) {
82                                 repoClient.releaseRepositorySession(serviceContext, repoSession);
83                         }
84                 }
85
86                 return null;
87                 // return buildExportResult(csid, params, exportFileNameProperty, outMimeType.toString(), outExportFileName);
88         }
89
90         private Iterator<DocumentModel> findDocuments(
91                         TenantBindingConfigReaderImpl tenantBindingReader,
92                         ResourceMap resourceMap,
93                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
94                         CoreSessionInterface repoSession,
95                         InvocationContext invocationContext) throws Exception {
96
97                 String docType = invocationContext.getDocType();
98                 ServiceBindingType binding = tenantBindingReader.getServiceBindingForDocType(serviceContext.getTenantId(), docType);
99                 String serviceName = binding.getName();
100
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());
110                         default:
111                                 return null;
112                 }
113         }
114
115         private Iterator<DocumentModel> findDocumentByCsid(
116                         ResourceMap resourceMap,
117                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
118                         CoreSessionInterface repoSession,
119                         String serviceName,
120                         String csid) throws Exception {
121
122                 return findDocumentsByCsid(resourceMap, serviceContext, repoSession, serviceName, Arrays.asList(csid));
123         }
124
125         private Iterator<DocumentModel> findDocumentsByCsid(
126                         ResourceMap resourceMap,
127                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
128                         CoreSessionInterface repoSession,
129                         String serviceName,
130                         List<String> csids) throws Exception {
131
132                 return new DocumentsByCsidIterator(resourceMap, serviceContext, repoSession, serviceName, csids);
133         }
134
135         private Iterator<DocumentModel> findDocumentsByType(
136                         ResourceMap resourceMap,
137                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
138                         CoreSessionInterface repoSession,
139                         String docType) {
140
141                 return null;
142         }
143
144         private Iterator<DocumentModel> findDocumentsByGroup(
145                         ResourceMap resourceMap,
146                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
147                         CoreSessionInterface repoSession,
148                         String docType) {
149
150                 return null;
151         }
152
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;
160
161         //      return result;
162         // }
163
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;
169
170                 DocumentsByCsidIterator(
171                                 ResourceMap resourceMap,
172                                 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
173                                 CoreSessionInterface repoSession,
174                                 String serviceName,
175                                 List<String> csids) throws Exception {
176
177                         NuxeoBasedResource resource = (NuxeoBasedResource) resourceMap.get(serviceName.toLowerCase());
178
179                         if (resource == null) {
180                                 throw new Exception("Resource not found for service name " + serviceName);
181                         }
182
183                         this.resource = resource;
184                         this.serviceContext = serviceContext;
185                         this.repoSession = repoSession;
186                         this.csidIterator = csids.iterator();
187                 }
188
189                 @Override
190                 public boolean hasNext() {
191                         return csidIterator.hasNext();
192                 }
193
194                 @Override
195                 public DocumentModel next() {
196                         String csid = csidIterator.next();
197
198                         try {
199                                 // PoxPayloadOut payload = resource.getWithParentCtx(serviceContext, csid);
200                                 return null;
201                         }
202                         catch (Exception e) {
203                                 logger.warn("Could not get document with csid " + csid, e);
204
205                                 return null;
206                         }
207                 }
208         }
209 }