1 package org.collectionspace.services.client;
3 import java.util.ArrayList;
6 import javax.ws.rs.core.MediaType;
7 import javax.ws.rs.core.MultivaluedMap;
8 import javax.ws.rs.core.Response;
10 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
11 import org.collectionspace.services.client.test.ServiceRequestType;
12 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
13 import org.collectionspace.services.vocabulary.VocabulariesCommon;
14 import org.jboss.resteasy.client.ClientResponse;
15 //import org.jboss.resteasy.plugins.providers.multipart.PoxPayloadOut;
16 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 public class VocabularyClientUtils {
21 private static final Logger logger =
22 LoggerFactory.getLogger(VocabularyClientUtils.class);
24 public static PoxPayloadOut createEnumerationInstance(
25 String displayName, String shortIdentifier, String headerLabel ) {
26 VocabulariesCommon vocabulary = new VocabulariesCommon();
27 vocabulary.setDisplayName(displayName);
28 vocabulary.setShortIdentifier(shortIdentifier);
29 //String refName = createVocabularyRefName(shortIdentifier, displayName);
30 //vocabulary.setRefName(refName);
31 vocabulary.setVocabType("enum");
32 PoxPayloadOut multipart = new PoxPayloadOut(VocabularyClient.SERVICE_PAYLOAD_NAME);
33 PayloadOutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
34 commonPart.setLabel(headerLabel);
36 if(logger.isDebugEnabled()){
37 logger.debug("to be created, vocabulary common for enumeration ",
38 vocabulary, VocabulariesCommon.class);
44 // Note that we do not use the map, but we will once we add more info to the
46 public static PoxPayloadOut createVocabularyItemInstance(
47 String vocabularyRefName, Map<String, String> vocabItemInfo, String headerLabel){
48 VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
49 String shortId = vocabItemInfo.get(AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
50 String displayName = vocabItemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME);
51 vocabularyItem.setShortIdentifier(shortId);
52 vocabularyItem.setDisplayName(displayName);
53 //String refName = createVocabularyItemRefName(vocabularyRefName, shortId, displayName);
54 //vocabularyItem.setRefName(refName);
55 PoxPayloadOut multipart = new PoxPayloadOut(VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME);
56 PayloadOutputPart commonPart = multipart.addPart(vocabularyItem,
57 MediaType.APPLICATION_XML_TYPE);
58 commonPart.setLabel(headerLabel);
60 if(logger.isDebugEnabled()){
61 logger.debug("to be created, vocabularyItem common ", vocabularyItem, VocabularyitemsCommon.class);
67 public static String createItemInVocabulary(String vcsid,
68 String vocabularyRefName, Map<String,String> itemMap,
69 VocabularyClient client ) {
70 // Expected status code: 201 Created
71 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
72 // Type of service request being tested
73 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
75 if(logger.isDebugEnabled()){
76 logger.debug("Import: Create Item: \""+itemMap.get(AuthorityItemJAXBSchema.SHORT_IDENTIFIER)
77 +"\" in vocabularyAuthority: \"" + vcsid +"\"");
79 PoxPayloadOut multipart = createVocabularyItemInstance(null, //vocabularyRefName,
80 itemMap, client.getItemCommonPartName());
81 ClientResponse<Response> res = client.createItem(vcsid, multipart);
83 int statusCode = res.getStatus();
85 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
86 throw new RuntimeException("Could not create Item: \"" + itemMap.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
87 + "\" in personAuthority: \"" + vcsid //vocabularyRefName
88 + "\" " + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
90 if(statusCode != EXPECTED_STATUS_CODE) {
91 throw new RuntimeException("Unexpected Status when creating Item: \""+itemMap.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
92 + "\" in vocabularyAuthority: \"" + vcsid /*vocabularyRefName*/ + "\", Status:" + statusCode);
95 return extractId(res);
99 * Returns an error message indicating that the status code returned by a
100 * specific call to a service does not fall within a set of valid status
101 * codes for that service.
103 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
105 * @param statusCode The invalid status code that was returned in the response,
106 * from submitting that type of request to the service.
108 * @return An error message.
110 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
111 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
112 requestType.validStatusCodesAsString();
115 public static String extractId(ClientResponse<Response> res) {
116 MultivaluedMap<String, Object> mvm = res.getMetadata();
117 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
118 if(logger.isDebugEnabled()){
119 logger.info("extractId:uri=" + uri);
121 String[] segments = uri.split("/");
122 String id = segments[segments.length - 1];
123 if(logger.isDebugEnabled()){
124 logger.debug("id=" + id);
130 public static String createVocabularyRefName(String shortIdentifier, String displaySuffix) {
131 String refName = "urn:cspace:org.collectionspace.demo:vocabulary:name("
132 + shortIdentifier + ")";
133 if(displaySuffix != null && !displaySuffix.isEmpty())
134 refName += "'" + displaySuffix + "'";
138 public static String createVocabularyItemRefName(
139 String vocabularyRefName, String shortIdentifier, String displaySuffix) {
140 String refName = vocabularyRefName+":item:name("+shortIdentifier+")";
141 if(displaySuffix != null && !displaySuffix.isEmpty())
142 refName += "'" + displaySuffix + "'";