]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fb28b2979c69fa08360a569e8be56270a636f491
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Arrays;\r
5 import java.util.List;\r
6 import java.util.Map;\r
7 \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 \r
12 import org.collectionspace.services.PersonJAXBSchema;\r
13 import org.collectionspace.services.client.test.ServiceRequestType;\r
14 import org.collectionspace.services.person.PersonsCommon;\r
15 import org.collectionspace.services.person.PersonauthoritiesCommon;\r
16 import org.jboss.resteasy.client.ClientResponse;\r
17 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
18 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
19 import org.slf4j.Logger;\r
20 import org.slf4j.LoggerFactory;\r
21 \r
22 public class PersonAuthorityClientUtils {\r
23     private static final Logger logger =\r
24         LoggerFactory.getLogger(PersonAuthorityClientUtils.class);\r
25 \r
26     public static MultipartOutput createPersonAuthorityInstance(\r
27                 String displayName, String refName, String headerLabel ) {\r
28         PersonauthoritiesCommon personAuthority = new PersonauthoritiesCommon();\r
29         personAuthority.setDisplayName(displayName);\r
30         personAuthority.setRefName(refName);\r
31         personAuthority.setVocabType("PersonAuthority");\r
32         MultipartOutput multipart = new MultipartOutput();\r
33         OutputPart commonPart = multipart.addPart(personAuthority, MediaType.APPLICATION_XML_TYPE);\r
34         commonPart.getHeaders().add("label", headerLabel);\r
35 \r
36         if(logger.isDebugEnabled()){\r
37                 logger.debug("to be created, personAuthority common ", \r
38                                         personAuthority, PersonauthoritiesCommon.class);\r
39         }\r
40 \r
41         return multipart;\r
42     }\r
43 \r
44     public static MultipartOutput createPersonInstance(String inAuthority, \r
45                 String personRefName, Map<String, String> personInfo, String headerLabel){\r
46         PersonsCommon person = new PersonsCommon();\r
47         person.setInAuthority(inAuthority);\r
48         person.setRefName(personRefName);\r
49         String value = null;\r
50         value = personInfo.get(PersonJAXBSchema.DISPLAY_NAME_COMPUTED);\r
51         boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true"); \r
52         person.setDisplayNameComputed(displayNameComputed);\r
53         if((value = (String)personInfo.get(PersonJAXBSchema.FORE_NAME))!=null)\r
54                 person.setForeName(value);\r
55         if((value = (String)personInfo.get(PersonJAXBSchema.MIDDLE_NAME))!=null)\r
56                 person.setMiddleName(value);\r
57         if((value = (String)personInfo.get(PersonJAXBSchema.SUR_NAME))!=null)\r
58                 person.setSurName(value);\r
59         if((value = (String)personInfo.get(PersonJAXBSchema.INITIALS))!=null)\r
60                 person.setInitials(value);\r
61         if((value = (String)personInfo.get(PersonJAXBSchema.SALUTATIONS))!=null)\r
62                 person.setSalutation(value);\r
63         if((value = (String)personInfo.get(PersonJAXBSchema.TITLE))!=null)\r
64                 person.setTitle(value);\r
65         if((value = (String)personInfo.get(PersonJAXBSchema.NAME_ADDITIONS))!=null)\r
66                 person.setNameAdditions(value);\r
67         if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_DATE))!=null)\r
68                 person.setBirthDate(value);\r
69         if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_DATE))!=null)\r
70                 person.setDeathDate(value);\r
71         if((value = (String)personInfo.get(PersonJAXBSchema.BIRTH_PLACE))!=null)\r
72                 person.setBirthPlace(value);\r
73         if((value = (String)personInfo.get(PersonJAXBSchema.DEATH_PLACE))!=null)\r
74                 person.setDeathPlace(value);\r
75         if((value = (String)personInfo.get(PersonJAXBSchema.GROUP))!=null)\r
76                 person.setGroup(value);\r
77         if((value = (String)personInfo.get(PersonJAXBSchema.NATIONALITY))!=null)\r
78                 person.setNationality(value);\r
79         if((value = (String)personInfo.get(PersonJAXBSchema.GENDER))!=null)\r
80                 person.setGender(value);\r
81         if((value = (String)personInfo.get(PersonJAXBSchema.OCCUPATION))!=null)\r
82                 person.setOccupation(value);\r
83         if((value = (String)personInfo.get(PersonJAXBSchema.SCHOOL_OR_STYLE))!=null)\r
84                 person.setSchoolOrStyle(value);\r
85         if((value = (String)personInfo.get(PersonJAXBSchema.BIO_NOTE))!=null)\r
86                 person.setBioNote(value);\r
87         if((value = (String)personInfo.get(PersonJAXBSchema.NAME_NOTE))!=null)\r
88                 person.setNameNote(value);\r
89         MultipartOutput multipart = new MultipartOutput();\r
90         OutputPart commonPart = multipart.addPart(person,\r
91             MediaType.APPLICATION_XML_TYPE);\r
92         commonPart.getHeaders().add("label", headerLabel);\r
93 \r
94         if(logger.isDebugEnabled()){\r
95                 logger.debug("to be created, person common ", person, PersonsCommon.class);\r
96         }\r
97 \r
98         return multipart;\r
99     }\r
100     \r
101     public static String createItemInAuthority(String vcsid, \r
102                 String personAuthorityRefName, Map<String,String> personMap,\r
103                 PersonAuthorityClient client ) {\r
104         // Expected status code: 201 Created\r
105         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
106         // Type of service request being tested\r
107         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
108         \r
109         String displayName = personMap.get(PersonJAXBSchema.DISPLAY_NAME);\r
110         String displayNameComputedStr = personMap.get(PersonJAXBSchema.DISPLAY_NAME_COMPUTED);\r
111         boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");\r
112         if( displayName == null ) {\r
113                 if(!displayNameComputed) {\r
114                         throw new RuntimeException(\r
115                         "CreateItem: Must supply a displayName if displayNameComputed is set to false.");\r
116                 }\r
117                 displayName = \r
118                         prepareDefaultDisplayName(\r
119                         personMap.get(PersonJAXBSchema.FORE_NAME),\r
120                         personMap.get(PersonJAXBSchema.MIDDLE_NAME),\r
121                         personMap.get(PersonJAXBSchema.SUR_NAME),\r
122                         personMap.get(PersonJAXBSchema.BIRTH_DATE),\r
123                         personMap.get(PersonJAXBSchema.DEATH_DATE));\r
124         }\r
125         \r
126         String refName = createPersonRefName(personAuthorityRefName, displayName, true);\r
127 \r
128         if(logger.isDebugEnabled()){\r
129                 logger.debug("Import: Create Item: \""+displayName\r
130                                 +"\" in personAuthorityulary: \"" + personAuthorityRefName +"\"");\r
131         }\r
132         MultipartOutput multipart = \r
133                 createPersonInstance( vcsid, refName,\r
134                         personMap, client.getItemCommonPartName() );\r
135         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
136 \r
137         int statusCode = res.getStatus();\r
138 \r
139         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
140                 throw new RuntimeException("Could not create Item: \""+refName\r
141                                 +"\" in personAuthority: \"" + personAuthorityRefName\r
142                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
143         }\r
144         if(statusCode != EXPECTED_STATUS_CODE) {\r
145                 throw new RuntimeException("Unexpected Status when creating Item: \""+refName\r
146                                 +"\" in personAuthority: \"" + personAuthorityRefName +"\", Status:"+ statusCode);\r
147         }\r
148 \r
149         return extractId(res);\r
150     }\r
151 \r
152     public static String createPersonAuthRefName(String personAuthorityName, boolean withDisplaySuffix) {\r
153         String refName = "urn:cspace:org.collectionspace.demo:personauthority:name("\r
154                         +personAuthorityName+")";\r
155         if(withDisplaySuffix)\r
156                 refName += "'"+personAuthorityName+"'";\r
157         return refName;\r
158     }\r
159 \r
160     public static String createPersonRefName(\r
161                                                 String personAuthRefName, String personName, boolean withDisplaySuffix) {\r
162         String refName = personAuthRefName+":person:name("+personName+")";\r
163         if(withDisplaySuffix)\r
164                 refName += "'"+personName+"'";\r
165         return refName;\r
166     }\r
167 \r
168     public static String extractId(ClientResponse<Response> res) {\r
169         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
170         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
171         if(logger.isDebugEnabled()){\r
172                 logger.debug("extractId:uri=" + uri);\r
173         }\r
174         String[] segments = uri.split("/");\r
175         String id = segments[segments.length - 1];\r
176         if(logger.isDebugEnabled()){\r
177                 logger.debug("id=" + id);\r
178         }\r
179         return id;\r
180     }\r
181     \r
182     /**\r
183      * Returns an error message indicating that the status code returned by a\r
184      * specific call to a service does not fall within a set of valid status\r
185      * codes for that service.\r
186      *\r
187      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
188      *\r
189      * @param statusCode  The invalid status code that was returned in the response,\r
190      *                    from submitting that type of request to the service.\r
191      *\r
192      * @return An error message.\r
193      */\r
194     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
195         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
196                 requestType.validStatusCodesAsString();\r
197     }\r
198 \r
199 \r
200     \r
201     /**\r
202      * Produces a default displayName from the basic name and dates fields.\r
203      * @see PersonDocumentModelHandler.prepareDefaultDisplayName() which\r
204      * duplicates this logic, until we define a service-general utils package\r
205      * that is neither client nor service specific.\r
206      * @param foreName  \r
207      * @param middleName\r
208      * @param surName\r
209      * @param birthDate\r
210      * @param deathDate\r
211      * @return\r
212      */\r
213     public static String prepareDefaultDisplayName(\r
214                 String foreName, String middleName, String surName,\r
215                 String birthDate, String deathDate ) {\r
216         StringBuilder newStr = new StringBuilder();\r
217                 final String sep = " ";\r
218                 final String dateSep = "-";\r
219                 List<String> nameStrings = \r
220                         Arrays.asList(foreName, middleName, surName);\r
221                 boolean firstAdded = false;\r
222         for(String partStr : nameStrings ){\r
223                         if(null != partStr ) {\r
224                                 if(firstAdded) {\r
225                                         newStr.append(sep);\r
226                                 }\r
227                                 newStr.append(partStr);\r
228                                 firstAdded = true;\r
229                         }\r
230         }\r
231         // Now we add the dates. In theory could have dates with no name, but that is their problem.\r
232         boolean foundBirth = false;\r
233                 if(null != birthDate) {\r
234                         if(firstAdded) {\r
235                                 newStr.append(sep);\r
236                         }\r
237                         newStr.append(birthDate);\r
238                 newStr.append(dateSep);         // Put this in whether there is a death date or not\r
239                         foundBirth = true;\r
240                 }\r
241                 if(null != deathDate) {\r
242                         if(!foundBirth) {\r
243                                 if(firstAdded) {\r
244                                         newStr.append(sep);\r
245                                 }\r
246                         newStr.append(dateSep);\r
247                         }\r
248                         newStr.append(deathDate);\r
249                 }\r
250                 return newStr.toString();\r
251     }\r
252     \r
253 \r
254 \r
255 }\r