2 * AbstractMultiPartCollectionSpaceResourceImpl.java
\r
4 * {Purpose of This Class}
\r
6 * {Other Notes Relating to This Class (Optional)}
\r
9 * $LastChangedRevision: $
\r
10 * $LastChangedDate: $
\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
16 * http://www.collectionspace.org
\r
17 * http://wiki.collectionspace.org
\r
19 * Copyright © 2009 {Contributing Institution}
\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
24 * You may obtain a copy of the ECL 2.0 License at
\r
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
27 package org.collectionspace.services.common;
\r
29 import java.util.Iterator;
\r
30 import java.util.List;
\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
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
52 import org.slf4j.Logger;
\r
53 import org.slf4j.LoggerFactory;
\r
56 * The Class AbstractMultiPartCollectionSpaceResourceImpl.
\r
58 public abstract class AbstractMultiPartCollectionSpaceResourceImpl extends AbstractCollectionSpaceResourceImpl<PoxPayloadIn, PoxPayloadOut> {
\r
60 protected final Logger logger = LoggerFactory.getLogger(this.getClass());
\r
63 public ServiceContextFactory<PoxPayloadIn, PoxPayloadOut> getServiceContextFactory() {
\r
64 return MultipartServiceContextFactory.get();
\r
67 abstract protected String getOrderByField(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx);
\r
69 abstract protected String getPartialTermMatchField(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx);
\r
72 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
73 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), getCommonPartClass());
\r
77 * Creates the document handler.
\r
79 * @param serviceContext the service context
\r
80 * @param schemaName the schema name
\r
81 * @param commonClass the common class
\r
83 * @return the document handler
\r
85 * @throws Exception the exception
\r
87 public DocumentHandler createDocumentHandler(ServiceContext<PoxPayloadIn, PoxPayloadOut> serviceContext,
\r
89 Class<?> commonClass) throws Exception {
\r
90 DocumentHandler result = null;
\r
92 MultipartServiceContext ctx = (MultipartServiceContext) serviceContext;
\r
93 Object commonPart = null;
\r
94 if (ctx.getInput() != null) {
\r
95 commonPart = ctx.getInputPart(schemaName);
\r
97 result = super.createDocumentHandler(ctx, commonPart);
\r
103 * Creates the document handler.
\r
105 * @param ctx the ctx
\r
106 * @param commonClass the common class
\r
108 * @return the document handler
\r
110 * @throws Exception the exception
\r
112 public DocumentHandler createDocumentHandler(
\r
113 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
\r
114 Class<Object> commonClass) throws Exception {
\r
115 return createDocumentHandler(ctx, ctx.getCommonPartLabel(), commonClass);
\r
119 * Creates the contact document handler.
\r
121 * @param ctx the ctx
\r
122 * @param inAuthority the in authority
\r
123 * @param inItem the in item
\r
125 * @return the document handler
\r
127 * @throws Exception the exception
\r
129 protected WorkflowDocumentModelHandler createWorkflowDocumentHandler(
\r
130 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) throws Exception {
\r
132 WorkflowDocumentModelHandler docHandler = (WorkflowDocumentModelHandler) createDocumentHandler(ctx,
\r
133 WorkflowClient.SERVICE_COMMONPART_NAME,
\r
134 WorkflowCommon.class);
\r
140 * JAX-RS Annotated methods
\r
143 @Path(WorkflowClient.SERVICE_PATH)
\r
144 public Lifecycle getWorkflow() {
\r
147 String documentType = "undefined";
\r
148 MultipartServiceContext ctx = null;
\r
150 ctx = (MultipartServiceContext) createServiceContext();
\r
151 DocumentHandler handler = ctx.getDocumentHandler();
\r
152 result = handler.getLifecycle();
\r
153 } catch (Exception e) {
\r
154 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, ctx.getDocumentType());
\r
157 if (result == null) {
\r
158 result = new Lifecycle();
\r
159 result.setName("No life cycle defined for:" + documentType);
\r
166 * JAX-RS Annotated methods
\r
170 * We should change this method. The RepositoryClient (from call to getRepositoryClient) should support a call getWorkflowTransition() instead.
\r
173 @Path("{csid}" + WorkflowClient.SERVICE_PATH)
\r
174 public byte[] getWorkflow(
\r
175 @PathParam("csid") String csid) {
\r
176 PoxPayloadOut result = null;
\r
179 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
180 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
182 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
\r
183 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
184 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
185 getRepositoryClient(ctx).get(ctx, csid, handler);
\r
186 result = ctx.getOutput();
\r
187 } catch (Exception e) {
\r
188 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
191 return result.getBytes();
\r
194 protected TransitionDef getTransitionDef(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String transition) {
\r
195 TransitionDef result = null;
\r
198 Lifecycle lifecycle = ctx.getDocumentHandler().getLifecycle();
\r
199 List<TransitionDef> transitionDefList = lifecycle.getTransitionDefList().getTransitionDef();
\r
200 Iterator<TransitionDef> iter = transitionDefList.iterator();
\r
201 boolean found = false;
\r
202 while (iter.hasNext() && found == false) {
\r
203 TransitionDef transitionDef = iter.next();
\r
204 if (transitionDef.getName().equalsIgnoreCase(transition)) {
\r
205 result = transitionDef;
\r
209 } catch (Exception e) {
\r
210 logger.error("Exception trying to retreive life cycle information for: " + ctx.getDocumentType());
\r
216 private PoxPayloadIn synthEmptyWorkflowInput() {
\r
217 PoxPayloadIn result = null;
\r
219 PoxPayloadOut output = new PoxPayloadOut(WorkflowClient.SERVICE_PAYLOAD_NAME);
\r
220 WorkflowCommon workflowCommons = new WorkflowCommon();
\r
221 PayloadOutputPart commonPart = output.addPart(WorkflowClient.SERVICE_COMMONPART_NAME, workflowCommons);
\r
222 String payloadXML = output.toXML();
\r
224 result = new PoxPayloadIn(payloadXML);
\r
225 } catch (DocumentException e) {
\r
226 // TODO Auto-generated catch block
\r
227 e.printStackTrace();
\r
234 * We should change this code. The RepositoryClient (from call to getRepositoryClient) should support a call doWorkflowTransition() instead.
\r
236 //FIXME: This method is almost identical to the method org.collectionspace.services.common.vocabulary.updateWorkflowWithTransition() so
\r
237 // they should be consolidated -be DRY (don't repeat yourself).
\r
240 @Path("{csid}" + WorkflowClient.SERVICE_PATH + "/" + "{transition}")
\r
241 public byte[] updateWorkflowWithTransition(@PathParam("csid") String csid,
\r
242 @PathParam("transition") String transition) {
\r
243 PoxPayloadOut result = null;
\r
248 // Create an empty workflow_commons input part and set it into a new "workflow" sub-resource context
\r
249 PoxPayloadIn input = new PoxPayloadIn(WorkflowClient.SERVICE_PAYLOAD_NAME, new WorkflowCommon(),
\r
250 WorkflowClient.SERVICE_COMMONPART_NAME);
\r
251 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, input);
\r
253 // Create a service context and document handler for the parent resource.
\r
254 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext();
\r
255 DocumentHandler parentDocHandler = this.createDocumentHandler(parentCtx);
\r
256 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
258 // When looking for the document, we need to use the parent's workspace name -not the "workflow" workspace name
\r
259 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
\r
260 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
\r
262 // 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
263 TransitionDef transitionDef = getTransitionDef(parentCtx, transition);
\r
264 ctx.setProperty(WorkflowClient.TRANSITION_ID, transitionDef);
\r
266 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
\r
267 getRepositoryClient(ctx).update(ctx, csid, handler);
\r
268 result = ctx.getOutput();
\r
269 } catch (Exception e) {
\r
270 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
\r
272 return result.getBytes();
\r