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
64 public static String createItemInVocabulary(String vcsid,
\r
65 String vocabularyRefName, Map<String,String> itemMap,
\r
66 VocabularyClient client ) {
\r
67 // Expected status code: 201 Created
\r
68 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
69 // Type of service request being tested
\r
70 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
71 String displayName = itemMap.get(VocabularyItemJAXBSchema.DISPLAY_NAME);
\r
72 String refName = createVocabularyItemRefName(vocabularyRefName, displayName, true);
\r
74 if(logger.isDebugEnabled()){
\r
75 logger.debug("Import: Create Item: \""+displayName
\r
76 +"\" in personAuthorityulary: \"" + vocabularyRefName +"\"");
\r
78 MultipartOutput multipart =
\r
79 createVocabularyItemInstance( vcsid, refName,
\r
80 itemMap, client.getItemCommonPartName() );
\r
81 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
83 int statusCode = res.getStatus();
\r
85 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
86 throw new RuntimeException("Could not create Item: \""+refName
\r
87 +"\" in personAuthority: \"" + vocabularyRefName
\r
88 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
90 if(statusCode != EXPECTED_STATUS_CODE) {
\r
91 throw new RuntimeException("Unexpected Status when creating Item: \""+refName
\r
92 +"\" in personAuthority: \"" + vocabularyRefName +"\", Status:"+ statusCode);
\r
95 return extractId(res);
\r
99 * Returns an error message indicating that the status code returned by a
\r
100 * specific call to a service does not fall within a set of valid status
\r
101 * codes for that service.
\r
103 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\r
105 * @param statusCode The invalid status code that was returned in the response,
\r
106 * from submitting that type of request to the service.
\r
108 * @return An error message.
\r
110 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
\r
111 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
\r
112 requestType.validStatusCodesAsString();
\r
115 public static String extractId(ClientResponse<Response> res) {
\r
116 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
117 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
\r
118 if(logger.isDebugEnabled()){
\r
119 logger.info("extractId:uri=" + uri);
\r
121 String[] segments = uri.split("/");
\r
122 String id = segments[segments.length - 1];
\r
123 if(logger.isDebugEnabled()){
\r
124 logger.debug("id=" + id);
\r
129 public static String createVocabularyRefName(String vocabularyName, boolean withDisplaySuffix) {
\r
130 String refName = "urn:cspace:org.collectionspace.demo:vocabulary:name("
\r
131 +vocabularyName+")";
\r
132 if(withDisplaySuffix)
\r
133 refName += "'"+vocabularyName+"'";
\r
137 public static String createVocabularyItemRefName(
\r
138 String vocabularyRefName, String vocabItemName, boolean withDisplaySuffix) {
\r
139 String refName = vocabularyRefName+":item:name("+vocabItemName+")";
\r
140 if(withDisplaySuffix)
\r
141 refName += "'"+vocabItemName+"'";
\r