1 package org.collectionspace.services.client;
4 import java.util.ArrayList;
5 import java.util.Arrays;
9 import javax.ws.rs.core.MediaType;
10 import javax.ws.rs.core.MultivaluedMap;
11 import javax.ws.rs.core.Response;
13 import org.apache.commons.io.FileUtils;
14 import org.collectionspace.services.TaxonomyJAXBSchema;
15 import org.collectionspace.services.client.test.ServiceRequestType;
16 import org.collectionspace.services.taxonomy.TaxonCommon;
17 import org.collectionspace.services.taxonomy.TaxonomyauthorityCommon;
18 import org.dom4j.DocumentException;
19 import org.jboss.resteasy.client.ClientResponse;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.testng.Assert;
24 public class TaxonomyAuthorityClientUtils {
26 private static final Logger logger =
27 LoggerFactory.getLogger(TaxonomyAuthorityClientUtils.class);
30 * Creates a new Taxonomy Authority
31 * @param displayName The displayName used in UI, etc.
32 * @param refName The proper refName for this authority
33 * @param headerLabel The common part label
34 * @return The PoxPayloadOut payload for the create call
36 public static PoxPayloadOut createTaxonomyAuthorityInstance(
37 String displayName, String shortIdentifier, String headerLabel) {
38 TaxonomyauthorityCommon Taxonomyauthority = new TaxonomyauthorityCommon();
39 Taxonomyauthority.setDisplayName(displayName);
40 Taxonomyauthority.setShortIdentifier(shortIdentifier);
41 String refName = createTaxonomyAuthRefName(shortIdentifier, displayName);
42 Taxonomyauthority.setRefName(refName);
43 Taxonomyauthority.setVocabType("Taxonomyauthority"); //FIXME: REM - Should this really be hard-coded?
44 PoxPayloadOut multipart = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_PAYLOAD_NAME);
45 PayloadOutputPart commonPart = multipart.addPart(Taxonomyauthority, MediaType.APPLICATION_XML_TYPE);
46 commonPart.setLabel(headerLabel);
48 if (logger.isDebugEnabled()) {
49 logger.debug("to be created, Taxonomyauthority common ",
50 Taxonomyauthority, TaxonomyauthorityCommon.class);
57 * @param taxonomyRefName The proper refName for this authority
58 * @param taxonInfo the properties for the new Taxonomy. Can pass in one condition
59 * note and date string.
60 * @param headerLabel The common part label
61 * @return The PoxPayloadOut payload for the create call
63 public static PoxPayloadOut createTaxonInstance(
64 String taxonomyAuthRefName, Map<String, String> taxonInfo,
66 TaxonCommon taxon = new TaxonCommon();
67 String shortId = taxonInfo.get(TaxonomyJAXBSchema.SHORT_IDENTIFIER);
68 String displayName = taxonInfo.get(TaxonomyJAXBSchema.DISPLAY_NAME);
69 taxon.setShortIdentifier(shortId);
70 String taxonomyRefName = createTaxonomyRefName(taxonomyAuthRefName, shortId, displayName);
71 taxon.setRefName(taxonomyRefName);
73 value = taxonInfo.get(TaxonomyJAXBSchema.DISPLAY_NAME_COMPUTED);
74 boolean displayNameComputed = (value == null) || value.equalsIgnoreCase("true");
75 taxon.setDisplayNameComputed(displayNameComputed);
76 if ((value = (String) taxonInfo.get(TaxonomyJAXBSchema.NAME)) != null) {
77 taxon.setTaxonFullName(value);
79 if ((value = (String) taxonInfo.get(TaxonomyJAXBSchema.RANK)) != null) {
80 taxon.setTaxonRank(value);
82 if ((value = (String) taxonInfo.get(TaxonomyJAXBSchema.TERM_STATUS)) != null) {
83 taxon.setTermStatus(value);
85 // FIXME: Add additional fields in the Taxon record here.
87 PoxPayloadOut multipart = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
88 PayloadOutputPart commonPart = multipart.addPart(taxon,
89 MediaType.APPLICATION_XML_TYPE);
90 commonPart.setLabel(headerLabel);
92 if (logger.isDebugEnabled()) {
93 logger.debug("to be created, taxon common ", taxon, TaxonCommon.class);
100 * @param vcsid CSID of the authority to create a new taxon in
101 * @param TaxonomyauthorityRefName The refName for the authority
102 * @param taxonMap the properties for the new Taxonomy
103 * @param client the service client
104 * @return the CSID of the new item
106 public static String createItemInAuthority(String vcsid,
107 String TaxonomyauthorityRefName, Map<String, String> taxonMap,
108 TaxonomyAuthorityClient client) {
109 // Expected status code: 201 Created
110 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
111 // Type of service request being tested
112 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
114 String displayName = taxonMap.get(TaxonomyJAXBSchema.DISPLAY_NAME);
115 String displayNameComputedStr = taxonMap.get(TaxonomyJAXBSchema.DISPLAY_NAME_COMPUTED);
116 boolean displayNameComputed = (displayNameComputedStr == null) || displayNameComputedStr.equalsIgnoreCase("true");
117 if (displayName == null) {
118 if (!displayNameComputed) {
119 throw new RuntimeException(
120 "CreateItem: Must supply a displayName if displayNameComputed is set to false.");
123 prepareDefaultDisplayName(
124 taxonMap.get(TaxonomyJAXBSchema.NAME));
127 if (logger.isDebugEnabled()) {
128 logger.debug("Import: Create Item: \"" + displayName
129 + "\" in Taxonomyauthority: \"" + TaxonomyauthorityRefName + "\"");
131 PoxPayloadOut multipart =
132 createTaxonInstance(TaxonomyauthorityRefName,
133 taxonMap, client.getItemCommonPartName());
135 ClientResponse<Response> res = client.createItem(vcsid, multipart);
137 int statusCode = res.getStatus();
139 if (!REQUEST_TYPE.isValidStatusCode(statusCode)) {
140 throw new RuntimeException("Could not create Item: \""
141 + taxonMap.get(TaxonomyJAXBSchema.SHORT_IDENTIFIER)
142 + "\" in Taxonomyauthority: \"" + TaxonomyauthorityRefName
143 + "\" " + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
145 if (statusCode != EXPECTED_STATUS_CODE) {
146 throw new RuntimeException("Unexpected Status when creating Item: \""
147 + taxonMap.get(TaxonomyJAXBSchema.SHORT_IDENTIFIER)
148 + "\" in Taxonomyauthority: \"" + TaxonomyauthorityRefName + "\", Status:" + statusCode);
150 newID = extractId(res);
152 res.releaseConnection();
158 public static PoxPayloadOut createTaxonomyInstance(
159 String commonPartXML, String headerLabel) throws DocumentException {
160 PoxPayloadOut multipart = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
161 PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
162 MediaType.APPLICATION_XML_TYPE);
163 commonPart.setLabel(headerLabel);
165 if (logger.isDebugEnabled()) {
166 logger.debug("to be created, Taxon common ", commonPartXML);
172 public static String createItemInAuthority(String vcsid,
173 String commonPartXML,
174 TaxonomyAuthorityClient client) throws DocumentException {
175 // Expected status code: 201 Created
176 int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
177 // Type of service request being tested
178 ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
180 PoxPayloadOut multipart =
181 createTaxonomyInstance(commonPartXML, client.getItemCommonPartName());
183 ClientResponse<Response> res = client.createItem(vcsid, multipart);
185 int statusCode = res.getStatus();
187 if (!REQUEST_TYPE.isValidStatusCode(statusCode)) {
188 throw new RuntimeException("Could not create Item: \"" + commonPartXML
189 + "\" in Taxonomyauthority: \"" + vcsid
190 + "\" " + invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
192 if (statusCode != EXPECTED_STATUS_CODE) {
193 throw new RuntimeException("Unexpected Status when creating Item: \"" + commonPartXML
194 + "\" in Taxonomyauthority: \"" + vcsid + "\", Status:" + statusCode);
196 newID = extractId(res);
198 res.releaseConnection();
205 * Creates the from xml file.
207 * @param fileName the file name
208 * @return new CSID as string
209 * @throws Exception the exception
211 private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName,
212 TaxonomyAuthorityClient client) throws Exception {
213 byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
214 String commonPartXML = new String(b);
215 return createItemInAuthority(vcsid, commonPartXML, client);
219 * Creates the Taxonomyauthority ref name.
221 * @param shortId the Taxonomyauthority shortIdentifier
222 * @param displaySuffix displayName to be appended, if non-null
225 public static String createTaxonomyAuthRefName(String shortId, String displaySuffix) {
226 String refName = "urn:cspace:org.collectionspace.demo:taxonomyauthority:name("
228 if (displaySuffix != null && !displaySuffix.isEmpty()) {
229 refName += "'" + displaySuffix + "'";
235 * Creates the taxon ref name.
237 * @param taxonomyAuthRefName the Taxonomyauthority ref name
238 * @param shortId the taxon shortIdentifier
239 * @param displaySuffix displayName to be appended, if non-null
242 public static String createTaxonomyRefName(
243 String taxonomyAuthRefName, String shortId, String displaySuffix) {
244 String refName = taxonomyAuthRefName + ":taxon:name(" + shortId + ")";
245 if (displaySuffix != null && !displaySuffix.isEmpty()) {
246 refName += "'" + displaySuffix + "'";
251 public static String extractId(ClientResponse<Response> res) {
252 MultivaluedMap<String, Object> mvm = res.getMetadata();
253 String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
254 if (logger.isDebugEnabled()) {
255 logger.debug("extractId:uri=" + uri);
257 String[] segments = uri.split("/");
258 String id = segments[segments.length - 1];
259 if (logger.isDebugEnabled()) {
260 logger.debug("id=" + id);
266 * Returns an error message indicating that the status code returned by a
267 * specific call to a service does not fall within a set of valid status
268 * codes for that service.
270 * @param serviceRequestType A type of service request (e.g. CREATE, DELETE).
272 * @param statusCode The invalid status code that was returned in the response,
273 * from submitting that type of request to the service.
275 * @return An error message.
277 public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
278 return "Status code '" + statusCode + "' in response is NOT within the expected set: "
279 + requestType.validStatusCodesAsString();
283 * Produces a default displayName from the basic name and dates fields.
284 * @see TaxonomyDocumentModelHandler.prepareDefaultDisplayName() which
285 * duplicates this logic, until we define a service-general utils package
286 * that is neither client nor service specific.
290 public static String prepareDefaultDisplayName(
292 StringBuilder newStr = new StringBuilder();
294 return newStr.toString();