]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
5b54df800c62bda75eb6fe3d820e506f741d8447
[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
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  */
24 package org.collectionspace.services.dimension;
25
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.PathParam;
34 import javax.ws.rs.WebApplicationException;
35 import javax.ws.rs.core.Context;
36 import javax.ws.rs.core.MultivaluedMap;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.UriBuilder;
39 import javax.ws.rs.core.UriInfo;
40
41 import org.collectionspace.services.client.PoxPayloadIn;
42 import org.collectionspace.services.client.PoxPayloadOut;
43 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
44 //import org.collectionspace.services.dimension.DimensionsCommonList.*;
45
46 import org.collectionspace.services.common.ClientType;
47 import org.collectionspace.services.common.ServiceMain;
48 //import org.collectionspace.services.common.context.MultipartServiceContext;
49 //import org.collectionspace.services.common.context.MultipartServiceContextFactory;
50 import org.collectionspace.services.common.context.ServiceContext;
51 import org.collectionspace.services.common.document.DocumentNotFoundException;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.jboss.resteasy.util.HttpResponseCodes;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 /**
58  * The Class DimensionResource.
59  */
60 @Path("/dimensions")
61 @Consumes("application/xml")
62 @Produces("application/xml")
63 public class DimensionResource extends
64                 AbstractMultiPartCollectionSpaceResourceImpl {
65
66     /** The Constant serviceName. */
67     private final static String serviceName = "dimensions";
68     
69     /** The logger. */
70     final Logger logger = LoggerFactory.getLogger(DimensionResource.class);
71     //FIXME retrieve client type from configuration
72     /** The Constant CLIENT_TYPE. */
73     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
74
75     /**
76      * Instantiates a new dimension resource.
77      */
78     public DimensionResource() {
79         // do nothing
80     }
81
82     /* (non-Javadoc)
83      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
84      */
85     @Override
86     public String getVersionString() {
87         /** The last change revision. */
88         final String lastChangeRevision = "$LastChangedRevision$";
89         return lastChangeRevision;
90     }
91     
92     /* (non-Javadoc)
93      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
94      */
95     @Override
96     public String getServiceName() {
97         return serviceName;
98     }
99
100 //    @Override
101 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
102 //        DocumentHandler docHandler = ctx.getDocumentHandler();
103 //        if (ctx.getInput() != null) {
104 //            Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), DimensionsCommon.class);
105 //            if (obj != null) {
106 //                docHandler.setCommonPart((DimensionsCommon) obj);
107 //            }
108 //        }
109 //        return docHandler;
110 //    }
111     
112     /* (non-Javadoc)
113  * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
114  */
115 @Override
116     public Class<DimensionsCommon> getCommonPartClass() {
117         return DimensionsCommon.class;
118     }
119
120     /**
121      * Creates the dimension.
122      * 
123      * @param xmlText an XML payload
124      * 
125      * @return the response
126      */
127     @POST
128     public Response createDimension(String xmlText) {
129         try {
130             PoxPayloadIn input = new PoxPayloadIn(xmlText);
131             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(input);
132             DocumentHandler handler = createDocumentHandler(ctx);
133             String csid = getRepositoryClient(ctx).create(ctx, handler);
134             //dimensionObject.setCsid(csid);
135             UriBuilder path = UriBuilder.fromResource(DimensionResource.class);
136             path.path("" + csid);
137             Response response = Response.created(path.build()).build();
138             return response;
139         } catch (Exception e) {
140             if (logger.isDebugEnabled()) {
141                 logger.debug("Caught exception in createDimension", e);
142             }
143             Response response = Response.status(
144                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
145             throw new WebApplicationException(response);
146         }
147     }
148
149     /**
150      * Gets the dimension.
151      * 
152      * @param csid the csid
153      * 
154      * @return the dimension
155      */
156     @GET
157     @Path("{csid}")
158     public byte[] getDimension(
159             @PathParam("csid") String csid) {
160         if (logger.isDebugEnabled()) {
161             logger.debug("getDimension with csid=" + csid);
162         }
163         if (csid == null || "".equals(csid)) {
164             logger.error("getDimension: missing csid!");
165             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
166                     "get failed on Dimension csid=" + csid).type(
167                     "text/plain").build();
168             throw new WebApplicationException(response);
169         }
170         PoxPayloadOut result = null;
171         try {
172             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
173             DocumentHandler handler = createDocumentHandler(ctx);
174             getRepositoryClient(ctx).get(ctx, csid, handler);
175             result = ctx.getOutput();
176         } catch (DocumentNotFoundException dnfe) {
177             if (logger.isDebugEnabled()) {
178                 logger.debug("getDimension", dnfe);
179             }
180             Response response = Response.status(Response.Status.NOT_FOUND).entity(
181                     "Get failed on Dimension csid=" + csid).type(
182                     "text/plain").build();
183             throw new WebApplicationException(response);
184         } catch (Exception e) {
185             if (logger.isDebugEnabled()) {
186                 logger.debug("getDimension", e);
187             }
188             Response response = Response.status(
189                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
190             throw new WebApplicationException(response);
191         }
192         if (result == null) {
193             Response response = Response.status(Response.Status.NOT_FOUND).entity(
194                     "Get failed, the requested Dimension CSID:" + csid + ": was not found.").type(
195                     "text/plain").build();
196             throw new WebApplicationException(response);
197         }
198         return result.getBytes();
199     }
200
201     /**
202      * Gets the dimension list.
203      * 
204      * @param ui the ui
205      * 
206      * @return the dimension list
207      */
208     @GET
209     @Produces("application/xml")
210     public DimensionsCommonList getDimensionList(@Context UriInfo ui) {
211         DimensionsCommonList dimensionObjectList = new DimensionsCommonList();
212         MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
213         try {
214             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
215             DocumentHandler handler = createDocumentHandler(ctx);
216             getRepositoryClient(ctx).getFiltered(ctx, handler);
217             dimensionObjectList = (DimensionsCommonList) handler.getCommonPartList();
218         } catch (Exception e) {
219             if (logger.isDebugEnabled()) {
220                 logger.debug("Caught exception in getDimensionList", e);
221             }
222             Response response = Response.status(
223                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
224             throw new WebApplicationException(response);
225         }
226         return dimensionObjectList;
227     }
228
229     /**
230      * Update dimension.
231      * 
232      * @param csid the csid
233      * @param xmlText an XML payload
234      * 
235      * @return the multipart output
236      */
237     @PUT
238     @Path("{csid}")
239     public byte[] updateDimension(
240             @PathParam("csid") String csid, String xmlText) {
241         if (logger.isDebugEnabled()) {
242             logger.debug("updateDimension with csid=" + csid);
243         }
244         if (csid == null || "".equals(csid)) {
245             logger.error("updateDimension: missing csid!");
246             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
247                     "update failed on Dimension csid=" + csid).type(
248                     "text/plain").build();
249             throw new WebApplicationException(response);
250         }
251         PoxPayloadOut result = null;
252         try {
253             PoxPayloadIn update = new PoxPayloadIn(xmlText);
254             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(update);
255             DocumentHandler handler = createDocumentHandler(ctx);
256             getRepositoryClient(ctx).update(ctx, csid, handler);
257             result = ctx.getOutput();
258         } catch (DocumentNotFoundException dnfe) {
259             if (logger.isDebugEnabled()) {
260                 logger.debug("Caught exception in updateDimension", dnfe);
261             }
262             Response response = Response.status(Response.Status.NOT_FOUND).entity(
263                     "Update failed on Dimension csid=" + csid).type(
264                     "text/plain").build();
265             throw new WebApplicationException(response);
266         } catch (Exception e) {
267             Response response = Response.status(
268                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
269             throw new WebApplicationException(response);
270         }
271         return result.getBytes();
272     }
273
274     /**
275      * Delete dimension.
276      * 
277      * @param csid the csid
278      * 
279      * @return the response
280      */
281     @DELETE
282     @Path("{csid}")
283     public Response deleteDimension(@PathParam("csid") String csid) {
284
285         if (logger.isDebugEnabled()) {
286             logger.debug("deleteDimension with csid=" + csid);
287         }
288         if (csid == null || "".equals(csid)) {
289             logger.error("deleteDimension: missing csid!");
290             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
291                     "delete failed on Dimension csid=" + csid).type(
292                     "text/plain").build();
293             throw new WebApplicationException(response);
294         }
295         try {
296             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
297             getRepositoryClient(ctx).delete(ctx, csid);
298             return Response.status(HttpResponseCodes.SC_OK).build();
299         } catch (DocumentNotFoundException dnfe) {
300             if (logger.isDebugEnabled()) {
301                 logger.debug("caught exception in deleteDimension", dnfe);
302             }
303             Response response = Response.status(Response.Status.NOT_FOUND).entity(
304                     "Delete failed on Dimension csid=" + csid).type(
305                     "text/plain").build();
306             throw new WebApplicationException(response);
307         } catch (Exception e) {
308             Response response = Response.status(
309                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
310             throw new WebApplicationException(response);
311         }
312
313     }
314 }