]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6b1f83a07ee4a27a8c95805fe47e653ac2d44a43
[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.HashMap;
27 import java.util.Map;
28
29 import javax.ws.rs.core.MediaType;
30
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.DocHandlerBase;
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;
45
46 public class WorkflowDocumentModelHandler
47         extends DocHandlerBase<WorkflowCommon> {
48
49     /** The logger. */
50     private static final Logger logger = LoggerFactory.getLogger(WorkflowDocumentModelHandler.class);
51     /*
52      * Workflow transitions
53      *
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:
57      *
58      * org.nuxeo.ecm.core.LifecycleCoreExtensions--lifecycle (as opposed to --types)
59      */
60     private static final String TRANSITION_UNKNOWN = "unknown";
61
62     
63     @Override
64     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
65         //
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.
68         //
69         ServiceContext ctx = this.getServiceContext();
70         DocumentModelHandler docHandler = (DocumentModelHandler)ctx.getProperty(WorkflowClient.PARENT_DOCHANDLER);
71         TransitionDef transitionDef =  (TransitionDef)ctx.getProperty(WorkflowClient.TRANSITION_ID);
72         docHandler.handleWorkflowTransition(wrapDoc, transitionDef);
73         //
74         // If no exception occurred, then call the super's method
75         //
76         super.handleUpdate(wrapDoc);
77     }
78     
79     /*
80      * Handle read (GET)
81      */
82     @Override
83     protected Map<String, Object> extractPart(DocumentModel docModel,
84             String schema,
85             ObjectPartType partMeta,
86             Map<String, Object> addToMap)
87             throws Exception {
88         Map<String, Object> result = null;
89
90         MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed.  Everything is POX
91         if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
92             Map<String, Object> unQObjectProperties =
93                     (addToMap != null) ? addToMap : (new HashMap<String, Object>());
94             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
95             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
96             result = unQObjectProperties;
97         } //TODO: handle other media types
98
99         return result;
100     }
101
102     @Override
103     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
104             throws Exception {
105         DocumentModel docModel = wrapDoc.getWrappedObject();
106         String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
107         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
108         for (String schema : schemas) {
109             ObjectPartType partMeta = partsMetaMap.get(schema);
110             if (partMeta == null) {
111                 continue; // unknown part, ignore
112             }
113             Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
114             addOutputPart(unQObjectProperties, schema, partMeta);
115         }
116     }
117
118     /**
119      * Get the identifier for the transition that sets a document to
120      * the supplied, destination workflow state.
121      *
122      * @param state a destination workflow state.
123      * @return an identifier for the transition required to
124      * place the document in that workflow state.
125      */
126     @Deprecated 
127     private String getTransitionFromState(String state) {
128         String result = TRANSITION_UNKNOWN;
129
130         // FIXME We may wish to add calls, such as those in
131         // org.nuxeo.ecm.core.lifecycle.impl.LifeCycleImpl, to validate incoming
132         // destination workflow state and the set of allowable state transitions.
133
134         if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
135             result = WorkflowClient.WORKFLOWTRANSITION_DELETE;
136         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_ACTIVE)) {
137             result = WorkflowClient.WORKFLOWTRANSITION_UNDELETE; //FIXME, could also be transition WORKFLOWTRANSITION_UNLOCK
138         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_LOCKED)) {
139             result = WorkflowClient.WORKFLOWTRANSITION_LOCK;
140         } else {
141                 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
142                                 + state);
143         }        
144         
145         return result;
146     }
147     
148     /*
149      * Handle Update (PUT)
150      */
151
152     @Override
153     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
154         String transitionToFollow = null;
155         
156         DocumentModel docModel = wrapDoc.getWrappedObject();
157         MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
158         
159         try {
160                 TransitionDef transitionDef = (TransitionDef)this.getServiceContext().getProperty(WorkflowClient.TRANSITION_ID);
161                 transitionToFollow = transitionDef.getName();
162                 docModel.followTransition(transitionToFollow);
163         } catch (Exception e) {
164                 String msg = "Unable to follow workflow transition to state = "
165                                 + transitionToFollow;
166                 logger.error(msg, e);
167                 ClientException ce = new ClientException("Unable to follow workflow transition: " + transitionToFollow);
168                 throw ce;
169         }
170     }    
171 }
172