2 * OrgAuthorityClientUtils.java
\r
4 * {Purpose of This Class}
\r
6 * {Other Notes Relating to This Class (Optional)}
\r
9 * $LastChangedRevision: $
\r
10 * $LastChangedDate: $
\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
16 * http://www.collectionspace.org
\r
17 * http://wiki.collectionspace.org
\r
19 * Copyright © 2009 {Contributing Institution}
\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
24 * You may obtain a copy of the ECL 2.0 License at
\r
25 * https://source.collectionspace.org/collection-space/LICENSE.txt
\r
27 package org.collectionspace.services.client;
\r
29 import java.util.ArrayList;
\r
30 import java.util.Map;
\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
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
47 * The Class OrgAuthorityClientUtils.
\r
49 public class OrgAuthorityClientUtils {
\r
51 /** The Constant logger. */
\r
52 private static final Logger logger =
\r
53 LoggerFactory.getLogger(OrgAuthorityClientUtils.class);
\r
56 * Creates the org authority instance.
\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
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
73 if(logger.isDebugEnabled()){
\r
74 logger.debug("to be created, orgAuthority common ",
\r
75 orgAuthority, OrgauthoritiesCommon.class);
\r
82 * Creates the item in authority.
\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
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
105 displayName = prepareDefaultDisplayName(
\r
106 orgInfo.get(OrganizationJAXBSchema.SHORT_NAME ),
\r
107 orgInfo.get(OrganizationJAXBSchema.FOUNDING_PLACE ));
\r
109 String refName = createOrganizationRefName(orgAuthorityRefName, displayName, true);
\r
111 if(logger.isDebugEnabled()){
\r
112 logger.debug("Import: Create Item: \""+displayName
\r
113 +"\" in orgAuthority: \"" + orgAuthorityRefName +"\"");
\r
115 MultipartOutput multipart =
\r
116 createOrganizationInstance(vcsid, refName, orgInfo, client.getItemCommonPartName());
\r
117 ClientResponse<Response> res = client.createItem(vcsid, multipart);
\r
120 int statusCode = res.getStatus();
\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
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
132 result = extractId(res);
\r
134 res.releaseConnection();
\r
141 * Creates the organization instance.
\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
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.TERM_STATUS))!=null)
\r
183 organization.setTermStatus(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
189 if(logger.isDebugEnabled()){
\r
190 logger.debug("to be created, organization common ", organization, OrganizationsCommon.class);
\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
201 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
\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
206 * @return An error message.
\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
216 * @param res the res
\r
217 * @return the string
\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
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
234 * Creates the org auth ref name.
\r
236 * @param orgAuthorityName the org authority name
\r
237 * @param withDisplaySuffix the with display suffix
\r
238 * @return the string
\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
249 * Creates the organization ref name.
\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
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
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
270 * @param foundingPlace
\r
272 * @throws Exception
\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
283 // Now we add the place
\r
284 if(null != foundingPlace ) {
\r
286 newStr.append(sep);
\r
288 newStr.append(foundingPlace);
\r
290 return newStr.toString();
\r