]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
95f067c37ffdba62160ffcd63fd1affd2f9e796d
[tmp/jakarta-migration.git] /
1 /**     \r
2  * AbstractMultiPartCollectionSpaceResourceImpl.java\r
3  *\r
4  * {Purpose of This Class}\r
5  *\r
6  * {Other Notes Relating to This Class (Optional)}\r
7  *\r
8  * $LastChangedBy: $\r
9  * $LastChangedRevision: $\r
10  * $LastChangedDate: $\r
11  *\r
12  * This document is a part of the source code and related artifacts\r
13  * for CollectionSpace, an open source collections management system\r
14  * for museums and related institutions:\r
15  *\r
16  * http://www.collectionspace.org\r
17  * http://wiki.collectionspace.org\r
18  *\r
19  * Copyright © 2009 {Contributing Institution}\r
20  *\r
21  * Licensed under the Educational Community License (ECL), Version 2.0.\r
22  * You may not use this file except in compliance with this License.\r
23  *\r
24  * You may obtain a copy of the ECL 2.0 License at\r
25  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
26  */\r
27 package org.collectionspace.services.common;\r
28 \r
29 import java.util.Iterator;\r
30 import java.util.List;\r
31 \r
32 import javax.ws.rs.GET;\r
33 import javax.ws.rs.PUT;\r
34 import javax.ws.rs.Path;\r
35 import javax.ws.rs.PathParam;\r
36 \r
37 import org.collectionspace.services.client.PayloadOutputPart;\r
38 import org.collectionspace.services.client.PoxPayloadIn;\r
39 import org.collectionspace.services.client.PoxPayloadOut;\r
40 import org.collectionspace.services.client.workflow.WorkflowClient;\r
41 import org.collectionspace.services.common.context.MultipartServiceContext;\r
42 import org.collectionspace.services.common.context.MultipartServiceContextFactory;\r
43 import org.collectionspace.services.common.context.ServiceContext;\r
44 import org.collectionspace.services.common.context.ServiceContextFactory;\r
45 import org.collectionspace.services.common.document.DocumentHandler;\r
46 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;\r
47 import org.collectionspace.services.lifecycle.Lifecycle;\r
48 import org.collectionspace.services.lifecycle.TransitionDef;\r
49 import org.collectionspace.services.workflow.WorkflowCommon;\r
50 import org.dom4j.DocumentException;\r
51 \r
52 import org.slf4j.Logger;\r
53 import org.slf4j.LoggerFactory;\r
54 \r
55 /**\r
56  * The Class AbstractMultiPartCollectionSpaceResourceImpl.\r
57  */\r
58 public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {\r
59 \r
60     protected final Logger logger = LoggerFactory.getLogger(this.getClass());\r
61 \r
62     @Override\r
63     public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {\r
64         return MultipartServiceContextFactory.get();\r
65     }\r
66 \r
67     @Override\r
68     public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {\r
69         return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());\r
70     }\r
71 \r
72     /**\r
73      * Creates the document handler.\r
74      * \r
75      * @param serviceContext the service context\r
76      * @param schemaName the schema name\r
77      * @param commonClass the common class\r
78      * \r
79      * @return the document handler\r
80      * \r
81      * @throws Exception the exception\r
82      */\r
83     public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,\r
84             String schemaName,\r
85             Class<?> commonClass) throws Exception {\r
86         DocumentHandler result = null;\r
87 \r
88         MultipartServiceContext ctx = (MultipartServiceContext) serviceContext;\r
89         Object commonPart = null;\r
90         if (ctx.getInput() != null) {\r
91             commonPart = ctx.getInputPart(schemaName);\r
92         }\r
93         result = super.createDocumentHandler(ctx, commonPart);\r
94 \r
95         return result;\r
96     }\r
97 \r
98     /**\r
99      * Creates the document handler.\r
100      * \r
101      * @param ctx the ctx\r
102      * @param commonClass the common class\r
103      * \r
104      * @return the document handler\r
105      * \r
106      * @throws Exception the exception\r
107      */\r
108     public DocumentHandler createDocumentHandler(\r
109             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,\r
110             Class<Object> commonClass) throws Exception {\r
111         return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);\r
112     }\r
113 \r
114     /**\r
115      * Creates the contact document handler.\r
116      * \r
117      * @param ctx the ctx\r
118      * @param inAuthority the in authority\r
119      * @param inItem the in item\r
120      * \r
121      * @return the document handler\r
122      * \r
123      * @throws Exception the exception\r
124      */\r
125     protected WorkflowDocumentModelHandler createWorkflowDocumentHandler(\r
126             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {\r
127 \r
128         WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler) createDocumentHandler(ctx,\r
129                 WorkflowClient.SERVICE_COMMONPART_NAME,\r
130                 WorkflowCommon.class);\r
131 \r
132         return docHandler;\r
133     }\r
134 \r
135     /*\r
136      * JAX-RS Annotated methods\r
137      */\r
138     @GET\r
139     @Path(WorkflowClient.SERVICE_PATH)\r
140     public Lifecycle getWorkflow() {\r
141         Lifecycle result;\r
142 \r
143         String documentType = "undefined";\r
144         MultipartServiceContext ctx = null;\r
145         try {\r
146             ctx = (MultipartServiceContext) createServiceContext();\r
147             DocumentHandler handler = ctx.getDocumentHandler();\r
148             result = handler.getLifecycle();\r
149         } catch (Exception e) {\r
150             throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, ctx.getDocumentType());\r
151         }\r
152 \r
153         if (result == null) {\r
154                 result = new Lifecycle();\r
155                 result.setName("No life cycle defined for:" + documentType);\r
156         }\r
157         \r
158         return result;\r
159     }\r
160     \r
161     /*\r
162      * JAX-RS Annotated methods\r
163      */\r
164     \r
165     /*\r
166      * We should change this method.  The RepositoryClient (from call to getRepositoryClient) should support a call getWorkflowTransition() instead.\r
167      */    \r
168     @GET\r
169     @Path("{csid}" + WorkflowClient.SERVICE_PATH)\r
170     public byte[] getWorkflow(\r
171             @PathParam("csid") String csid) {\r
172         PoxPayloadOut result = null;\r
173 \r
174         try {\r
175             ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();\r
176             String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();\r
177 \r
178             MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);\r
179             WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);\r
180             ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace\r
181             getRepositoryClient(ctx).get(ctx, csid, handler);\r
182             result = ctx.getOutput();\r
183         } catch (Exception e) {\r
184             throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);\r
185         }\r
186 \r
187         return result.getBytes();\r
188     }\r
189     \r
190     protected TransitionDef getTransitionDef(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String transition) {\r
191         TransitionDef result = null;\r
192         \r
193         try {\r
194                         Lifecycle lifecycle = ctx.getDocumentHandler().getLifecycle();\r
195                         List<TransitionDef> transitionDefList = lifecycle.getTransitionDefList().getTransitionDef();\r
196                         Iterator<TransitionDef> iter = transitionDefList.iterator();\r
197                         boolean found = false;\r
198                         while (iter.hasNext() && found == false) {\r
199                                 TransitionDef transitionDef = iter.next();\r
200                                 if (transitionDef.getName().equalsIgnoreCase(transition)) {\r
201                                         result = transitionDef;\r
202                                         found = true;\r
203                                 }\r
204                         }\r
205                 } catch (Exception e) {\r
206                         logger.error("Exception trying to retreive life cycle information for: " + ctx.getDocumentType());\r
207                 }\r
208         \r
209         return result;\r
210     }\r
211     \r
212     private PoxPayloadIn synthEmptyWorkflowInput() {\r
213         PoxPayloadIn result = null;\r
214         \r
215         PoxPayloadOut output = new PoxPayloadOut(WorkflowClient.SERVICE_PAYLOAD_NAME);\r
216         WorkflowCommon workflowCommons = new WorkflowCommon();\r
217         PayloadOutputPart commonPart = output.addPart(WorkflowClient.SERVICE_COMMONPART_NAME, workflowCommons);\r
218         String payloadXML = output.toXML();\r
219         try {\r
220                         result = new PoxPayloadIn(payloadXML);\r
221                 } catch (DocumentException e) {\r
222                         // TODO Auto-generated catch block\r
223                         e.printStackTrace();\r
224                 }\r
225         \r
226         return result;\r
227     }\r
228     \r
229     /*\r
230      * We should change this code.  The RepositoryClient (from call to getRepositoryClient) should support a call doWorkflowTransition() instead.\r
231      */\r
232     //FIXME: This method is almost identical to the method org.collectionspace.services.common.vocabulary.updateWorkflowWithTransition() so\r
233     // they should be consolidated -be DRY (don't repeat yourself).\r
234 \r
235     @PUT\r
236     @Path("{csid}" + WorkflowClient.SERVICE_PATH + "/" + "{transition}")\r
237     public byte[] updateWorkflowWithTransition(@PathParam("csid") String csid,\r
238                 @PathParam("transition") String transition) {\r
239         PoxPayloadOut result = null;\r
240         \r
241         \r
242         try {\r
243                 //\r
244                 // Create an empty workflow_commons input part and set it into a new "workflow" sub-resource context\r
245                 PoxPayloadIn input = new PoxPayloadIn(WorkflowClient.SERVICE_PAYLOAD_NAME, new WorkflowCommon(), \r
246                                 WorkflowClient.SERVICE_COMMONPART_NAME);\r
247             MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, input);\r
248                 \r
249             // Create a service context and document handler for the parent resource.\r
250             ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();\r
251             DocumentHandler parentDocHandler = this.createDocumentHandler(parentCtx);      \r
252             ctx.setProperty(WorkflowClient.PARENT_DOCHANDLER, parentDocHandler); //added as a context param for the workflow document handler -it will call the parent's dochandler "prepareForWorkflowTranstion" method\r
253 \r
254             // When looking for the document, we need to use the parent's workspace name -not the "workflow" workspace name\r
255             String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();\r
256             ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace\r
257             \r
258                 // Get the type of transition we're being asked to make and store it as a context parameter -used by the workflow document handler\r
259             TransitionDef transitionDef = getTransitionDef(parentCtx, transition);\r
260             ctx.setProperty(WorkflowClient.TRANSITION_ID, transitionDef);\r
261 \r
262             WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);\r
263             getRepositoryClient(ctx).update(ctx, csid, handler);\r
264             result = ctx.getOutput();\r
265         } catch (Exception e) {\r
266             throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);\r
267         }\r
268         return result.getBytes();\r
269     }\r
270     \r
271 }\r