]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b564c9fb50002239414c612648855aa83d68eeac
[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 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
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 MultipartOutput createVocabularyItemInstance(String inVocabulary, \r
47                 String vocabularyRefName, Map<String, String> vocabItemInfo, String headerLabel){\r
48         VocabularyitemsCommon vocabularyItem = new VocabularyitemsCommon();\r
49         vocabularyItem.setInVocabulary(inVocabulary);\r
50         String shortId = vocabItemInfo.get(VocabularyItemJAXBSchema.SHORT_IDENTIFIER);\r
51         String displayName = vocabItemInfo.get(VocabularyItemJAXBSchema.DISPLAY_NAME);\r
52         vocabularyItem.setShortIdentifier(shortId);\r
53         vocabularyItem.setDisplayName(displayName);\r
54         String refName = createVocabularyItemRefName(vocabularyRefName, shortId, displayName);\r
55         vocabularyItem.setRefName(refName);\r
56         MultipartOutput multipart = new MultipartOutput();\r
57         OutputPart commonPart = multipart.addPart(vocabularyItem,\r
58             MediaType.APPLICATION_XML_TYPE);\r
59         commonPart.getHeaders().add("label", headerLabel);\r
60 \r
61         if(logger.isDebugEnabled()){\r
62                 logger.debug("to be created, vocabularyItem common ", vocabularyItem, VocabularyitemsCommon.class);\r
63         }\r
64 \r
65         return multipart;\r
66     }\r
67 \r
68     public static String createItemInVocabulary(String vcsid, \r
69                 String vocabularyRefName, Map<String,String> itemMap,\r
70                 VocabularyClient client ) {\r
71         // Expected status code: 201 Created\r
72         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
73         // Type of service request being tested\r
74         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
75 \r
76         if(logger.isDebugEnabled()){\r
77                 logger.debug("Import: Create Item: \""+itemMap.get(VocabularyItemJAXBSchema.DISPLAY_NAME)\r
78                                 +"\" in personAuthorityulary: \"" + vocabularyRefName +"\"");\r
79         }\r
80         MultipartOutput multipart = \r
81                 createVocabularyItemInstance( vcsid, vocabularyRefName,\r
82                                 itemMap, client.getItemCommonPartName() );\r
83         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
84 \r
85         int statusCode = res.getStatus();\r
86 \r
87         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
88                 throw new RuntimeException("Could not create Item: \""+itemMap.get(VocabularyItemJAXBSchema.DISPLAY_NAME)\r
89                                 +"\" in personAuthority: \"" + vocabularyRefName\r
90                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
91         }\r
92         if(statusCode != EXPECTED_STATUS_CODE) {\r
93                 throw new RuntimeException("Unexpected Status when creating Item: \""+itemMap.get(VocabularyItemJAXBSchema.DISPLAY_NAME)\r
94                                 +"\" in personAuthority: \"" + vocabularyRefName +"\", Status:"+ statusCode);\r
95         }\r
96 \r
97         return extractId(res);\r
98     }\r
99 \r
100     /**\r
101      * Returns an error message indicating that the status code returned by a\r
102      * specific call to a service does not fall within a set of valid status\r
103      * codes for that service.\r
104      *\r
105      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
106      *\r
107      * @param statusCode  The invalid status code that was returned in the response,\r
108      *                    from submitting that type of request to the service.\r
109      *\r
110      * @return An error message.\r
111      */\r
112     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
113         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
114                 requestType.validStatusCodesAsString();\r
115     }\r
116 \r
117     public static String extractId(ClientResponse<Response> res) {\r
118         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
119         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
120         if(logger.isDebugEnabled()){\r
121                 logger.info("extractId:uri=" + uri);\r
122         }\r
123         String[] segments = uri.split("/");\r
124         String id = segments[segments.length - 1];\r
125         if(logger.isDebugEnabled()){\r
126                 logger.debug("id=" + id);\r
127         }\r
128         return id;\r
129     }\r
130     \r
131     public static String createVocabularyRefName(String shortIdentifier, String displaySuffix) {\r
132         String refName = "urn:cspace:org.collectionspace.demo:vocabulary:name("\r
133                         +shortIdentifier+")";\r
134         if(displaySuffix!=null&&!displaySuffix.isEmpty())\r
135                 refName += "'"+displaySuffix+"'";\r
136         return refName;\r
137     }\r
138 \r
139     public static String createVocabularyItemRefName(\r
140                                                 String vocabularyRefName, String shortIdentifier, String displaySuffix) {\r
141         String refName = vocabularyRefName+":item:name("+shortIdentifier+")";\r
142         if(displaySuffix!=null&&!displaySuffix.isEmpty())\r
143                 refName += "'"+displaySuffix+"'";\r
144         return refName;\r
145     }\r
146 \r
147 }\r