]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c13420c0819125ece36ab1627e595deaf1e10bb6
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.batch.nuxeo;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6
7 import javax.ws.rs.core.Response;
8
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;
18
19 public class CreateAndLinkLoanOutBatchJob implements BatchInvocable {
20
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();
33         
34         public CreateAndLinkLoanOutBatchJob() {
35                 CreateAndLinkLoanOutBatchJob.setupClassStatics();
36                 context = null;
37                 completionStatus = STATUS_UNSTARTED;
38                 resourceMap = null;
39                 results = new InvocationResults();
40                 errorInfo = null;
41         }
42
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);
48                 }
49         }
50
51         /**
52          * @return a set of modes that this plugin can support on invocation. Must be non-empty.
53          */
54         public List<String> getSupportedInvocationModes() {
55                 return CreateAndLinkLoanOutBatchJob.invocationModes;
56         }
57         
58         /**
59          * Sets the invocation context for the batch job. Called before run().
60          * @param context an instance of InvocationContext.
61          */
62         public void setInvocationContext(InvocationContext context) {
63                 this.context = context;
64         }
65
66         /**
67          * Sets the invocation context for the batch job. Called before run().
68          * @param context an instance of InvocationContext.
69          */
70         public void setResourceMap(ResourceMap resourceMap) {
71                 this.resourceMap = resourceMap;
72         }
73
74         /**
75          * The main work logic of the batch job. Will be called after setContext.
76          */
77         public void run() {
78                 completionStatus = STATUS_MIN_PROGRESS;
79
80                 try {
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;
90                                         }
91                                 } else if(INVOCATION_MODE_LIST.equalsIgnoreCase(context.getMode())) {
92                                         InvocationContext.ListCSIDs listWrapper = context.getListCSIDs();
93                                         List<String> csids = listWrapper.getCsid();
94                                         if(csids.size()==0) {
95                                                 completionStatus = STATUS_ERROR;
96                                                 errorInfo = new InvocationError(BAD_REQUEST_STATUS,
97                                                                 "CreateAndLinkLoanOutBatchJob: no CSIDs in list of documents!");
98                                                 results.setUserNote(errorInfo.getMessage());
99                                         }
100                                         String loanCSID = results.getPrimaryURICreated();
101                                         int nCreated = 0;
102                                         for(String csid:csids) {
103                                                 if(createRelation(loanCSID, csid) == STATUS_ERROR) {
104                                                         break;
105                                                 } else {
106                                                         nCreated++;
107                                                 }
108                                         }
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;
114                                         }
115                                 }
116                         }
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());
122                 }
123         }
124         
125         private int createLoan() {
126                 String newLoanNumber = "NewLoan-"+ GregorianCalendarDateTimeUtils.timestampUTC();
127
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>";
134
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());
144                 } else {
145                         String newId = CollectionSpaceClientUtils.extractId(response);
146                         results.setPrimaryURICreated(newId);
147                 }
148                 return completionStatus;
149         }
150         
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());
170                 }
171                 return completionStatus;
172         }
173
174         /**
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.
178          */
179         public int getCompletionStatus() {
180                 return completionStatus;
181         }
182
183         /**
184          * @return information about the batch job actions and results
185          */
186         public InvocationResults getResults() {
187                 if(completionStatus != STATUS_COMPLETE)
188                         return null;
189                 return results;
190         }
191
192         /**
193          * @return a user-presentable note when an error occurs in batch processing. Will only
194          * be called if getCompletionStatus() returns STATUS_ERROR.
195          */
196         public InvocationError getErrorInfo() {
197                 return errorInfo;
198         }
199
200
201 }