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.core.Response;
30 import org.collectionspace.services.BatchJAXBSchema;
31 import org.collectionspace.services.jaxb.InvocableJAXBSchema;
32 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
33 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
34 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
35 import org.collectionspace.services.batch.BatchCommon;
36 import org.collectionspace.services.batch.BatchInvocable;
37 import org.collectionspace.services.client.PoxPayloadIn;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.common.ResourceMap;
40 import org.collectionspace.services.common.context.ServiceContext;
41 import org.collectionspace.services.common.document.BadRequestException;
42 import org.collectionspace.services.common.document.DocumentException;
43 import org.collectionspace.services.common.document.DocumentWrapper;
44 import org.collectionspace.services.common.invocable.Invocable;
45 import org.collectionspace.services.common.invocable.InvocationContext;
46 import org.collectionspace.services.common.invocable.InvocationResults;
47 import org.collectionspace.services.common.invocable.Invocable.InvocationError;
49 import org.jboss.resteasy.spi.ResteasyProviderFactory;
51 import org.nuxeo.ecm.core.api.DocumentModel;
52 import org.nuxeo.ecm.core.api.model.PropertyException;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler<BatchCommon> {
58 private final Logger logger = LoggerFactory.getLogger(BatchDocumentModelHandler.class);
60 protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode();
62 public InvocationResults invokeBatchJob(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String csid,
63 ResourceMap resourceMap, InvocationContext invContext) throws Exception {
65 CoreSessionInterface repoSession = null;
66 boolean releaseRepoSession = false;
68 String invocationMode = invContext.getMode();
69 String modeProperty = null;
70 boolean checkDocType = true;
71 if (BatchInvocable.INVOCATION_MODE_SINGLE.equalsIgnoreCase(invocationMode)) {
72 modeProperty = BatchJAXBSchema.SUPPORTS_SINGLE_DOC;
73 } else if (BatchInvocable.INVOCATION_MODE_LIST.equalsIgnoreCase(invocationMode)) {
74 modeProperty = BatchJAXBSchema.SUPPORTS_DOC_LIST;
75 } else if (BatchInvocable.INVOCATION_MODE_GROUP.equalsIgnoreCase(invocationMode)) {
76 modeProperty = BatchJAXBSchema.SUPPORTS_GROUP;
77 } else if (Invocable.INVOCATION_MODE_NO_CONTEXT.equalsIgnoreCase(invocationMode)) {
78 modeProperty = InvocableJAXBSchema.SUPPORTS_NO_CONTEXT;
81 throw new BadRequestException("BatchResource: unknown Invocation Mode: " + invocationMode);
84 RepositoryJavaClientImpl repoClient = (RepositoryJavaClientImpl) this.getRepositoryClient(ctx);
85 repoSession = this.getRepositorySession();
86 if (repoSession == null) {
87 repoSession = repoClient.getRepositorySession(ctx);
88 releaseRepoSession = true;
91 String className = null;
92 // Get properties from the batch docModel, and release the session
94 DocumentWrapper<DocumentModel> wrapper = repoClient.getDoc(repoSession, ctx, csid);
95 DocumentModel docModel = wrapper.getWrappedObject();
96 Boolean supports = (Boolean) docModel.getPropertyValue(modeProperty);
98 throw new BadRequestException("BatchResource: This Batch Job does not support Invocation Mode: "
102 List<String> forDocTypeList = (List<String>) docModel.getPropertyValue(BatchJAXBSchema.FOR_DOC_TYPES);
103 if (forDocTypeList == null || !forDocTypeList.contains(invContext.getDocType())) {
104 throw new BadRequestException("BatchResource: Invoked with unsupported document type: "
105 + invContext.getDocType());
108 className = (String) docModel.getPropertyValue(BatchJAXBSchema.BATCH_CLASS_NAME);
109 } catch (PropertyException pe) {
110 if (logger.isDebugEnabled()) {
111 logger.debug("Property exception getting batch values: ", pe);
114 } catch (DocumentException de) {
115 if (logger.isDebugEnabled()) {
116 logger.debug("Problem getting batch doc: ", de);
119 } catch (Exception e) {
120 if (logger.isDebugEnabled()) {
121 logger.debug("Caught exception ", e);
123 throw new DocumentException(e);
125 if (releaseRepoSession && repoSession != null) {
126 repoClient.releaseRepositorySession(ctx, repoSession);
130 className = className.trim();
131 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
132 Class<?> c = tccl.loadClass(className);
133 // enable validation assertions
134 tccl.setClassAssertionStatus(className, true);
135 if (!BatchInvocable.class.isAssignableFrom(c)) {
136 throw new RuntimeException("BatchResource: Class: " + className + " does not implement BatchInvocable!");
139 BatchInvocable batchInstance = (BatchInvocable) c.newInstance();
140 List<String> modes = batchInstance.getSupportedInvocationModes();
141 if (!modes.contains(invocationMode)) {
142 throw new BadRequestException("BatchResource: Invoked with unsupported context mode: " + invocationMode);
145 batchInstance.setInvocationContext(invContext);
147 if (resourceMap != null) {
148 batchInstance.setResourceMap(resourceMap);
150 resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
151 if (resourceMap != null) {
152 batchInstance.setResourceMap(resourceMap);
154 logger.warn("BatchResource.invoke did not get a resourceMapHolder in Context!");
159 int status = batchInstance.getCompletionStatus();
160 if (status == Invocable.STATUS_ERROR) {
161 InvocationError error = batchInstance.getErrorInfo();
162 if (error.getResponseCode() == BAD_REQUEST_STATUS) {
163 throw new BadRequestException("BatchResouce: batchProcess encountered error: "
164 + batchInstance.getErrorInfo());
166 throw new RuntimeException("BatchResouce: batchProcess encountered error: "
167 + batchInstance.getErrorInfo());
172 InvocationResults results = batchInstance.getResults();