]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5c4930645647d3aabf94d08db356d3dfc7592462
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.Map;\r
5 \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
9 \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.PoxPayloadOut;\r
16 //import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
17 import org.slf4j.Logger;\r
18 import org.slf4j.LoggerFactory;\r
19 \r
20 public class VocabularyClientUtils {\r
21     private static final Logger logger =\r
22         LoggerFactory.getLogger(VocabularyClientUtils.class);\r
23     \r
24     public static PoxPayloadOut 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         PoxPayloadOut multipart = new PoxPayloadOut(VocabularyClient.SERVICE_PAYLOAD_NAME);\r
33         PayloadOutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);\r
34         commonPart.setLabel(headerLabel);\r
35 \r
36         if(logger.isDebugEnabled()){\r
37                 logger.debug("to be created, vocabulary common for enumeration ", \r
38                                         vocabulary, VocabulariesCommon.class);\r
39         }\r
40 \r
41         return multipart;\r
42     }\r
43 \r
44                 // Note that we do not use the map, but we will once we add more info to the \r
45                 // items\r
46     public static PoxPayloadOut 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         PoxPayloadOut multipart = new PoxPayloadOut(VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME);\r
56         PayloadOutputPart commonPart = multipart.addPart(vocabularyItem,\r
57             MediaType.APPLICATION_XML_TYPE);\r
58         commonPart.setLabel(headerLabel);\r
59 \r
60         if(logger.isDebugEnabled()){\r
61                 logger.debug("to be created, vocabularyItem common ", vocabularyItem, VocabularyitemsCommon.class);\r
62         }\r
63 \r
64         return multipart;\r
65     }\r
66 \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
74 \r
75         if(logger.isDebugEnabled()){\r
76                 logger.debug("Import: Create Item: \""+itemMap.get(AuthorityItemJAXBSchema.SHORT_IDENTIFIER)\r
77                                 +"\" in vocabularyAuthority: \"" + vcsid +"\"");\r
78         }\r
79         PoxPayloadOut multipart = createVocabularyItemInstance(null, //vocabularyRefName,\r
80                                 itemMap, client.getItemCommonPartName());\r
81         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
82 \r
83         int statusCode = res.getStatus();\r
84 \r
85         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
86                 throw new RuntimeException("Could not create Item: \"" + itemMap.get(AuthorityItemJAXBSchema.DISPLAY_NAME)\r
87                                 + "\" in personAuthority: \"" + vcsid //vocabularyRefName\r
88                                 + "\" " + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
89         }\r
90         if(statusCode != EXPECTED_STATUS_CODE) {\r
91                 throw new RuntimeException("Unexpected Status when creating Item: \""+itemMap.get(AuthorityItemJAXBSchema.DISPLAY_NAME)\r
92                                 + "\" in vocabularyAuthority: \"" + vcsid /*vocabularyRefName*/ + "\", Status:" + statusCode);\r
93         }\r
94 \r
95         return extractId(res);\r
96     }\r
97 \r
98     /**\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
102      *\r
103      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
104      *\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
107      *\r
108      * @return An error message.\r
109      */\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
113     }\r
114 \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
120         }\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
125         }\r
126         return id;\r
127     }\r
128     \r
129     /*\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
135         return refName;\r
136     }\r
137 \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
143         return refName;\r
144     }\r
145     */\r
146 \r
147 }\r