]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
54c9a1f34076fa177e6fbde703d2ea197b29b2b0
[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     @Override
80     protected void handleRefNameChanges(ServiceContext ctx, DocumentModel docModel) throws ClientException {
81         //
82         // We are intentionally overriding this method to do nothing since the Workflow resource is a meta-resource without a refname
83         //
84     }    
85     
86     /*
87      * Handle read (GET)
88      */
89     @Override
90     protected Map<String, Object> extractPart(DocumentModel docModel,
91             String schema,
92             ObjectPartType partMeta,
93             Map<String, Object> addToMap)
94             throws Exception {
95         Map<String, Object> result = null;
96
97         MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed.  Everything is POX
98         if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
99             Map<String, Object> unQObjectProperties =
100                     (addToMap != null) ? addToMap : (new HashMap<String, Object>());
101             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
102             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
103             result = unQObjectProperties;
104         } //TODO: handle other media types
105
106         return result;
107     }
108
109     @Override
110     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
111             throws Exception {
112         DocumentModel docModel = wrapDoc.getWrappedObject();
113         String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
114         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
115         for (String schema : schemas) {
116             ObjectPartType partMeta = partsMetaMap.get(schema);
117             if (partMeta == null) {
118                 continue; // unknown part, ignore
119             }
120             Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
121             addOutputPart(unQObjectProperties, schema, partMeta);
122         }
123     }
124
125     /**
126      * Get the identifier for the transition that sets a document to
127      * the supplied, destination workflow state.
128      *
129      * @param state a destination workflow state.
130      * @return an identifier for the transition required to
131      * place the document in that workflow state.
132      */
133     @Deprecated 
134     private String getTransitionFromState(String state) {
135         String result = TRANSITION_UNKNOWN;
136
137         // FIXME We may wish to add calls, such as those in
138         // org.nuxeo.ecm.core.lifecycle.impl.LifeCycleImpl, to validate incoming
139         // destination workflow state and the set of allowable state transitions.
140
141         if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
142             result = WorkflowClient.WORKFLOWTRANSITION_DELETE;
143         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_ACTIVE)) {
144             result = WorkflowClient.WORKFLOWTRANSITION_UNDELETE; //FIXME, could also be transition WORKFLOWTRANSITION_UNLOCK
145         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_LOCKED)) {
146             result = WorkflowClient.WORKFLOWTRANSITION_LOCK;
147         } else {
148                 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
149                                 + state);
150         }        
151         
152         return result;
153     }
154     
155     /*
156      * Handle Update (PUT)
157      */
158
159     @Override
160     public void fillAllParts(DocumentWrapper<DocumentModel> wrapDoc, Action action) throws Exception {
161         String transitionToFollow = null;
162         
163         DocumentModel docModel = wrapDoc.getWrappedObject();
164         MultipartServiceContext ctx = (MultipartServiceContext) getServiceContext();
165         
166         try {
167                 TransitionDef transitionDef = (TransitionDef)this.getServiceContext().getProperty(WorkflowClient.TRANSITION_ID);
168                 transitionToFollow = transitionDef.getName();
169                 docModel.followTransition(transitionToFollow);
170         } catch (Exception e) {
171                 String msg = "Unable to follow workflow transition to state = "
172                                 + transitionToFollow;
173                 logger.error(msg, e);
174                 ClientException ce = new ClientException("Unable to follow workflow transition: " + transitionToFollow);
175                 throw ce;
176         }
177     }    
178 }
179