1 package org.collectionspace.services.client;
\r
3 import java.util.ArrayList;
\r
4 import java.util.List;
\r
5 import java.util.Map;
\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
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
21 public class OrgAuthorityClientUtils {
\r
22 private static final Logger logger =
\r
23 LoggerFactory.getLogger(OrgAuthorityClientUtils.class);
\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
35 if(logger.isDebugEnabled()){
\r
36 logger.debug("to be created, orgAuthority common ",
\r
37 orgAuthority, OrgauthoritiesCommon.class);
\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
72 if(logger.isDebugEnabled()){
\r
73 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);
\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
84 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\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
89 * @return An error message.
\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
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
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
110 public static String createRefName(String displayName) {
\r
111 return displayName.replaceAll("\\W", "");
\r
114 public static String createOrgAuthRefName(String orgAuthorityName) {
\r
115 return "urn:cspace:org.collectionspace.demo:orgauthority:name("
\r
116 +orgAuthorityName+")";
\r
119 public static String createOrganizationRefName(
\r
120 String orgAuthRefName, String orgName) {
\r
121 return orgAuthRefName+":organization:name("+orgName+")";
\r