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.LocationJAXBSchema;
\r
13 import org.collectionspace.services.client.test.ServiceRequestType;
\r
14 import org.collectionspace.services.location.LocationsCommon;
\r
15 import org.collectionspace.services.location.LocationauthoritiesCommon;
\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 LocationAuthorityClientUtils {
\r
23 private static final Logger logger =
\r
24 LoggerFactory.getLogger(LocationAuthorityClientUtils.class);
\r
26 public static MultipartOutput createLocationAuthorityInstance(
\r
27 String displayName, String refName, String headerLabel ) {
\r
28 LocationauthoritiesCommon locationAuthority = new LocationauthoritiesCommon();
\r
29 locationAuthority.setDisplayName(displayName);
\r
30 locationAuthority.setRefName(refName);
\r
31 locationAuthority.setVocabType("LocationAuthority");
\r
32 MultipartOutput multipart = new MultipartOutput();
\r
33 OutputPart commonPart = multipart.addPart(locationAuthority, MediaType.APPLICATION_XML_TYPE);
\r
34 commonPart.getHeaders().add("label", headerLabel);
\r
36 if(logger.isDebugEnabled()){
\r
37 logger.debug("to be created, locationAuthority common ",
\r
38 locationAuthority, LocationauthoritiesCommon.class);
\r
44 public static MultipartOutput createLocationInstance(String inAuthority,
\r
45 String locationRefName, Map<String, String> locationInfo, String headerLabel){
\r
46 LocationsCommon location = new LocationsCommon();
\r
47 location.setInAuthority(inAuthority);
\r
48 location.setRefName(locationRefName);
\r
49 String value = null;
\r
50 value = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);
\r
51 boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true");
\r
52 location.setDisplayNameComputed(displayNameComputed);
\r
53 if((value = (String)locationInfo.get(LocationJAXBSchema.NAME))!=null)
\r
54 location.setName(value);
\r
55 if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE))!=null)
\r
56 location.setConditionNote(value);
\r
57 if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE_DATE))!=null)
\r
58 location.setConditionNoteDate(value);
\r
59 if((value = (String)locationInfo.get(LocationJAXBSchema.SECURITY_NOTE))!=null)
\r
60 location.setSecurityNote(value);
\r
61 if((value = (String)locationInfo.get(LocationJAXBSchema.LOCATION_TYPE))!=null)
\r
62 location.setLocationType(value);
\r
63 if((value = (String)locationInfo.get(LocationJAXBSchema.STATUS))!=null)
\r
64 location.setStatus(value);
\r
65 MultipartOutput multipart = new MultipartOutput();
\r
66 OutputPart commonPart = multipart.addPart(location,
\r
67 MediaType.APPLICATION_XML_TYPE);
\r
68 commonPart.getHeaders().add("label", headerLabel);
\r
70 if(logger.isDebugEnabled()){
\r
71 logger.debug("to be created, location common ", location, LocationsCommon.class);
\r
77 public static String createItemInAuthority(String vcsid,
\r
78 String locationAuthorityRefName, Map<String,String> locationMap,
\r
79 LocationAuthorityClient client ) {
\r
80 // Expected status code: 201 Created
\r
81 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
82 // Type of service request being tested
\r
83 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
85 String displayName = locationMap.get(LocationJAXBSchema.DISPLAY_NAME);
\r
86 String displayNameComputedStr = locationMap.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);
\r
87 boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");
\r
88 if( displayName == null ) {
\r
89 if(!displayNameComputed) {
\r
90 throw new RuntimeException(
\r
91 "CreateItem: Must supply a displayName if displayNameComputed is set to false.");
\r
94 prepareDefaultDisplayName(
\r
95 locationMap.get(LocationJAXBSchema.NAME));
\r
98 String refName = createLocationRefName(locationAuthorityRefName, displayName, true);
\r
100 if(logger.isDebugEnabled()){
\r
101 logger.debug("Import: Create Item: \""+displayName
\r
102 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\"");
\r
104 MultipartOutput multipart =
\r
105 createLocationInstance( vcsid, refName,
\r
106 locationMap, client.getItemCommonPartName() );
\r
107 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
109 int statusCode = res.getStatus();
\r
111 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
112 throw new RuntimeException("Could not create Item: \""+refName
\r
113 +"\" in locationAuthority: \"" + locationAuthorityRefName
\r
114 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
116 if(statusCode != EXPECTED_STATUS_CODE) {
\r
117 throw new RuntimeException("Unexpected Status when creating Item: \""+refName
\r
118 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\", Status:"+ statusCode);
\r
121 return extractId(res);
\r
124 public static String createLocationAuthRefName(String locationAuthorityName, boolean withDisplaySuffix) {
\r
125 String refName = "urn:cspace:org.collectionspace.demo:locationauthority:name("
\r
126 +locationAuthorityName+")";
\r
127 if(withDisplaySuffix)
\r
128 refName += "'"+locationAuthorityName+"'";
\r
132 public static String createLocationRefName(
\r
133 String locationAuthRefName, String locationName, boolean withDisplaySuffix) {
\r
134 String refName = locationAuthRefName+":location:name("+locationName+")";
\r
135 if(withDisplaySuffix)
\r
136 refName += "'"+locationName+"'";
\r
140 public static String extractId(ClientResponse<Response> res) {
\r
141 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
142 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
\r
143 if(logger.isDebugEnabled()){
\r
144 logger.debug("extractId:uri=" + uri);
\r
146 String[] segments = uri.split("/");
\r
147 String id = segments[segments.length - 1];
\r
148 if(logger.isDebugEnabled()){
\r
149 logger.debug("id=" + id);
\r
155 * Returns an error message indicating that the status code returned by a
\r
156 * specific call to a service does not fall within a set of valid status
\r
157 * codes for that service.
\r
159 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\r
161 * @param statusCode The invalid status code that was returned in the response,
\r
162 * from submitting that type of request to the service.
\r
164 * @return An error message.
\r
166 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
\r
167 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
\r
168 requestType.validStatusCodesAsString();
\r
174 * Produces a default displayName from the basic name and dates fields.
\r
175 * @see LocationDocumentModelHandler.prepareDefaultDisplayName() which
\r
176 * duplicates this logic, until we define a service-general utils package
\r
177 * that is neither client nor service specific.
\r
181 public static String prepareDefaultDisplayName(
\r
183 StringBuilder newStr = new StringBuilder();
\r
184 newStr.append(name);
\r
185 return newStr.toString();
\r