]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
4777d291f5b9b1073350c45797c2f1d16b10029b
[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.batch.nuxeo;
25
26 import java.util.List;
27
28 import javax.ws.rs.PathParam;
29 import javax.ws.rs.core.Context;
30 import javax.ws.rs.core.Response;
31
32 import org.collectionspace.services.BatchJAXBSchema;
33 import org.collectionspace.services.jaxb.InvocableJAXBSchema;
34 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
35 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
36 import org.collectionspace.services.batch.BatchCommon;
37 import org.collectionspace.services.batch.BatchInvocable;
38 import org.collectionspace.services.client.PoxPayloadIn;
39 import org.collectionspace.services.client.PoxPayloadOut;
40 import org.collectionspace.services.common.ResourceMap;
41 import org.collectionspace.services.common.ServiceMessages;
42 import org.collectionspace.services.common.context.ServiceContext;
43 import org.collectionspace.services.common.document.BadRequestException;
44 import org.collectionspace.services.common.document.DocumentException;
45 import org.collectionspace.services.common.document.DocumentFilter;
46 import org.collectionspace.services.common.document.DocumentHandler;
47 import org.collectionspace.services.common.document.DocumentWrapper;
48 import org.collectionspace.services.common.invocable.Invocable;
49 import org.collectionspace.services.common.invocable.InvocationContext;
50 import org.collectionspace.services.common.invocable.InvocationResults;
51 import org.collectionspace.services.common.invocable.Invocable.InvocationError;
52 import org.jboss.resteasy.spi.ResteasyProviderFactory;
53 import org.nuxeo.ecm.core.api.DocumentModel;
54 import org.nuxeo.ecm.core.api.model.PropertyException;
55 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class BatchDocumentModelHandler 
60         extends DocHandlerBase<BatchCommon> {
61     private final Logger logger = LoggerFactory.getLogger(BatchDocumentModelHandler.class);
62
63         protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode();
64         
65         public InvocationResults invokeBatchJob(
66                         ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
67                         String csid,
68                         ResourceMap resourceMap, 
69                         InvocationContext invContext) throws Exception {
70
71                 RepositoryInstance repoSession = null;
72                 boolean releaseRepoSession = false;
73
74                 String invocationMode = invContext.getMode();
75                 String modeProperty = null;
76                 boolean checkDocType = true;
77                 if(BatchInvocable.INVOCATION_MODE_SINGLE.equalsIgnoreCase(invocationMode)) {
78                         modeProperty = BatchJAXBSchema.SUPPORTS_SINGLE_DOC;
79                 } else if(BatchInvocable.INVOCATION_MODE_LIST.equalsIgnoreCase(invocationMode)) {
80                         modeProperty = BatchJAXBSchema.SUPPORTS_DOC_LIST;
81                 } else if(BatchInvocable.INVOCATION_MODE_GROUP.equalsIgnoreCase(invocationMode)) {
82                         modeProperty = BatchJAXBSchema.SUPPORTS_GROUP;
83                 } else if(Invocable.INVOCATION_MODE_NO_CONTEXT.equalsIgnoreCase(invocationMode)) {
84                         modeProperty = InvocableJAXBSchema.SUPPORTS_NO_CONTEXT;
85                         checkDocType = false;
86                 } else {
87                         throw new BadRequestException("BatchResource: unknown Invocation Mode: "
88                                         +invocationMode);
89                 }
90
91                 RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl)this.getRepositoryClient(ctx);
92                 repoSession = this.getRepositorySession();
93                 if (repoSession == null) {
94                         repoSession = repoClient.getRepositorySession();
95                         releaseRepoSession = true;
96                 }
97
98                 String className = null;
99                 // Get properties from the batch docModel, and release the session
100                 try {
101                         DocumentWrapper<DocumentModel> wrapper = repoClient.getDoc(repoSession, ctx, csid);
102                         DocumentModel docModel = wrapper.getWrappedObject();
103                         Boolean supports = (Boolean)docModel.getPropertyValue(modeProperty);
104                         if(!supports) {
105                                 throw new BadRequestException("BatchResource: This Batch Job does not support Invocation Mode: "
106                                                 +invocationMode);
107                         }
108                         if(checkDocType) {
109                                 List<String> forDocTypeList = 
110                                                 (List<String>)docModel.getPropertyValue(BatchJAXBSchema.FOR_DOC_TYPES);
111                                 if(forDocTypeList==null
112                                                 || !forDocTypeList.contains(invContext.getDocType())) {
113                                         throw new BadRequestException(
114                                                         "BatchResource: Invoked with unsupported document type: "
115                                                                         +invContext.getDocType());
116                                 }
117                         }
118                         className = (String)docModel.getPropertyValue(BatchJAXBSchema.BATCH_CLASS_NAME);
119                 } catch (PropertyException pe) {
120                         if (logger.isDebugEnabled()) {
121                                 logger.debug("Property exception getting batch values: ", pe);
122                         }
123                         throw pe;
124                 } catch (DocumentException de) {
125                         if (logger.isDebugEnabled()) {
126                                 logger.debug("Problem getting batch doc: ", de);
127                         }
128                         throw de;
129                 } catch (Exception e) {
130                         if (logger.isDebugEnabled()) {
131                                 logger.debug("Caught exception ", e);
132                         }
133                         throw new DocumentException(e);
134                 } finally {
135                         if (releaseRepoSession && repoSession != null) {
136                                 repoClient.releaseRepositorySession(repoSession);
137                         }
138                 }
139                 className = className.trim();
140                 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
141                 Class<?> c = tccl.loadClass(className);
142                 // enable validation assertions
143                 tccl.setClassAssertionStatus(className, true);
144                 if(!BatchInvocable.class.isAssignableFrom(c)) {
145                         throw new RuntimeException("BatchResource: Class: "
146                                         +className+" does not implement BatchInvocable!");
147                 }
148                 BatchInvocable batchInstance = (BatchInvocable)c.newInstance();
149                 List<String> modes = batchInstance.getSupportedInvocationModes();
150                 if(!modes.contains(invocationMode)) {
151                         throw new BadRequestException(
152                                         "BatchResource: Invoked with unsupported context mode: "
153                                                         +invocationMode);
154                 }
155                 batchInstance.setInvocationContext(invContext);
156                 if(resourceMap!=null) {
157                         batchInstance.setResourceMap(resourceMap);
158                 } else {
159                         resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
160                         if(resourceMap!=null) {
161                                 batchInstance.setResourceMap(resourceMap);
162                         } else {
163                                 logger.warn("BatchResource.invoke did not get a resourceMapHolder in Context!");
164                         }
165                 }
166                 batchInstance.run();
167                 int status = batchInstance.getCompletionStatus();
168                 if(status == Invocable.STATUS_ERROR) {
169                         InvocationError error = batchInstance.getErrorInfo();
170                         if(error.getResponseCode() == BAD_REQUEST_STATUS) {
171                                 throw new BadRequestException(
172                                                 "BatchResouce: batchProcess encountered error: "
173                                                                 +batchInstance.getErrorInfo());
174                         } else {
175                                 throw new RuntimeException(
176                                                 "BatchResouce: batchProcess encountered error: "
177                                                                 +batchInstance.getErrorInfo());
178
179                         }
180                 }
181                 InvocationResults results = batchInstance.getResults();
182                 return results;
183         }
184 }
185