]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
217cf60e8bfa715f7e6e7148f9fafcebdd62b66c
[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.Consumes;\r
33 import javax.ws.rs.GET;\r
34 import javax.ws.rs.PUT;\r
35 import javax.ws.rs.Path;\r
36 import javax.ws.rs.PathParam;\r
37 import javax.ws.rs.Produces;\r
38 import javax.ws.rs.WebApplicationException;\r
39 import javax.ws.rs.core.Response;\r
40 \r
41 import org.collectionspace.services.client.PayloadOutputPart;\r
42 import org.collectionspace.services.client.PoxPayloadIn;\r
43 import org.collectionspace.services.client.PoxPayloadOut;\r
44 import org.collectionspace.services.client.workflow.WorkflowClient;\r
45 import org.collectionspace.services.common.api.Tools;\r
46 import org.collectionspace.services.common.context.MultipartServiceContext;\r
47 import org.collectionspace.services.common.context.MultipartServiceContextFactory;\r
48 import org.collectionspace.services.common.context.ServiceContext;\r
49 import org.collectionspace.services.common.context.ServiceContextFactory;\r
50 import org.collectionspace.services.common.document.DocumentHandler;\r
51 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;\r
52 import org.collectionspace.services.lifecycle.Lifecycle;\r
53 import org.collectionspace.services.lifecycle.TransitionDef;\r
54 import org.collectionspace.services.lifecycle.TransitionDefList;\r
55 import org.collectionspace.services.workflow.WorkflowCommon;\r
56 import org.dom4j.DocumentException;\r
57 import org.jboss.resteasy.client.ClientResponse;\r
58 \r
59 import org.slf4j.Logger;\r
60 import org.slf4j.LoggerFactory;\r
61 \r
62 /**\r
63  * The Class AbstractMultiPartCollectionSpaceResourceImpl.\r
64  */\r
65 public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {\r
66 \r
67     protected final Logger logger = LoggerFactory.getLogger(this.getClass());\r
68 \r
69     @Override\r
70     public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {\r
71         return MultipartServiceContextFactory.get();\r
72     }\r
73 \r
74 \r
75 \r
76 \r
77     @Override\r
78     public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {\r
79         return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());\r
80     }\r
81 \r
82     /**\r
83      * Creates the document handler.\r
84      * \r
85      * @param serviceContext the service context\r
86      * @param schemaName the schema name\r
87      * @param commonClass the common class\r
88      * \r
89      * @return the document handler\r
90      * \r
91      * @throws Exception the exception\r
92      */\r
93     public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,\r
94             String schemaName,\r
95             Class<?> commonClass) throws Exception {\r
96         DocumentHandler result = null;\r
97 \r
98         MultipartServiceContext ctx = (MultipartServiceContext) serviceContext;\r
99         Object commonPart = null;\r
100         if (ctx.getInput() != null) {\r
101             commonPart = ctx.getInputPart(schemaName);\r
102         }\r
103         result = super.createDocumentHandler(ctx, commonPart);\r
104 \r
105         return result;\r
106     }\r
107 \r
108     /**\r
109      * Creates the document handler.\r
110      * \r
111      * @param ctx the ctx\r
112      * @param commonClass the common class\r
113      * \r
114      * @return the document handler\r
115      * \r
116      * @throws Exception the exception\r
117      */\r
118     public DocumentHandler createDocumentHandler(\r
119             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,\r
120             Class<Object> commonClass) throws Exception {\r
121         return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);\r
122     }\r
123 \r
124     /**\r
125      * Creates the contact document handler.\r
126      * \r
127      * @param ctx the ctx\r
128      * @param inAuthority the in authority\r
129      * @param inItem the in item\r
130      * \r
131      * @return the document handler\r
132      * \r
133      * @throws Exception the exception\r
134      */\r
135     protected WorkflowDocumentModelHandler createWorkflowDocumentHandler(\r
136             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {\r
137 \r
138         WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler) createDocumentHandler(ctx,\r
139                 WorkflowClient.SERVICE_COMMONPART_NAME,\r
140                 WorkflowCommon.class);\r
141 \r
142         return docHandler;\r
143     }\r
144 \r
145     /*\r
146      * JAX-RS Annotated methods\r
147      */\r
148     @GET\r
149     @Path(WorkflowClient.SERVICE_PATH)\r
150     public Lifecycle getWorkflow() {\r
151         Lifecycle result;\r
152 \r
153         String documentType = "undefined";\r
154         MultipartServiceContext ctx = null;\r
155         try {\r
156             ctx = (MultipartServiceContext) createServiceContext();\r
157             DocumentHandler handler = ctx.getDocumentHandler();\r
158             result = handler.getLifecycle();\r
159         } catch (Exception e) {\r
160             throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, ctx.getDocumentType());\r
161         }\r
162 \r
163         if (result == null) {\r
164                 result = new Lifecycle();\r
165                 result.setName("No life cycle defined for:" + documentType);\r
166         }\r
167         \r
168         return result;\r
169     }\r
170     \r
171     /*\r
172      * JAX-RS Annotated methods\r
173      */\r
174     @GET\r
175     @Path("{csid}" + WorkflowClient.SERVICE_PATH)\r
176     public byte[] getWorkflow(\r
177             @PathParam("csid") String csid) {\r
178         PoxPayloadOut result = null;\r
179 \r
180         try {\r
181             ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();\r
182             String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();\r
183 \r
184             MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);\r
185             WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);\r
186             ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace\r
187             getRepositoryClient(ctx).get(ctx, csid, handler);\r
188             result = ctx.getOutput();\r
189         } catch (Exception e) {\r
190             throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);\r
191         }\r
192 \r
193         return result.getBytes();\r
194     }\r
195     \r
196     protected TransitionDef getTransitionDef(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String transition) {\r
197         TransitionDef result = null;\r
198         \r
199         try {\r
200                         Lifecycle lifecycle = ctx.getDocumentHandler().getLifecycle();\r
201                         List<TransitionDef> transitionDefList = lifecycle.getTransitionDefList().getTransitionDef();\r
202                         Iterator<TransitionDef> iter = transitionDefList.iterator();\r
203                         boolean found = false;\r
204                         while (iter.hasNext() && found == false) {\r
205                                 TransitionDef transitionDef = iter.next();\r
206                                 if (transitionDef.getName().equalsIgnoreCase(transition)) {\r
207                                         result = transitionDef;\r
208                                         found = true;\r
209                                 }\r
210                         }\r
211                 } catch (Exception e) {\r
212                         logger.error("Exception trying to retreive life cycle information for: " + ctx.getDocumentType());\r
213                 }\r
214         \r
215         return result;\r
216     }\r
217     \r
218     private PoxPayloadIn synthEmptyWorkflowInput() {\r
219         PoxPayloadIn result = null;\r
220         \r
221         PoxPayloadOut output = new PoxPayloadOut(WorkflowClient.SERVICE_PAYLOAD_NAME);\r
222         WorkflowCommon workflowCommons = new WorkflowCommon();\r
223         PayloadOutputPart commonPart = output.addPart(WorkflowClient.SERVICE_COMMONPART_NAME, workflowCommons);\r
224         String payloadXML = output.toXML();\r
225         try {\r
226                         result = new PoxPayloadIn(payloadXML);\r
227                 } catch (DocumentException e) {\r
228                         // TODO Auto-generated catch block\r
229                         e.printStackTrace();\r
230                 }\r
231         \r
232         return result;\r
233     }\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         try {\r
241                 PoxPayloadIn input = new PoxPayloadIn(WorkflowClient.SERVICE_PAYLOAD_NAME, new WorkflowCommon(), \r
242                                 WorkflowClient.SERVICE_COMMONPART_NAME);\r
243                 \r
244             ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();\r
245             String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();\r
246                 \r
247             TransitionDef transitionDef = getTransitionDef(parentCtx, transition);\r
248             MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, input);\r
249             ctx.setProperty(WorkflowClient.TRANSITION_ID, transitionDef);\r
250             WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);\r
251             ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace\r
252             getRepositoryClient(ctx).update(ctx, csid, handler);\r
253             result = ctx.getOutput();\r
254         } catch (Exception e) {\r
255             throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);\r
256         }\r
257         return result.getBytes();\r
258     }\r
259     \r
260 }\r