]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2f47a3a84f97640a144ff2ed1403448df64eddde
[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
32 import org.collectionspace.services.client.PayloadInputPart;
33 import org.collectionspace.services.client.PoxPayloadIn;
34 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.common.context.MultipartServiceContext;
36 import org.collectionspace.services.common.context.ServiceContext;
37 import org.collectionspace.services.common.document.BadRequestException;
38 import org.collectionspace.services.common.document.DocumentUtils;
39 import org.collectionspace.services.common.document.DocumentWrapper;
40 import org.collectionspace.services.common.document.DocumentHandler.Action;
41 import org.collectionspace.services.common.service.ObjectPartType;
42 import org.collectionspace.services.common.workflow.client.WorkflowClient;
43 import org.collectionspace.services.common.workflow.jaxb.WorkflowJAXBSchema;
44 import org.collectionspace.services.nuxeo.client.java.DocHandlerBase;
45 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
46 import org.collectionspace.services.workflow.WorkflowsCommon;
47 import org.dom4j.Element;
48 import org.nuxeo.ecm.core.api.DocumentModel;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class WorkflowDocumentModelHandler 
53         extends DocHandlerBase<WorkflowsCommon> {
54
55     /** The logger. */
56     private final Logger logger = LoggerFactory.getLogger(WorkflowDocumentModelHandler.class);
57     
58     /*
59      * Workflow transitions
60      */
61     private static final String TRANSITION_DELETE = "delete";
62     private static final String TRANSITION_APPROVE = "approve";
63     private static final String TRANSITION_UNKNOWN = "unknown";
64
65     /*
66          * Handle read (GET)
67          */
68         
69         @Override
70     protected Map<String, Object> extractPart(DocumentModel docModel, 
71             String schema,
72             ObjectPartType partMeta,
73             Map<String, Object> addToMap)
74                 throws Exception {
75         Map<String, Object> result = null;
76
77         MediaType mt = MediaType.valueOf(partMeta.getContent().getContentType()); //FIXME: REM - This is no longer needed.  Everything is POX
78         if (mt.equals(MediaType.APPLICATION_XML_TYPE)) {
79             Map<String, Object> unQObjectProperties =
80                     (addToMap != null) ? addToMap : (new HashMap<String, Object>());
81             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_LIFECYCLEPOLICY, docModel.getLifeCyclePolicy());
82             unQObjectProperties.put(WorkflowJAXBSchema.WORKFLOW_CURRENTLIFECYCLESTATE, docModel.getCurrentLifeCycleState());
83             result = unQObjectProperties;
84         } //TODO: handle other media types
85
86         return result;
87     }
88         
89     @Override
90     public void extractAllParts(DocumentWrapper<DocumentModel> wrapDoc)
91             throws Exception {
92         DocumentModel docModel = wrapDoc.getWrappedObject();
93         String[] schemas = {WorkflowClient.SERVICE_COMMONPART_NAME};
94         Map<String, ObjectPartType> partsMetaMap = getServiceContext().getPartsMetadata();
95         for (String schema : schemas) {
96             ObjectPartType partMeta = partsMetaMap.get(schema);
97             if (partMeta == null) {
98                 continue; // unknown part, ignore
99             }
100             Map<String, Object> unQObjectProperties = extractPart(docModel, schema, partMeta);
101             addOutputPart(unQObjectProperties, schema, partMeta);
102         }
103     }
104     
105     private String getTransitionFromState(String state) {
106         String result = TRANSITION_UNKNOWN;
107         if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_DELETED)) {
108                 result = TRANSITION_DELETE;
109         } else if (state.equalsIgnoreCase(WorkflowClient.WORKFLOWSTATE_APPROVED)) {
110                 result = TRANSITION_APPROVE;
111         } 
112         return result;
113     }
114     /*
115      * Handle Update (PUT)
116      */
117         
118     @Override
119         protected void fillPart(PayloadInputPart part, DocumentModel docModel,
120                 ObjectPartType partMeta, Action action,
121                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
122                 throws Exception {
123                 WorkflowsCommon workflowsCommon = (WorkflowsCommon)part.getBody();
124                 docModel.followTransition(getTransitionFromState(workflowsCommon.getCurrentLifeCycleState()));
125     }
126
127 }
128