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