]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
14b773cec67037439e438c090fcee3c592e68b42
[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.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;
45
46 public class WorkflowDocumentModelHandler
47         extends NuxeoDocumentModelHandler<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 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
74         //
75         // If no exception occurred, then call the super's method
76         //
77         super.handleUpdate(wrapDoc);
78     }
79     
80     @Override
81     protected void handleRefNameChanges(ServiceContext ctx, DocumentModel docModel) throws ClientException {
82         //
83         // We are intentionally overriding this method to do nothing since the Workflow resource is a meta-resource without a refname
84         //
85     }    
86     
87     /*
88      * Handle read (GET)
89      */
90     @Override
91     protected Map<String, Object> extractPart(DocumentModel docModel,
92             String schema,
93             ObjectPartType partMeta,
94             Map<String, Object> addToMap)
95             throws Exception {
96         Map<String, Object> result = null;
97
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
106
107         return result;
108     }
109
110     @Override
111     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
112             throws Exception {
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
120             }
121             Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
122             addOutputPart(unQObjectProperties, schema, partMeta);
123         }
124     }
125
126     /**
127      * Get the identifier for the transition that sets a document to
128      * the supplied, destination workflow state.
129      *
130      * @param state a destination workflow state.
131      * @return an identifier for the transition required to
132      * place the document in that workflow state.
133      */
134     @Deprecated 
135     private String getTransitionFromState(String state) {
136         String result = TRANSITION_UNKNOWN;
137
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.
141
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;
148         } else {
149                 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
150                                 + state);
151         }        
152         
153         return result;
154     }
155     
156     /*
157      * Handle Update (PUT)
158      */
159
160     @Override
161     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
162         String transitionToFollow = null;
163         
164         DocumentModel docModel = wrapDoc.getWrappedObject();
165         MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
166         
167         try {
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);
176                 throw ce;
177         }
178     }    
179 }
180