1 package org.collectionspace.services.batch.nuxeo;
3 import java.util.Arrays;
6 import javax.ws.rs.core.Response;
8 import org.collectionspace.services.batch.AbstractBatchInvocable;
9 import org.collectionspace.services.client.CollectionSpaceClientUtils;
10 import org.collectionspace.services.common.NuxeoBasedResource;
11 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
12 import org.collectionspace.services.common.invocable.InvocationContext;
13 import org.collectionspace.services.client.LoanoutClient;
14 import org.collectionspace.services.client.RelationClient;
16 public class CreateAndLinkLoanOutBatchJob extends AbstractBatchInvocable {
18 private final String RELATION_TYPE = "affects";
19 private final String LOAN_DOCTYPE = "LoanOut";
20 private final String RELATION_PREDICATE_DISP = "affects";
22 public CreateAndLinkLoanOutBatchJob() {
23 setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST));
27 * The main work logic of the batch job. Will be called after setContext.
31 completionStatus = STATUS_MIN_PROGRESS;
34 // First, create the Loanout
35 if (createLoan() != STATUS_ERROR) {
36 if(INVOCATION_MODE_SINGLE.equalsIgnoreCase(invocationCtx.getMode())) {
37 if(createRelation(results.getPrimaryURICreated(),
38 invocationCtx.getSingleCSID()) != STATUS_ERROR) {
39 results.setNumAffected(1);
40 results.setUserNote("CreateAndLinkLoanOutBatchJob created new Loanout: "
41 +results.getPrimaryURICreated()+" with a link to the passed "+invocationCtx.getDocType());
42 completionStatus = STATUS_COMPLETE;
44 } else if(INVOCATION_MODE_LIST.equalsIgnoreCase(invocationCtx.getMode())) {
45 InvocationContext.ListCSIDs listWrapper = invocationCtx.getListCSIDs();
46 List<String> csids = listWrapper.getCsid();
48 completionStatus = STATUS_ERROR;
49 errorInfo = new InvocationError(BAD_REQUEST_STATUS,
50 "CreateAndLinkLoanOutBatchJob: no CSIDs in list of documents!");
51 results.setUserNote(errorInfo.getMessage());
53 String loanCSID = results.getPrimaryURICreated();
55 for(String csid:csids) {
56 if(createRelation(loanCSID, csid) == STATUS_ERROR) {
62 if(completionStatus!=STATUS_ERROR) {
63 results.setNumAffected(nCreated);
64 results.setUserNote("CreateAndLinkLoanOutBatchJob created new Loanout: "
65 +results.getPrimaryURICreated()+" with "+nCreated+" link(s) to "+invocationCtx.getDocType());
66 completionStatus = STATUS_COMPLETE;
70 } catch(Exception e) {
71 completionStatus = STATUS_ERROR;
72 errorInfo = new InvocationError(INT_ERROR_STATUS,
73 "CreateAndLinkLoanOutBatchJob problem creating new Loanout: "+e.getLocalizedMessage());
74 results.setUserNote(errorInfo.getMessage());
78 private int createLoan() {
79 String newLoanNumber = "NewLoan-"+ GregorianCalendarDateTimeUtils.timestampUTC();
81 String loanoutPayload = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
82 +"<document name=\"loansout\">"
83 +"<ns2:loansout_common xmlns:ns2=\"http://collectionspace.org/services/loanout\""
84 +" xmlns:ns3=\"http://collectionspace.org/services/jaxb\">"
85 +"<loanOutNumber>"+newLoanNumber+"</loanOutNumber>"
86 +"</ns2:loansout_common></document>";
88 // First, create the Loanout
89 // We fetch the resource class by service name
90 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get( LoanoutClient.SERVICE_NAME);
91 Response response = resource.create(getResourceMap(), null, loanoutPayload);
92 if(response.getStatus() != CREATED_STATUS) {
93 completionStatus = STATUS_ERROR;
94 errorInfo = new InvocationError(INT_ERROR_STATUS,
95 "CreateAndLinkLoanOutBatchJob problem creating new Loanout!");
96 results.setUserNote(errorInfo.getMessage());
98 String newId = CollectionSpaceClientUtils.extractId(response);
99 results.setPrimaryURICreated(newId);
101 return completionStatus;
104 private int createRelation(String loanCSID, String toCSID) {
105 // Now, create the relation that links the input object to the loanout
106 String relationPayload = "<document name=\"relations\">"
107 + "<ns2:relations_common xmlns:ns2=\"http://collectionspace.org/services/relation\""
108 + " xmlns:ns3=\"http://collectionspace.org/services/jaxb\">"
109 + "<subjectCsid>"+loanCSID+"</subjectCsid>"
110 + "<subjectDocumentType>"+LOAN_DOCTYPE+"</subjectDocumentType>"
111 + "<objectCsid>"+toCSID+"</objectCsid>"
112 + "<objectDocumentType>"+invocationCtx.getDocType()+"</objectDocumentType>"
113 + "<relationshipType>"+RELATION_TYPE+"</relationshipType>"
114 + "<predicateDisplayName>"+RELATION_PREDICATE_DISP+"</predicateDisplayName>"
115 + "</ns2:relations_common></document>";
116 NuxeoBasedResource resource = (NuxeoBasedResource) getResourceMap().get(RelationClient.SERVICE_NAME);
117 Response response = resource.create(getResourceMap(), null, relationPayload);
118 if(response.getStatus() != CREATED_STATUS) {
119 completionStatus = STATUS_ERROR;
120 errorInfo = new InvocationError(INT_ERROR_STATUS,
121 "CreateAndLinkLoanOutBatchJob problem creating new relation!");
122 results.setUserNote(errorInfo.getMessage());
124 return completionStatus;