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