]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
caccb3523eb7ff59c095bc4ad8387206f658081a
[tmp/jakarta-migration.git] /
1 /**     \r
2  * OrgAuthorityClientUtils.java\r
3  *\r
4  * {Purpose of This Class}\r
5  *\r
6  * {Other Notes Relating to This Class (Optional)}\r
7  *\r
8  * $LastChangedBy: $\r
9  * $LastChangedRevision: $\r
10  * $LastChangedDate: $\r
11  *\r
12  * This document is a part of the source code and related artifacts\r
13  * for CollectionSpace, an open source collections management system\r
14  * for museums and related institutions:\r
15  *\r
16  * http://www.collectionspace.org\r
17  * http://wiki.collectionspace.org\r
18  *\r
19  * Copyright © 2009 {Contributing Institution}\r
20  *\r
21  * Licensed under the Educational Community License (ECL), Version 2.0.\r
22  * You may not use this file except in compliance with this License.\r
23  *\r
24  * You may obtain a copy of the ECL 2.0 License at\r
25  * https://source.collectionspace.org/collection-space/LICENSE.txt\r
26  */\r
27 package org.collectionspace.services.client;\r
28 \r
29 import java.util.ArrayList;\r
30 import java.util.Map;\r
31 \r
32 import javax.ws.rs.core.MediaType;\r
33 import javax.ws.rs.core.MultivaluedMap;\r
34 import javax.ws.rs.core.Response;\r
35 \r
36 import org.collectionspace.services.OrganizationJAXBSchema;\r
37 import org.collectionspace.services.client.test.ServiceRequestType;\r
38 import org.collectionspace.services.organization.OrganizationsCommon;\r
39 import org.collectionspace.services.organization.OrgauthoritiesCommon;\r
40 import org.jboss.resteasy.client.ClientResponse;\r
41 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
42 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
43 import org.slf4j.Logger;\r
44 import org.slf4j.LoggerFactory;\r
45 \r
46 /**\r
47  * The Class OrgAuthorityClientUtils.\r
48  */\r
49 public class OrgAuthorityClientUtils {\r
50     \r
51     /** The Constant logger. */\r
52     private static final Logger logger =\r
53         LoggerFactory.getLogger(OrgAuthorityClientUtils.class);\r
54 \r
55     /**\r
56      * Creates the org authority instance.\r
57      *\r
58      * @param displayName the display name\r
59      * @param refName the ref name\r
60      * @param headerLabel the header label\r
61      * @return the multipart output\r
62      */\r
63     public static MultipartOutput createOrgAuthorityInstance(\r
64                 String displayName, String refName, String headerLabel ) {\r
65         OrgauthoritiesCommon orgAuthority = new OrgauthoritiesCommon();\r
66         orgAuthority.setDisplayName(displayName);\r
67         orgAuthority.setRefName(refName);\r
68         orgAuthority.setVocabType("OrgAuthority");\r
69         MultipartOutput multipart = new MultipartOutput();\r
70         OutputPart commonPart = multipart.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);\r
71         commonPart.getHeaders().add("label", headerLabel);\r
72 \r
73         if(logger.isDebugEnabled()){\r
74                 logger.debug("to be created, orgAuthority common ", \r
75                                         orgAuthority, OrgauthoritiesCommon.class);\r
76         }\r
77 \r
78         return multipart;\r
79     }\r
80 \r
81     /**\r
82      * Creates the item in authority.\r
83      *\r
84      * @param vcsid the vcsid\r
85      * @param orgAuthorityRefName the org authority ref name\r
86      * @param orgInfo the org info\r
87      * @param client the client\r
88      * @return the string\r
89      */\r
90     public static String createItemInAuthority(String vcsid, \r
91                 String orgAuthorityRefName, Map<String, String> orgInfo,\r
92                 OrgAuthorityClient client) {\r
93         // Expected status code: 201 Created\r
94         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
95         // Type of service request being tested\r
96         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
97         String displayName = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME);\r
98         String displayNameComputedStr = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
99         boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");\r
100         if( displayName == null ) {\r
101                 if(!displayNameComputed) {\r
102                         throw new RuntimeException(\r
103                         "CreateItem: Must supply a displayName if displayNameComputed is set to false.");\r
104                 }\r
105                 displayName = prepareDefaultDisplayName(\r
106                                 orgInfo.get(OrganizationJAXBSchema.SHORT_NAME ),                        \r
107                                 orgInfo.get(OrganizationJAXBSchema.FOUNDING_PLACE ));\r
108         }\r
109         String refName = createOrganizationRefName(orgAuthorityRefName, displayName, true);\r
110 \r
111         if(logger.isDebugEnabled()){\r
112                 logger.debug("Import: Create Item: \""+displayName\r
113                                 +"\" in orgAuthority: \"" + orgAuthorityRefName +"\"");\r
114         }\r
115         MultipartOutput multipart =\r
116                 createOrganizationInstance(vcsid, refName, orgInfo, client.getItemCommonPartName());\r
117         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
118         String result;\r
119         try {   \r
120                 int statusCode = res.getStatus();\r
121         \r
122                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
123                         throw new RuntimeException("Could not create Item: \""+displayName\r
124                                         +"\" in orgAuthority: \"" + orgAuthorityRefName\r
125                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
126                 }\r
127                 if(statusCode != EXPECTED_STATUS_CODE) {\r
128                         throw new RuntimeException("Unexpected Status when creating Item: \""+ displayName\r
129                                         +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode);\r
130                 }\r
131         \r
132                 result = extractId(res);\r
133         } finally {\r
134                 res.releaseConnection();\r
135         }\r
136         \r
137         return result;\r
138     }\r
139 \r
140     /**\r
141      * Creates the organization instance.\r
142      *\r
143      * @param inAuthority the in authority\r
144      * @param orgRefName the org ref name\r
145      * @param orgInfo the org info\r
146      * @param headerLabel the header label\r
147      * @return the multipart output\r
148      */\r
149     public static MultipartOutput createOrganizationInstance(String inAuthority, \r
150                 String orgRefName, Map<String, String> orgInfo, String headerLabel){\r
151         OrganizationsCommon organization = new OrganizationsCommon();\r
152         organization.setInAuthority(inAuthority);\r
153         organization.setRefName(orgRefName);\r
154         String value = null;\r
155         value = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
156         boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true"); \r
157                 organization.setDisplayNameComputed(displayNameComputed);\r
158         if((value = (String)orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME))!=null)\r
159                 organization.setDisplayName(value);\r
160         if((value = (String)orgInfo.get(OrganizationJAXBSchema.SHORT_NAME))!=null)\r
161                 organization.setShortName(value);\r
162         if((value = (String)orgInfo.get(OrganizationJAXBSchema.LONG_NAME))!=null)\r
163                 organization.setLongName(value);\r
164         if((value = (String)orgInfo.get(OrganizationJAXBSchema.NAME_ADDITIONS))!=null)\r
165                 organization.setNameAdditions(value);\r
166         if((value = (String)orgInfo.get(OrganizationJAXBSchema.CONTACT_NAME))!=null)\r
167                 organization.setContactName(value);\r
168         if((value = (String)orgInfo.get(OrganizationJAXBSchema.FOUNDING_DATE))!=null)\r
169                 organization.setFoundingDate(value);\r
170         if((value = (String)orgInfo.get(OrganizationJAXBSchema.DISSOLUTION_DATE))!=null)\r
171                 organization.setDissolutionDate(value);\r
172         if((value = (String)orgInfo.get(OrganizationJAXBSchema.FOUNDING_PLACE))!=null)\r
173                 organization.setFoundingPlace(value);\r
174         if((value = (String)orgInfo.get(OrganizationJAXBSchema.GROUP))!=null)\r
175                 organization.setGroup(value);\r
176         if((value = (String)orgInfo.get(OrganizationJAXBSchema.FUNCTION))!=null)\r
177                 organization.setFunction(value);\r
178         if((value = (String)orgInfo.get(OrganizationJAXBSchema.SUB_BODY))!=null)\r
179                 organization.setSubBody(value);\r
180         if((value = (String)orgInfo.get(OrganizationJAXBSchema.HISTORY))!=null)\r
181                 organization.setHistory(value);\r
182         if((value = (String)orgInfo.get(OrganizationJAXBSchema.STATUS))!=null)\r
183                 organization.setStatus(value);\r
184         MultipartOutput multipart = new MultipartOutput();\r
185         OutputPart commonPart = multipart.addPart(organization,\r
186             MediaType.APPLICATION_XML_TYPE);\r
187         commonPart.getHeaders().add("label", headerLabel);\r
188 \r
189         if(logger.isDebugEnabled()){\r
190                 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);\r
191         }\r
192 \r
193         return multipart;\r
194     }\r
195 \r
196     /**\r
197      * Returns an error message indicating that the status code returned by a\r
198      * specific call to a service does not fall within a set of valid status\r
199      * codes for that service.\r
200      *\r
201      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
202      *\r
203      * @param statusCode  The invalid status code that was returned in the response,\r
204      *                    from submitting that type of request to the service.\r
205      *\r
206      * @return An error message.\r
207      */\r
208     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
209         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
210                 requestType.validStatusCodesAsString();\r
211     }\r
212 \r
213     /**\r
214      * Extract id.\r
215      *\r
216      * @param res the res\r
217      * @return the string\r
218      */\r
219     public static String extractId(ClientResponse<Response> res) {\r
220         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
221         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
222         if(logger.isDebugEnabled()){\r
223                 logger.info("extractId:uri=" + uri);\r
224         }\r
225         String[] segments = uri.split("/");\r
226         String id = segments[segments.length - 1];\r
227         if(logger.isDebugEnabled()){\r
228                 logger.debug("id=" + id);\r
229         }\r
230         return id;\r
231     }\r
232     \r
233     /**\r
234      * Creates the org auth ref name.\r
235      *\r
236      * @param orgAuthorityName the org authority name\r
237      * @param withDisplaySuffix the with display suffix\r
238      * @return the string\r
239      */\r
240     public static String createOrgAuthRefName(String orgAuthorityName, boolean withDisplaySuffix) {\r
241         String refName = "urn:cspace:org.collectionspace.demo:orgauthority:name("\r
242                         +orgAuthorityName+")";\r
243         if(withDisplaySuffix)\r
244                 refName += "'"+orgAuthorityName+"'";\r
245         return refName;\r
246     }\r
247 \r
248     /**\r
249      * Creates the organization ref name.\r
250      *\r
251      * @param orgAuthRefName the org auth ref name\r
252      * @param orgName the org name\r
253      * @param withDisplaySuffix the with display suffix\r
254      * @return the string\r
255      */\r
256     public static String createOrganizationRefName(\r
257                                                 String orgAuthRefName, String orgName, boolean withDisplaySuffix) {\r
258         String refName = orgAuthRefName+":organization:name("+orgName+")";\r
259         if(withDisplaySuffix)\r
260                 refName += "'"+orgName+"'";\r
261         return refName;\r
262     }\r
263 \r
264     /**\r
265      * Produces a default displayName from the basic name and foundingPlace fields.\r
266      * @see OrgAuthorityDocumentModelHandler.prepareDefaultDisplayName() which\r
267      * duplicates this logic, until we define a service-general utils package\r
268      * that is neither client nor service specific.\r
269      * @param shortName\r
270      * @param foundingPlace\r
271      * @return\r
272      * @throws Exception\r
273      */\r
274     public static String prepareDefaultDisplayName(\r
275                 String shortName, String foundingPlace ) {\r
276         StringBuilder newStr = new StringBuilder();\r
277                 final String sep = " ";\r
278                 boolean firstAdded = false;\r
279                 if(null != shortName ) {\r
280                         newStr.append(shortName);\r
281                         firstAdded = true;\r
282                 }\r
283         // Now we add the place\r
284                 if(null != foundingPlace ) {\r
285                         if(firstAdded) {\r
286                                 newStr.append(sep);\r
287                         }\r
288                         newStr.append(foundingPlace);\r
289                 }\r
290                 return newStr.toString();\r
291     }\r
292     \r
293 }\r