]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
a627080480626fd405c7e199daad054088e06625
[tmp/jakarta-migration.git] /
1 /**     \r
2  * PersonAuthorityClientUtils.java\r
3  *\r
4  * {Purpose of This Class}\r
5  *\r
6  * {Other Notes Relating to This Class (Optional)}\r
7  *\r
8  * $LastChangedBy: $\r
9  * $LastChangedRevision: $\r
10  * $LastChangedDate: $\r
11  *\r
12  * This document is a part of the source code and related artifacts\r
13  * for CollectionSpace, an open source collections management system\r
14  * for museums and related institutions:\r
15  *\r
16  * http://www.collectionspace.org\r
17  * http://wiki.collectionspace.org\r
18  *\r
19  * Copyright © 2009 {Contributing Institution}\r
20  *\r
21  * Licensed under the Educational Community License (ECL), Version 2.0.\r
22  * You may not use this file except in compliance with this License.\r
23  *\r
24  * You may obtain a copy of the ECL 2.0 License at\r
25  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
26  */\r
27 package org.collectionspace.services.client;\r
28 \r
29 import java.util.ArrayList;\r
30 import java.util.Arrays;\r
31 import java.util.List;\r
32 import java.util.Map;\r
33 \r
34 import javax.ws.rs.core.MediaType;\r
35 import javax.ws.rs.core.MultivaluedMap;\r
36 import javax.ws.rs.core.Response;\r
37 \r
38 import org.collectionspace.services.PersonJAXBSchema;\r
39 import org.collectionspace.services.client.test.ServiceRequestType;\r
40 import org.collectionspace.services.person.PersonsCommon;\r
41 import org.collectionspace.services.person.PersonauthoritiesCommon;\r
42 import org.jboss.resteasy.client.ClientResponse;\r
43 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
44 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
45 import org.slf4j.Logger;\r
46 import org.slf4j.LoggerFactory;\r
47 \r
48 /**\r
49  * The Class PersonAuthorityClientUtils.\r
50  */\r
51 public class PersonAuthorityClientUtils {\r
52     \r
53     /** The Constant logger. */\r
54     private static final Logger logger =\r
55         LoggerFactory.getLogger(PersonAuthorityClientUtils.class);\r
56 \r
57     /**\r
58      * Creates the person authority instance.\r
59      *\r
60      * @param displayName the display name\r
61      * @param refName the ref name\r
62      * @param headerLabel the header label\r
63      * @return the multipart output\r
64      */\r
65     public static MultipartOutput createPersonAuthorityInstance(\r
66                 String displayName, String refName, String headerLabel ) {\r
67         PersonauthoritiesCommon personAuthority = new PersonauthoritiesCommon();\r
68         personAuthority.setDisplayName(displayName);\r
69         personAuthority.setRefName(refName);\r
70         personAuthority.setVocabType("PersonAuthority");\r
71         MultipartOutput multipart = new MultipartOutput();\r
72         OutputPart commonPart = multipart.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);\r
73         commonPart.getHeaders().add("label", headerLabel);\r
74 \r
75         if(logger.isDebugEnabled()){\r
76                 logger.debug("to be created, personAuthority common ", \r
77                                         personAuthority, PersonauthoritiesCommon.class);\r
78         }\r
79 \r
80         return multipart;\r
81     }\r
82 \r
83     /**\r
84      * Creates the person instance.\r
85      *\r
86      * @param inAuthority the in authority\r
87      * @param personRefName the person ref name\r
88      * @param personInfo the person info\r
89      * @param headerLabel the header label\r
90      * @return the multipart output\r
91      */\r
92     public static MultipartOutput createPersonInstance(String inAuthority, \r
93                 String personRefName, Map<String, String> personInfo, String headerLabel){\r
94         PersonsCommon person = new PersonsCommon();\r
95         person.setInAuthority(inAuthority);\r
96         person.setRefName(personRefName);\r
97         String value = null;\r
98         value = personInfo.get(PersonJAXBSchema.DISPLAY_NAME_COMPUTED);\r
99         boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true"); \r
100         person.setDisplayNameComputed(displayNameComputed);\r
101         if((value = (String)personInfo.get(PersonJAXBSchema.FORE_NAME))!=null)\r
102                 person.setForeName(value);\r
103         if((value = (String)personInfo.get(PersonJAXBSchema.MIDDLE_NAME))!=null)\r
104                 person.setMiddleName(value);\r
105         if((value = (String)personInfo.get(PersonJAXBSchema.SUR_NAME))!=null)\r
106                 person.setSurName(value);\r
107         if((value = (String)personInfo.get(PersonJAXBSchema.INITIALS))!=null)\r
108                 person.setInitials(value);\r
109         if((value = (String)personInfo.get(PersonJAXBSchema.SALUTATIONS))!=null)\r
110                 person.setSalutation(value);\r
111         if((value = (String)personInfo.get(PersonJAXBSchema.TITLE))!=null)\r
112                 person.setTitle(value);\r
113         if((value = (String)personInfo.get(PersonJAXBSchema.NAME_ADDITIONS))!=null)\r
114                 person.setNameAdditions(value);\r
115         if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_DATE))!=null)\r
116                 person.setBirthDate(value);\r
117         if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_DATE))!=null)\r
118                 person.setDeathDate(value);\r
119         if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_PLACE))!=null)\r
120                 person.setBirthPlace(value);\r
121         if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_PLACE))!=null)\r
122                 person.setDeathPlace(value);\r
123         if((value = (String)personInfo.get(PersonJAXBSchema.GROUP))!=null)\r
124                 person.setGroup(value);\r
125         if((value = (String)personInfo.get(PersonJAXBSchema.NATIONALITY))!=null)\r
126                 person.setNationality(value);\r
127         if((value = (String)personInfo.get(PersonJAXBSchema.GENDER))!=null)\r
128                 person.setGender(value);\r
129         if((value = (String)personInfo.get(PersonJAXBSchema.OCCUPATION))!=null)\r
130                 person.setOccupation(value);\r
131         if((value = (String)personInfo.get(PersonJAXBSchema.SCHOOL_OR_STYLE))!=null)\r
132                 person.setSchoolOrStyle(value);\r
133         if((value = (String)personInfo.get(PersonJAXBSchema.BIO_NOTE))!=null)\r
134                 person.setBioNote(value);\r
135         if((value = (String)personInfo.get(PersonJAXBSchema.NAME_NOTE))!=null)\r
136                 person.setNameNote(value);\r
137         MultipartOutput multipart = new MultipartOutput();\r
138         OutputPart commonPart = multipart.addPart(person,\r
139             MediaType.APPLICATION_XML_TYPE);\r
140         commonPart.getHeaders().add("label", headerLabel);\r
141 \r
142         if(logger.isDebugEnabled()){\r
143                 logger.debug("to be created, person common ", person, PersonsCommon.class);\r
144         }\r
145 \r
146         return multipart;\r
147     }\r
148     \r
149     /**\r
150      * Creates the item in authority.\r
151      *\r
152      * @param vcsid the vcsid\r
153      * @param personAuthorityRefName the person authority ref name\r
154      * @param personMap the person map\r
155      * @param client the client\r
156      * @return the string\r
157      */\r
158     public static String createItemInAuthority(String vcsid, \r
159                 String personAuthorityRefName, Map<String,String> personMap,\r
160                 PersonAuthorityClient client ) {\r
161         // Expected status code: 201 Created\r
162         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
163         // Type of service request being tested\r
164         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
165         \r
166         String displayName = personMap.get(PersonJAXBSchema.DISPLAY_NAME);\r
167         String displayNameComputedStr = personMap.get(PersonJAXBSchema.DISPLAY_NAME_COMPUTED);\r
168         boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");\r
169         if( displayName == null ) {\r
170                 if(!displayNameComputed) {\r
171                         throw new RuntimeException(\r
172                         "CreateItem: Must supply a displayName if displayNameComputed is set to false.");\r
173                 }\r
174                 displayName = \r
175                         prepareDefaultDisplayName(\r
176                         personMap.get(PersonJAXBSchema.FORE_NAME),\r
177                         personMap.get(PersonJAXBSchema.MIDDLE_NAME),\r
178                         personMap.get(PersonJAXBSchema.SUR_NAME),\r
179                         personMap.get(PersonJAXBSchema.BIRTH_DATE),\r
180                         personMap.get(PersonJAXBSchema.DEATH_DATE));\r
181         }\r
182         \r
183         String refName = createPersonRefName(personAuthorityRefName, displayName, true);\r
184 \r
185         if(logger.isDebugEnabled()){\r
186                 logger.debug("Import: Create Item: \""+displayName\r
187                                 +"\" in personAuthorityulary: \"" + personAuthorityRefName +"\"");\r
188         }\r
189         MultipartOutput multipart = \r
190                 createPersonInstance( vcsid, refName,\r
191                         personMap, client.getItemCommonPartName() );\r
192         \r
193         String result = null;\r
194         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
195         try {\r
196                 int statusCode = res.getStatus();\r
197         \r
198                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
199                         throw new RuntimeException("Could not create Item: \""+refName\r
200                                         +"\" in personAuthority: \"" + personAuthorityRefName\r
201                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
202                 }\r
203                 if(statusCode != EXPECTED_STATUS_CODE) {\r
204                         throw new RuntimeException("Unexpected Status when creating Item: \""+refName\r
205                                         +"\" in personAuthority: \"" + personAuthorityRefName +"\", Status:"+ statusCode);\r
206                 }\r
207         \r
208                 result = extractId(res);\r
209         } finally {\r
210                 res.releaseConnection();\r
211         }\r
212         \r
213         return result;\r
214     }\r
215 \r
216     /**\r
217      * Creates the person auth ref name.\r
218      *\r
219      * @param personAuthorityName the person authority name\r
220      * @param withDisplaySuffix the with display suffix\r
221      * @return the string\r
222      */\r
223     public static String createPersonAuthRefName(String personAuthorityName, boolean withDisplaySuffix) {\r
224         String refName = "urn:cspace:org.collectionspace.demo:personauthority:name("\r
225                         +personAuthorityName+")";\r
226         if(withDisplaySuffix)\r
227                 refName += "'"+personAuthorityName+"'";\r
228         return refName;\r
229     }\r
230 \r
231     /**\r
232      * Creates the person ref name.\r
233      *\r
234      * @param personAuthRefName the person auth ref name\r
235      * @param personName the person name\r
236      * @param withDisplaySuffix the with display suffix\r
237      * @return the string\r
238      */\r
239     public static String createPersonRefName(\r
240                                                 String personAuthRefName, String personName, boolean withDisplaySuffix) {\r
241         String refName = personAuthRefName+":person:name("+personName+")";\r
242         if(withDisplaySuffix)\r
243                 refName += "'"+personName+"'";\r
244         return refName;\r
245     }\r
246 \r
247     /**\r
248      * Extract id.\r
249      *\r
250      * @param res the res\r
251      * @return the string\r
252      */\r
253     public static String extractId(ClientResponse<Response> res) {\r
254         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
255         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
256         if(logger.isDebugEnabled()){\r
257                 logger.debug("extractId:uri=" + uri);\r
258         }\r
259         String[] segments = uri.split("/");\r
260         String id = segments[segments.length - 1];\r
261         if(logger.isDebugEnabled()){\r
262                 logger.debug("id=" + id);\r
263         }\r
264         return id;\r
265     }\r
266     \r
267     /**\r
268      * Returns an error message indicating that the status code returned by a\r
269      * specific call to a service does not fall within a set of valid status\r
270      * codes for that service.\r
271      *\r
272      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
273      *\r
274      * @param statusCode  The invalid status code that was returned in the response,\r
275      *                    from submitting that type of request to the service.\r
276      *\r
277      * @return An error message.\r
278      */\r
279     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
280         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
281                 requestType.validStatusCodesAsString();\r
282     }\r
283 \r
284 \r
285     \r
286     /**\r
287      * Produces a default displayName from the basic name and dates fields.\r
288      * @see PersonDocumentModelHandler.prepareDefaultDisplayName() which\r
289      * duplicates this logic, until we define a service-general utils package\r
290      * that is neither client nor service specific.\r
291      * @param foreName  \r
292      * @param middleName\r
293      * @param surName\r
294      * @param birthDate\r
295      * @param deathDate\r
296      * @return display name\r
297      */\r
298     public static String prepareDefaultDisplayName(\r
299                 String foreName, String middleName, String surName,\r
300                 String birthDate, String deathDate ) {\r
301         StringBuilder newStr = new StringBuilder();\r
302                 final String sep = " ";\r
303                 final String dateSep = "-";\r
304                 List<String> nameStrings = \r
305                         Arrays.asList(foreName, middleName, surName);\r
306                 boolean firstAdded = false;\r
307         for(String partStr : nameStrings ){\r
308                         if(null != partStr ) {\r
309                                 if(firstAdded) {\r
310                                         newStr.append(sep);\r
311                                 }\r
312                                 newStr.append(partStr);\r
313                                 firstAdded = true;\r
314                         }\r
315         }\r
316         // Now we add the dates. In theory could have dates with no name, but that is their problem.\r
317         boolean foundBirth = false;\r
318                 if(null != birthDate) {\r
319                         if(firstAdded) {\r
320                                 newStr.append(sep);\r
321                         }\r
322                         newStr.append(birthDate);\r
323                 newStr.append(dateSep);         // Put this in whether there is a death date or not\r
324                         foundBirth = true;\r
325                 }\r
326                 if(null != deathDate) {\r
327                         if(!foundBirth) {\r
328                                 if(firstAdded) {\r
329                                         newStr.append(sep);\r
330                                 }\r
331                         newStr.append(dateSep);\r
332                         }\r
333                         newStr.append(deathDate);\r
334                 }\r
335                 return newStr.toString();\r
336     }\r
337     \r
338 \r
339 \r
340 }\r