]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
14a1e7801f10591a1a1df87a99db334335a1fb0c
[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.PayloadInputPart;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.client.workflow.WorkflowClient;
35 import org.collectionspace.services.common.context.ServiceContext;
36 import org.collectionspace.services.common.document.DocumentWrapper;
37 import org.collectionspace.services.common.workflow.jaxb.WorkflowJAXBSchema;
38 import org.collectionspace.services.config.service.ObjectPartType;
39 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
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_DELETE = "delete";
61     private static final String TRANSITION_APPROVE = "approve";
62     private static final String TRANSITION_UNDELETE = "undelete";
63     private static final String TRANSITION_UNKNOWN = "unknown";
64
65     /*
66      * Handle read (GET)
67      */
68     @Override
69     protected Map<String, Object> extractPart(DocumentModel docModel,
70             String schema,
71             ObjectPartType partMeta,
72             Map<String, Object> addToMap)
73             throws Exception {
74         Map<String, Object> result = null;
75
76         MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed.  Everything is POX
77         if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
78             Map<String, Object> unQObjectProperties =
79                     (addToMap != null) ? addToMap : (new HashMap<String, Object>());
80             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
81             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
82             result = unQObjectProperties;
83         } //TODO: handle other media types
84
85         return result;
86     }
87
88     @Override
89     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
90             throws Exception {
91         DocumentModel docModel = wrapDoc.getWrappedObject();
92         String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
93         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
94         for (String schema : schemas) {
95             ObjectPartType partMeta = partsMetaMap.get(schema);
96             if (partMeta == null) {
97                 continue; // unknown part, ignore
98             }
99             Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
100             addOutputPart(unQObjectProperties, schema, partMeta);
101         }
102     }
103
104     /**
105      * Get the identifier for the transition that sets a document to
106      * the supplied, destination workflow state.
107      *
108      * @param state a destination workflow state.
109      * @return an identifier for the transition required to
110      * place the document in that workflow state.
111      */
112     private String getTransitionFromState(String state) {
113         String result = TRANSITION_UNKNOWN;
114
115         // FIXME We may wish to add calls, such as those in
116         // org.nuxeo.ecm.core.lifecycle.impl.LifeCycleImpl, to validate incoming
117         // destination workflow state and the set of allowable state transitions.
118
119         if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
120             result = TRANSITION_DELETE;
121         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_APPROVED)) {
122             result = TRANSITION_APPROVE;
123         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_PROJECT)) {
124             result = TRANSITION_UNDELETE;
125         } else {
126                 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
127                                 + state);
128         }        
129         
130         return result;
131     }
132     
133     /*
134      * Handle Update (PUT)
135      */
136
137     @Override
138     protected void fillPart(PayloadInputPart part, DocumentModel docModel,
139             ObjectPartType partMeta, Action action,
140             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
141             throws Exception {
142         String toState = null;
143         
144         try {
145                 WorkflowCommon workflowsCommon = (WorkflowCommon) part.getBody();
146                 toState = getTransitionFromState(workflowsCommon.getCurrentLifeCycleState());
147                 docModel.followTransition(toState);
148         } catch (Exception e) {
149                 String msg = "Unable to follow workflow transition to state = "
150                                 + toState;
151                 if (logger.isDebugEnabled() == true) {
152                         logger.debug(msg, e);
153                 }
154                 ClientException ce = new ClientException("Unable to follow workflow transition to state = "
155                                 + toState);
156                 throw ce;
157         }
158     }
159 }
160