]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
e3ecb77f5e10ef92a61fd36438d2ab6e121d8258
[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.common.AbstractMultiPartCollectionSpaceResourceImpl;
42 //import org.collectionspace.services.dimension.DimensionsCommonList.*;
43
44 import org.collectionspace.services.common.ClientType;
45 import org.collectionspace.services.common.ServiceMain;
46 import org.collectionspace.services.common.context.MultipartServiceContext;
47 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.DocumentNotFoundException;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
52 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
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("multipart/mixed")
62 @Produces("multipart/mixed")
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     protected 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 input the input
124      * 
125      * @return the response
126      */
127     @POST
128     public Response createDimension(MultipartInput input) {
129         try {
130                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
131             DocumentHandler handler = createDocumentHandler(ctx);
132             String csid = getRepositoryClient(ctx).create(ctx, handler);
133             //dimensionObject.setCsid(csid);
134             UriBuilder path = UriBuilder.fromResource(DimensionResource.class);
135             path.path("" + csid);
136             Response response = Response.created(path.build()).build();
137             return response;
138         } catch (Exception e) {
139             if (logger.isDebugEnabled()) {
140                 logger.debug("Caught exception in createDimension", e);
141             }
142             Response response = Response.status(
143                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
144             throw new WebApplicationException(response);
145         }
146     }
147
148     /**
149      * Gets the dimension.
150      * 
151      * @param csid the csid
152      * 
153      * @return the dimension
154      */
155     @GET
156     @Path("{csid}")
157     public MultipartOutput getDimension(
158             @PathParam("csid") String csid) {
159         if (logger.isDebugEnabled()) {
160             logger.debug("getDimension with csid=" + csid);
161         }
162         if (csid == null || "".equals(csid)) {
163             logger.error("getDimension: missing csid!");
164             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
165                     "get failed on Dimension csid=" + csid).type(
166                     "text/plain").build();
167             throw new WebApplicationException(response);
168         }
169         MultipartOutput result = null;
170         try {
171                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
172             DocumentHandler handler = createDocumentHandler(ctx);
173             getRepositoryClient(ctx).get(ctx, csid, handler);
174             result = (MultipartOutput) ctx.getOutput();
175         } catch (DocumentNotFoundException dnfe) {
176             if (logger.isDebugEnabled()) {
177                 logger.debug("getDimension", dnfe);
178             }
179             Response response = Response.status(Response.Status.NOT_FOUND).entity(
180                     "Get failed on Dimension csid=" + csid).type(
181                     "text/plain").build();
182             throw new WebApplicationException(response);
183         } catch (Exception e) {
184             if (logger.isDebugEnabled()) {
185                 logger.debug("getDimension", e);
186             }
187             Response response = Response.status(
188                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
189             throw new WebApplicationException(response);
190         }
191         if (result == null) {
192             Response response = Response.status(Response.Status.NOT_FOUND).entity(
193                     "Get failed, the requested Dimension CSID:" + csid + ": was not found.").type(
194                     "text/plain").build();
195             throw new WebApplicationException(response);
196         }
197         return result;
198     }
199
200     /**
201      * Gets the dimension list.
202      * 
203      * @param ui the ui
204      * 
205      * @return the dimension list
206      */
207     @GET
208     @Produces("application/xml")
209     public DimensionsCommonList getDimensionList(@Context UriInfo ui) {
210         DimensionsCommonList dimensionObjectList = new DimensionsCommonList();
211         MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
212         try {
213                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
214             DocumentHandler handler = createDocumentHandler(ctx);
215             getRepositoryClient(ctx).getAll(ctx, handler);
216             dimensionObjectList = (DimensionsCommonList) handler.getCommonPartList();
217         } catch (Exception e) {
218             if (logger.isDebugEnabled()) {
219                 logger.debug("Caught exception in getDimensionList", e);
220             }
221             Response response = Response.status(
222                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
223             throw new WebApplicationException(response);
224         }
225         return dimensionObjectList;
226     }
227
228     /**
229      * Update dimension.
230      * 
231      * @param csid the csid
232      * @param theUpdate the the update
233      * 
234      * @return the multipart output
235      */
236     @PUT
237     @Path("{csid}")
238     public MultipartOutput updateDimension(
239             @PathParam("csid") String csid,
240             MultipartInput theUpdate) {
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         MultipartOutput result = null;
252         try {
253                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
254             DocumentHandler handler = createDocumentHandler(ctx);
255             getRepositoryClient(ctx).update(ctx, csid, handler);
256             result = (MultipartOutput) ctx.getOutput();
257         } catch (DocumentNotFoundException dnfe) {
258             if (logger.isDebugEnabled()) {
259                 logger.debug("caugth exception in updateDimension", dnfe);
260             }
261             Response response = Response.status(Response.Status.NOT_FOUND).entity(
262                     "Update failed on Dimension csid=" + csid).type(
263                     "text/plain").build();
264             throw new WebApplicationException(response);
265         } catch (Exception e) {
266             Response response = Response.status(
267                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
268             throw new WebApplicationException(response);
269         }
270         return result;
271     }
272
273     /**
274      * Delete dimension.
275      * 
276      * @param csid the csid
277      * 
278      * @return the response
279      */
280     @DELETE
281     @Path("{csid}")
282     public Response deleteDimension(@PathParam("csid") String csid) {
283
284         if (logger.isDebugEnabled()) {
285             logger.debug("deleteDimension with csid=" + csid);
286         }
287         if (csid == null || "".equals(csid)) {
288             logger.error("deleteDimension: missing csid!");
289             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
290                     "delete failed on Dimension csid=" + csid).type(
291                     "text/plain").build();
292             throw new WebApplicationException(response);
293         }
294         try {
295                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
296             getRepositoryClient(ctx).delete(ctx, csid);
297             return Response.status(HttpResponseCodes.SC_OK).build();
298         } catch (DocumentNotFoundException dnfe) {
299             if (logger.isDebugEnabled()) {
300                 logger.debug("caught exception in deleteDimension", dnfe);
301             }
302             Response response = Response.status(Response.Status.NOT_FOUND).entity(
303                     "Delete failed on Dimension csid=" + csid).type(
304                     "text/plain").build();
305             throw new WebApplicationException(response);
306         } catch (Exception e) {
307             Response response = Response.status(
308                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
309             throw new WebApplicationException(response);
310         }
311
312     }
313 }