]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5b3f20fd0ad8a8b5b469f833b1a6065cbb3de458
[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.Date;
6 import java.util.List;
7
8 import javax.ws.rs.core.MediaType;
9 import javax.ws.rs.core.MultivaluedMap;
10 import javax.ws.rs.core.Response;
11
12 import org.apache.commons.io.FileUtils;
13 import org.collectionspace.services.client.test.ServiceRequestType;
14 import org.collectionspace.services.common.api.Tools;
15 import org.collectionspace.services.concept.ConceptTermGroup;
16 import org.collectionspace.services.concept.ConceptauthoritiesCommon;
17 import org.dom4j.DocumentException;
18 import org.jboss.resteasy.client.ClientResponse;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class ConceptAuthorityClientUtils {
23     private static final Logger logger =
24         LoggerFactory.getLogger(ConceptAuthorityClientUtils.class);
25
26     /**
27      * Creates a new Concept Authority
28      * @param displayName       The displayName used in UI, etc.
29      * @param refName           The proper refName for this authority
30      * @param headerLabel       The common part label
31      * @return  The PoxPayloadOut payload for the create call
32      */
33     public static PoxPayloadOut createConceptAuthorityInstance(
34                 String displayName, String shortIdentifier, String headerLabel ) {
35         ConceptauthoritiesCommon conceptAuthority = new ConceptauthoritiesCommon();
36         conceptAuthority.setDisplayName(displayName);
37         conceptAuthority.setShortIdentifier(shortIdentifier);
38         conceptAuthority.setVocabType("ConceptAuthority"); //FIXME: REM - Should this really be hard-coded?
39         PoxPayloadOut multipart = new PoxPayloadOut(ConceptAuthorityClient.SERVICE_PAYLOAD_NAME);
40         PayloadOutputPart commonPart = multipart.addPart(conceptAuthority, MediaType.APPLICATION_XML_TYPE);
41         commonPart.setLabel(headerLabel);
42
43         if(logger.isDebugEnabled()){
44                 logger.debug("to be created, conceptAuthority common ", 
45                                         conceptAuthority, ConceptauthoritiesCommon.class);
46         }
47
48         return multipart;
49     }
50
51     /**
52      * @param commonPartXML the XML payload for the common part.
53      * @param headerLabel       The common part label
54      * @return  The PoxPayloadOut payload for the create call
55      * @throws DocumentException
56      */
57     public static PoxPayloadOut createConceptInstance(
58                 String commonPartXML, String headerLabel)  throws DocumentException {
59         PoxPayloadOut multipart = new PoxPayloadOut(ConceptAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
60         /*
61         PayloadOutputPart commonPart = multipart.addPart(commonPartXML,
62             MediaType.APPLICATION_XML_TYPE);
63         commonPart.setLabel(headerLabel);
64         */
65         PayloadOutputPart commonPart = multipart.addPart(
66                         ConceptAuthorityClient.SERVICE_ITEM_COMMON_PART_NAME,
67                         commonPartXML);
68
69         if(logger.isDebugEnabled()){
70                 logger.debug("to be created, concept common ", commonPart.asXML());
71         }
72
73         return multipart;
74     }
75     
76     public static String createItemInAuthority(String vcsid,
77                 String commonPartXML,
78                 ConceptAuthorityClient client ) throws DocumentException {
79         // Expected status code: 201 Created
80         int EXPECTED_STATUS_CODE = Response.Status.CREATED.getStatusCode();
81         // Type of service request being tested
82         ServiceRequestType REQUEST_TYPE = ServiceRequestType.CREATE;
83         
84         PoxPayloadOut multipart = 
85                 createConceptInstance(commonPartXML, client.getItemCommonPartName());
86         String newID = null;
87         Response res = client.createItem(vcsid, multipart);
88         try {
89                 int statusCode = res.getStatus();
90         
91                 if(!REQUEST_TYPE.isValidStatusCode(statusCode)) {
92                         throw new RuntimeException("Could not create Item: \""+commonPartXML
93                                         +"\" in conceptAuthority: \"" + vcsid
94                                         +"\" "+ invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
95                 }
96                 if(statusCode != EXPECTED_STATUS_CODE) {
97                         throw new RuntimeException("Unexpected Status when creating Item: \""+commonPartXML
98                                         +"\" in conceptAuthority: \"" + vcsid +"\", Status:"+ statusCode);
99                 }
100                 newID = extractId(res);
101         } finally {
102                 res.close();
103         }
104
105         return newID;
106     }
107     
108     /**
109      * Creates the from xml file.
110      *
111      * @param fileName the file name
112      * @return new CSID as string
113      * @throws Exception the exception
114      */
115     private String createItemInAuthorityFromXmlFile(String vcsid, String commonPartFileName, 
116                 ConceptAuthorityClient client) throws Exception {
117         byte[] b = FileUtils.readFileToByteArray(new File(commonPartFileName));
118         String commonPartXML = new String(b);
119         return createItemInAuthority(vcsid, commonPartXML, client );
120     }    
121
122     public static String extractId(Response res) {
123         MultivaluedMap<String, Object> mvm = res.getMetadata();
124         String uri = (String) ((ArrayList<Object>) mvm.get("Location")).get(0);
125         if(logger.isDebugEnabled()){
126                 logger.debug("extractId:uri=" + uri);
127         }
128         String[] segments = uri.split("/");
129         String id = segments[segments.length - 1];
130         if(logger.isDebugEnabled()){
131                 logger.debug("id=" + id);
132         }
133         return id;
134     }
135     
136     /**
137      * Returns an error message indicating that the status code returned by a
138      * specific call to a service does not fall within a set of valid status
139      * codes for that service.
140      *
141      * @param serviceRequestType  A type of service request (e.g. CREATE, DELETE).
142      *
143      * @param statusCode  The invalid status code that was returned in the response,
144      *                    from submitting that type of request to the service.
145      *
146      * @return An error message.
147      */
148     public static String invalidStatusCodeMessage(ServiceRequestType requestType, int statusCode) {
149         return "Status code '" + statusCode + "' in response is NOT within the expected set: " +
150                 requestType.validStatusCodesAsString();
151     }
152     
153      public static List<ConceptTermGroup> getTermGroupInstance(String identifier) {
154         if (Tools.isBlank(identifier)) {
155             identifier = getGeneratedIdentifier();
156         }
157         List<ConceptTermGroup> terms = new ArrayList<ConceptTermGroup>();
158         ConceptTermGroup term = new ConceptTermGroup();
159         term.setTermDisplayName(identifier);
160         term.setTermName(identifier);
161         terms.add(term);
162         return terms;
163     }
164     
165     private static String getGeneratedIdentifier() {
166         return "id" + new Date().getTime(); 
167    }
168
169 }