2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Licensed under the Educational Community License (ECL), Version 2.0.
10 * You may not use this file except in compliance with this License.
12 * You may obtain a copy of the ECL 2.0 License at
13 * https://source.collectionspace.org/collection-space/LICENSE.txt
15 package org.collectionspace.services.client;
17 import java.util.Collections;
18 import java.util.Date;
19 import java.util.List;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.core.Response;
25 import org.collectionspace.services.ChronologyJAXBSchema;
26 import org.collectionspace.services.chronology.ChronologiesCommon;
27 import org.collectionspace.services.chronology.ChronologyTermGroup;
28 import org.collectionspace.services.chronology.ChronologyTermGroupList;
29 import org.collectionspace.services.chronology.ChronologyauthoritiesCommon;
30 import org.collectionspace.services.client.test.ServiceRequestType;
31 import org.collectionspace.services.common.api.Tools;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class ChronologyAuthorityClientUtils {
37 private static final Logger logger = LoggerFactory.getLogger(ChronologyAuthorityClientUtils.class);
40 * Create a new Chronology authority
42 * @param displayName the display name
43 * @param shortIdentifier the short identifier
44 * @param serviceCommonPartName the common part label
45 * @return The PoxPayloadOut for the create call
47 public static PoxPayloadOut createChronologyAuthorityInstance(final String displayName,
48 final String shortIdentifier,
49 final String serviceCommonPartName) {
50 final ChronologyauthoritiesCommon common = new ChronologyauthoritiesCommon();
51 common.setDisplayName(displayName);
52 common.setShortIdentifier(shortIdentifier);
53 common.setVocabType(ChronologyAuthorityClient.SERVICE_BINDING_NAME);
55 final PoxPayloadOut poxPayloadOut = new PoxPayloadOut(ChronologyAuthorityClient.SERVICE_PAYLOAD_NAME);
56 final PayloadOutputPart payloadPart = poxPayloadOut.addPart(common, MediaType.APPLICATION_XML_TYPE);
57 payloadPart.setLabel(serviceCommonPartName);
59 logger.debug("to be created, chronologyAuthority common {}", common);
65 * @param vcsid the csid of the authority
66 * @param authRefName the refname of the authority
67 * @param materialMap properties for the new chronology
68 * @param terms terms for the new chronology
69 * @param client the service client
70 * @return the csid of the new item
72 public static String createItemInAuthority(final String vcsid,
73 final String authRefName,
74 final Map<String, String> materialMap,
75 final List<ChronologyTermGroup> terms,
76 final ChronologyAuthorityClient client) {
77 // Expected status code: 201 Created
78 final int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
79 // Type of service request being tested
80 final ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
82 String displayName = "";
83 if (terms != null && !terms.isEmpty()) {
84 displayName = terms.get(0).getTermDisplayName();
86 logger.debug("Creating item with display name: {} in chronologyAuthority: {}", displayName, vcsid);
87 PoxPayloadOut multipart = createChronologyInstance(materialMap, terms, client.getItemCommonPartName());
90 final Response res = client.createItem(vcsid, multipart);
92 int statusCode = res.getStatus();
94 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
95 final String error = "Could not create Item: %s in chronologyAuthority: %s, %s";
96 throw new RuntimeException(String.format(error,
97 materialMap.get(ChronologyJAXBSchema.SHORT_IDENTIFIER),
99 invalidStatusCodeMessage(REQUEST_TYPE, statusCode)));
101 if(statusCode != EXPECTED_STATUS_CODE) {
102 final String error = "Unexpected Status when creating Item: %s in chronologyAuthority %s, Status: %d";
103 throw new RuntimeException(String.format(error,
104 materialMap.get(ChronologyJAXBSchema.SHORT_IDENTIFIER),
108 newID = extractId(res);
116 public static PoxPayloadOut createChronologyInstance(final Map<String, String> chronologyInfo,
117 final List<ChronologyTermGroup> terms,
118 final String headerLabel) {
119 final ChronologiesCommon common = new ChronologiesCommon();
120 common.setShortIdentifier(chronologyInfo.get(ChronologyJAXBSchema.SHORT_IDENTIFIER));
122 ChronologyTermGroupList termList = new ChronologyTermGroupList();
123 if (terms == null || terms.isEmpty()) {
124 termList.getChronologyTermGroup().addAll(getTermGroupInstance(getGeneratedIdentifier()));
126 termList.getChronologyTermGroup().addAll(terms);
128 common.setChronologyTermGroupList(termList);
130 PoxPayloadOut mp = new PoxPayloadOut(ChronologyAuthorityClient.SERVICE_ITEM_NAME);
131 PayloadOutputPart commonPart = mp.addPart(common, MediaType.APPLICATION_XML_TYPE);
132 commonPart.setLabel(headerLabel);
133 logger.debug("To be created, chronologies common {}", common);
138 public static List<ChronologyTermGroup> getTermGroupInstance(String identifier) {
139 if (Tools.isBlank(identifier)) {
140 identifier = getGeneratedIdentifier();
143 ChronologyTermGroup term = new ChronologyTermGroup();
144 term.setTermName(identifier);
145 term.setTermDisplayName(identifier);
146 return Collections.singletonList(term);
154 public static String extractId(final Response res) {
155 final MultivaluedMap<String, Object> mvm = res.getMetadata();
156 final String uri = (String) mvm.get("Location").get(0);
157 logger.debug("extractId:uri={}", uri);
159 final String[] segments = uri.split("/");
160 final String id = segments[segments.length - 1];
161 logger.debug("id=" + id);
171 public static String invalidStatusCodeMessage(final ServiceRequestType requestType, final int statusCode) {
172 return String.format("Status code '%d' is not within the expected set: %s", statusCode,
173 requestType.validStatusCodesAsString());
176 private static String getGeneratedIdentifier() {
177 return "id" + new Date().getTime();