]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5b39a29a4d33998a02f79ace0395409cf8df755d
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client;
2
3 import java.util.ArrayList;
4 import java.util.Map;
5
6 import javax.ws.rs.core.MediaType;
7 import javax.ws.rs.core.MultivaluedMap;
8 import javax.ws.rs.core.Response;
9
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;
19
20 public class VocabularyClientUtils {
21     private static final Logger logger =
22         LoggerFactory.getLogger(VocabularyClientUtils.class);
23     
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);
35
36         if(logger.isDebugEnabled()){
37                 logger.debug("to be created, vocabulary common for enumeration ", 
38                                         vocabulary, VocabulariesCommon.class);
39         }
40
41         return multipart;
42     }
43
44                 // Note that we do not use the map, but we will once we add more info to the 
45                 // items
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);
59
60         if(logger.isDebugEnabled()){
61                 logger.debug("to be created, vocabularyItem common ", vocabularyItem, VocabularyitemsCommon.class);
62         }
63
64         return multipart;
65     }
66
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;
74
75         if(logger.isDebugEnabled()){
76                 logger.debug("Import: Create Item: \""+itemMap.get(AuthorityItemJAXBSchema.SHORT_IDENTIFIER)
77                                 +"\" in vocabularyAuthority: \"" + vcsid +"\"");
78         }
79         PoxPayloadOut multipart = createVocabularyItemInstance(null, //vocabularyRefName,
80                                 itemMap, client.getItemCommonPartName());
81         ClientResponse<Response> res = client.createItem(vcsid, multipart);
82
83         int statusCode = res.getStatus();
84
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));
89         }
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);
93         }
94
95         return extractId(res);
96     }
97
98     /**
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.
102      *
103      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
104      *
105      * @param statusCode  The invalid status code that was returned in the response,
106      *                    from submitting that type of request to the service.
107      *
108      * @return An error message.
109      */
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();
113     }
114
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);
120         }
121         String[] segments = uri.split("/");
122         String id = segments[segments.length - 1];
123         if(logger.isDebugEnabled()){
124                 logger.debug("id=" + id);
125         }
126         return id;
127     }
128     
129     /*
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 + "'";
135         return refName;
136     }
137
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 + "'";
143         return refName;
144     }
145     */
146
147 }