]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0b94859e06a05381c96cbb077ed88c17676d1315
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.batch.nuxeo;
2
3 import java.util.Arrays;
4 import java.util.List;
5
6 import javax.ws.rs.core.Response;
7
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;
15
16 public class CreateAndLinkLoanOutBatchJob extends AbstractBatchInvocable {
17
18         private final String RELATION_TYPE = "affects"; 
19         private final String LOAN_DOCTYPE = "LoanOut";
20         private final String RELATION_PREDICATE_DISP = "affects"; 
21         
22         public CreateAndLinkLoanOutBatchJob() {
23         setSupportedInvocationModes(Arrays.asList(INVOCATION_MODE_SINGLE, INVOCATION_MODE_LIST));
24         }
25         
26         /**
27          * The main work logic of the batch job. Will be called after setContext.
28          */
29         @Override
30         public void run() {
31                 completionStatus = STATUS_MIN_PROGRESS;
32
33                 try {
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;
43                                         }
44                                 } else if(INVOCATION_MODE_LIST.equalsIgnoreCase(invocationCtx.getMode())) {
45                                         InvocationContext.ListCSIDs listWrapper = invocationCtx.getListCSIDs();
46                                         List<String> csids = listWrapper.getCsid();
47                                         if(csids.size()==0) {
48                                                 completionStatus = STATUS_ERROR;
49                                                 errorInfo = new InvocationError(BAD_REQUEST_STATUS,
50                                                                 "CreateAndLinkLoanOutBatchJob: no CSIDs in list of documents!");
51                                                 results.setUserNote(errorInfo.getMessage());
52                                         }
53                                         String loanCSID = results.getPrimaryURICreated();
54                                         int nCreated = 0;
55                                         for(String csid:csids) {
56                                                 if(createRelation(loanCSID, csid) == STATUS_ERROR) {
57                                                         break;
58                                                 } else {
59                                                         nCreated++;
60                                                 }
61                                         }
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;
67                                         }
68                                 }
69                         }
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());
75                 }
76         }
77         
78         private int createLoan() {
79                 String newLoanNumber = "NewLoan-"+ GregorianCalendarDateTimeUtils.timestampUTC();
80
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>";
87
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());
97                 } else {
98                         String newId = CollectionSpaceClientUtils.extractId(response);
99                         results.setPrimaryURICreated(newId);
100                 }
101                 return completionStatus;
102         }
103         
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());
123                 }
124                 return completionStatus;
125         }
126 }