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