1 package org.collectionspace.services.client;
4 import java.util.ArrayList;
6 import java.util.HashMap;
10 import javax.ws.rs.core.MediaType;
11 import javax.ws.rs.core.MultivaluedMap;
12 import javax.ws.rs.core.Response;
13 import org.apache.commons.io.FileUtils;
15 import org.collectionspace.services.PlaceJAXBSchema;
16 import org.collectionspace.services.client.test.ServiceRequestType;
17 import org.collectionspace.services.common.api.Tools;
18 import org.collectionspace.services.place.PlaceTermGroup;
19 import org.collectionspace.services.place.PlaceTermGroupList;
20 import org.collectionspace.services.place.PlaceauthoritiesCommon;
21 import org.collectionspace.services.place.PlacesCommon;
23 import org.dom4j.DocumentException;
24 import org.jboss.resteasy.client.ClientResponse;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 public class PlaceAuthorityClientUtils {
29 private static final Logger logger =
30 LoggerFactory.getLogger(PlaceAuthorityClientUtils.class);
33 * Creates a new Place Authority
34 * @param displayName The displayName used in UI, etc.
35 * @param refName The proper refName for this authority
36 * @param headerLabel The common part label
37 * @return The PoxPayloadOut payload for the create call
39 public static PoxPayloadOut createPlaceAuthorityInstance(
40 String displayName, String shortIdentifier, String headerLabel ) {
41 PlaceauthoritiesCommon placeAuthority = new PlaceauthoritiesCommon();
42 placeAuthority.setDisplayName(displayName);
43 placeAuthority.setShortIdentifier(shortIdentifier);
44 String refName = createPlaceAuthRefName(shortIdentifier, displayName);
45 placeAuthority.setRefName(refName);
46 placeAuthority.setVocabType("PlaceAuthority"); //FIXME: REM - Should this really be hard-coded?
47 PoxPayloadOut multipart = new PoxPayloadOut(PlaceAuthorityClient.SERVICE_PAYLOAD_NAME);
48 PayloadOutputPart commonPart = multipart.addPart(placeAuthority, MediaType.APPLICATION_XML_TYPE);
49 commonPart.setLabel(headerLabel);
51 if(logger.isDebugEnabled()){
52 logger.debug("to be created, placeAuthority common ",
53 placeAuthority, PlaceauthoritiesCommon.class);
60 * @param placeRefName The proper refName for this authority
61 * @param placeInfo the properties for the new Place. Can pass in one condition
62 * note and date string.
63 * @param headerLabel The common part label
64 * @return The PoxPayloadOut payload for the create call
66 public static PoxPayloadOut createPlaceInstance(
67 String placeAuthRefName, Map<String, String> placeInfo,
68 List<PlaceTermGroup> terms, String headerLabel){
69 PlacesCommon place = new PlacesCommon();
70 String shortId = placeInfo.get(PlaceJAXBSchema.SHORT_IDENTIFIER);
71 place.setShortIdentifier(shortId);
73 // Set values in the Term Information Group
74 PlaceTermGroupList termList = new PlaceTermGroupList();
75 if (terms == null || terms.isEmpty()) {
76 terms = getTermGroupInstance(getGeneratedIdentifier());
78 termList.getPlaceTermGroup().addAll(terms);
79 place.setPlaceTermGroupList(termList);
81 PoxPayloadOut multipart = new PoxPayloadOut(PlaceAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
82 PayloadOutputPart commonPart = multipart.addPart(place,
83 MediaType.APPLICATION_XML_TYPE);
84 commonPart.setLabel(headerLabel);
86 if(logger.isDebugEnabled()){
87 logger.debug("to be created, place common ", place, PlacesCommon.class);
94 * @param vcsid CSID of the authority to create a new place
95 * @param placeAuthorityRefName The refName for the authority
96 * @param placeMap the properties for the new Place
97 * @param client the service client
98 * @return the CSID of the new item
100 public static String createItemInAuthority(String vcsid,
101 String placeAuthorityRefName, Map<String,String> placeMap,
102 List<PlaceTermGroup> terms, PlaceAuthorityClient client ) {
103 // Expected status code: 201 Created
104 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
105 // Type of service request being tested
106 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
108 String displayName = "";
109 if ((terms !=null) && (! terms.isEmpty())) {
110 displayName = terms.get(0).getTermDisplayName();
112 if(logger.isDebugEnabled()){
113 logger.debug("Creating item with display name: \"" + displayName
114 +"\" in locationAuthority: \"" + vcsid +"\"");
116 PoxPayloadOut multipart =
117 createPlaceInstance( placeAuthorityRefName,
118 placeMap, terms, client.getItemCommonPartName() );
120 Response res = client.createItem(vcsid, multipart);
122 int statusCode = res.getStatus();
124 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
125 throw new RuntimeException("Could not create Item: \""
126 +placeMap.get(PlaceJAXBSchema.SHORT_IDENTIFIER)
127 +"\" in placeAuthority: \"" + placeAuthorityRefName
128 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
130 if(statusCode != EXPECTED_STATUS_CODE) {
131 throw new RuntimeException("Unexpected Status when creating Item: \""
132 +placeMap.get(PlaceJAXBSchema.SHORT_IDENTIFIER)
133 +"\" in placeAuthority: \"" + placeAuthorityRefName +"\", Status:"+ statusCode);
135 newID = extractId(res);
143 public static PoxPayloadOut createPlaceInstance(
144 String commonPartXML, String headerLabel) throws DocumentException {
145 PoxPayloadOut multipart = new PoxPayloadOut(PlaceAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
146 PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
147 MediaType.APPLICATION_XML_TYPE);
148 commonPart.setLabel(headerLabel);
150 if(logger.isDebugEnabled()){
151 logger.debug("to be created, place common ", commonPartXML);
157 public static String createItemInAuthority(String vcsid,
158 String commonPartXML,
159 PlaceAuthorityClient client ) throws DocumentException {
160 // Expected status code: 201 Created
161 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
162 // Type of service request being tested
163 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
165 PoxPayloadOut multipart =
166 createPlaceInstance(commonPartXML, client.getItemCommonPartName());
168 Response res = client.createItem(vcsid, multipart);
170 int statusCode = res.getStatus();
172 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
173 throw new RuntimeException("Could not create Item: \""+commonPartXML
174 +"\" in placeAuthority: \"" + vcsid
175 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177 if(statusCode != EXPECTED_STATUS_CODE) {
178 throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
179 +"\" in placeAuthority: \"" + vcsid +"\", Status:"+ statusCode);
181 newID = extractId(res);
190 * Creates the from xml file.
192 * @param fileName the file name
193 * @return new CSID as string
194 * @throws Exception the exception
196 private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName,
197 PlaceAuthorityClient client) throws Exception {
198 byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
199 String commonPartXML = new String(b);
200 return createItemInAuthority(vcsid, commonPartXML, client );
204 * Creates the placeAuthority ref name.
206 * @param shortId the placeAuthority shortIdentifier
207 * @param displaySuffix displayName to be appended, if non-null
210 public static String createPlaceAuthRefName(String shortId, String displaySuffix) {
211 String refName = "urn:cspace:org.collectionspace.demo:placeauthority:name("
213 if(displaySuffix!=null&&!displaySuffix.isEmpty())
214 refName += "'"+displaySuffix+"'";
219 * Creates the place ref name.
221 * @param placeAuthRefName the placeAuthority ref name
222 * @param shortId the place shortIdentifier
223 * @param displaySuffix displayName to be appended, if non-null
226 public static String createPlaceRefName(
227 String placeAuthRefName, String shortId, String displaySuffix) {
228 String refName = placeAuthRefName+":place:name("+shortId+")";
229 if(displaySuffix!=null&&!displaySuffix.isEmpty())
230 refName += "'"+displaySuffix+"'";
234 public static String extractId(Response res) {
235 MultivaluedMap<String, Object> mvm = res.getMetadata();
236 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
237 if(logger.isDebugEnabled()){
238 logger.debug("extractId:uri=" + uri);
240 String[] segments = uri.split("/");
241 String id = segments[segments.length - 1];
242 if(logger.isDebugEnabled()){
243 logger.debug("id=" + id);
249 * Returns an error message indicating that the status code returned by a
250 * specific call to a service does not fall within a set of valid status
251 * codes for that service.
253 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
255 * @param statusCode The invalid status code that was returned in the response,
256 * from submitting that type of request to the service.
258 * @return An error message.
260 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
261 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
262 requestType.validStatusCodesAsString();
268 * Produces a default displayName from one or more supplied field(s).
269 * @see PlaceAuthorityDocumentModelHandler.prepareDefaultDisplayName() which
270 * duplicates this logic, until we define a service-general utils package
271 * that is neither client nor service specific.
273 * @return a display name
275 public static String prepareDefaultDisplayName(
277 StringBuilder newStr = new StringBuilder();
278 newStr.append(placeName);
279 return newStr.toString();
282 private static List<PlaceTermGroup> getTermGroupInstance(String shortIdentifier, String displayName) {
283 if (Tools.isBlank(shortIdentifier)) {
284 shortIdentifier = getGeneratedIdentifier();
286 if (Tools.isBlank(displayName)) {
287 displayName = shortIdentifier;
290 List<PlaceTermGroup> terms = new ArrayList<PlaceTermGroup>();
291 PlaceTermGroup term = new PlaceTermGroup();
292 term.setTermDisplayName(displayName);
293 term.setTermName(shortIdentifier);
298 public static List<PlaceTermGroup> getTermGroupInstance(String identifier) {
299 return getTermGroupInstance(identifier, null);
302 private static String getGeneratedIdentifier() {
303 return "id" + new Date().getTime();
306 public static PoxPayloadOut createPlaceInstance(String shortIdentifier, String displayName,
307 String serviceItemCommonPartName) {
308 List<PlaceTermGroup> terms = getTermGroupInstance(shortIdentifier, displayName);
310 Map<String, String> placeInfo = new HashMap<String, String>();
311 placeInfo.put(PlaceJAXBSchema.SHORT_IDENTIFIER, shortIdentifier);
313 return createPlaceInstance(null, placeInfo, terms, serviceItemCommonPartName);