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;
12 import org.apache.commons.io.FileUtils;
14 import org.collectionspace.services.client.test.ServiceRequestType;
15 import org.collectionspace.services.common.api.Tools;
16 import org.collectionspace.services.concept.ConceptTermGroup;
17 import org.collectionspace.services.concept.ConceptauthoritiesCommon;
19 import org.dom4j.DocumentException;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
23 public class ConceptAuthorityClientUtils {
24 private static final Logger logger =
25 LoggerFactory.getLogger(ConceptAuthorityClientUtils.class);
28 * Creates a new Concept Authority
29 * @param displayName The displayName used in UI, etc.
30 * @param refName The proper refName for this authority
31 * @param headerLabel The common part label
32 * @return The PoxPayloadOut payload for the create call
34 public static PoxPayloadOut createConceptAuthorityInstance(
35 String displayName, String shortIdentifier, String headerLabel ) {
36 ConceptauthoritiesCommon conceptAuthority = new ConceptauthoritiesCommon();
37 conceptAuthority.setDisplayName(displayName);
38 conceptAuthority.setShortIdentifier(shortIdentifier);
39 conceptAuthority.setVocabType("ConceptAuthority"); //FIXME: REM - Should this really be hard-coded?
40 PoxPayloadOut multipart = new PoxPayloadOut(ConceptAuthorityClient.SERVICE_PAYLOAD_NAME);
41 PayloadOutputPart commonPart = multipart.addPart(conceptAuthority, MediaType.APPLICATION_XML_TYPE);
42 commonPart.setLabel(headerLabel);
44 if(logger.isDebugEnabled()){
45 logger.debug("to be created, conceptAuthority common ",
46 conceptAuthority, ConceptauthoritiesCommon.class);
53 * @param commonPartXML the XML payload for the common part.
54 * @param headerLabel The common part label
55 * @return The PoxPayloadOut payload for the create call
56 * @throws DocumentException
58 public static PoxPayloadOut createConceptInstance(
59 String commonPartXML, String headerLabel) throws DocumentException {
60 PoxPayloadOut multipart = new PoxPayloadOut(ConceptAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
62 PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
63 MediaType.APPLICATION_XML_TYPE);
64 commonPart.setLabel(headerLabel);
66 PayloadOutputPart commonPart = multipart.addPart(
67 ConceptAuthorityClient.SERVICE_ITEM_COMMON_PART_NAME,
70 if(logger.isDebugEnabled()){
71 logger.debug("to be created, concept common ", commonPart.asXML());
77 public static String createItemInAuthority(String vcsid,
79 ConceptAuthorityClient client ) throws DocumentException {
80 // Expected status code: 201 Created
81 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
82 // Type of service request being tested
83 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
85 PoxPayloadOut multipart =
86 createConceptInstance(commonPartXML, client.getItemCommonPartName());
88 Response res = client.createItem(vcsid, multipart);
90 int statusCode = res.getStatus();
92 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
93 throw new RuntimeException("Could not create Item: \""+commonPartXML
94 +"\" in conceptAuthority: \"" + vcsid
95 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
97 if(statusCode != EXPECTED_STATUS_CODE) {
98 throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
99 +"\" in conceptAuthority: \"" + vcsid +"\", Status:"+ statusCode);
101 newID = extractId(res);
110 * Creates the from xml file.
112 * @param fileName the file name
113 * @return new CSID as string
114 * @throws Exception the exception
116 private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName,
117 ConceptAuthorityClient client) throws Exception {
118 byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
119 String commonPartXML = new String(b);
120 return createItemInAuthority(vcsid, commonPartXML, client );
123 public static String extractId(Response res) {
124 MultivaluedMap<String, Object> mvm = res.getMetadata();
125 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
126 if(logger.isDebugEnabled()){
127 logger.debug("extractId:uri=" + uri);
129 String[] segments = uri.split("/");
130 String id = segments[segments.length - 1];
131 if(logger.isDebugEnabled()){
132 logger.debug("id=" + id);
138 * Returns an error message indicating that the status code returned by a
139 * specific call to a service does not fall within a set of valid status
140 * codes for that service.
142 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
144 * @param statusCode The invalid status code that was returned in the response,
145 * from submitting that type of request to the service.
147 * @return An error message.
149 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
150 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
151 requestType.validStatusCodesAsString();
154 public static List<ConceptTermGroup> getTermGroupInstance(String identifier) {
155 if (Tools.isBlank(identifier)) {
156 identifier = getGeneratedIdentifier();
158 List<ConceptTermGroup> terms = new ArrayList<ConceptTermGroup>();
159 ConceptTermGroup term = new ConceptTermGroup();
160 term.setTermDisplayName(identifier);
161 term.setTermName(identifier);
166 private static String getGeneratedIdentifier() {
167 return "id" + new Date().getTime();