]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f4d12ae6e5b2391fcd99f60f76974aec9bb172fb
[tmp/jakarta-migration.git] /
1 /**
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:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
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.
23  */
24 package org.collectionspace.services.common.workflow.service.nuxeo;
25
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import javax.ws.rs.core.MediaType;
31
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;
49
50 public class WorkflowDocumentModelHandler
51         extends DocHandlerBase<WorkflowCommon> {
52
53     /** The logger. */
54     private static final Logger logger = LoggerFactory.getLogger(WorkflowDocumentModelHandler.class);
55     /*
56      * Workflow transitions
57      *
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:
61      *
62      * org.nuxeo.ecm.core.LifecycleCoreExtensions--lifecycle (as opposed to --types)
63      */
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";
68
69     /*
70      * Handle read (GET)
71      */
72     @Override
73     protected Map<String, Object> extractPart(DocumentModel docModel,
74             String schema,
75             ObjectPartType partMeta,
76             Map<String, Object> addToMap)
77             throws Exception {
78         Map<String, Object> result = null;
79
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
88
89         return result;
90     }
91
92     @Override
93     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
94             throws Exception {
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
102             }
103             Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
104             addOutputPart(unQObjectProperties, schema, partMeta);
105         }
106     }
107
108     /**
109      * Get the identifier for the transition that sets a document to
110      * the supplied, destination workflow state.
111      *
112      * @param state a destination workflow state.
113      * @return an identifier for the transition required to
114      * place the document in that workflow state.
115      */
116     @Deprecated 
117     private String getTransitionFromState(String state) {
118         String result = TRANSITION_UNKNOWN;
119
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.
123
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;
130         } else {
131                 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
132                                 + state);
133         }        
134         
135         return result;
136     }
137     
138     /*
139      * Handle Update (PUT)
140      */
141
142     @Override
143     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
144         String transitionToFollow = null;
145         
146         DocumentModel docModel = wrapDoc.getWrappedObject();
147         MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
148         
149         try {
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);
158                 throw ce;
159         }
160     }    
161 }
162