]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
dd21227b5c500450428009cc41d880a3f1ddc4fc
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client;\r
2 \r
3 import java.util.ArrayList;\r
4 import java.util.List;\r
5 import java.util.Map;\r
6 \r
7 import javax.ws.rs.core.MediaType;\r
8 import javax.ws.rs.core.MultivaluedMap;\r
9 import javax.ws.rs.core.Response;\r
10 \r
11 import org.collectionspace.services.OrganizationJAXBSchema;\r
12 import org.collectionspace.services.client.test.ServiceRequestType;\r
13 import org.collectionspace.services.organization.OrganizationsCommon;\r
14 import org.collectionspace.services.organization.OrgauthoritiesCommon;\r
15 import org.jboss.resteasy.client.ClientResponse;\r
16 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;\r
17 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;\r
18 import org.slf4j.Logger;\r
19 import org.slf4j.LoggerFactory;\r
20 \r
21 public class OrgAuthorityClientUtils {\r
22     private static final Logger logger =\r
23         LoggerFactory.getLogger(OrgAuthorityClientUtils.class);\r
24 \r
25     public static MultipartOutput createOrgAuthorityInstance(\r
26                 String displayName, String refName, String headerLabel ) {\r
27         OrgauthoritiesCommon orgAuthority = new OrgauthoritiesCommon();\r
28         orgAuthority.setDisplayName(displayName);\r
29         orgAuthority.setRefName(refName);\r
30         orgAuthority.setVocabType("OrgAuthority");\r
31         MultipartOutput multipart = new MultipartOutput();\r
32         OutputPart commonPart = multipart.addPart(orgAuthority, MediaType.APPLICATION_XML_TYPE);\r
33         commonPart.getHeaders().add("label", headerLabel);\r
34 \r
35         if(logger.isDebugEnabled()){\r
36                 logger.debug("to be created, orgAuthority common ", \r
37                                         orgAuthority, OrgauthoritiesCommon.class);\r
38         }\r
39 \r
40         return multipart;\r
41     }\r
42 \r
43     public static MultipartOutput createOrganizationInstance(String inAuthority, \r
44                 String orgRefName, Map<String, String> orgInfo, String headerLabel){\r
45         OrganizationsCommon organization = new OrganizationsCommon();\r
46         organization.setInAuthority(inAuthority);\r
47         organization.setRefName(orgRefName);\r
48         String value = null;\r
49         if((value = (String)orgInfo.get(OrganizationJAXBSchema.SHORT_NAME))!=null)\r
50                 organization.setShortName(value);\r
51         if((value = (String)orgInfo.get(OrganizationJAXBSchema.LONG_NAME))!=null)\r
52                 organization.setLongName(value);\r
53         if((value = (String)orgInfo.get(OrganizationJAXBSchema.NAME_ADDITIONS))!=null)\r
54                 organization.setNameAdditions(value);\r
55         if((value = (String)orgInfo.get(OrganizationJAXBSchema.CONTACT_NAME))!=null)\r
56                 organization.setContactName(value);\r
57         if((value = (String)orgInfo.get(OrganizationJAXBSchema.FOUNDING_DATE))!=null)\r
58                 organization.setFoundingDate(value);\r
59         if((value = (String)orgInfo.get(OrganizationJAXBSchema.DISSOLUTION_DATE))!=null)\r
60                 organization.setDissolutionDate(value);\r
61         if((value = (String)orgInfo.get(OrganizationJAXBSchema.FOUNDING_PLACE))!=null)\r
62                 organization.setFoundingPlace(value);\r
63         if((value = (String)orgInfo.get(OrganizationJAXBSchema.FUNCTION))!=null)\r
64                 organization.setFunction(value);\r
65         if((value = (String)orgInfo.get(OrganizationJAXBSchema.DESCRIPTION))!=null)\r
66                 organization.setDescription(value);\r
67         MultipartOutput multipart = new MultipartOutput();\r
68         OutputPart commonPart = multipart.addPart(organization,\r
69             MediaType.APPLICATION_XML_TYPE);\r
70         commonPart.getHeaders().add("label", headerLabel);\r
71 \r
72         if(logger.isDebugEnabled()){\r
73                 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);\r
74         }\r
75 \r
76         return multipart;\r
77     }\r
78 \r
79     /**\r
80      * Returns an error message indicating that the status code returned by a\r
81      * specific call to a service does not fall within a set of valid status\r
82      * codes for that service.\r
83      *\r
84      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).\r
85      *\r
86      * @param statusCode  The invalid status code that was returned in the response,\r
87      *                    from submitting that type of request to the service.\r
88      *\r
89      * @return An error message.\r
90      */\r
91     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {\r
92         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +\r
93                 requestType.validStatusCodesAsString();\r
94     }\r
95 \r
96     public static String extractId(ClientResponse<Response> res) {\r
97         MultivaluedMap<String, Object> mvm = res.getMetadata();\r
98         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);\r
99         if(logger.isDebugEnabled()){\r
100                 logger.info("extractId:uri=" + uri);\r
101         }\r
102         String[] segments = uri.split("/");\r
103         String id = segments[segments.length - 1];\r
104         if(logger.isDebugEnabled()){\r
105                 logger.debug("id=" + id);\r
106         }\r
107         return id;\r
108     }\r
109     \r
110     public static String createRefName(String displayName) {\r
111         return displayName.replaceAll("\\W", "");\r
112     }\r
113 \r
114     public static String createOrgAuthRefName(String orgAuthorityName) {\r
115         return "urn:cspace:org.collectionspace.demo:orgauthority:name("\r
116                         +orgAuthorityName+")";\r
117     }\r
118 \r
119     public static String createOrganizationRefName(\r
120                                                 String orgAuthRefName, String orgName) {\r
121         return orgAuthRefName+":organization:name("+orgName+")";\r
122     }\r
123 \r
124 }\r