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.RepositoryClientImpl;
35 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
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.context.ServiceContext;
42 import org.collectionspace.services.common.document.BadRequestException;
43 import org.collectionspace.services.common.document.DocumentException;
44 import org.collectionspace.services.common.document.DocumentWrapper;
45 import org.collectionspace.services.common.invocable.Invocable;
46 import org.collectionspace.services.common.invocable.InvocationContext;
47 import org.collectionspace.services.common.invocable.InvocationResults;
48 import org.collectionspace.services.common.invocable.Invocable.InvocationError;
49 import org.jboss.resteasy.spi.ResteasyProviderFactory;
50 import org.nuxeo.ecm.core.api.DocumentModel;
51 import org.nuxeo.ecm.core.api.model.PropertyException;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class BatchDocumentModelHandler extends NuxeoDocumentModelHandler<BatchCommon> {
56 private final Logger logger = LoggerFactory.getLogger(BatchDocumentModelHandler.class);
58 protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode();
60 public InvocationResults invokeBatchJob(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String csid,
61 ResourceMap resourceMap, InvocationContext invocationCtx) throws Exception {
63 CoreSessionInterface repoSession = null;
64 boolean releaseRepoSession = false;
66 String invocationMode = invocationCtx.getMode();
67 String modeProperty = null;
68 boolean checkDocType = true;
69 if (BatchInvocable.INVOCATION_MODE_SINGLE.equalsIgnoreCase(invocationMode)) {
70 modeProperty = BatchJAXBSchema.SUPPORTS_SINGLE_DOC;
71 } else if (BatchInvocable.INVOCATION_MODE_LIST.equalsIgnoreCase(invocationMode)) {
72 modeProperty = BatchJAXBSchema.SUPPORTS_DOC_LIST;
73 } else if (BatchInvocable.INVOCATION_MODE_GROUP.equalsIgnoreCase(invocationMode)) {
74 modeProperty = BatchJAXBSchema.SUPPORTS_GROUP;
75 } else if (Invocable.INVOCATION_MODE_NO_CONTEXT.equalsIgnoreCase(invocationMode)) {
76 modeProperty = InvocableJAXBSchema.SUPPORTS_NO_CONTEXT;
79 throw new BadRequestException("BatchResource: unknown Invocation Mode: " + invocationMode);
82 RepositoryClientImpl repoClient = (RepositoryClientImpl) this.getRepositoryClient(ctx);
83 repoSession = this.getRepositorySession();
84 if (repoSession == null) {
85 repoSession = repoClient.getRepositorySession(ctx);
86 releaseRepoSession = true;
89 String className = null;
90 // Get properties from the batch docModel, and release the session
92 DocumentWrapper<DocumentModel> wrapper = repoClient.getDoc(repoSession, ctx, csid);
93 DocumentModel docModel = wrapper.getWrappedObject();
94 Boolean supports = (Boolean) NuxeoUtils.getProperyValue(docModel, modeProperty);
96 throw new BadRequestException("BatchResource: This Batch Job does not support Invocation Mode: "
100 List<String> forDocTypeList = (List<String>) NuxeoUtils.getProperyValue(docModel, BatchJAXBSchema.FOR_DOC_TYPES); //docModel.getPropertyValue(BatchJAXBSchema.FOR_DOC_TYPES);
101 if (forDocTypeList == null || !forDocTypeList.contains(invocationCtx.getDocType())) {
102 throw new BadRequestException("BatchResource: Invoked with unsupported document type: "
103 + invocationCtx.getDocType());
106 className = (String) NuxeoUtils.getProperyValue(docModel, BatchJAXBSchema.BATCH_CLASS_NAME); //docModel.getPropertyValue(BatchJAXBSchema.BATCH_CLASS_NAME);
107 } catch (PropertyException pe) {
108 if (logger.isDebugEnabled()) {
109 logger.debug("Property exception getting batch values: ", pe);
112 } catch (DocumentException de) {
113 if (logger.isDebugEnabled()) {
114 logger.debug("Problem getting batch doc: ", de);
117 } catch (Exception e) {
118 if (logger.isDebugEnabled()) {
119 logger.debug("Caught exception ", e);
121 throw new DocumentException(e);
123 if (releaseRepoSession && repoSession != null) {
124 repoClient.releaseRepositorySession(ctx, repoSession);
128 className = className.trim();
129 ClassLoader tccl = Thread.currentThread().getContextClassLoader();
130 Class<?> c = tccl.loadClass(className);
131 // enable validation assertions
132 tccl.setClassAssertionStatus(className, true);
133 if (!BatchInvocable.class.isAssignableFrom(c)) {
134 throw new RuntimeException("BatchResource: Class: " + className + " does not implement BatchInvocable!");
137 BatchInvocable batchInstance = (BatchInvocable) c.newInstance();
138 List<String> modes = batchInstance.getSupportedInvocationModes();
139 if (!modes.contains(invocationMode)) {
140 throw new BadRequestException("BatchResource: Invoked with unsupported context mode: " + invocationMode);
143 batchInstance.setInvocationContext(invocationCtx);
144 batchInstance.setServiceContext(ctx);
146 if (resourceMap != null) {
147 batchInstance.setResourceMap(resourceMap);
149 resourceMap = ResteasyProviderFactory.getContextData(ResourceMap.class);
150 if (resourceMap != null) {
151 batchInstance.setResourceMap(resourceMap);
153 logger.warn("BatchResource.invoke did not get a resourceMapHolder in Context!");
158 int status = batchInstance.getCompletionStatus();
159 if (status == Invocable.STATUS_ERROR) {
160 InvocationError error = batchInstance.getErrorInfo();
161 if (error.getResponseCode() == BAD_REQUEST_STATUS) {
162 throw new BadRequestException("BatchResouce: batchProcess encountered error: "
163 + batchInstance.getErrorInfo());
165 throw new RuntimeException("BatchResouce: batchProcess encountered error: "
166 + batchInstance.getErrorInfo());
171 InvocationResults results = batchInstance.getResults();