]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
602ef0de0a73342d96cbede55850049354c0974a
[tmp/jakarta-migration.git] /
1 /**
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:
5  * <p>
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  * <p>
9  * Licensed under the Educational Community License (ECL), Version 2.0.
10  * You may not use this file except in compliance with this License.
11  * <p>
12  * You may obtain a copy of the ECL 2.0 License at
13  * https://source.collectionspace.org/collection-space/LICENSE.txt
14  */
15 package org.collectionspace.services.client;
16
17 import java.util.Collections;
18 import java.util.Date;
19 import java.util.List;
20 import java.util.Map;
21 import javax.ws.rs.core.MediaType;
22 import javax.ws.rs.core.MultivaluedMap;
23 import javax.ws.rs.core.Response;
24
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;
34
35 public class ChronologyAuthorityClientUtils {
36
37     private static final Logger logger = LoggerFactory.getLogger(ChronologyAuthorityClientUtils.class);
38
39     /**
40      * Create a new Chronology authority
41      *
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
46      */
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);
54
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);
58
59         logger.debug("to be created, chronologyAuthority common {}", common);
60         return poxPayloadOut;
61     }
62
63     /**
64      *
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
71      */
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;
81
82         String displayName = "";
83         if (terms != null && !terms.isEmpty()) {
84             displayName = terms.get(0).getTermDisplayName();
85         }
86         logger.debug("Creating item with display name: {} in chronologyAuthority: {}", displayName, vcsid);
87         PoxPayloadOut multipart = createChronologyInstance(materialMap, terms, client.getItemCommonPartName());
88         String newID;
89
90         final Response res = client.createItem(vcsid, multipart);
91         try {
92             int statusCode = res.getStatus();
93
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),
98                                                          authRefName,
99                                                          invalidStatusCodeMessage(REQUEST_TYPE, statusCode)));
100             }
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),
105                                                          authRefName,
106                                                          statusCode));
107             }
108             newID = extractId(res);
109         } finally {
110             res.close();
111         }
112
113         return newID;
114     }
115
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));
121
122         ChronologyTermGroupList termList = new ChronologyTermGroupList();
123         if (terms == null || terms.isEmpty()) {
124             termList.getChronologyTermGroup().addAll(getTermGroupInstance(getGeneratedIdentifier()));
125         } else {
126             termList.getChronologyTermGroup().addAll(terms);
127         }
128         common.setChronologyTermGroupList(termList);
129
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);
134
135         return mp;
136     }
137
138     public static List<ChronologyTermGroup> getTermGroupInstance(String identifier) {
139         if (Tools.isBlank(identifier)) {
140             identifier = getGeneratedIdentifier();
141         }
142
143         ChronologyTermGroup term = new ChronologyTermGroup();
144         term.setTermName(identifier);
145         term.setTermDisplayName(identifier);
146         return Collections.singletonList(term);
147     }
148
149     /**
150      *
151      * @param res
152      * @return
153      */
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);
158
159         final String[] segments = uri.split("/");
160         final String id = segments[segments.length - 1];
161         logger.debug("id=" + id);
162         return id;
163     }
164
165     /**
166      *
167      * @param requestType
168      * @param statusCode
169      * @return
170      */
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());
174     }
175
176     private static String getGeneratedIdentifier() {
177         return "id" + new Date().getTime();
178     }
179
180 }