1 package org.collectionspace.services.client;
\r
3 import java.util.ArrayList;
\r
4 import java.util.Map;
\r
6 import javax.ws.rs.core.MediaType;
\r
7 import javax.ws.rs.core.MultivaluedMap;
\r
8 import javax.ws.rs.core.Response;
\r
10 import org.collectionspace.services.VocabularyItemJAXBSchema;
\r
11 import org.collectionspace.services.client.test.ServiceRequestType;
\r
12 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
\r
13 import org.collectionspace.services.vocabulary.VocabulariesCommon;
\r
14 import org.jboss.resteasy.client.ClientResponse;
\r
15 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
\r
16 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
\r
17 import org.slf4j.Logger;
\r
18 import org.slf4j.LoggerFactory;
\r
20 public class VocabularyClientUtils {
\r
21 private static final Logger logger =
\r
22 LoggerFactory.getLogger(VocabularyClientUtils.class);
\r
24 public static MultipartOutput createEnumerationInstance(
\r
25 String displayName, String refName, String headerLabel ) {
\r
26 VocabulariesCommon vocabulary = new VocabulariesCommon();
\r
27 vocabulary.setDisplayName(displayName);
\r
28 vocabulary.setRefName(refName);
\r
29 vocabulary.setVocabType("enum");
\r
30 MultipartOutput multipart = new MultipartOutput();
\r
31 OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
\r
32 commonPart.getHeaders().add("label", headerLabel);
\r
34 if(logger.isDebugEnabled()){
\r
35 logger.debug("to be created, vocabulary common for enumeration ",
\r
36 vocabulary, VocabulariesCommon.class);
\r
42 // Note that we do not use the map, but we will once we add more info to the
\r
44 public static MultipartOutput createVocabularyItemInstance(String inVocabulary,
\r
45 String vocabItemRefName, Map<String, String> vocabItemInfo, String headerLabel){
\r
46 VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
\r
47 vocabularyItem.setInVocabulary(inVocabulary);
\r
48 vocabularyItem.setRefName(vocabItemRefName);
\r
49 String value = null;
\r
50 if((value = (String)vocabItemInfo.get(VocabularyItemJAXBSchema.DISPLAY_NAME))!=null)
\r
51 vocabularyItem.setDisplayName(value);
\r
52 MultipartOutput multipart = new MultipartOutput();
\r
53 OutputPart commonPart = multipart.addPart(vocabularyItem,
\r
54 MediaType.APPLICATION_XML_TYPE);
\r
55 commonPart.getHeaders().add("label", headerLabel);
\r
57 if(logger.isDebugEnabled()){
\r
58 logger.debug("to be created, vocabularyItem common ", vocabularyItem, VocabularyitemsCommon.class);
\r
65 * Returns an error message indicating that the status code returned by a
\r
66 * specific call to a service does not fall within a set of valid status
\r
67 * codes for that service.
\r
69 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\r
71 * @param statusCode The invalid status code that was returned in the response,
\r
72 * from submitting that type of request to the service.
\r
74 * @return An error message.
\r
76 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
\r
77 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
\r
78 requestType.validStatusCodesAsString();
\r
81 public static String extractId(ClientResponse<Response> res) {
\r
82 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
83 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
\r
84 if(logger.isDebugEnabled()){
\r
85 logger.info("extractId:uri=" + uri);
\r
87 String[] segments = uri.split("/");
\r
88 String id = segments[segments.length - 1];
\r
89 if(logger.isDebugEnabled()){
\r
90 logger.debug("id=" + id);
\r
95 public static String createVocabularyRefName(String vocabularyName) {
\r
96 return "urn:cspace:org.collectionspace.demo:vocabulary:name("
\r
97 +vocabularyName+")";
\r
100 public static String createVocabularyItemRefName(
\r
101 String vocabularyRefName, String vocabItemName) {
\r
102 return vocabularyRefName+":item:name("+vocabItemName+")";
\r