]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
59b3348000b3679390224e1c548ba24f8cad9622
[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.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.MultivaluedMap;
32
33 import org.collectionspace.services.client.PayloadInputPart;
34 import org.collectionspace.services.client.PoxPayloadIn;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.client.workflow.WorkflowClient;
37 import org.collectionspace.services.common.context.MultipartServiceContext;
38 import org.collectionspace.services.common.context.ServiceContext;
39 import org.collectionspace.services.common.document.BadRequestException;
40 import org.collectionspace.services.common.document.DocumentNotFoundException;
41 import org.collectionspace.services.common.document.DocumentUtils;
42 import org.collectionspace.services.common.document.DocumentWrapper;
43 import org.collectionspace.services.common.document.DocumentHandler.Action;
44 import org.collectionspace.services.common.service.ObjectPartType;
45 import org.collectionspace.services.common.workflow.jaxb.WorkflowJAXBSchema;
46 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
47 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
48 import org.collectionspace.services.workflow.WorkflowCommon;
49 import org.dom4j.Element;
50 import org.nuxeo.ecm.core.api.ClientException;
51 import org.nuxeo.ecm.core.api.DocumentModel;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
54
55 public class WorkflowDocumentModelHandler
56         extends DocHandlerBase<WorkflowCommon> {
57
58     /** The logger. */
59     private static final Logger logger = LoggerFactory.getLogger(WorkflowDocumentModelHandler.class);
60     /*
61      * Workflow transitions
62      *
63      * See the "Nuxeo core default life cycle definition", an XML configuration
64      * for Nuxeo's "lifecycle" extension point that specifies valid workflow
65      * states and the operations that transition documents to those states, via:
66      *
67      * org.nuxeo.ecm.core.LifecycleCoreExtensions--lifecycle (as opposed to --types)
68      */
69     private static final String TRANSITION_DELETE = "delete";
70     private static final String TRANSITION_APPROVE = "approve";
71     private static final String TRANSITION_UNDELETE = "undelete";
72     private static final String TRANSITION_UNKNOWN = "unknown";
73
74     /*
75      * Handle read (GET)
76      */
77     @Override
78     protected Map<String, Object> extractPart(DocumentModel docModel,
79             String schema,
80             ObjectPartType partMeta,
81             Map<String, Object> addToMap)
82             throws Exception {
83         Map<String, Object> result = null;
84
85         MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed.  Everything is POX
86         if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
87             Map<String, Object> unQObjectProperties =
88                     (addToMap != null) ? addToMap : (new HashMap<String, Object>());
89             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
90             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
91             result = unQObjectProperties;
92         } //TODO: handle other media types
93
94         return result;
95     }
96
97     @Override
98     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
99             throws Exception {
100         DocumentModel docModel = wrapDoc.getWrappedObject();
101         String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
102         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
103         for (String schema : schemas) {
104             ObjectPartType partMeta = partsMetaMap.get(schema);
105             if (partMeta == null) {
106                 continue; // unknown part, ignore
107             }
108             Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
109             addOutputPart(unQObjectProperties, schema, partMeta);
110         }
111     }
112
113     /**
114      * Get the identifier for the transition that sets a document to
115      * the supplied, destination workflow state.
116      *
117      * @param state a destination workflow state.
118      * @return an identifier for the transition required to
119      * place the document in that workflow state.
120      */
121     private String getTransitionFromState(String state) {
122         String result = TRANSITION_UNKNOWN;
123
124         // FIXME We may wish to add calls, such as those in
125         // org.nuxeo.ecm.core.lifecycle.impl.LifeCycleImpl, to validate incoming
126         // destination workflow state and the set of allowable state transitions.
127
128         if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
129             result = TRANSITION_DELETE;
130         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_APPROVED)) {
131             result = TRANSITION_APPROVE;
132         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_PROJECT)) {
133             result = TRANSITION_UNDELETE;
134         } else {
135                 logger.warn("An attempt was made to transition a document to an unknown workflow state = "
136                                 + state);
137         }        
138         
139         return result;
140     }
141     
142     /*
143      * Handle Update (PUT)
144      */
145
146     @Override
147     protected void fillPart(PayloadInputPart part, DocumentModel docModel,
148             ObjectPartType partMeta, Action action,
149             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
150             throws Exception {
151         String toState = null;
152         
153         try {
154                 WorkflowCommon workflowsCommon = (WorkflowCommon) part.getBody();
155                 toState = getTransitionFromState(workflowsCommon.getCurrentLifeCycleState());
156                 docModel.followTransition(toState);
157         } catch (Exception e) {
158                 String msg = "Unable to follow workflow transition to state = "
159                                 + toState;
160                 if (logger.isDebugEnabled() == true) {
161                         logger.debug(msg, e);
162                 }
163                 ClientException ce = new ClientException("Unable to follow workflow transition to state = "
164                                 + toState);
165                 throw ce;
166         }
167     }
168 }
169