]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
60339293c6da89aefdb3c39d441332b462112048
[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     /*
62      * Workflow transitions
63      */
64     private static final String TRANSITION_DELETE = "delete";
65     private static final String TRANSITION_APPROVE = "approve";
66     private static final String TRANSITION_UNKNOWN = "unknown";
67
68     /*
69          * Handle read (GET)
70          */
71         
72         @Override
73     protected Map<String, Object> extractPart(DocumentModel docModel, 
74             String schema,
75             ObjectPartType partMeta,
76             Map<String, Object> addToMap)
77                 throws Exception {
78         Map<String, Object> result = null;
79
80         MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed.  Everything is POX
81         if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
82             Map<String, Object> unQObjectProperties =
83                     (addToMap != null) ? addToMap : (new HashMap<String, Object>());
84             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
85             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
86             result = unQObjectProperties;
87         } //TODO: handle other media types
88
89         return result;
90     }
91         
92     @Override
93     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
94             throws Exception {
95         DocumentModel docModel = wrapDoc.getWrappedObject();
96         String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
97         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
98         for (String schema : schemas) {
99             ObjectPartType partMeta = partsMetaMap.get(schema);
100             if (partMeta == null) {
101                 continue; // unknown part, ignore
102             }
103             Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
104             addOutputPart(unQObjectProperties, schema, partMeta);
105         }
106     }
107     
108     private String getTransitionFromState(String state) {
109         String result = TRANSITION_UNKNOWN;
110         if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
111                 result = TRANSITION_DELETE;
112         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_APPROVED)) {
113                 result = TRANSITION_APPROVE;
114         } 
115         return result;
116     }
117     /*
118      * Handle Update (PUT)
119      */
120         
121     @Override
122         protected void fillPart(PayloadInputPart part, DocumentModel docModel,
123                 ObjectPartType partMeta, Action action,
124                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
125                 throws Exception {
126                 WorkflowCommon workflowsCommon = (WorkflowCommon)part.getBody();
127                 docModel.followTransition(getTransitionFromState(workflowsCommon.getCurrentLifeCycleState()));
128     }    
129 }
130