]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
ad235b9a3615fdd0ab6215c3dd1ec9b5f3e52b84
[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.OrganizationJAXBSchema;\r
11 import org.collectionspace.services.client.test.ServiceRequestType;\r
12 import org.collectionspace.services.organization.OrganizationsCommon;\r
13 import org.collectionspace.services.organization.OrgauthoritiesCommon;\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 OrgAuthorityClientUtils {\r
21     private static final Logger logger =\r
22         LoggerFactory.getLogger(OrgAuthorityClientUtils.class);\r
23 \r
24     public static MultipartOutput createOrgAuthorityInstance(\r
25                 String displayName, String refName, String headerLabel ) {\r
26         OrgauthoritiesCommon orgAuthority = new OrgauthoritiesCommon();\r
27         orgAuthority.setDisplayName(displayName);\r
28         orgAuthority.setRefName(refName);\r
29         orgAuthority.setVocabType("OrgAuthority");\r
30         MultipartOutput multipart = new MultipartOutput();\r
31         OutputPart commonPart = multipart.addPart(orgAuthority, 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, orgAuthority common ", \r
36                                         orgAuthority, OrgauthoritiesCommon.class);\r
37         }\r
38 \r
39         return multipart;\r
40     }\r
41 \r
42     public static String createItemInAuthority(String vcsid, \r
43                 String orgAuthorityRefName, Map<String, String> orgInfo,\r
44                 OrgAuthorityClient client) {\r
45         // Expected status code: 201 Created\r
46         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();\r
47         // Type of service request being tested\r
48         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;\r
49         String displayName = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME);\r
50         String displayNameComputedStr = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
51         boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");\r
52         if( displayName == null ) {\r
53                 if(!displayNameComputed) {\r
54                         throw new RuntimeException(\r
55                         "CreateItem: Must supply a displayName if displayNameComputed is set to false.");\r
56                 }\r
57                 displayName = prepareDefaultDisplayName(\r
58                                 orgInfo.get(OrganizationJAXBSchema.SHORT_NAME ),                        \r
59                                 orgInfo.get(OrganizationJAXBSchema.FOUNDING_PLACE ));\r
60         }\r
61         String refName = createOrganizationRefName( orgAuthorityRefName, displayName, true);\r
62 \r
63         if(logger.isDebugEnabled()){\r
64                 logger.debug("Import: Create Item: \""+displayName\r
65                                 +"\" in orgAuthorityulary: \"" + orgAuthorityRefName +"\"");\r
66         }\r
67         MultipartOutput multipart =\r
68                 createOrganizationInstance( vcsid, refName, orgInfo, client.getItemCommonPartName() );\r
69         ClientResponse<Response> res = client.createItem(vcsid, multipart);\r
70 \r
71         int statusCode = res.getStatus();\r
72 \r
73         if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {\r
74                 throw new RuntimeException("Could not create Item: \""+displayName\r
75                                 +"\" in orgAuthority: \"" + orgAuthorityRefName\r
76                                 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));\r
77         }\r
78         if(statusCode != EXPECTED_STATUS_CODE) {\r
79                 throw new RuntimeException("Unexpected Status when creating Item: \""+displayName\r
80                                 +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode);\r
81         }\r
82 \r
83         return extractId(res);\r
84     }\r
85 \r
86     public static MultipartOutput createOrganizationInstance(String inAuthority, \r
87                 String orgRefName, Map<String, String> orgInfo, String headerLabel){\r
88         OrganizationsCommon organization = new OrganizationsCommon();\r
89         organization.setInAuthority(inAuthority);\r
90         organization.setRefName(orgRefName);\r
91         String value = null;\r
92         value = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);\r
93         boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true"); \r
94                 organization.setDisplayNameComputed(displayNameComputed);\r
95         if((value = (String)orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME))!=null)\r
96                 organization.setDisplayName(value);\r
97         if((value = (String)orgInfo.get(OrganizationJAXBSchema.SHORT_NAME))!=null)\r
98                 organization.setShortName(value);\r
99         if((value = (String)orgInfo.get(OrganizationJAXBSchema.LONG_NAME))!=null)\r
100                 organization.setLongName(value);\r
101         if((value = (String)orgInfo.get(OrganizationJAXBSchema.NAME_ADDITIONS))!=null)\r
102                 organization.setNameAdditions(value);\r
103         if((value = (String)orgInfo.get(OrganizationJAXBSchema.CONTACT_NAME))!=null)\r
104                 organization.setContactName(value);\r
105         if((value = (String)orgInfo.get(OrganizationJAXBSchema.FOUNDING_DATE))!=null)\r
106                 organization.setFoundingDate(value);\r
107         if((value = (String)orgInfo.get(OrganizationJAXBSchema.DISSOLUTION_DATE))!=null)\r
108                 organization.setDissolutionDate(value);\r
109         if((value = (String)orgInfo.get(OrganizationJAXBSchema.FOUNDING_PLACE))!=null)\r
110                 organization.setFoundingPlace(value);\r
111         if((value = (String)orgInfo.get(OrganizationJAXBSchema.GROUP))!=null)\r
112                 organization.setGroup(value);\r
113         if((value = (String)orgInfo.get(OrganizationJAXBSchema.FUNCTION))!=null)\r
114                 organization.setFunction(value);\r
115         if((value = (String)orgInfo.get(OrganizationJAXBSchema.SUB_BODY))!=null)\r
116                 organization.setSubBody(value);\r
117         if((value = (String)orgInfo.get(OrganizationJAXBSchema.HISTORY))!=null)\r
118                 organization.setHistory(value);\r
119         if((value = (String)orgInfo.get(OrganizationJAXBSchema.STATUS))!=null)\r
120                 organization.setStatus(value);\r
121         MultipartOutput multipart = new MultipartOutput();\r
122         OutputPart commonPart = multipart.addPart(organization,\r
123             MediaType.APPLICATION_XML_TYPE);\r
124         commonPart.getHeaders().add("label", headerLabel);\r
125 \r
126         if(logger.isDebugEnabled()){\r
127                 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);\r
128         }\r
129 \r
130         return multipart;\r
131     }\r
132 \r
133     /**\r
134      * Returns an error message indicating that the status code returned by a\r
135      * specific call to a service does not fall within a set of valid status\r
136      * codes for that service.\r
137      *\r
138      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
139      *\r
140      * @param statusCode  The invalid status code that was returned in the response,\r
141      *                    from submitting that type of request to the service.\r
142      *\r
143      * @return An error message.\r
144      */\r
145     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
146         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
147                 requestType.validStatusCodesAsString();\r
148     }\r
149 \r
150     public static String extractId(ClientResponse<Response> res) {\r
151         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
152         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
153         if(logger.isDebugEnabled()){\r
154                 logger.info("extractId:uri=" + uri);\r
155         }\r
156         String[] segments = uri.split("/");\r
157         String id = segments[segments.length - 1];\r
158         if(logger.isDebugEnabled()){\r
159                 logger.debug("id=" + id);\r
160         }\r
161         return id;\r
162     }\r
163     \r
164     public static String createOrgAuthRefName(String orgAuthorityName, boolean withDisplaySuffix) {\r
165         String refName = "urn:cspace:org.collectionspace.demo:orgauthority:name("\r
166                         +orgAuthorityName+")";\r
167         if(withDisplaySuffix)\r
168                 refName += "'"+orgAuthorityName+"'";\r
169         return refName;\r
170     }\r
171 \r
172     public static String createOrganizationRefName(\r
173                                                 String orgAuthRefName, String orgName, boolean withDisplaySuffix) {\r
174         String refName = orgAuthRefName+":organization:name("+orgName+")";\r
175         if(withDisplaySuffix)\r
176                 refName += "'"+orgName+"'";\r
177         return refName;\r
178     }\r
179 \r
180     /**\r
181      * Produces a default displayName from the basic name and foundingPlace fields.\r
182      * @see OrgAuthorityDocumentModelHandler.prepareDefaultDisplayName() which\r
183      * duplicates this logic, until we define a service-general utils package\r
184      * that is neither client nor service specific.\r
185      * @param shortName\r
186      * @param foundingPlace\r
187      * @return\r
188      * @throws Exception\r
189      */\r
190     public static String prepareDefaultDisplayName(\r
191                 String shortName, String foundingPlace ) {\r
192         StringBuilder newStr = new StringBuilder();\r
193                 final String sep = " ";\r
194                 boolean firstAdded = false;\r
195                 if(null != shortName ) {\r
196                         newStr.append(shortName);\r
197                         firstAdded = true;\r
198                 }\r
199         // Now we add the place\r
200                 if(null != foundingPlace ) {\r
201                         if(firstAdded) {\r
202                                 newStr.append(sep);\r
203                         }\r
204                         newStr.append(foundingPlace);\r
205                 }\r
206                 return newStr.toString();\r
207     }\r
208     \r
209 }\r