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.datetime.GregorianCalendarDateTimeUtils;
14 import org.collectionspace.services.common.invocable.InvocationContext;
15 import org.collectionspace.services.common.invocable.InvocationResults;
17 public class CreateAndLinkLoanOutBatchJob implements BatchInvocable {
19 private static ArrayList<String> invocationModes = null;
20 private InvocationContext context;
21 private int completionStatus;
22 private ResourceMap resourceMap;
23 private InvocationResults results;
24 private InvocationError errorInfo;
25 private final String RELATION_TYPE = "affects";
26 private final String LOAN_DOCTYPE = "LoanOut";
27 private final String RELATION_PREDICATE_DISP = "affects";
28 protected final int CREATED_STATUS = Response.Status.CREATED.getStatusCode();
29 protected final int BAD_REQUEST_STATUS = Response.Status.BAD_REQUEST.getStatusCode();
30 protected final int INT_ERROR_STATUS = Response.Status.INTERNAL_SERVER_ERROR.getStatusCode();
32 public CreateAndLinkLoanOutBatchJob() {
33 CreateAndLinkLoanOutBatchJob.setupClassStatics();
35 completionStatus = STATUS_UNSTARTED;
37 results = new InvocationResults();
41 private static void setupClassStatics() {
42 if(invocationModes == null ) {
43 invocationModes = new ArrayList<String>(1);
44 invocationModes.add(INVOCATION_MODE_SINGLE);
45 invocationModes.add(INVOCATION_MODE_LIST);
50 * @return a set of modes that this plugin can support on invocation. Must be non-empty.
52 public List<String> getSupportedInvocationModes() {
53 return CreateAndLinkLoanOutBatchJob.invocationModes;
57 * Sets the invocation context for the batch job. Called before run().
58 * @param context an instance of InvocationContext.
60 public void setInvocationContext(InvocationContext context) {
61 this.context = context;
65 * Sets the invocation context for the batch job. Called before run().
66 * @param context an instance of InvocationContext.
68 public void setResourceMap(ResourceMap resourceMap) {
69 this.resourceMap = resourceMap;
73 * The main work logic of the batch job. Will be called after setContext.
76 completionStatus = STATUS_MIN_PROGRESS;
79 // First, create the Loanout
80 if(createLoan() != STATUS_ERROR) {
81 if(INVOCATION_MODE_SINGLE.equalsIgnoreCase(context.getMode())) {
82 if(createRelation(results.getPrimaryURICreated(),
83 context.getSingleCSID()) != STATUS_ERROR) {
84 results.setNumAffected(1);
85 results.setUserNote("CreateAndLinkLoanOutBatchJob created new Loanout: "
86 +results.getPrimaryURICreated()+" with a link to the passed "+context.getDocType());
87 completionStatus = STATUS_COMPLETE;
89 } else if(INVOCATION_MODE_LIST.equalsIgnoreCase(context.getMode())) {
90 InvocationContext.ListCSIDs listWrapper = context.getListCSIDs();
91 List<String> csids = listWrapper.getCsid();
93 completionStatus = STATUS_ERROR;
94 errorInfo = new InvocationError(BAD_REQUEST_STATUS,
95 "CreateAndLinkLoanOutBatchJob: no CSIDs in list of documents!");
96 results.setUserNote(errorInfo.getMessage());
98 String loanCSID = results.getPrimaryURICreated();
100 for(String csid:csids) {
101 if(createRelation(loanCSID, csid) == STATUS_ERROR) {
107 if(completionStatus!=STATUS_ERROR) {
108 results.setNumAffected(nCreated);
109 results.setUserNote("CreateAndLinkLoanOutBatchJob created new Loanout: "
110 +results.getPrimaryURICreated()+" with "+nCreated+" link(s) to "+context.getDocType());
111 completionStatus = STATUS_COMPLETE;
115 } catch(Exception e) {
116 completionStatus = STATUS_ERROR;
117 errorInfo = new InvocationError(INT_ERROR_STATUS,
118 "CreateAndLinkLoanOutBatchJob problem creating new Loanout: "+e.getLocalizedMessage());
119 results.setUserNote(errorInfo.getMessage());
123 private int createLoan() {
124 String newLoanNumber = "NewLoan-"+ GregorianCalendarDateTimeUtils.timestampUTC();
126 String loanoutPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
127 +"<document name=\"loansout\">"
128 +"<ns2:loansout_common xmlns:ns2=\"http://collectionspace.org/services/loanout\""
129 +" xmlns:ns3=\"http://collectionspace.org/services/jaxb\">"
130 +"<loanOutNumber>"+newLoanNumber+"</loanOutNumber>"
131 +"</ns2:loansout_common></document>";
133 // First, create the Loanout
134 ResourceBase resource =
135 resourceMap.get("org.collectionspace.services.loanout.LoanoutResource");
136 Response response = resource.create(null, loanoutPayload);
137 if(response.getStatus() != CREATED_STATUS) {
138 completionStatus = STATUS_ERROR;
139 errorInfo = new InvocationError(INT_ERROR_STATUS,
140 "CreateAndLinkLoanOutBatchJob problem creating new Loanout!");
141 results.setUserNote(errorInfo.getMessage());
143 String newId = CollectionSpaceClientUtils.extractId(response);
144 results.setPrimaryURICreated(newId);
146 return completionStatus;
149 private int createRelation(String loanCSID, String toCSID) {
150 // Now, create the relation that links the input object to the loanout
151 String relationPayload = "<document name=\"relations\">"
152 + "<ns2:relations_common xmlns:ns2=\"http://collectionspace.org/services/relation\""
153 + " xmlns:ns3=\"http://collectionspace.org/services/jaxb\">"
154 + "<documentId1>"+loanCSID+"</documentId1>"
155 + "<documentType1>"+LOAN_DOCTYPE+"</documentType1>"
156 + "<documentId2>"+toCSID+"</documentId2>"
157 + "<documentType2>"+context.getDocType()+"</documentType2>"
158 + "<relationshipType>"+RELATION_TYPE+"</relationshipType>"
159 + "<predicateDisplayName>"+RELATION_PREDICATE_DISP+"</predicateDisplayName>"
160 + "</ns2:relations_common></document>";
161 ResourceBase resource =
162 resourceMap.get("org.collectionspace.services.relation.RelationResource");
163 Response response = resource.create(null, relationPayload);
164 if(response.getStatus() != CREATED_STATUS) {
165 completionStatus = STATUS_ERROR;
166 errorInfo = new InvocationError(INT_ERROR_STATUS,
167 "CreateAndLinkLoanOutBatchJob problem creating new relation!");
168 results.setUserNote(errorInfo.getMessage());
170 return completionStatus;
174 * @return one of the STATUS_* constants, or a value from 1-99 to indicate progress.
175 * Implementations need not support partial completion (progress) values, and can transition
176 * from STATUS_MIN_PROGRESS to STATUS_COMPLETE.
178 public int getCompletionStatus() {
179 return completionStatus;
183 * @return information about the batch job actions and results
185 public InvocationResults getResults() {
186 if(completionStatus != STATUS_COMPLETE)
192 * @return a user-presentable note when an error occurs in batch processing. Will only
193 * be called if getCompletionStatus() returns STATUS_ERROR.
195 public InvocationError getErrorInfo() {