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 String createItemInAuthority(String vcsid,
\r
44 String orgAuthorityRefName, Map<String, String> orgInfo,
\r
45 OrgAuthorityClient client) {
\r
46 // Expected status code: 201 Created
\r
47 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
\r
48 // Type of service request being tested
\r
49 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
\r
50 String displayName = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME);
\r
51 String displayNameComputedStr = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
\r
52 boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");
\r
53 if( displayName == null ) {
\r
54 if(!displayNameComputed) {
\r
55 throw new RuntimeException(
\r
56 "CreateItem: Must supply a displayName if displayNameComputed is set to false.");
\r
58 displayName = prepareDefaultDisplayName(
\r
59 orgInfo.get(OrganizationJAXBSchema.SHORT_NAME ),
\r
60 orgInfo.get(OrganizationJAXBSchema.FOUNDING_PLACE ));
\r
62 String refName = createOrganizationRefName( orgAuthorityRefName, displayName, true);
\r
64 if(logger.isDebugEnabled()){
\r
65 logger.debug("Import: Create Item: \""+displayName
\r
66 +"\" in orgAuthorityulary: \"" + orgAuthorityRefName +"\"");
\r
68 MultipartOutput multipart =
\r
69 createOrganizationInstance( vcsid, refName, orgInfo, client.getItemCommonPartName() );
\r
70 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
72 int statusCode = res.getStatus();
\r
74 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
\r
75 throw new RuntimeException("Could not create Item: \""+displayName
\r
76 +"\" in orgAuthority: \"" + orgAuthorityRefName
\r
77 +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
\r
79 if(statusCode != EXPECTED_STATUS_CODE) {
\r
80 throw new RuntimeException("Unexpected Status when creating Item: \""+displayName
\r
81 +"\" in orgAuthority: \"" + orgAuthorityRefName +"\", Status:"+ statusCode);
\r
84 return extractId(res);
\r
87 public static MultipartOutput createOrganizationInstance(String inAuthority,
\r
88 String orgRefName, Map<String, String> orgInfo, String headerLabel){
\r
89 OrganizationsCommon organization = new OrganizationsCommon();
\r
90 organization.setInAuthority(inAuthority);
\r
91 organization.setRefName(orgRefName);
\r
92 String value = null;
\r
93 value = orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME_COMPUTED);
\r
94 boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true");
\r
95 organization.setDisplayNameComputed(displayNameComputed);
\r
96 if((value = (String)orgInfo.get(OrganizationJAXBSchema.DISPLAY_NAME))!=null)
\r
97 organization.setDisplayName(value);
\r
98 if((value = (String)orgInfo.get(OrganizationJAXBSchema.SHORT_NAME))!=null)
\r
99 organization.setShortName(value);
\r
100 if((value = (String)orgInfo.get(OrganizationJAXBSchema.LONG_NAME))!=null)
\r
101 organization.setLongName(value);
\r
102 if((value = (String)orgInfo.get(OrganizationJAXBSchema.NAME_ADDITIONS))!=null)
\r
103 organization.setNameAdditions(value);
\r
104 if((value = (String)orgInfo.get(OrganizationJAXBSchema.CONTACT_NAME))!=null)
\r
105 organization.setContactName(value);
\r
106 if((value = (String)orgInfo.get(OrganizationJAXBSchema.FOUNDING_DATE))!=null)
\r
107 organization.setFoundingDate(value);
\r
108 if((value = (String)orgInfo.get(OrganizationJAXBSchema.DISSOLUTION_DATE))!=null)
\r
109 organization.setDissolutionDate(value);
\r
110 if((value = (String)orgInfo.get(OrganizationJAXBSchema.FOUNDING_PLACE))!=null)
\r
111 organization.setFoundingPlace(value);
\r
112 if((value = (String)orgInfo.get(OrganizationJAXBSchema.FUNCTION))!=null)
\r
113 organization.setFunction(value);
\r
114 if((value = (String)orgInfo.get(OrganizationJAXBSchema.DESCRIPTION))!=null)
\r
115 organization.setDescription(value);
\r
116 MultipartOutput multipart = new MultipartOutput();
\r
117 OutputPart commonPart = multipart.addPart(organization,
\r
118 MediaType.APPLICATION_XML_TYPE);
\r
119 commonPart.getHeaders().add("label", headerLabel);
\r
121 if(logger.isDebugEnabled()){
\r
122 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);
\r
129 * Returns an error message indicating that the status code returned by a
\r
130 * specific call to a service does not fall within a set of valid status
\r
131 * codes for that service.
\r
133 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\r
135 * @param statusCode The invalid status code that was returned in the response,
\r
136 * from submitting that type of request to the service.
\r
138 * @return An error message.
\r
140 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
\r
141 return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
\r
142 requestType.validStatusCodesAsString();
\r
145 public static String extractId(ClientResponse<Response> res) {
\r
146 MultivaluedMap<String, Object> mvm = res.getMetadata();
\r
147 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
\r
148 if(logger.isDebugEnabled()){
\r
149 logger.info("extractId:uri=" + uri);
\r
151 String[] segments = uri.split("/");
\r
152 String id = segments[segments.length - 1];
\r
153 if(logger.isDebugEnabled()){
\r
154 logger.debug("id=" + id);
\r
159 public static String createOrgAuthRefName(String orgAuthorityName, boolean withDisplaySuffix) {
\r
160 String refName = "urn:cspace:org.collectionspace.demo:orgauthority:name("
\r
161 +orgAuthorityName+")";
\r
162 if(withDisplaySuffix)
\r
163 refName += "'"+orgAuthorityName+"'";
\r
167 public static String createOrganizationRefName(
\r
168 String orgAuthRefName, String orgName, boolean withDisplaySuffix) {
\r
169 String refName = orgAuthRefName+":organization:name("+orgName+")";
\r
170 if(withDisplaySuffix)
\r
171 refName += "'"+orgName+"'";
\r
176 * Produces a default displayName from the basic name and foundingPlace fields.
\r
177 * @see OrgAuthorityDocumentModelHandler.prepareDefaultDisplayName() which
\r
178 * duplicates this logic, until we define a service-general utils package
\r
179 * that is neither client nor service specific.
\r
181 * @param foundingPlace
\r
183 * @throws Exception
\r
185 public static String prepareDefaultDisplayName(
\r
186 String shortName, String foundingPlace ) {
\r
187 StringBuilder newStr = new StringBuilder();
\r
188 final String sep = " ";
\r
189 boolean firstAdded = false;
\r
190 if(null != shortName ) {
\r
191 newStr.append(shortName);
\r
194 // Now we add the place
\r
195 if(null != foundingPlace ) {
\r
197 newStr.append(sep);
\r
199 newStr.append(foundingPlace);
\r
201 return newStr.toString();
\r