]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7cee06e85ded769668f8a09896606a873d0c1d2e
[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.citation.CitationTermGroup;
14 import org.collectionspace.services.citation.CitationauthoritiesCommon;
15 import org.collectionspace.services.client.test.ServiceRequestType;
16 import org.collectionspace.services.common.api.Tools;
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 CitationAuthorityClientUtils {
23     private static final Logger logger =
24         LoggerFactory.getLogger(CitationAuthorityClientUtils.class);
25     private static final String CITATION_VOCAB_TYPE = "CitationAuthority";
26
27     /**
28      * Creates a new Citation 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 createCitationAuthorityInstance(
35                 String displayName, String shortIdentifier, String headerLabel ) {
36         CitationauthoritiesCommon citationAuthority = new CitationauthoritiesCommon();
37         citationAuthority.setDisplayName(displayName);
38         citationAuthority.setShortIdentifier(shortIdentifier);
39         citationAuthority.setVocabType(CITATION_VOCAB_TYPE); //FIXME: REM - Should this really be hard-coded?
40         PoxPayloadOut multipart = new PoxPayloadOut(CitationAuthorityClient.SERVICE_PAYLOAD_NAME);
41         PayloadOutputPart commonPart = multipart.addPart(citationAuthority, MediaType.APPLICATION_XML_TYPE);
42         commonPart.setLabel(headerLabel);
43
44         if(logger.isDebugEnabled()){
45                 logger.debug("to be created, citationAuthority common ", 
46                                         citationAuthority, CitationauthoritiesCommon.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 createCitationInstance(
59                 String commonPartXML, String headerLabel)  throws DocumentException {
60         PoxPayloadOut multipart = new PoxPayloadOut(CitationAuthorityClient.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                         CitationAuthorityClient.SERVICE_ITEM_COMMON_PART_NAME,
68                         commonPartXML);
69
70         if(logger.isDebugEnabled()){
71                 logger.debug("to be created, citation common ", commonPart.asXML());
72         }
73
74         return multipart;
75     }
76     
77     public static String createItemInAuthority(String vcsid,
78                 String commonPartXML,
79                 CitationAuthorityClient 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                 createCitationInstance(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 citationAuthority: \"" + 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 citationAuthority: \"" + vcsid +"\", Status:"+ statusCode);
100                 }
101                 newID = extractId(res);
102         } finally {
103                 res.close();
104         }
105
106         return newID;
107     }
108     
109     /**
110      * Creates an item in the authority from an 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                 CitationAuthorityClient 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<CitationTermGroup> getTermGroupInstance(String identifier) {
155         if (Tools.isBlank(identifier)) {
156             identifier = getGeneratedIdentifier();
157         }
158         List<CitationTermGroup> terms = new ArrayList<CitationTermGroup>();
159         CitationTermGroup term = new CitationTermGroup();
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 }