]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
142aa80fe423a0af2832b64e2eba2732537ab3a7
[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         \r
98         //\r
99         // If the 'DISPLAY_NAME_COMPUTED' property is null or empty then\r
100         // we'll assume that the service consumer wants us to compute the\r
101         // display name.  Otherwise, we'll parse the value with the Boolean class.\r
102         //\r
103         String booleanStr = personInfo.get(PersonJAXBSchema.DISPLAY_NAME_COMPUTED);\r
104         boolean displayNameComputed = true;\r
105         if (booleanStr != null && booleanStr.length() > 0) {\r
106                 displayNameComputed = Boolean.parseBoolean(booleanStr);\r
107         }\r
108         person.setDisplayNameComputed(displayNameComputed);\r
109 \r
110         String displayName = personInfo.get(PersonJAXBSchema.DISPLAY_NAME);\r
111         person.setDisplayName(displayName);\r
112         if (displayNameComputed == false && displayName == null) {\r
113                 throw new IllegalArgumentException("displayName cannot be null when displayComputed is 'false'");\r
114         }       \r
115         \r
116         String value;\r
117         if((value = (String)personInfo.get(PersonJAXBSchema.FORE_NAME))!=null) //FIXME: REM - I don't think we need to check for null -null is a valid value and won't cause any problems. \r
118                 person.setForeName(value);\r
119         if((value = (String)personInfo.get(PersonJAXBSchema.MIDDLE_NAME))!=null)\r
120                 person.setMiddleName(value);\r
121         if((value = (String)personInfo.get(PersonJAXBSchema.SUR_NAME))!=null)\r
122                 person.setSurName(value);\r
123         if((value = (String)personInfo.get(PersonJAXBSchema.INITIALS))!=null)\r
124                 person.setInitials(value);\r
125         if((value = (String)personInfo.get(PersonJAXBSchema.SALUTATIONS))!=null)\r
126                 person.setSalutation(value);\r
127         if((value = (String)personInfo.get(PersonJAXBSchema.TITLE))!=null)\r
128                 person.setTitle(value);\r
129         if((value = (String)personInfo.get(PersonJAXBSchema.NAME_ADDITIONS))!=null)\r
130                 person.setNameAdditions(value);\r
131         if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_DATE))!=null)\r
132                 person.setBirthDate(value);\r
133         if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_DATE))!=null)\r
134                 person.setDeathDate(value);\r
135         if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_PLACE))!=null)\r
136                 person.setBirthPlace(value);\r
137         if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_PLACE))!=null)\r
138                 person.setDeathPlace(value);\r
139         if((value = (String)personInfo.get(PersonJAXBSchema.GROUP))!=null)\r
140                 person.setGroup(value);\r
141         if((value = (String)personInfo.get(PersonJAXBSchema.NATIONALITY))!=null)\r
142                 person.setNationality(value);\r
143         if((value = (String)personInfo.get(PersonJAXBSchema.GENDER))!=null)\r
144                 person.setGender(value);\r
145         if((value = (String)personInfo.get(PersonJAXBSchema.OCCUPATION))!=null)\r
146                 person.setOccupation(value);\r
147         if((value = (String)personInfo.get(PersonJAXBSchema.SCHOOL_OR_STYLE))!=null)\r
148                 person.setSchoolOrStyle(value);\r
149         if((value = (String)personInfo.get(PersonJAXBSchema.BIO_NOTE))!=null)\r
150                 person.setBioNote(value);\r
151         if((value = (String)personInfo.get(PersonJAXBSchema.NAME_NOTE))!=null)\r
152                 person.setNameNote(value);\r
153         \r
154         MultipartOutput multipart = new MultipartOutput();\r
155         OutputPart commonPart = multipart.addPart(person,\r
156             MediaType.APPLICATION_XML_TYPE);\r
157         commonPart.getHeaders().add("label", headerLabel);\r
158 \r
159         if(logger.isDebugEnabled()){\r
160                 logger.debug("to be created, person common ", person, PersonsCommon.class);\r
161         }\r
162 \r
163         return multipart;\r
164     }\r
165     \r
166     /**\r
167      * Creates the item in authority.\r
168      *\r
169      * @param vcsid the vcsid\r
170      * @param personAuthorityRefName the person authority ref name\r
171      * @param personMap the person map\r
172      * @param client the client\r
173      * @return the string\r
174      */\r
175     public static String createItemInAuthority(String vcsid, \r
176                 String personAuthorityRefName, Map<String,String> personMap,\r
177                 PersonAuthorityClient 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         String displayName = personMap.get(PersonJAXBSchema.DISPLAY_NAME);\r
184         String displayNameComputedStr = personMap.get(PersonJAXBSchema.DISPLAY_NAME_COMPUTED);\r
185         boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");\r
186         if( displayName == null ) {\r
187                 if(!displayNameComputed) {\r
188                         throw new RuntimeException(\r
189                         "CreateItem: Must supply a displayName if displayNameComputed is set to false.");\r
190                 }\r
191                 displayName = \r
192                         prepareDefaultDisplayName(\r
193                         personMap.get(PersonJAXBSchema.FORE_NAME),\r
194                         personMap.get(PersonJAXBSchema.MIDDLE_NAME),\r
195                         personMap.get(PersonJAXBSchema.SUR_NAME),\r
196                         personMap.get(PersonJAXBSchema.BIRTH_DATE),\r
197                         personMap.get(PersonJAXBSchema.DEATH_DATE));\r
198         }\r
199         \r
200         String refName = createPersonRefName(personAuthorityRefName, displayName, true);\r
201 \r
202         if(logger.isDebugEnabled()){\r
203                 logger.debug("Import: Create Item: \"" + displayName\r
204                                 +"\" in personAuthorityulary: \"" + personAuthorityRefName +"\"");\r
205         }\r
206         MultipartOutput multipart = \r
207                 createPersonInstance(vcsid, refName,\r
208                         personMap, client.getItemCommonPartName());\r
209         \r
210         String result = null;\r
211         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
212         try {\r
213                 int statusCode = res.getStatus();\r
214         \r
215                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
216                         throw new RuntimeException("Could not create Item: \""+refName\r
217                                         +"\" in personAuthority: \"" + personAuthorityRefName\r
218                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
219                 }\r
220                 if(statusCode != EXPECTED_STATUS_CODE) {\r
221                         throw new RuntimeException("Unexpected Status when creating Item: \""+refName\r
222                                         +"\" in personAuthority: \"" + personAuthorityRefName +"\", Status:"+ statusCode);\r
223                 }\r
224         \r
225                 result = extractId(res);\r
226         } finally {\r
227                 res.releaseConnection();\r
228         }\r
229         \r
230         return result;\r
231     }\r
232 \r
233     /**\r
234      * Creates the person auth ref name.\r
235      *\r
236      * @param personAuthorityName the person authority name\r
237      * @param withDisplaySuffix the with display suffix\r
238      * @return the string\r
239      */\r
240     public static String createPersonAuthRefName(String personAuthorityName, boolean withDisplaySuffix) {\r
241         String refName = "urn:cspace:org.collectionspace.demo:personauthority:name("\r
242                         +personAuthorityName+")";\r
243         if(withDisplaySuffix)\r
244                 refName += "'"+personAuthorityName+"'";\r
245         return refName;\r
246     }\r
247 \r
248     /**\r
249      * Creates the person ref name.\r
250      *\r
251      * @param personAuthRefName the person auth ref name\r
252      * @param personName the person name\r
253      * @param withDisplaySuffix the with display suffix\r
254      * @return the string\r
255      */\r
256     public static String createPersonRefName(\r
257                                                 String personAuthRefName, String personName, boolean withDisplaySuffix) {\r
258         String refName = personAuthRefName+":person:name("+personName+")";\r
259         if(withDisplaySuffix)\r
260                 refName += "'"+personName+"'";\r
261         return refName;\r
262     }\r
263 \r
264     /**\r
265      * Extract id.\r
266      *\r
267      * @param res the res\r
268      * @return the string\r
269      */\r
270     public static String extractId(ClientResponse<Response> res) {\r
271         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
272         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
273         if(logger.isDebugEnabled()){\r
274                 logger.debug("extractId:uri=" + uri);\r
275         }\r
276         String[] segments = uri.split("/");\r
277         String id = segments[segments.length - 1];\r
278         if(logger.isDebugEnabled()){\r
279                 logger.debug("id=" + id);\r
280         }\r
281         return id;\r
282     }\r
283     \r
284     /**\r
285      * Returns an error message indicating that the status code returned by a\r
286      * specific call to a service does not fall within a set of valid status\r
287      * codes for that service.\r
288      *\r
289      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
290      *\r
291      * @param statusCode  The invalid status code that was returned in the response,\r
292      *                    from submitting that type of request to the service.\r
293      *\r
294      * @return An error message.\r
295      */\r
296     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
297         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
298                 requestType.validStatusCodesAsString();\r
299     }\r
300 \r
301 \r
302     \r
303     /**\r
304      * Produces a default displayName from the basic name and dates fields.\r
305      * @see PersonDocumentModelHandler.prepareDefaultDisplayName() which\r
306      * duplicates this logic, until we define a service-general utils package\r
307      * that is neither client nor service specific.\r
308      * @param foreName  \r
309      * @param middleName\r
310      * @param surName\r
311      * @param birthDate\r
312      * @param deathDate\r
313      * @return display name\r
314      */\r
315     public static String prepareDefaultDisplayName(\r
316                 String foreName, String middleName, String surName,\r
317                 String birthDate, String deathDate ) {\r
318         StringBuilder newStr = new StringBuilder();\r
319                 final String sep = " ";\r
320                 final String dateSep = "-";\r
321                 List<String> nameStrings = \r
322                         Arrays.asList(foreName, middleName, surName);\r
323                 boolean firstAdded = false;\r
324         for(String partStr : nameStrings ){\r
325                         if(null != partStr ) {\r
326                                 if(firstAdded) {\r
327                                         newStr.append(sep);\r
328                                 }\r
329                                 newStr.append(partStr);\r
330                                 firstAdded = true;\r
331                         }\r
332         }\r
333         // Now we add the dates. In theory could have dates with no name, but that is their problem.\r
334         boolean foundBirth = false;\r
335                 if(null != birthDate) {\r
336                         if(firstAdded) {\r
337                                 newStr.append(sep);\r
338                         }\r
339                         newStr.append(birthDate);\r
340                 newStr.append(dateSep);         // Put this in whether there is a death date or not\r
341                         foundBirth = true;\r
342                 }\r
343                 if(null != deathDate) {\r
344                         if(!foundBirth) {\r
345                                 if(firstAdded) {\r
346                                         newStr.append(sep);\r
347                                 }\r
348                         newStr.append(dateSep);\r
349                         }\r
350                         newStr.append(deathDate);\r
351                 }\r
352                 return newStr.toString();\r
353     }\r
354     \r
355 \r
356 \r
357 }\r