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;
29 import javax.ws.rs.core.MediaType;
31 import org.collectionspace.services.client.workflow.WorkflowClient;
32 import org.collectionspace.services.common.context.MultipartServiceContext;
33 import org.collectionspace.services.common.context.ServiceContext;
34 import org.collectionspace.services.common.document.DocumentWrapper;
35 import org.collectionspace.services.common.workflow.jaxb.WorkflowJAXBSchema;
36 import org.collectionspace.services.config.service.ObjectPartType;
37 import org.collectionspace.services.lifecycle.TransitionDef;
38 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
39 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
40 import org.collectionspace.services.workflow.WorkflowCommon;
41 import org.nuxeo.ecm.core.api.ClientException;
42 import org.nuxeo.ecm.core.api.DocumentModel;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public class WorkflowDocumentModelHandler
47 extends NuxeoDocumentModelHandler<WorkflowCommon> {
50 private static final Logger logger = LoggerFactory.getLogger(WorkflowDocumentModelHandler.class);
52 * Workflow transitions
54 * See the "Nuxeo core default life cycle definition", an XML configuration
55 * for Nuxeo's "lifecycle" extension point that specifies valid workflow
56 * states and the operations that transition documents to those states, via:
58 * org.nuxeo.ecm.core.LifecycleCoreExtensions--lifecycle (as opposed to --types)
60 private static final String TRANSITION_UNKNOWN = "unknown";
64 public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
66 // First, call the parent document handler to give it a chance to handle the workflow transition -otherwise, call
67 // the super/parent handleUpdate() method.
69 ServiceContext ctx = this.getServiceContext();
70 DocumentModelHandler targetDocHandler = (DocumentModelHandler)ctx.getProperty(WorkflowClient.TARGET_DOCHANDLER);
71 targetDocHandler.setRepositorySession(this.getRepositorySession()); // Make sure the target doc handler has a repository session to work with
72 TransitionDef transitionDef = (TransitionDef)ctx.getProperty(WorkflowClient.TRANSITION_ID);
73 targetDocHandler.handleWorkflowTransition(wrapDoc, transitionDef); // Call the parent resouce's handler first
75 // If no exception occurred, then call the super's method
77 super.handleUpdate(wrapDoc);
81 protected void handleRefNameChanges(ServiceContext ctx, DocumentModel docModel) throws ClientException {
83 // We are intentionally overriding this method to do nothing since the Workflow resource is a meta-resource without a refname
91 protected Map<String, Object> extractPart(DocumentModel docModel,
93 ObjectPartType partMeta,
94 Map<String, Object> addToMap)
96 Map<String, Object> result = null;
98 MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed. Everything is POX
99 if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
100 Map<String, Object> unQObjectProperties =
101 (addToMap != null) ? addToMap : (new HashMap<String, Object>());
102 unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
103 unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
104 result = unQObjectProperties;
105 } //TODO: handle other media types
111 public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
113 DocumentModel docModel = wrapDoc.getWrappedObject();
114 String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
115 Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
116 for (String schema : schemas) {
117 ObjectPartType partMeta = partsMetaMap.get(schema);
118 if (partMeta == null) {
119 continue; // unknown part, ignore
121 Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
122 addOutputPart(unQObjectProperties, schema, partMeta);
127 * Get the identifier for the transition that sets a document to
128 * the supplied, destination workflow state.
130 * @param state a destination workflow state.
131 * @return an identifier for the transition required to
132 * place the document in that workflow state.
135 private String getTransitionFromState(String state) {
136 String result = TRANSITION_UNKNOWN;
138 // FIXME We may wish to add calls, such as those in
139 // org.nuxeo.ecm.core.lifecycle.impl.LifeCycleImpl, to validate incoming
140 // destination workflow state and the set of allowable state transitions.
142 if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
143 result = WorkflowClient.WORKFLOWTRANSITION_DELETE;
144 } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_ACTIVE)) {
145 result = WorkflowClient.WORKFLOWTRANSITION_UNDELETE; //FIXME, could also be transition WORKFLOWTRANSITION_UNLOCK
146 } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_LOCKED)) {
147 result = WorkflowClient.WORKFLOWTRANSITION_LOCK;
149 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
157 * Handle Update (PUT)
161 public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
162 String transitionToFollow = null;
164 DocumentModel docModel = wrapDoc.getWrappedObject();
165 MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
168 TransitionDef transitionDef = (TransitionDef)this.getServiceContext().getProperty(WorkflowClient.TRANSITION_ID);
169 transitionToFollow = transitionDef.getName();
170 docModel.followTransition(transitionToFollow);
171 } catch (Exception e) {
172 String msg = "Unable to follow workflow transition to state = "
173 + transitionToFollow;
174 logger.error(msg, e);
175 ClientException ce = new ClientException("Unable to follow workflow transition: " + transitionToFollow);