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