]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
56709df5fc531e5adc17858f0a9ceb605ec4437c
[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         String result = null;
71         
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;
76
77         if(logger.isDebugEnabled()){
78                 logger.debug("Import: Create Item: \""+itemMap.get(AuthorityItemJAXBSchema.SHORT_IDENTIFIER)
79                                 +"\" in vocabularyAuthority: \"" + vcsid +"\"");
80         }
81         PoxPayloadOut multipart = createVocabularyItemInstance(null, //vocabularyRefName,
82                                 itemMap, client.getItemCommonPartName());
83         Response res = client.createItem(vcsid, multipart);
84
85         try {
86                 int statusCode = res.getStatus();
87         
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));
92                 }
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);
96                 }
97                 result = extractId(res);
98         } finally {
99                 res.close();
100         }
101
102         return result;
103     }
104
105     /**
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.
109      *
110      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
111      *
112      * @param statusCode  The invalid status code that was returned in the response,
113      *                    from submitting that type of request to the service.
114      *
115      * @return An error message.
116      */
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();
120     }
121
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);
127         }
128         String[] segments = uri.split("/");
129         String id = segments[segments.length - 1];
130         if(logger.isDebugEnabled()){
131                 logger.debug("id=" + id);
132         }
133         return id;
134     }
135     
136     /*
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 + "'";
142         return refName;
143     }
144
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 + "'";
150         return refName;
151     }
152     */
153
154 }