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 ) {
72 // Expected status code: 201 Created
73 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
74 // Type of service request being tested
75 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
77 if(logger.isDebugEnabled()){
78 logger.debug("Import: Create Item: \""+itemMap.get(AuthorityItemJAXBSchema.SHORT_IDENTIFIER)
79 +"\" in vocabularyAuthority: \"" + vcsid +"\"");
81 PoxPayloadOut multipart = createVocabularyItemInstance(null, //vocabularyRefName,
82 itemMap, client.getItemCommonPartName());
83 Response res = client.createItem(vcsid, multipart);
86 int statusCode = res.getStatus();
88 if (!REQUEST_TYPE.isValidStatusCode(statusCode)) {
89 throw new RuntimeException("Could not create Item: \"" + itemMap.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
90 + "\" in personAuthority: \"" + vcsid //vocabularyRefName
91 + "\" " + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
93 if (statusCode != EXPECTED_STATUS_CODE) {
94 throw new RuntimeException("Unexpected Status when creating Item: \""+itemMap.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
95 + "\" in vocabularyAuthority: \"" + vcsid /*vocabularyRefName*/ + "\", Status:" + statusCode);
97 result = extractId(res);
106 * Returns an error message indicating that the status code returned by a
107 * specific call to a service does not fall within a set of valid status
108 * codes for that service.
110 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
112 * @param statusCode The invalid status code that was returned in the response,
113 * from submitting that type of request to the service.
115 * @return An error message.
117 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
118 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
119 requestType.validStatusCodesAsString();
122 public static String extractId(Response res) {
123 MultivaluedMap<String, Object> mvm = res.getMetadata();
124 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
125 if(logger.isDebugEnabled()){
126 logger.info("extractId:uri=" + uri);
128 String[] segments = uri.split("/");
129 String id = segments[segments.length - 1];
130 if(logger.isDebugEnabled()){
131 logger.debug("id=" + id);
137 public static String createVocabularyRefName(String shortIdentifier, String displaySuffix) {
138 String refName = "urn:cspace:org.collectionspace.demo:vocabulary:name("
139 + shortIdentifier + ")";
140 if(displaySuffix != null && !displaySuffix.isEmpty())
141 refName += "'" + displaySuffix + "'";
145 public static String createVocabularyItemRefName(
146 String vocabularyRefName, String shortIdentifier, String displaySuffix) {
147 String refName = vocabularyRefName+":item:name("+shortIdentifier+")";
148 if(displaySuffix != null && !displaySuffix.isEmpty())
149 refName += "'" + displaySuffix + "'";