1 package org.collectionspace.services.batch.nuxeo;
3 import java.util.ArrayList;
4 import java.util.HashMap;
7 import javax.ws.rs.core.Response;
9 import org.collectionspace.services.batch.BatchInvocable;
10 import org.collectionspace.services.client.CollectionSpaceClientUtils;
11 import org.collectionspace.services.common.ResourceBase;
12 import org.collectionspace.services.common.ResourceMap;
13 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
14 import org.collectionspace.services.common.invocable.InvocationContext;
15 import org.collectionspace.services.common.invocable.InvocationResults;
16 import org.collectionspace.services.client.LoanoutClient;
17 import org.collectionspace.services.client.RelationClient;
19 public class CreateAndLinkLoanOutBatchJob implements BatchInvocable {
21 private static ArrayList<String> invocationModes = null;
22 private InvocationContext context;
23 private int completionStatus;
24 private ResourceMap resourceMap;
25 private InvocationResults results;
26 private InvocationError errorInfo;
27 private final String RELATION_TYPE = "affects";
28 private final String LOAN_DOCTYPE = "LoanOut";
29 private final String RELATION_PREDICATE_DISP = "affects";
30 protected final int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
31 protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode();
32 protected final int INT_ERROR_STATUS = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
34 public CreateAndLinkLoanOutBatchJob() {
35 CreateAndLinkLoanOutBatchJob.setupClassStatics();
37 completionStatus = STATUS_UNSTARTED;
39 results = new InvocationResults();
43 private static void setupClassStatics() {
44 if(invocationModes == null ) {
45 invocationModes = new ArrayList<String>(1);
46 invocationModes.add(INVOCATION_MODE_SINGLE);
47 invocationModes.add(INVOCATION_MODE_LIST);
52 * @return a set of modes that this plugin can support on invocation. Must be non-empty.
54 public List<String> getSupportedInvocationModes() {
55 return CreateAndLinkLoanOutBatchJob.invocationModes;
59 * Sets the invocation context for the batch job. Called before run().
60 * @param context an instance of InvocationContext.
62 public void setInvocationContext(InvocationContext context) {
63 this.context = context;
67 * Sets the invocation context for the batch job. Called before run().
68 * @param context an instance of InvocationContext.
70 public void setResourceMap(ResourceMap resourceMap) {
71 this.resourceMap = resourceMap;
75 * The main work logic of the batch job. Will be called after setContext.
78 completionStatus = STATUS_MIN_PROGRESS;
81 // First, create the Loanout
82 if(createLoan() != STATUS_ERROR) {
83 if(INVOCATION_MODE_SINGLE.equalsIgnoreCase(context.getMode())) {
84 if(createRelation(results.getPrimaryURICreated(),
85 context.getSingleCSID()) != STATUS_ERROR) {
86 results.setNumAffected(1);
87 results.setUserNote("CreateAndLinkLoanOutBatchJob created new Loanout: "
88 +results.getPrimaryURICreated()+" with a link to the passed "+context.getDocType());
89 completionStatus = STATUS_COMPLETE;
91 } else if(INVOCATION_MODE_LIST.equalsIgnoreCase(context.getMode())) {
92 InvocationContext.ListCSIDs listWrapper = context.getListCSIDs();
93 List<String> csids = listWrapper.getCsid();
95 completionStatus = STATUS_ERROR;
96 errorInfo = new InvocationError(BAD_REQUEST_STATUS,
97 "CreateAndLinkLoanOutBatchJob: no CSIDs in list of documents!");
98 results.setUserNote(errorInfo.getMessage());
100 String loanCSID = results.getPrimaryURICreated();
102 for(String csid:csids) {
103 if(createRelation(loanCSID, csid) == STATUS_ERROR) {
109 if(completionStatus!=STATUS_ERROR) {
110 results.setNumAffected(nCreated);
111 results.setUserNote("CreateAndLinkLoanOutBatchJob created new Loanout: "
112 +results.getPrimaryURICreated()+" with "+nCreated+" link(s) to "+context.getDocType());
113 completionStatus = STATUS_COMPLETE;
117 } catch(Exception e) {
118 completionStatus = STATUS_ERROR;
119 errorInfo = new InvocationError(INT_ERROR_STATUS,
120 "CreateAndLinkLoanOutBatchJob problem creating new Loanout: "+e.getLocalizedMessage());
121 results.setUserNote(errorInfo.getMessage());
125 private int createLoan() {
126 String newLoanNumber = "NewLoan-"+ GregorianCalendarDateTimeUtils.timestampUTC();
128 String loanoutPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
129 +"<document name=\"loansout\">"
130 +"<ns2:loansout_common xmlns:ns2=\"http://collectionspace.org/services/loanout\""
131 +" xmlns:ns3=\"http://collectionspace.org/services/jaxb\">"
132 +"<loanOutNumber>"+newLoanNumber+"</loanOutNumber>"
133 +"</ns2:loansout_common></document>";
135 // First, create the Loanout
136 // We fetch the resource class by service name
137 ResourceBase resource = resourceMap.get( LoanoutClient.SERVICE_NAME);
138 Response response = resource.create(resourceMap, null, loanoutPayload);
139 if(response.getStatus() != CREATED_STATUS) {
140 completionStatus = STATUS_ERROR;
141 errorInfo = new InvocationError(INT_ERROR_STATUS,
142 "CreateAndLinkLoanOutBatchJob problem creating new Loanout!");
143 results.setUserNote(errorInfo.getMessage());
145 String newId = CollectionSpaceClientUtils.extractId(response);
146 results.setPrimaryURICreated(newId);
148 return completionStatus;
151 private int createRelation(String loanCSID, String toCSID) {
152 // Now, create the relation that links the input object to the loanout
153 String relationPayload = "<document name=\"relations\">"
154 + "<ns2:relations_common xmlns:ns2=\"http://collectionspace.org/services/relation\""
155 + " xmlns:ns3=\"http://collectionspace.org/services/jaxb\">"
156 + "<subjectCsid>"+loanCSID+"</subjectCsid>"
157 + "<subjectDocumentType>"+LOAN_DOCTYPE+"</subjectDocumentType>"
158 + "<objectCsid>"+toCSID+"</objectCsid>"
159 + "<objectDocumentType>"+context.getDocType()+"</objectDocumentType>"
160 + "<relationshipType>"+RELATION_TYPE+"</relationshipType>"
161 + "<predicateDisplayName>"+RELATION_PREDICATE_DISP+"</predicateDisplayName>"
162 + "</ns2:relations_common></document>";
163 ResourceBase resource = resourceMap.get(RelationClient.SERVICE_NAME);
164 Response response = resource.create(resourceMap, null, relationPayload);
165 if(response.getStatus() != CREATED_STATUS) {
166 completionStatus = STATUS_ERROR;
167 errorInfo = new InvocationError(INT_ERROR_STATUS,
168 "CreateAndLinkLoanOutBatchJob problem creating new relation!");
169 results.setUserNote(errorInfo.getMessage());
171 return completionStatus;
175 * @return one of the STATUS_* constants, or a value from 1-99 to indicate progress.
176 * Implementations need not support partial completion (progress) values, and can transition
177 * from STATUS_MIN_PROGRESS to STATUS_COMPLETE.
179 public int getCompletionStatus() {
180 return completionStatus;
184 * @return information about the batch job actions and results
186 public InvocationResults getResults() {
187 if(completionStatus != STATUS_COMPLETE)
193 * @return a user-presentable note when an error occurs in batch processing. Will only
194 * be called if getCompletionStatus() returns STATUS_ERROR.
196 public InvocationError getErrorInfo() {