]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
75bf77a5710f591e4870d5de236e2681f6802ec8
[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 import javax.ws.rs.core.Context;\r
37 import javax.ws.rs.core.UriInfo;\r
38 \r
39 import org.collectionspace.services.client.PayloadOutputPart;\r
40 import org.collectionspace.services.client.PoxPayloadIn;\r
41 import org.collectionspace.services.client.PoxPayloadOut;\r
42 import org.collectionspace.services.client.workflow.WorkflowClient;\r
43 import org.collectionspace.services.common.context.MultipartServiceContext;\r
44 import org.collectionspace.services.common.context.MultipartServiceContextFactory;\r
45 import org.collectionspace.services.common.context.ServiceContext;\r
46 import org.collectionspace.services.common.context.ServiceContextFactory;\r
47 import org.collectionspace.services.common.document.DocumentHandler;\r
48 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;\r
49 import org.collectionspace.services.lifecycle.Lifecycle;\r
50 import org.collectionspace.services.lifecycle.TransitionDef;\r
51 import org.collectionspace.services.workflow.WorkflowCommon;\r
52 import org.dom4j.DocumentException;\r
53 \r
54 import org.slf4j.Logger;\r
55 import org.slf4j.LoggerFactory;\r
56 \r
57 /**\r
58  * The Class AbstractMultiPartCollectionSpaceResourceImpl.\r
59  */\r
60 public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {\r
61 \r
62     protected final Logger logger = LoggerFactory.getLogger(this.getClass());\r
63 \r
64     @Override\r
65     public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {\r
66         return MultipartServiceContextFactory.get();\r
67     }\r
68     \r
69     abstract protected String getOrderByField(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx);    \r
70 \r
71         abstract protected String getPartialTermMatchField(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx);\r
72     \r
73     @Override\r
74     public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {\r
75         return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());\r
76     }\r
77 \r
78     /**\r
79      * Creates the document handler.\r
80      * \r
81      * @param serviceContext the service context\r
82      * @param schemaName the schema name\r
83      * @param commonClass the common class\r
84      * \r
85      * @return the document handler\r
86      * \r
87      * @throws Exception the exception\r
88      */\r
89     public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,\r
90             String schemaName,\r
91             Class<?> commonClass) throws Exception {\r
92         DocumentHandler result = null;\r
93 \r
94         MultipartServiceContext ctx = (MultipartServiceContext) serviceContext;\r
95         Object commonPart = null;\r
96         if (ctx.getInput() != null) {\r
97             commonPart = ctx.getInputPart(schemaName);\r
98         }\r
99         result = super.createDocumentHandler(ctx, commonPart);\r
100 \r
101         return result;\r
102     }\r
103 \r
104     /**\r
105      * Creates the document handler.\r
106      * \r
107      * @param ctx the ctx\r
108      * @param commonClass the common class\r
109      * \r
110      * @return the document handler\r
111      * \r
112      * @throws Exception the exception\r
113      */\r
114     public DocumentHandler createDocumentHandler(\r
115             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,\r
116             Class<Object> commonClass) throws Exception {\r
117         return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);\r
118     }\r
119 \r
120     /**\r
121      * Creates the contact document handler.\r
122      * \r
123      * @param ctx the ctx\r
124      * @param inAuthority the in authority\r
125      * @param inItem the in item\r
126      * \r
127      * @return the document handler\r
128      * \r
129      * @throws Exception the exception\r
130      */\r
131     protected WorkflowDocumentModelHandler createWorkflowDocumentHandler(\r
132             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {\r
133 \r
134         WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler) createDocumentHandler(ctx,\r
135                 WorkflowClient.SERVICE_COMMONPART_NAME,\r
136                 WorkflowCommon.class);\r
137 \r
138         return docHandler;\r
139     }\r
140 \r
141     /*\r
142      * JAX-RS Annotated methods\r
143      */\r
144     @GET\r
145     @Path(WorkflowClient.SERVICE_PATH)\r
146     public Lifecycle getWorkflow(@Context UriInfo uriInfo) {\r
147         Lifecycle result;\r
148 \r
149         String documentType = "undefined";\r
150         MultipartServiceContext ctx = null;\r
151         try {\r
152             ctx = (MultipartServiceContext) createServiceContext(uriInfo);\r
153             DocumentHandler handler = ctx.getDocumentHandler();\r
154             result = handler.getLifecycle();\r
155         } catch (Exception e) {\r
156             throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, ctx.getDocumentType());\r
157         }\r
158 \r
159         if (result == null) {\r
160                 result = new Lifecycle();\r
161                 result.setName("No life cycle defined for:" + documentType);\r
162         }\r
163         \r
164         return result;\r
165     }\r
166     \r
167     /*\r
168      * JAX-RS Annotated methods\r
169      */\r
170     \r
171     /*\r
172      * We should change this method.  The RepositoryClient (from call to getRepositoryClient) should support a call getWorkflowTransition() instead.\r
173      */    \r
174     @GET\r
175     @Path("{csid}" + WorkflowClient.SERVICE_PATH)\r
176     public byte[] getWorkflow(\r
177                 @Context UriInfo uriInfo,\r
178             @PathParam("csid") String csid) {\r
179         return getWorkflow(uriInfo, csid, null);\r
180         \r
181     }\r
182     \r
183     // Added to expose this functionality to batch jobs via the ResourceMap\r
184     // of ResourceBase classes.\r
185     //\r
186     // Argument 'params' is unused, and exists only to avoid duplicating method signatures.\r
187     public byte[] getWorkflow(UriInfo uriInfo, String csid, String params) {\r
188         PoxPayloadOut result = null;\r
189 \r
190         try {\r
191             ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(uriInfo);\r
192             String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();\r
193 \r
194             MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, uriInfo);\r
195             WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);\r
196             ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace\r
197             getRepositoryClient(ctx).get(ctx, csid, handler);\r
198             result = ctx.getOutput();\r
199         } catch (Exception e) {\r
200             throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);\r
201         }\r
202 \r
203         return result.getBytes();\r
204     }\r
205     \r
206     protected TransitionDef getTransitionDef(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String transition) {\r
207         TransitionDef result = null;\r
208         \r
209         try {\r
210                         Lifecycle lifecycle = ctx.getDocumentHandler().getLifecycle();\r
211                         List<TransitionDef> transitionDefList = lifecycle.getTransitionDefList().getTransitionDef();\r
212                         Iterator<TransitionDef> iter = transitionDefList.iterator();\r
213                         boolean found = false;\r
214                         while (iter.hasNext() && found == false) {\r
215                                 TransitionDef transitionDef = iter.next();\r
216                                 if (transitionDef.getName().equalsIgnoreCase(transition)) {\r
217                                         result = transitionDef;\r
218                                         found = true;\r
219                                 }\r
220                         }\r
221                 } catch (Exception e) {\r
222                         logger.error("Exception trying to retreive life cycle information for: " + ctx.getDocumentType());\r
223                 }\r
224         \r
225         return result;\r
226     }\r
227     \r
228     private PoxPayloadIn synthEmptyWorkflowInput() {\r
229         PoxPayloadIn result = null;\r
230         \r
231         PoxPayloadOut output = new PoxPayloadOut(WorkflowClient.SERVICE_PAYLOAD_NAME);\r
232         WorkflowCommon workflowCommons = new WorkflowCommon();\r
233         PayloadOutputPart commonPart = output.addPart(WorkflowClient.SERVICE_COMMONPART_NAME, workflowCommons);\r
234         String payloadXML = output.toXML();\r
235         try {\r
236                         result = new PoxPayloadIn(payloadXML);\r
237                 } catch (DocumentException e) {\r
238                         // TODO Auto-generated catch block\r
239                         e.printStackTrace();\r
240                 }\r
241         \r
242         return result;\r
243     }\r
244     \r
245     /*\r
246      * We should change this code.  The RepositoryClient (from call to getRepositoryClient) should support a call doWorkflowTransition() instead.\r
247      */\r
248     //FIXME: This method is almost identical to the method org.collectionspace.services.common.vocabulary.updateWorkflowWithTransition() so\r
249     // they should be consolidated -be DRY (don't repeat yourself).\r
250 \r
251     @PUT\r
252     @Path("{csid}" + WorkflowClient.SERVICE_PATH + "/" + "{transition}")\r
253     public byte[] updateWorkflowWithTransition(\r
254                 @Context UriInfo uriInfo,\r
255                 @PathParam("csid") String csid,\r
256                 @PathParam("transition") String transition) {\r
257         PoxPayloadOut result = null;\r
258                 \r
259         try {\r
260                 //\r
261                 // Create an empty workflow_commons input part and set it into a new "workflow" sub-resource context\r
262                 PoxPayloadIn input = new PoxPayloadIn(WorkflowClient.SERVICE_PAYLOAD_NAME, new WorkflowCommon(), \r
263                                 WorkflowClient.SERVICE_COMMONPART_NAME);\r
264             MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, input, uriInfo);\r
265                 \r
266             // Create a service context and document handler for the parent resource.\r
267             ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(uriInfo);\r
268             DocumentHandler parentDocHandler = this.createDocumentHandler(parentCtx);      \r
269             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
270 \r
271             // When looking for the document, we need to use the parent's workspace name -not the "workflow" workspace name\r
272             String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();\r
273             ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace\r
274             \r
275                 // 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
276             TransitionDef transitionDef = getTransitionDef(parentCtx, transition);\r
277             ctx.setProperty(WorkflowClient.TRANSITION_ID, transitionDef);\r
278 \r
279             WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);\r
280             getRepositoryClient(ctx).update(ctx, csid, handler);\r
281             result = ctx.getOutput();\r
282         } catch (Exception e) {\r
283             throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);\r
284         }\r
285         return result.getBytes();\r
286     }\r
287     \r
288 }\r