1 package org.collectionspace.services.client;
\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
8 import javax.ws.rs.core.MediaType;
\r
9 import javax.ws.rs.core.MultivaluedMap;
\r
10 import javax.ws.rs.core.Response;
\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
22 public class PersonAuthorityClientUtils {
\r
23 private static final Logger logger =
\r
24 LoggerFactory.getLogger(PersonAuthorityClientUtils.class);
\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
36 if(logger.isDebugEnabled()){
\r
37 logger.debug("to be created, personAuthority common ",
\r
38 personAuthority, PersonauthoritiesCommon.class);
\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
94 if(logger.isDebugEnabled()){
\r
95 logger.debug("to be created, person common ", person, PersonsCommon.class);
\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
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
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
126 String refName = createPersonRefName(personAuthorityRefName, displayName, true);
\r
128 if(logger.isDebugEnabled()){
\r
129 logger.debug("Import: Create Item: \""+displayName
\r
130 +"\" in personAuthorityulary: \"" + personAuthorityRefName +"\"");
\r
132 MultipartOutput multipart =
\r
133 createPersonInstance( vcsid, refName,
\r
134 personMap, client.getItemCommonPartName() );
\r
135 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
137 int statusCode = res.getStatus();
\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
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
149 return extractId(res);
\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
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
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
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
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
187 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\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
192 * @return An error message.
\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
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
207 * @param middleName
\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
225 newStr.append(sep);
\r
227 newStr.append(partStr);
\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
235 newStr.append(sep);
\r
237 newStr.append(birthDate);
\r
238 newStr.append(dateSep); // Put this in whether there is a death date or not
\r
241 if(null != deathDate) {
\r
244 newStr.append(sep);
\r
246 newStr.append(dateSep);
\r
248 newStr.append(deathDate);
\r
250 return newStr.toString();
\r