]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6490045501d427e713ded4bd4aec4a2338abea73
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client;\r
2 \r
3 import java.io.File;\r
4 import java.util.ArrayList;\r
5 import java.util.List;\r
6 import java.util.Map;\r
7 \r
8 import javax.ws.rs.core.MediaType;\r
9 import javax.ws.rs.core.MultivaluedMap;\r
10 import javax.ws.rs.core.Response;\r
11 \r
12 import org.apache.commons.io.FileUtils;\r
13 import org.collectionspace.services.LocationJAXBSchema;\r
14 import org.collectionspace.services.client.test.ServiceRequestType;\r
15 import org.collectionspace.services.location.LocationsCommon;\r
16 import org.collectionspace.services.location.ConditionGroupList;\r
17 import org.collectionspace.services.location.ConditionGroup;\r
18 import org.collectionspace.services.location.LocationauthoritiesCommon;\r
19 import org.dom4j.DocumentException;\r
20 import org.jboss.resteasy.client.ClientResponse;\r
21 import org.slf4j.Logger;\r
22 import org.slf4j.LoggerFactory;\r
23 \r
24 public class LocationAuthorityClientUtils {\r
25     private static final Logger logger =\r
26         LoggerFactory.getLogger(LocationAuthorityClientUtils.class);\r
27 \r
28     /**\r
29      * Creates a new Location Authority\r
30      * @param displayName       The displayName used in UI, etc.\r
31      * @param refName           The proper refName for this authority\r
32      * @param headerLabel       The common part label\r
33      * @return  The PoxPayloadOut payload for the create call\r
34      */\r
35     public static PoxPayloadOut createLocationAuthorityInstance(\r
36                 String displayName, String shortIdentifier, String headerLabel ) {\r
37         \r
38         LocationauthoritiesCommon locationAuthority = new LocationauthoritiesCommon();\r
39         locationAuthority.setDisplayName(displayName);\r
40         locationAuthority.setShortIdentifier(shortIdentifier);\r
41         // String refName = createLocationAuthRefName(shortIdentifier, displayName);\r
42         // locationAuthority.setRefName(refName);\r
43         locationAuthority.setVocabType("LocationAuthority"); //FIXME: REM - Should this really be hard-coded?\r
44         \r
45         PoxPayloadOut multipart = new PoxPayloadOut(LocationAuthorityClient.SERVICE_PAYLOAD_NAME);\r
46         PayloadOutputPart commonPart = multipart.addPart(headerLabel, locationAuthority);\r
47 \r
48         if(logger.isDebugEnabled()){\r
49                 logger.debug("to be created, locationAuthority common ", \r
50                                         locationAuthority, LocationauthoritiesCommon.class);\r
51         }\r
52 \r
53         return multipart;\r
54     }\r
55 \r
56     /**\r
57      * @param locationRefName  The proper refName for this authority\r
58      * @param locationInfo the properties for the new Location. Can pass in one condition\r
59      *                                          note and date string.\r
60      * @param headerLabel       The common part label\r
61      * @return  The PoxPayloadOut payload for the create call\r
62      */\r
63     public static PoxPayloadOut createLocationInstance( \r
64                 String locationAuthRefName, Map<String, String> locationInfo, \r
65                                 String headerLabel){\r
66         LocationsCommon location = new LocationsCommon();\r
67         String shortId = locationInfo.get(LocationJAXBSchema.SHORT_IDENTIFIER);\r
68         String displayName = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME);\r
69         location.setShortIdentifier(shortId);\r
70         // String locationRefName = createLocationRefName(locationAuthRefName, shortId, displayName);\r
71         // location.setRefName(locationRefName);\r
72         String value = null;\r
73         value = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
74         boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true"); \r
75         location.setDisplayNameComputed(displayNameComputed);\r
76         if((value = (String)locationInfo.get(LocationJAXBSchema.NAME))!=null)\r
77                 location.setName(value);\r
78         if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE))!=null) {\r
79             ConditionGroupList conditionGroupList = new ConditionGroupList();\r
80             List<ConditionGroup> conditionGroups = conditionGroupList.getConditionGroup();\r
81             ConditionGroup conditionGroup = new ConditionGroup();\r
82             conditionGroup.setConditionNote(value);\r
83             if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE_DATE))!=null)\r
84                 conditionGroup.setConditionNoteDate(value);\r
85             conditionGroups.add(conditionGroup);\r
86             location.setConditionGroupList(conditionGroupList);\r
87         }\r
88         if((value = (String)locationInfo.get(LocationJAXBSchema.SECURITY_NOTE))!=null)\r
89                 location.setSecurityNote(value);\r
90         if((value = (String)locationInfo.get(LocationJAXBSchema.ACCESS_NOTE))!=null)\r
91                 location.setAccessNote(value);\r
92         if((value = (String)locationInfo.get(LocationJAXBSchema.LOCATION_TYPE))!=null)\r
93                 location.setLocationType(value);\r
94         if((value = (String)locationInfo.get(LocationJAXBSchema.ADDRESS))!=null)\r
95                 location.setAddress(value);\r
96         if((value = (String)locationInfo.get(LocationJAXBSchema.TERM_STATUS))!=null)\r
97                 location.setTermStatus(value);\r
98 \r
99         PoxPayloadOut multipart = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);\r
100         PayloadOutputPart commonPart = multipart.addPart(location,\r
101             MediaType.APPLICATION_XML_TYPE);\r
102         commonPart.setLabel(headerLabel);\r
103 \r
104         if(logger.isDebugEnabled()){\r
105                 logger.debug("to be created, location common ", location, LocationsCommon.class);\r
106         }\r
107 \r
108         return multipart;\r
109     }\r
110     \r
111     /**\r
112      * @param vcsid CSID of the authority to create a new location in\r
113      * @param locationAuthorityRefName The refName for the authority\r
114      * @param locationMap the properties for the new Location\r
115      * @param client the service client\r
116      * @return the CSID of the new item\r
117      */\r
118     public static String createItemInAuthority(String vcsid, \r
119                 String locationAuthorityRefName, Map<String,String> locationMap,\r
120                 LocationAuthorityClient client ) {\r
121         // Expected status code: 201 Created\r
122         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
123         // Type of service request being tested\r
124         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
125         \r
126         String displayName = locationMap.get(LocationJAXBSchema.DISPLAY_NAME);\r
127         String displayNameComputedStr = locationMap.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
128         boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");\r
129         if( displayName == null ) {\r
130                 if(!displayNameComputed) {\r
131                         throw new RuntimeException(\r
132                         "CreateItem: Must supply a displayName if displayNameComputed is set to false.");\r
133                 }\r
134                 displayName = \r
135                         prepareDefaultDisplayName(\r
136                         locationMap.get(LocationJAXBSchema.NAME));\r
137         }\r
138         \r
139         if(logger.isDebugEnabled()){\r
140                 logger.debug("Import: Create Item: \""+displayName\r
141                                 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\"");\r
142         }\r
143         PoxPayloadOut multipart = \r
144                 createLocationInstance( locationAuthorityRefName,\r
145                         locationMap, client.getItemCommonPartName() );\r
146         String newID = null;\r
147         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
148         try {\r
149                 int statusCode = res.getStatus();\r
150         \r
151                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
152                         throw new RuntimeException("Could not create Item: \""\r
153                                         +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)\r
154                                         +"\" in locationAuthority: \"" + locationAuthorityRefName\r
155                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
156                 }\r
157                 if(statusCode != EXPECTED_STATUS_CODE) {\r
158                         throw new RuntimeException("Unexpected Status when creating Item: \""\r
159                                         +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)\r
160                                         +"\" in locationAuthority: \"" + locationAuthorityRefName +"\", Status:"+ statusCode);\r
161                 }\r
162                 newID = extractId(res);\r
163         } finally {\r
164                 res.releaseConnection();\r
165         }\r
166 \r
167         return newID;\r
168     }\r
169 \r
170     public static PoxPayloadOut createLocationInstance(\r
171                 String commonPartXML, String headerLabel)  throws DocumentException {\r
172         PoxPayloadOut multipart = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);\r
173         PayloadOutputPart commonPart = multipart.addPart(commonPartXML,\r
174             MediaType.APPLICATION_XML_TYPE);\r
175         commonPart.setLabel(headerLabel);\r
176 \r
177         if(logger.isDebugEnabled()){\r
178                 logger.debug("to be created, location common ", commonPartXML);\r
179         }\r
180 \r
181         return multipart;\r
182     }\r
183     \r
184     public static String createItemInAuthority(String vcsid,\r
185                 String commonPartXML,\r
186                 LocationAuthorityClient client ) throws DocumentException {\r
187         // Expected status code: 201 Created\r
188         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
189         // Type of service request being tested\r
190         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
191         \r
192         PoxPayloadOut multipart = \r
193                 createLocationInstance(commonPartXML, client.getItemCommonPartName());\r
194         String newID = null;\r
195         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
196         try {\r
197                 int statusCode = res.getStatus();\r
198         \r
199                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
200                         throw new RuntimeException("Could not create Item: \""+commonPartXML\r
201                                         +"\" in locationAuthority: \"" + vcsid\r
202                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
203                 }\r
204                 if(statusCode != EXPECTED_STATUS_CODE) {\r
205                         throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML\r
206                                         +"\" in locationAuthority: \"" + vcsid +"\", Status:"+ statusCode);\r
207                 }\r
208                 newID = extractId(res);\r
209         } finally {\r
210                 res.releaseConnection();\r
211         }\r
212 \r
213         return newID;\r
214     }\r
215     \r
216     /**\r
217      * Creates the from xml file.\r
218      *\r
219      * @param fileName the file name\r
220      * @return new CSID as string\r
221      * @throws Exception the exception\r
222      */\r
223     private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName, \r
224                 LocationAuthorityClient client) throws Exception {\r
225         byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));\r
226         String commonPartXML = new String(b);\r
227         return createItemInAuthority(vcsid, commonPartXML, client );\r
228     }    \r
229 \r
230     /**\r
231      * Creates the locationAuthority ref name.\r
232      *\r
233      * @param shortId the locationAuthority shortIdentifier\r
234      * @param displaySuffix displayName to be appended, if non-null\r
235      * @return the string\r
236      */\r
237     public static String createLocationAuthRefName(String shortId, String displaySuffix) {\r
238         String refName = "urn:cspace:org.collectionspace.demo:locationauthority:name("\r
239                         +shortId+")";\r
240                 if(displaySuffix!=null&&!displaySuffix.isEmpty())\r
241                         refName += "'"+displaySuffix+"'";\r
242         return refName;\r
243     }\r
244 \r
245     /**\r
246      * Creates the location ref name.\r
247      *\r
248      * @param locationAuthRefName the locationAuthority ref name\r
249      * @param shortId the location shortIdentifier\r
250      * @param displaySuffix displayName to be appended, if non-null\r
251      * @return the string\r
252      */\r
253     public static String createLocationRefName(\r
254                                                 String locationAuthRefName, String shortId, String displaySuffix) {\r
255         String refName = locationAuthRefName+":location:name("+shortId+")";\r
256                 if(displaySuffix!=null&&!displaySuffix.isEmpty())\r
257                         refName += "'"+displaySuffix+"'";\r
258         return refName;\r
259     }\r
260 \r
261     public static String extractId(ClientResponse<Response> res) {\r
262         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
263         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
264         if(logger.isDebugEnabled()){\r
265                 logger.debug("extractId:uri=" + uri);\r
266         }\r
267         String[] segments = uri.split("/");\r
268         String id = segments[segments.length - 1];\r
269         if(logger.isDebugEnabled()){\r
270                 logger.debug("id=" + id);\r
271         }\r
272         return id;\r
273     }\r
274     \r
275     /**\r
276      * Returns an error message indicating that the status code returned by a\r
277      * specific call to a service does not fall within a set of valid status\r
278      * codes for that service.\r
279      *\r
280      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
281      *\r
282      * @param statusCode  The invalid status code that was returned in the response,\r
283      *                    from submitting that type of request to the service.\r
284      *\r
285      * @return An error message.\r
286      */\r
287     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
288         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
289                 requestType.validStatusCodesAsString();\r
290     }\r
291 \r
292 \r
293     \r
294     /**\r
295      * Produces a default displayName from the basic name and dates fields.\r
296      * @see LocationDocumentModelHandler.prepareDefaultDisplayName() which\r
297      * duplicates this logic, until we define a service-general utils package\r
298      * that is neither client nor service specific.\r
299      * @param name      \r
300      * @return\r
301      */\r
302     public static String prepareDefaultDisplayName(\r
303                 String name ) {\r
304         StringBuilder newStr = new StringBuilder();\r
305                         newStr.append(name);\r
306                 return newStr.toString();\r
307     }\r
308     \r
309 \r
310 \r
311 }\r