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