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