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.ArrayList;
27 import java.util.HashMap;
30 import javax.ws.rs.core.MediaType;
32 import org.collectionspace.services.client.PayloadInputPart;
33 import org.collectionspace.services.client.PoxPayloadIn;
34 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.client.workflow.WorkflowClient;
36 import org.collectionspace.services.common.context.MultipartServiceContext;
37 import org.collectionspace.services.common.context.ServiceContext;
38 import org.collectionspace.services.common.document.DocumentWrapper;
39 import org.collectionspace.services.common.document.DocumentHandler.Action;
40 import org.collectionspace.services.common.workflow.jaxb.WorkflowJAXBSchema;
41 import org.collectionspace.services.config.service.ObjectPartType;
42 import org.collectionspace.services.lifecycle.TransitionDef;
43 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
44 import org.collectionspace.services.workflow.WorkflowCommon;
45 import org.nuxeo.ecm.core.api.ClientException;
46 import org.nuxeo.ecm.core.api.DocumentModel;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class WorkflowDocumentModelHandler
51 extends DocHandlerBase<WorkflowCommon> {
54 private static final Logger logger = LoggerFactory.getLogger(WorkflowDocumentModelHandler.class);
56 * Workflow transitions
58 * See the "Nuxeo core default life cycle definition", an XML configuration
59 * for Nuxeo's "lifecycle" extension point that specifies valid workflow
60 * states and the operations that transition documents to those states, via:
62 * org.nuxeo.ecm.core.LifecycleCoreExtensions--lifecycle (as opposed to --types)
64 private static final String TRANSITION_DELETE = "delete";
65 private static final String TRANSITION_APPROVE = "approve";
66 private static final String TRANSITION_UNDELETE = "undelete";
67 private static final String TRANSITION_UNKNOWN = "unknown";
73 protected Map<String, Object> extractPart(DocumentModel docModel,
75 ObjectPartType partMeta,
76 Map<String, Object> addToMap)
78 Map<String, Object> result = null;
80 MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed. Everything is POX
81 if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
82 Map<String, Object> unQObjectProperties =
83 (addToMap != null) ? addToMap : (new HashMap<String, Object>());
84 unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
85 unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
86 result = unQObjectProperties;
87 } //TODO: handle other media types
93 public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
95 DocumentModel docModel = wrapDoc.getWrappedObject();
96 String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
97 Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
98 for (String schema : schemas) {
99 ObjectPartType partMeta = partsMetaMap.get(schema);
100 if (partMeta == null) {
101 continue; // unknown part, ignore
103 Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
104 addOutputPart(unQObjectProperties, schema, partMeta);
109 * Get the identifier for the transition that sets a document to
110 * the supplied, destination workflow state.
112 * @param state a destination workflow state.
113 * @return an identifier for the transition required to
114 * place the document in that workflow state.
117 private String getTransitionFromState(String state) {
118 String result = TRANSITION_UNKNOWN;
120 // FIXME We may wish to add calls, such as those in
121 // org.nuxeo.ecm.core.lifecycle.impl.LifeCycleImpl, to validate incoming
122 // destination workflow state and the set of allowable state transitions.
124 if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
125 result = WorkflowClient.WORKFLOWTRANSITION_DELETE;
126 } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_ACTIVE)) {
127 result = WorkflowClient.WORKFLOWTRANSITION_UNDELETE; //FIXME, could also be transition WORKFLOWTRANSITION_UNLOCK
128 } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_LOCKED)) {
129 result = WorkflowClient.WORKFLOWTRANSITION_LOCK;
131 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
139 * Handle Update (PUT)
143 public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
144 String transitionToFollow = null;
146 DocumentModel docModel = wrapDoc.getWrappedObject();
147 MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
150 TransitionDef transitionDef = (TransitionDef)this.getServiceContext().getProperty(WorkflowClient.TRANSITION_ID);
151 transitionToFollow = transitionDef.getName();
152 docModel.followTransition(transitionToFollow);
153 } catch (Exception e) {
154 String msg = "Unable to follow workflow transition to state = "
155 + transitionToFollow;
156 logger.error(msg, e);
157 ClientException ce = new ClientException("Unable to follow workflow transition: " + transitionToFollow);