]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b539b7e9f04eaf904785e3ff9265ce2bb1f2d6e8
[tmp/jakarta-migration.git] /
1 package org.collectionspace.services.client;
2
3 import java.io.File;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7 import java.util.Map;
8
9 import javax.ws.rs.core.MediaType;
10 import javax.ws.rs.core.MultivaluedMap;
11 import javax.ws.rs.core.Response;
12
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.TaxonomyCommon;
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;
23
24 public class TaxonomyAuthorityClientUtils {
25     private static final Logger logger =
26         LoggerFactory.getLogger(TaxonomyAuthorityClientUtils.class);
27
28     /**
29      * Creates a new Taxonomy Authority
30      * @param displayName       The displayName used in UI, etc.
31      * @param refName           The proper refName for this authority
32      * @param headerLabel       The common part label
33      * @return  The PoxPayloadOut payload for the create call
34      */
35     public static PoxPayloadOut createTaxonomyauthorityInstance(
36                 String displayName, String shortIdentifier, String headerLabel ) {
37         TaxonomyauthorityCommon Taxonomyauthority = new TaxonomyauthorityCommon();
38         Taxonomyauthority.setDisplayName(displayName);
39         Taxonomyauthority.setShortIdentifier(shortIdentifier);
40         String refName = createTaxonomyAuthRefName(shortIdentifier, displayName);
41         Taxonomyauthority.setRefName(refName);
42         Taxonomyauthority.setVocabType("Taxonomyauthority"); //FIXME: REM - Should this really be hard-coded?
43         PoxPayloadOut multipart = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_PAYLOAD_NAME);
44         PayloadOutputPart commonPart = multipart.addPart(Taxonomyauthority, MediaType.APPLICATION_XML_TYPE);
45         commonPart.setLabel(headerLabel);
46
47         if(logger.isDebugEnabled()){
48                 logger.debug("to be created, Taxonomyauthority common ", 
49                                         Taxonomyauthority, TaxonomyauthorityCommon.class);
50         }
51
52         return multipart;
53     }
54
55     /**
56      * @param taxonomyRefName  The proper refName for this authority
57      * @param taxonomyInfo the properties for the new Taxonomy. Can pass in one condition
58      *                                          note and date string.
59      * @param headerLabel       The common part label
60      * @return  The PoxPayloadOut payload for the create call
61      */
62     public static PoxPayloadOut createTaxonomyInstance( 
63                 String taxonomyAuthRefName, Map<String, String> taxonomyInfo, 
64                                 String headerLabel){
65         TaxonomyCommon taxonomy = new TaxonomyCommon();
66         String shortId = taxonomyInfo.get(TaxonomyJAXBSchema.SHORT_IDENTIFIER);
67         String displayName = taxonomyInfo.get(TaxonomyJAXBSchema.DISPLAY_NAME);
68         taxonomy.setShortIdentifier(shortId);
69         String taxonomyRefName = createTaxonomyRefName(taxonomyAuthRefName, shortId, displayName);
70         taxonomy.setRefName(taxonomyRefName);
71         String value = null;
72         value = taxonomyInfo.get(TaxonomyJAXBSchema.DISPLAY_NAME_COMPUTED);
73         boolean displayNameComputed = (value==null) || value.equalsIgnoreCase("true"); 
74         taxonomy.setDisplayNameComputed(displayNameComputed);
75         if((value = (String)taxonomyInfo.get(TaxonomyJAXBSchema.FULL_NAME))!=null)
76                 taxonomy.setTaxonFullName(value);
77         if((value = (String)taxonomyInfo.get(TaxonomyJAXBSchema.TAXON_RANK))!=null)
78                 taxonomy.setTaxonRank(value);
79         if((value = (String)taxonomyInfo.get(TaxonomyJAXBSchema.TERM_STATUS))!=null)
80                 taxonomy.setTermStatus(value);
81
82         PoxPayloadOut multipart = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
83         PayloadOutputPart commonPart = multipart.addPart(taxonomy,
84             MediaType.APPLICATION_XML_TYPE);
85         commonPart.setLabel(headerLabel);
86
87         if(logger.isDebugEnabled()){
88                 logger.debug("to be created, taxonomy common ", taxonomy, TaxonomyCommon.class);
89         }
90
91         return multipart;
92     }
93     
94     /**
95      * @param vcsid CSID of the authority to create a new taxonomy in
96      * @param TaxonomyauthorityRefName The refName for the authority
97      * @param taxonomyMap the properties for the new Taxonomy
98      * @param client the service client
99      * @return the CSID of the new item
100      */
101     public static String createItemInAuthority(String vcsid, 
102                 String TaxonomyauthorityRefName, Map<String,String> taxonomyMap,
103                 TaxonomyAuthorityClient client ) {
104         // Expected status code: 201 Created
105         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
106         // Type of service request being tested
107         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
108         
109         String displayName = taxonomyMap.get(TaxonomyJAXBSchema.DISPLAY_NAME);
110         String displayNameComputedStr = taxonomyMap.get(TaxonomyJAXBSchema.DISPLAY_NAME_COMPUTED);
111         boolean displayNameComputed = (displayNameComputedStr==null) || displayNameComputedStr.equalsIgnoreCase("true");
112         if( displayName == null ) {
113                 if(!displayNameComputed) {
114                         throw new RuntimeException(
115                         "CreateItem: Must supply a displayName if displayNameComputed is set to false.");
116                 }
117                 displayName = 
118                         prepareDefaultDisplayName(
119                         taxonomyMap.get(TaxonomyJAXBSchema.FULL_NAME));
120         }
121         
122         if(logger.isDebugEnabled()){
123                 logger.debug("Import: Create Item: \""+displayName
124                                 +"\" in Taxonomyauthority: \"" + TaxonomyauthorityRefName +"\"");
125         }
126         PoxPayloadOut multipart = 
127                 createTaxonomyInstance( TaxonomyauthorityRefName,
128                         taxonomyMap, client.getItemCommonPartName() );
129         String newID = null;
130         ClientResponse<Response> res = client.createItem(vcsid, multipart);
131         try {
132                 int statusCode = res.getStatus();
133         
134                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
135                         throw new RuntimeException("Could not create Item: \""
136                                         +taxonomyMap.get(TaxonomyJAXBSchema.SHORT_IDENTIFIER)
137                                         +"\" in Taxonomyauthority: \"" + TaxonomyauthorityRefName
138                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
139                 }
140                 if(statusCode != EXPECTED_STATUS_CODE) {
141                         throw new RuntimeException("Unexpected Status when creating Item: \""
142                                         +taxonomyMap.get(TaxonomyJAXBSchema.SHORT_IDENTIFIER)
143                                         +"\" in Taxonomyauthority: \"" + TaxonomyauthorityRefName +"\", Status:"+ statusCode);
144                 }
145                 newID = extractId(res);
146         } finally {
147                 res.releaseConnection();
148         }
149
150         return newID;
151     }
152
153     public static PoxPayloadOut createTaxonomyInstance(
154                 String commonPartXML, String headerLabel)  throws DocumentException {
155         PoxPayloadOut multipart = new PoxPayloadOut(TaxonomyAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
156         PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
157             MediaType.APPLICATION_XML_TYPE);
158         commonPart.setLabel(headerLabel);
159
160         if(logger.isDebugEnabled()){
161                 logger.debug("to be created, taxonomy common ", commonPartXML);
162         }
163
164         return multipart;
165     }
166     
167     public static String createItemInAuthority(String vcsid,
168                 String commonPartXML,
169                 TaxonomyAuthorityClient client ) throws DocumentException {
170         // Expected status code: 201 Created
171         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
172         // Type of service request being tested
173         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
174         
175         PoxPayloadOut multipart = 
176                 createTaxonomyInstance(commonPartXML, client.getItemCommonPartName());
177         String newID = null;
178         ClientResponse<Response> res = client.createItem(vcsid, multipart);
179         try {
180                 int statusCode = res.getStatus();
181         
182                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
183                         throw new RuntimeException("Could not create Item: \""+commonPartXML
184                                         +"\" in Taxonomyauthority: \"" + vcsid
185                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
186                 }
187                 if(statusCode != EXPECTED_STATUS_CODE) {
188                         throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
189                                         +"\" in Taxonomyauthority: \"" + vcsid +"\", Status:"+ statusCode);
190                 }
191                 newID = extractId(res);
192         } finally {
193                 res.releaseConnection();
194         }
195
196         return newID;
197     }
198     
199     /**
200      * Creates the from xml file.
201      *
202      * @param fileName the file name
203      * @return new CSID as string
204      * @throws Exception the exception
205      */
206     private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName, 
207                 TaxonomyAuthorityClient client) throws Exception {
208         byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
209         String commonPartXML = new String(b);
210         return createItemInAuthority(vcsid, commonPartXML, client );
211     }    
212
213     /**
214      * Creates the Taxonomyauthority ref name.
215      *
216      * @param shortId the Taxonomyauthority shortIdentifier
217      * @param displaySuffix displayName to be appended, if non-null
218      * @return the string
219      */
220     public static String createTaxonomyAuthRefName(String shortId, String displaySuffix) {
221         String refName = "urn:cspace:org.collectionspace.demo:taxonomyauthority:name("
222                         +shortId+")";
223                 if(displaySuffix!=null&&!displaySuffix.isEmpty())
224                         refName += "'"+displaySuffix+"'";
225         return refName;
226     }
227
228     /**
229      * Creates the taxonomy ref name.
230      *
231      * @param taxonomyAuthRefName the Taxonomyauthority ref name
232      * @param shortId the taxonomy shortIdentifier
233      * @param displaySuffix displayName to be appended, if non-null
234      * @return the string
235      */
236     public static String createTaxonomyRefName(
237                                                 String taxonomyAuthRefName, String shortId, String displaySuffix) {
238         String refName = taxonomyAuthRefName+":taxonomy:name("+shortId+")";
239                 if(displaySuffix!=null&&!displaySuffix.isEmpty())
240                         refName += "'"+displaySuffix+"'";
241         return refName;
242     }
243
244     public static String extractId(ClientResponse<Response> res) {
245         MultivaluedMap<String, Object> mvm = res.getMetadata();
246         String uri = (String) ((ArrayList<Object>) mvm.get("Taxonomy")).get(0);
247         if(logger.isDebugEnabled()){
248                 logger.debug("extractId:uri=" + uri);
249         }
250         String[] segments = uri.split("/");
251         String id = segments[segments.length - 1];
252         if(logger.isDebugEnabled()){
253                 logger.debug("id=" + id);
254         }
255         return id;
256     }
257     
258     /**
259      * Returns an error message indicating that the status code returned by a
260      * specific call to a service does not fall within a set of valid status
261      * codes for that service.
262      *
263      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
264      *
265      * @param statusCode  The invalid status code that was returned in the response,
266      *                    from submitting that type of request to the service.
267      *
268      * @return An error message.
269      */
270     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
271         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
272                 requestType.validStatusCodesAsString();
273     }
274
275
276     
277     /**
278      * Produces a default displayName from the basic name and dates fields.
279      * @see TaxonomyDocumentModelHandler.prepareDefaultDisplayName() which
280      * duplicates this logic, until we define a service-general utils package
281      * that is neither client nor service specific.
282      * @param name      
283      * @return
284      */
285     public static String prepareDefaultDisplayName(
286                 String name ) {
287         StringBuilder newStr = new StringBuilder();
288                         newStr.append(name);
289                 return newStr.toString();
290     }
291     
292
293
294 }