1 package org.collectionspace.services.client;
4 import java.util.ArrayList;
8 import javax.ws.rs.core.MediaType;
9 import javax.ws.rs.core.MultivaluedMap;
10 import javax.ws.rs.core.Response;
11 import org.apache.commons.io.FileUtils;
12 import org.collectionspace.services.LocationJAXBSchema;
13 import org.collectionspace.services.client.test.ServiceRequestType;
14 import org.collectionspace.services.common.api.Tools;
15 import org.collectionspace.services.location.*;
16 import org.dom4j.DocumentException;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public class LocationAuthorityClientUtils {
21 private static final Logger logger =
22 LoggerFactory.getLogger(LocationAuthorityClientUtils.class);
25 * Creates a new Location Authority
26 * @param displayName The displayName used in UI, etc.
27 * @param refName The proper refName for this authority
28 * @param headerLabel The common part label
29 * @return The PoxPayloadOut payload for the create call
31 public static PoxPayloadOut createLocationAuthorityInstance(
32 String displayName, String shortIdentifier, String headerLabel ) {
34 LocationauthoritiesCommon locationAuthority = new LocationauthoritiesCommon();
35 locationAuthority.setDisplayName(displayName);
36 locationAuthority.setShortIdentifier(shortIdentifier);
37 // String refName = createLocationAuthRefName(shortIdentifier, displayName);
38 // locationAuthority.setRefName(refName);
39 locationAuthority.setVocabType("LocationAuthority"); //FIXME: REM - Should this really be hard-coded?
41 PoxPayloadOut multipart = new PoxPayloadOut(LocationAuthorityClient.SERVICE_PAYLOAD_NAME);
42 PayloadOutputPart commonPart = multipart.addPart(headerLabel, locationAuthority);
44 if(logger.isDebugEnabled()){
45 logger.debug("to be created, locationAuthority common ",
46 locationAuthority, LocationauthoritiesCommon.class);
53 * @param locationRefName The proper refName for this authority
54 * @param locationInfo the properties for the new Location. Can pass in one condition
55 * note and date string.
56 * @param headerLabel The common part label
57 * @return The PoxPayloadOut payload for the create call
59 public static PoxPayloadOut createLocationInstance(
60 String locationAuthRefName, Map<String, String> locationInfo,
61 List<LocTermGroup> terms, String headerLabel){
62 LocationsCommon location = new LocationsCommon();
63 String shortId = locationInfo.get(LocationJAXBSchema.SHORT_IDENTIFIER);
64 String displayName = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME);
65 location.setShortIdentifier(shortId);
66 // String locationRefName = createLocationRefName(locationAuthRefName, shortId, displayName);
67 // location.setRefName(locationRefName);
69 value = locationInfo.get(LocationJAXBSchema.DISPLAY_NAME_COMPUTED);
70 boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true");
72 // Set values in the Term Information Group
73 LocTermGroupList termList = new LocTermGroupList();
74 if (terms == null || terms.isEmpty()) {
75 terms = getTermGroupInstance(getGeneratedIdentifier());
77 termList.getLocTermGroup().addAll(terms);
78 location.setLocTermGroupList(termList);
80 if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE))!=null) {
81 ConditionGroupList conditionGroupList = new ConditionGroupList();
82 List<ConditionGroup> conditionGroups = conditionGroupList.getConditionGroup();
83 ConditionGroup conditionGroup = new ConditionGroup();
84 conditionGroup.setConditionNote(value);
85 if((value = (String)locationInfo.get(LocationJAXBSchema.CONDITION_NOTE_DATE))!=null)
86 conditionGroup.setConditionNoteDate(value);
87 conditionGroups.add(conditionGroup);
88 location.setConditionGroupList(conditionGroupList);
90 if((value = (String)locationInfo.get(LocationJAXBSchema.SECURITY_NOTE))!=null)
91 location.setSecurityNote(value);
92 if((value = (String)locationInfo.get(LocationJAXBSchema.ACCESS_NOTE))!=null)
93 location.setAccessNote(value);
94 if((value = (String)locationInfo.get(LocationJAXBSchema.LOCATION_TYPE))!=null)
95 location.setLocationType(value);
96 if((value = (String)locationInfo.get(LocationJAXBSchema.ADDRESS))!=null)
97 location.setAddress(value);
99 PoxPayloadOut multipart = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
100 PayloadOutputPart commonPart = multipart.addPart(location,
101 MediaType.APPLICATION_XML_TYPE);
102 commonPart.setLabel(headerLabel);
104 if(logger.isDebugEnabled()){
105 logger.debug("to be created, location common ", location, LocationsCommon.class);
112 * @param vcsid CSID of the authority to create a new location in
113 * @param locationAuthorityRefName The refName for the authority
114 * @param locationMap the properties for the new Location
115 * @param client the service client
116 * @return the CSID of the new item
118 public static String createItemInAuthority(String vcsid,
119 String locationAuthorityRefName,
120 Map<String,String> locationMap,
121 List<LocTermGroup> terms,
122 LocationAuthorityClient client ) {
124 String displayName = "";
125 if (terms !=null && !terms.isEmpty()) {
126 displayName = terms.get(0).getTermDisplayName();
129 if(logger.isDebugEnabled()){
130 logger.debug("Creating item with display name: \"" + displayName
131 +"\" in locationAuthority: \"" + vcsid +"\"");
134 PoxPayloadOut multipart = createLocationInstance( locationAuthorityRefName,
135 locationMap, terms, client.getItemCommonPartName() );
137 Response res = client.createItem(vcsid, multipart);
139 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
140 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
141 int statusCode = res.getStatus();
143 if (!REQUEST_TYPE.isValidStatusCode(statusCode)) {
144 throw new RuntimeException("Could not create Item: \""
145 +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)
146 +"\" in locationAuthority: \"" + locationAuthorityRefName
147 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
149 if (statusCode != EXPECTED_STATUS_CODE) {
150 throw new RuntimeException("Unexpected Status when creating Item: \""
151 +locationMap.get(LocationJAXBSchema.SHORT_IDENTIFIER)
152 +"\" in locationAuthority: \"" + locationAuthorityRefName +"\", Status:"+ statusCode);
154 newID = extractId(res);
162 public static PoxPayloadOut createLocationInstance(
163 String commonPartXML, String headerLabel) throws DocumentException {
164 PoxPayloadOut multipart = new PoxPayloadOut(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
165 PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
166 MediaType.APPLICATION_XML_TYPE);
167 commonPart.setLabel(headerLabel);
169 if(logger.isDebugEnabled()){
170 logger.debug("to be created, location common ", commonPartXML);
176 public static String createItemInAuthority(String vcsid,
177 String commonPartXML,
178 LocationAuthorityClient client ) throws DocumentException {
179 // Expected status code: 201 Created
180 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
181 // Type of service request being tested
182 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
184 PoxPayloadOut multipart =
185 createLocationInstance(commonPartXML, client.getItemCommonPartName());
187 Response res = client.createItem(vcsid, multipart);
189 int statusCode = res.getStatus();
191 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
192 throw new RuntimeException("Could not create Item: \""+commonPartXML
193 +"\" in locationAuthority: \"" + vcsid
194 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
196 if(statusCode != EXPECTED_STATUS_CODE) {
197 throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
198 +"\" in locationAuthority: \"" + vcsid +"\", Status:"+ statusCode);
200 newID = extractId(res);
209 * Creates the from xml file.
211 * @param fileName the file name
212 * @return new CSID as string
213 * @throws Exception the exception
215 private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName,
216 LocationAuthorityClient client) throws Exception {
217 byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
218 String commonPartXML = new String(b);
219 return createItemInAuthority(vcsid, commonPartXML, client );
223 * Creates the locationAuthority ref name.
225 * @param shortId the locationAuthority shortIdentifier
226 * @param displaySuffix displayName to be appended, if non-null
229 public static String createLocationAuthRefName(String shortId, String displaySuffix) {
230 String refName = "urn:cspace:org.collectionspace.demo:locationauthority:name("
232 if(displaySuffix!=null&&!displaySuffix.isEmpty())
233 refName += "'"+displaySuffix+"'";
238 * Creates the location ref name.
240 * @param locationAuthRefName the locationAuthority ref name
241 * @param shortId the location shortIdentifier
242 * @param displaySuffix displayName to be appended, if non-null
245 public static String createLocationRefName(
246 String locationAuthRefName, String shortId, String displaySuffix) {
247 String refName = locationAuthRefName+":location:name("+shortId+")";
248 if(displaySuffix!=null&&!displaySuffix.isEmpty())
249 refName += "'"+displaySuffix+"'";
253 public static String extractId(Response res) {
254 MultivaluedMap<String, Object> mvm = res.getMetadata();
255 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
256 if(logger.isDebugEnabled()){
257 logger.debug("extractId:uri=" + uri);
259 String[] segments = uri.split("/");
260 String id = segments[segments.length - 1];
261 if(logger.isDebugEnabled()){
262 logger.debug("id=" + id);
268 * Returns an error message indicating that the status code returned by a
269 * specific call to a service does not fall within a set of valid status
270 * codes for that service.
272 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
274 * @param statusCode The invalid status code that was returned in the response,
275 * from submitting that type of request to the service.
277 * @return An error message.
279 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
280 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
281 requestType.validStatusCodesAsString();
287 * Produces a default displayName from the basic name and dates fields.
288 * @see LocationDocumentModelHandler.prepareDefaultDisplayName() which
289 * duplicates this logic, until we define a service-general utils package
290 * that is neither client nor service specific.
294 public static String prepareDefaultDisplayName(
296 StringBuilder newStr = new StringBuilder();
298 return newStr.toString();
301 public static List<LocTermGroup> getTermGroupInstance(String identifier) {
302 if (Tools.isBlank(identifier)) {
303 identifier = getGeneratedIdentifier();
305 List<LocTermGroup> terms = new ArrayList<LocTermGroup>();
306 LocTermGroup term = new LocTermGroup();
307 term.setTermDisplayName(identifier);
308 term.setTermName(identifier);
313 private static String getGeneratedIdentifier() {
314 return "id" + new Date().getTime();