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.common.vocabulary.AuthorityItemJAXBSchema;
\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 shortIdentifier, String headerLabel ) {
\r
26 VocabulariesCommon vocabulary = new VocabulariesCommon();
\r
27 vocabulary.setDisplayName(displayName);
\r
28 vocabulary.setShortIdentifier(shortIdentifier);
\r
29 String refName = createVocabularyRefName(shortIdentifier, displayName);
\r
30 vocabulary.setRefName(refName);
\r
31 vocabulary.setVocabType("enum");
\r
32 MultipartOutput multipart = new MultipartOutput();
\r
33 OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
\r
34 commonPart.getHeaders().add("label", headerLabel);
\r
36 if(logger.isDebugEnabled()){
\r
37 logger.debug("to be created, vocabulary common for enumeration ",
\r
38 vocabulary, VocabulariesCommon.class);
\r
44 // Note that we do not use the map, but we will once we add more info to the
\r
46 public static MultipartOutput createVocabularyItemInstance(
\r
47 String vocabularyRefName, Map<String, String> vocabItemInfo, String headerLabel){
\r
48 VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();
\r
49 String shortId = vocabItemInfo.get(AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
\r
50 String displayName = vocabItemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME);
\r
51 vocabularyItem.setShortIdentifier(shortId);
\r
52 vocabularyItem.setDisplayName(displayName);
\r
53 String refName = createVocabularyItemRefName(vocabularyRefName, shortId, displayName);
\r
54 vocabularyItem.setRefName(refName);
\r
55 MultipartOutput multipart = new MultipartOutput();
\r
56 OutputPart commonPart = multipart.addPart(vocabularyItem,
\r
57 MediaType.APPLICATION_XML_TYPE);
\r
58 commonPart.getHeaders().add("label", headerLabel);
\r
60 if(logger.isDebugEnabled()){
\r
61 logger.debug("to be created, vocabularyItem common ", vocabularyItem, VocabularyitemsCommon.class);
\r
67 public static String createItemInVocabulary(String vcsid,
\r
68 String vocabularyRefName, Map<String,String> itemMap,
\r
69 VocabularyClient client ) {
\r
70 // Expected status code: 201 Created
\r
71 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
72 // Type of service request being tested
\r
73 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
75 if(logger.isDebugEnabled()){
\r
76 logger.debug("Import: Create Item: \""+itemMap.get(AuthorityItemJAXBSchema.SHORT_IDENTIFIER)
\r
77 +"\" in personAuthority: \"" + vcsid +"\"");
\r
79 MultipartOutput multipart =
\r
80 createVocabularyItemInstance( vocabularyRefName,
\r
81 itemMap, client.getItemCommonPartName() );
\r
82 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
84 int statusCode = res.getStatus();
\r
86 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
87 throw new RuntimeException("Could not create Item: \""+itemMap.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
\r
88 +"\" in personAuthority: \"" + vocabularyRefName
\r
89 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
91 if(statusCode != EXPECTED_STATUS_CODE) {
\r
92 throw new RuntimeException("Unexpected Status when creating Item: \""+itemMap.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
\r
93 +"\" in personAuthority: \"" + vocabularyRefName +"\", Status:"+ statusCode);
\r
96 return extractId(res);
\r
100 * Returns an error message indicating that the status code returned by a
\r
101 * specific call to a service does not fall within a set of valid status
\r
102 * codes for that service.
\r
104 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\r
106 * @param statusCode The invalid status code that was returned in the response,
\r
107 * from submitting that type of request to the service.
\r
109 * @return An error message.
\r
111 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
\r
112 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
\r
113 requestType.validStatusCodesAsString();
\r
116 public static String extractId(ClientResponse<Response> res) {
\r
117 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
118 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
\r
119 if(logger.isDebugEnabled()){
\r
120 logger.info("extractId:uri=" + uri);
\r
122 String[] segments = uri.split("/");
\r
123 String id = segments[segments.length - 1];
\r
124 if(logger.isDebugEnabled()){
\r
125 logger.debug("id=" + id);
\r
130 public static String createVocabularyRefName(String shortIdentifier, String displaySuffix) {
\r
131 String refName = "urn:cspace:org.collectionspace.demo:vocabulary:name("
\r
132 +shortIdentifier+")";
\r
133 if(displaySuffix!=null&&!displaySuffix.isEmpty())
\r
134 refName += "'"+displaySuffix+"'";
\r
138 public static String createVocabularyItemRefName(
\r
139 String vocabularyRefName, String shortIdentifier, String displaySuffix) {
\r
140 String refName = vocabularyRefName+":item:name("+shortIdentifier+")";
\r
141 if(displaySuffix!=null&&!displaySuffix.isEmpty())
\r
142 refName += "'"+displaySuffix+"'";
\r