2 * PersonAuthorityClientUtils.java
\r
4 * {Purpose of This Class}
\r
6 * {Other Notes Relating to This Class (Optional)}
\r
9 * $LastChangedRevision: $
\r
10 * $LastChangedDate: $
\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
16 * http://www.collectionspace.org
\r
17 * http://wiki.collectionspace.org
\r
19 * Copyright © 2009 {Contributing Institution}
\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
24 * You may obtain a copy of the ECL 2.0 License at
\r
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
27 package org.collectionspace.services.client;
\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
34 import javax.ws.rs.core.MediaType;
\r
35 import javax.ws.rs.core.MultivaluedMap;
\r
36 import javax.ws.rs.core.Response;
\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
49 * The Class PersonAuthorityClientUtils.
\r
51 public class PersonAuthorityClientUtils {
\r
53 /** The Constant logger. */
\r
54 private static final Logger logger =
\r
55 LoggerFactory.getLogger(PersonAuthorityClientUtils.class);
\r
58 * Creates the person authority instance.
\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
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
75 if(logger.isDebugEnabled()){
\r
76 logger.debug("to be created, personAuthority common ",
\r
77 personAuthority, PersonauthoritiesCommon.class);
\r
84 * Creates the person instance.
\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
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
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
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
108 person.setDisplayNameComputed(displayNameComputed);
\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
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
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
159 if(logger.isDebugEnabled()){
\r
160 logger.debug("to be created, person common ", person, PersonsCommon.class);
\r
167 * Creates the item in authority.
\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
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
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
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
200 String refName = createPersonRefName(personAuthorityRefName, displayName, true);
\r
202 if(logger.isDebugEnabled()){
\r
203 logger.debug("Import: Create Item: \"" + displayName
\r
204 +"\" in personAuthorityulary: \"" + personAuthorityRefName +"\"");
\r
206 MultipartOutput multipart =
\r
207 createPersonInstance(vcsid, refName,
\r
208 personMap, client.getItemCommonPartName());
\r
210 String result = null;
\r
211 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
213 int statusCode = res.getStatus();
\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
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
225 result = extractId(res);
\r
227 res.releaseConnection();
\r
234 * Creates the person auth ref name.
\r
236 * @param personAuthorityName the person authority name
\r
237 * @param withDisplaySuffix the with display suffix
\r
238 * @return the string
\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
249 * Creates the person ref name.
\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
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
267 * @param res the res
\r
268 * @return the string
\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
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
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
289 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\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
294 * @return An error message.
\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
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
309 * @param middleName
\r
313 * @return display name
\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
327 newStr.append(sep);
\r
329 newStr.append(partStr);
\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
337 newStr.append(sep);
\r
339 newStr.append(birthDate);
\r
340 newStr.append(dateSep); // Put this in whether there is a death date or not
\r
343 if(null != deathDate) {
\r
346 newStr.append(sep);
\r
348 newStr.append(dateSep);
\r
350 newStr.append(deathDate);
\r
352 return newStr.toString();
\r