]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0894119849cfb6b605d42298b8a20a6e305f5296
[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.Collection;
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.workflow.WorkflowClient;
33 import org.collectionspace.services.common.context.MultipartServiceContext;
34 import org.collectionspace.services.common.context.ServiceContext;
35 import org.collectionspace.services.common.document.DocumentWrapper;
36 import org.collectionspace.services.common.workflow.jaxb.WorkflowJAXBSchema;
37 import org.collectionspace.services.config.service.ObjectPartType;
38 import org.collectionspace.services.lifecycle.TransitionDef;
39 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentModelHandler;
40 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
41 import org.collectionspace.services.workflow.WorkflowCommon;
42 import org.nuxeo.ecm.core.api.ClientException;
43 import org.nuxeo.ecm.core.api.DocumentModel;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 public class WorkflowDocumentModelHandler
48         extends NuxeoDocumentModelHandler<WorkflowCommon> {
49
50     /** The logger. */
51     private static final Logger logger = LoggerFactory.getLogger(WorkflowDocumentModelHandler.class);
52     /*
53      * Workflow transitions
54      *
55      * See the "Nuxeo core default life cycle definition", an XML configuration
56      * for Nuxeo's "lifecycle" extension point that specifies valid workflow
57      * states and the operations that transition documents to those states, via:
58      *
59      * org.nuxeo.ecm.core.LifecycleCoreExtensions--lifecycle (as opposed to --types)
60      */
61     private static final String TRANSITION_UNKNOWN = "unknown";
62
63     
64     @Override
65     public void handleUpdate(DocumentWrapper<DocumentModel> wrapDoc) throws Exception {
66         //
67         // First, call the parent document handler to give it a chance to handle the workflow transition -otherwise, call
68         // the super/parent handleUpdate() method.
69         //
70         ServiceContext ctx = this.getServiceContext();
71         DocumentModelHandler targetDocHandler = (DocumentModelHandler)ctx.getProperty(WorkflowClient.TARGET_DOCHANDLER);
72         
73         // We need to make sure the repo session is available to the handler and its service context
74         targetDocHandler.setRepositorySession(this.getRepositorySession()); // Make sure the target doc handler has a repository session to work with
75         targetDocHandler.getServiceContext().setCurrentRepositorySession(this.getRepositorySession());
76         
77         TransitionDef transitionDef =  (TransitionDef)ctx.getProperty(WorkflowClient.TRANSITION_ID);
78         targetDocHandler.handleWorkflowTransition(ctx, wrapDoc, transitionDef);  // Call the target resouce's handler first
79         //
80         // If no exception occurred, then call the super's method
81         //
82         super.handleUpdate(wrapDoc);
83     }
84     
85     @Override
86     protected void handleRefNameChanges(ServiceContext ctx, DocumentModel docModel) throws ClientException {
87         //
88         // We are intentionally overriding this method to do nothing since the Workflow resource is a meta-resource without a refname
89         //
90     }    
91     
92     /*
93      * Handle read (GET)
94      */
95     @Override
96     protected Map<String, Object> extractPart(DocumentModel docModel,
97             String schema,
98             ObjectPartType partMeta,
99             Map<String, Object> addToMap)
100             throws Exception {
101         Map<String, Object> result = null;
102
103         MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed.  Everything is POX
104         if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
105             Map<String, Object> unQObjectProperties =
106                     (addToMap != null) ? addToMap : (new HashMap<String, Object>());
107             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
108             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
109             result = unQObjectProperties;
110         } //TODO: handle other media types
111
112         return result;
113     }
114
115     @Override
116     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
117             throws Exception {
118         DocumentModel docModel = wrapDoc.getWrappedObject();
119         String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
120         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
121         for (String schema : schemas) {
122             ObjectPartType partMeta = partsMetaMap.get(schema);
123             if (partMeta == null) {
124                 continue; // unknown part, ignore
125             }
126             Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
127             addOutputPart(unQObjectProperties, schema, partMeta);
128         }
129     }
130
131     /**
132      * Get the identifier for the transition that sets a document to
133      * the supplied, destination workflow state.
134      *
135      * @param state a destination workflow state.
136      * @return an identifier for the transition required to
137      * place the document in that workflow state.
138      */
139     @Deprecated 
140     private String getTransitionFromState(String state) {
141         String result = TRANSITION_UNKNOWN;
142
143         // FIXME We may wish to add calls, such as those in
144         // org.nuxeo.ecm.core.lifecycle.impl.LifeCycleImpl, to validate incoming
145         // destination workflow state and the set of allowable state transitions.
146
147         if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
148             result = WorkflowClient.WORKFLOWTRANSITION_DELETE;
149         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_ACTIVE)) {
150             result = WorkflowClient.WORKFLOWTRANSITION_UNDELETE; //FIXME, could also be transition WORKFLOWTRANSITION_UNLOCK
151         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_LOCKED)) {
152             result = WorkflowClient.WORKFLOWTRANSITION_LOCK;
153         } else {
154                 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
155                                 + state);
156         }        
157         
158         return result;
159     }
160     
161     /*
162      * Maps the transition name to handle existing states like "locked" and "deleted".  This allows us to do things like lock "deleted"
163      * records, delete "locked" records, etc.  For example, this code maps the transition name "delete" to "delete_locked" on records in the "locked" state.
164      * As another example, it would map "undelete" to "undelete_locked" for locked records and just "undelete" for records in any other state.
165      * 
166      * Essentially, this mapping allows REST API clients to use the "delete", "undelete", "lock", "unlock", etc transitions on records no matter what
167      * their current state.  Without this mapping, REST API clients would need to calculate this on their own and use the longer forms like:
168      * "delete_locked", "undelete_locked", "lock_deleted", "unlocked_deleted", etc. 
169      */
170     String getQualifiedTransitionName(DocumentWrapper<DocumentModel> wrapDoc, TransitionDef transitionDef) {
171         String result = null;
172         
173         String currentTransitionName = result = transitionDef.getName(); // begin with result set to the current name
174         DocumentModel docModel = wrapDoc.getWrappedObject();
175         Collection<String> allowedTransitionList = docModel.getAllowedStateTransitions();
176         for (String allowedTransitionName:allowedTransitionList) {
177                 if (allowedTransitionName.startsWith(currentTransitionName)) {
178                         result = allowedTransitionName;
179                         break; // we found a mapping
180                 }
181         }
182
183         return result;
184     }
185     
186     /*
187      * Handle Update (PUT)
188      */
189
190     @Override
191     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
192         String transitionToFollow = null;
193         
194         DocumentModel docModel = wrapDoc.getWrappedObject();
195         MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
196         
197         try {
198                 TransitionDef transitionDef = (TransitionDef)this.getServiceContext().getProperty(WorkflowClient.TRANSITION_ID);
199                 transitionToFollow = getQualifiedTransitionName(wrapDoc, transitionDef);
200                 docModel.followTransition(transitionToFollow);
201         } catch (Exception e) {
202                 String msg = "Unable to follow workflow transition to state = "
203                                 + transitionToFollow;
204                 logger.error(msg, e);
205                 ClientException ce = new ClientException("Unable to follow workflow transition: " + transitionToFollow);
206                 throw ce;
207         }
208     }
209 }
210