]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0689bd9caddbb4fc8ae98f52623ce6ec981765f1
[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.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
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 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
33 \r
34         if(logger.isDebugEnabled()){\r
35                 logger.debug("to be created, vocabulary common for enumeration ", \r
36                                         vocabulary, VocabulariesCommon.class);\r
37         }\r
38 \r
39         return multipart;\r
40     }\r
41 \r
42                 // Note that we do not use the map, but we will once we add more info to the \r
43                 // items\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
56 \r
57         if(logger.isDebugEnabled()){\r
58                 logger.debug("to be created, vocabularyItem common ", vocabularyItem, VocabularyitemsCommon.class);\r
59         }\r
60 \r
61         return multipart;\r
62     }\r
63 \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
73 \r
74         if(logger.isDebugEnabled()){\r
75                 logger.debug("Import: Create Item: \""+displayName\r
76                                 +"\" in personAuthorityulary: \"" + vocabularyRefName +"\"");\r
77         }\r
78         MultipartOutput multipart = \r
79                 createVocabularyItemInstance( vcsid, refName,\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: \""+refName\r
87                                 +"\" in personAuthority: \"" + 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: \""+refName\r
92                                 +"\" in personAuthority: \"" + 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     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
134         return refName;\r
135     }\r
136 \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
142         return refName;\r
143     }\r
144 \r
145 }\r