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.common.workflow.service.nuxeo;
26 import java.util.HashMap;
27 import java.util.List;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
33 import org.collectionspace.services.client.PayloadInputPart;
34 import org.collectionspace.services.client.PoxPayloadIn;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.client.workflow.WorkflowClient;
37 import org.collectionspace.services.common.context.MultipartServiceContext;
38 import org.collectionspace.services.common.context.ServiceContext;
39 import org.collectionspace.services.common.document.BadRequestException;
40 import org.collectionspace.services.common.document.DocumentNotFoundException;
41 import org.collectionspace.services.common.document.DocumentUtils;
42 import org.collectionspace.services.common.document.DocumentWrapper;
43 import org.collectionspace.services.common.document.DocumentHandler.Action;
44 import org.collectionspace.services.common.service.ObjectPartType;
45 import org.collectionspace.services.common.workflow.jaxb.WorkflowJAXBSchema;
46 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
47 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
48 import org.collectionspace.services.workflow.WorkflowCommon;
49 import org.dom4j.Element;
50 import org.nuxeo.ecm.core.api.ClientException;
51 import org.nuxeo.ecm.core.api.DocumentModel;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 public class WorkflowDocumentModelHandler
56 extends DocHandlerBase<WorkflowCommon> {
59 private static final Logger logger = LoggerFactory.getLogger(WorkflowDocumentModelHandler.class);
61 * Workflow transitions
63 * See the "Nuxeo core default life cycle definition", an XML configuration
64 * for Nuxeo's "lifecycle" extension point that specifies valid workflow
65 * states and the operations that transition documents to those states, via:
67 * org.nuxeo.ecm.core.LifecycleCoreExtensions--lifecycle (as opposed to --types)
69 private static final String TRANSITION_DELETE = "delete";
70 private static final String TRANSITION_APPROVE = "approve";
71 private static final String TRANSITION_UNDELETE = "undelete";
72 private static final String TRANSITION_UNKNOWN = "unknown";
78 protected Map<String, Object> extractPart(DocumentModel docModel,
80 ObjectPartType partMeta,
81 Map<String, Object> addToMap)
83 Map<String, Object> result = null;
85 MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed. Everything is POX
86 if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
87 Map<String, Object> unQObjectProperties =
88 (addToMap != null) ? addToMap : (new HashMap<String, Object>());
89 unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
90 unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
91 result = unQObjectProperties;
92 } //TODO: handle other media types
98 public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
100 DocumentModel docModel = wrapDoc.getWrappedObject();
101 String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
102 Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
103 for (String schema : schemas) {
104 ObjectPartType partMeta = partsMetaMap.get(schema);
105 if (partMeta == null) {
106 continue; // unknown part, ignore
108 Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
109 addOutputPart(unQObjectProperties, schema, partMeta);
114 * Get the identifier for the transition that sets a document to
115 * the supplied, destination workflow state.
117 * @param state a destination workflow state.
118 * @return an identifier for the transition required to
119 * place the document in that workflow state.
121 private String getTransitionFromState(String state) {
122 String result = TRANSITION_UNKNOWN;
124 // FIXME We may wish to add calls, such as those in
125 // org.nuxeo.ecm.core.lifecycle.impl.LifeCycleImpl, to validate incoming
126 // destination workflow state and the set of allowable state transitions.
128 if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
129 result = TRANSITION_DELETE;
130 } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_APPROVED)) {
131 result = TRANSITION_APPROVE;
132 } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_PROJECT)) {
133 result = TRANSITION_UNDELETE;
135 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
143 * Handle Update (PUT)
147 protected void fillPart(PayloadInputPart part, DocumentModel docModel,
148 ObjectPartType partMeta, Action action,
149 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
151 String toState = null;
154 WorkflowCommon workflowsCommon = (WorkflowCommon) part.getBody();
155 toState = getTransitionFromState(workflowsCommon.getCurrentLifeCycleState());
156 docModel.followTransition(toState);
157 } catch (Exception e) {
158 String msg = "Unable to follow workflow transition to state = "
160 if (logger.isDebugEnabled() == true) {
161 logger.debug(msg, e);
163 ClientException ce = new ClientException("Unable to follow workflow transition to state = "