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