]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a971eb5bbe42354074e15b2dc5cd85273adca340
[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.File;
27 import java.io.FileInputStream;
28 import java.io.FileNotFoundException;
29 import java.io.FileOutputStream;
30 import java.io.InputStream;
31 import java.nio.file.Files;
32 import java.sql.Connection;
33 import java.sql.SQLException;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Map;
40
41 import javax.ws.rs.core.MediaType;
42 import javax.naming.NamingException;
43 import javax.ws.rs.core.Response;
44
45 import org.apache.commons.lang.StringUtils;
46 import org.collectionspace.authentication.AuthN;
47 import org.collectionspace.services.ExportJAXBSchema;
48 import org.collectionspace.services.account.AccountResource;
49 import org.collectionspace.services.authorization.AuthZ;
50 import org.collectionspace.services.authorization.CSpaceResource;
51 import org.collectionspace.services.authorization.PermissionException;
52 import org.collectionspace.services.authorization.URIResourceImpl;
53 import org.collectionspace.services.authorization.perms.ActionType;
54 import org.collectionspace.services.client.PoxPayloadIn;
55 import org.collectionspace.services.client.PoxPayloadOut;
56 import org.collectionspace.services.client.ExportClient;
57 import org.collectionspace.services.common.CSWebApplicationException;
58 import org.collectionspace.services.common.NuxeoBasedResource;
59 import org.collectionspace.services.common.ResourceMap;
60 import org.collectionspace.services.common.ServiceMain;
61 import org.collectionspace.services.common.api.JEEServerDeployment;
62 import org.collectionspace.services.common.api.FileTools;
63 import org.collectionspace.services.common.api.Tools;
64 import org.collectionspace.services.common.authorization_mgt.ActionGroup;
65 import org.collectionspace.services.common.config.TenantBindingConfigReaderImpl;
66 import org.collectionspace.services.common.context.ServiceBindingUtils;
67 import org.collectionspace.services.common.context.ServiceContext;
68 import org.collectionspace.services.common.document.BadRequestException;
69 import org.collectionspace.services.common.document.DocumentException;
70 import org.collectionspace.services.common.document.DocumentWrapper;
71 import org.collectionspace.services.common.invocable.Invocable;
72 import org.collectionspace.services.common.invocable.InvocationContext;
73 import org.collectionspace.services.common.invocable.InvocationContext.ListCSIDs;
74 import org.collectionspace.services.common.storage.JDBCTools;
75 import org.collectionspace.services.config.service.ServiceBindingType;
76 import org.collectionspace.services.config.types.PropertyItemType;
77 import org.collectionspace.services.export.ExportsCommon;
78 import org.collectionspace.services.jaxb.InvocableJAXBSchema;
79 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
80 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
81 import org.collectionspace.services.nuxeo.client.java.NuxeoRepositoryClientImpl;
82 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
83
84 import org.nuxeo.ecm.core.api.model.PropertyException;
85 import org.nuxeo.ecm.core.api.DocumentModel;
86
87 import org.slf4j.Logger;
88 import org.slf4j.LoggerFactory;
89
90 /**
91  * ExportDocumentModelHandler
92  *
93  * $LastChangedRevision: $
94  * $LastChangedDate: $
95  */
96 public class ExportDocumentModelHandler extends NuxeoDocumentModelHandler<ExportsCommon> {
97         private final Logger logger = LoggerFactory.getLogger(ExportDocumentModelHandler.class);
98
99         public InputStream invokeExport(
100                         TenantBindingConfigReaderImpl tenantBindingReader,
101                         ResourceMap resourceMap,
102                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
103                         InvocationContext invocationContext,
104                         StringBuffer outputMimeType,
105                         StringBuffer outputFileName) throws Exception {
106
107                 NuxeoRepositoryClientImpl repoClient = (NuxeoRepositoryClientImpl) this.getRepositoryClient(serviceContext);
108                 boolean releaseRepoSession = false;
109                 CoreSessionInterface repoSession = this.getRepositorySession();
110
111                 if (repoSession == null) {
112                         repoSession = repoClient.getRepositorySession(serviceContext);
113                         releaseRepoSession = true;
114                 }
115
116                 try {
117                         Iterator<DocumentModel> documents = findDocuments(tenantBindingReader, resourceMap, serviceContext, repoSession, invocationContext);
118
119                 }
120                 finally {
121                         if (releaseRepoSession && repoSession != null) {
122                                 repoClient.releaseRepositorySession(serviceContext, repoSession);
123                         }
124                 }
125
126                 return null;
127                 // return buildExportResult(csid, params, exportFileNameProperty, outMimeType.toString(), outExportFileName);
128         }
129
130         private Iterator<DocumentModel> findDocuments(
131                         TenantBindingConfigReaderImpl tenantBindingReader,
132                         ResourceMap resourceMap,
133                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
134                         CoreSessionInterface repoSession,
135                         InvocationContext invocationContext) throws Exception {
136
137                 String docType = invocationContext.getDocType();
138                 ServiceBindingType binding = tenantBindingReader.getServiceBindingForDocType(serviceContext.getTenantId(), docType);
139                 String serviceName = binding.getName();
140
141                 switch (invocationContext.getMode().toLowerCase()) {
142                         case Invocable.INVOCATION_MODE_SINGLE:
143                                 return findDocumentByCsid(resourceMap, serviceContext, repoSession, serviceName, invocationContext.getSingleCSID());
144                         case Invocable.INVOCATION_MODE_LIST:
145                                 return findDocumentsByCsid(resourceMap, serviceContext, repoSession, serviceName, invocationContext.getListCSIDs().getCsid());
146                         case Invocable.INVOCATION_MODE_GROUP:
147                                 return findDocumentsByGroup(resourceMap, serviceContext, repoSession, invocationContext.getGroupCSID());
148                         case Invocable.INVOCATION_MODE_NO_CONTEXT:
149                                 return findDocumentsByType(resourceMap, serviceContext, repoSession, invocationContext.getDocType());
150                         default:
151                                 return null;
152                 }
153         }
154
155         private Iterator<DocumentModel> findDocumentByCsid(
156                         ResourceMap resourceMap,
157                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
158                         CoreSessionInterface repoSession,
159                         String serviceName,
160                         String csid) throws Exception {
161
162                 return findDocumentsByCsid(resourceMap, serviceContext, repoSession, serviceName, Arrays.asList(csid));
163         }
164
165         private Iterator<DocumentModel> findDocumentsByCsid(
166                         ResourceMap resourceMap,
167                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
168                         CoreSessionInterface repoSession,
169                         String serviceName,
170                         List<String> csids) throws Exception {
171
172                 return new DocumentsByCsidIterator(resourceMap, serviceContext, repoSession, serviceName, csids);
173         }
174
175         private Iterator<DocumentModel> findDocumentsByType(
176                         ResourceMap resourceMap,
177                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
178                         CoreSessionInterface repoSession,
179                         String docType) {
180
181                 return null;
182         }
183
184         private Iterator<DocumentModel> findDocumentsByGroup(
185                         ResourceMap resourceMap,
186                         ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
187                         CoreSessionInterface repoSession,
188                         String docType) {
189
190                 return null;
191         }
192
193         // private InputStream buildExportResult(String exportCSID,
194         //              HashMap<String, Object> params,
195         //              String exportFileName,
196         //              String outputMimeType,
197         //              StringBuffer outExportFileName) throws Exception {
198         //      Connection conn = null;
199         //      InputStream result = null;
200
201         //      return result;
202         // }
203
204         private class DocumentsByCsidIterator implements Iterator<DocumentModel> {
205                 private NuxeoBasedResource resource;
206                 private ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext;
207                 private CoreSessionInterface repoSession;
208                 private Iterator<String> csidIterator;
209
210                 DocumentsByCsidIterator(
211                                 ResourceMap resourceMap,
212                                 ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
213                                 CoreSessionInterface repoSession,
214                                 String serviceName,
215                                 List<String> csids) throws Exception {
216
217                         NuxeoBasedResource resource = (NuxeoBasedResource) resourceMap.get(serviceName.toLowerCase());
218
219                         if (resource == null) {
220                                 throw new Exception("Resource not found for service name " + serviceName);
221                         }
222
223                         this.resource = resource;
224                         this.serviceContext = serviceContext;
225                         this.repoSession = repoSession;
226                         this.csidIterator = csids.iterator();
227                 }
228
229                 @Override
230                 public boolean hasNext() {
231                         return csidIterator.hasNext();
232                 }
233
234                 @Override
235                 public DocumentModel next() {
236                         String csid = csidIterator.next();
237
238                         try {
239                                 // PoxPayloadOut payload = resource.getWithParentCtx(serviceContext, csid);
240                                 return null;
241                         }
242                         catch (Exception e) {
243                                 logger.warn("Could not get document with csid " + csid, e);
244
245                                 return null;
246                         }
247                 }
248         }
249 }