]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
73ed3f3a967e02a639f07b10e525092cbf51889a
[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.Response;
37 import javax.ws.rs.core.UriBuilder;
38 import javax.ws.rs.core.UriInfo;
39
40 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
41 import org.collectionspace.services.dimension.DimensionsCommonList.*;
42
43 import org.collectionspace.services.common.ClientType;
44 import org.collectionspace.services.common.ServiceMain;
45 import org.collectionspace.services.common.context.MultipartServiceContext;
46 import org.collectionspace.services.common.context.MultipartServiceContextFactory;
47 import org.collectionspace.services.common.context.ServiceContext;
48 import org.collectionspace.services.common.document.DocumentNotFoundException;
49 import org.collectionspace.services.common.document.DocumentHandler;
50 import org.collectionspace.services.common.document.DocumentHandlerFactory;
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 @Path("/dimensions")
58 @Consumes("multipart/mixed")
59 @Produces("multipart/mixed")
60 public class DimensionResource extends AbstractCollectionSpaceResource {
61
62     private final static String serviceName = "dimensions";
63     final Logger logger = LoggerFactory.getLogger(DimensionResource.class);
64     //FIXME retrieve client type from configuration
65     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
66
67     public DimensionResource() {
68         // do nothing
69     }
70
71     @Override
72     protected String getVersionString() {
73         /** The last change revision. */
74         final String lastChangeRevision = "$LastChangedRevision$";
75         return lastChangeRevision;
76     }
77     
78     @Override
79     public String getServiceName() {
80         return serviceName;
81     }
82
83     @Override
84     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
85         DocumentHandler docHandler = DocumentHandlerFactory.getInstance().getHandler(
86                 ctx.getDocumentHandlerClass());
87         docHandler.setServiceContext(ctx);
88         if (ctx.getInput() != null) {
89             Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), DimensionsCommon.class);
90             if (obj != null) {
91                 docHandler.setCommonPart((DimensionsCommon) obj);
92             }
93         }
94         return docHandler;
95     }
96
97     @POST
98     public Response createDimension(MultipartInput input) {
99         try {
100             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
101             DocumentHandler handler = createDocumentHandler(ctx);
102             String csid = getRepositoryClient(ctx).create(ctx, handler);
103             //dimensionObject.setCsid(csid);
104             UriBuilder path = UriBuilder.fromResource(DimensionResource.class);
105             path.path("" + csid);
106             Response response = Response.created(path.build()).build();
107             return response;
108         } catch (Exception e) {
109             if (logger.isDebugEnabled()) {
110                 logger.debug("Caught exception in createDimension", e);
111             }
112             Response response = Response.status(
113                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
114             throw new WebApplicationException(response);
115         }
116     }
117
118     @GET
119     @Path("{csid}")
120     public MultipartOutput getDimension(
121             @PathParam("csid") String csid) {
122         if (logger.isDebugEnabled()) {
123             logger.debug("getDimension with csid=" + csid);
124         }
125         if (csid == null || "".equals(csid)) {
126             logger.error("getDimension: missing csid!");
127             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
128                     "get failed on Dimension csid=" + csid).type(
129                     "text/plain").build();
130             throw new WebApplicationException(response);
131         }
132         MultipartOutput result = null;
133         try {
134             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
135             DocumentHandler handler = createDocumentHandler(ctx);
136             getRepositoryClient(ctx).get(ctx, csid, handler);
137             result = (MultipartOutput) ctx.getOutput();
138         } catch (DocumentNotFoundException dnfe) {
139             if (logger.isDebugEnabled()) {
140                 logger.debug("getDimension", dnfe);
141             }
142             Response response = Response.status(Response.Status.NOT_FOUND).entity(
143                     "Get failed on Dimension csid=" + csid).type(
144                     "text/plain").build();
145             throw new WebApplicationException(response);
146         } catch (Exception e) {
147             if (logger.isDebugEnabled()) {
148                 logger.debug("getDimension", e);
149             }
150             Response response = Response.status(
151                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
152             throw new WebApplicationException(response);
153         }
154         if (result == null) {
155             Response response = Response.status(Response.Status.NOT_FOUND).entity(
156                     "Get failed, the requested Dimension CSID:" + csid + ": was not found.").type(
157                     "text/plain").build();
158             throw new WebApplicationException(response);
159         }
160         return result;
161     }
162
163     @GET
164     @Produces("application/xml")
165     public DimensionsCommonList getDimensionList(@Context UriInfo ui) {
166         DimensionsCommonList dimensionObjectList = new DimensionsCommonList();
167         try {
168             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
169             DocumentHandler handler = createDocumentHandler(ctx);
170             getRepositoryClient(ctx).getAll(ctx, handler);
171             dimensionObjectList = (DimensionsCommonList) handler.getCommonPartList();
172         } catch (Exception e) {
173             if (logger.isDebugEnabled()) {
174                 logger.debug("Caught exception in getDimensionList", e);
175             }
176             Response response = Response.status(
177                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
178             throw new WebApplicationException(response);
179         }
180         return dimensionObjectList;
181     }
182
183     @PUT
184     @Path("{csid}")
185     public MultipartOutput updateDimension(
186             @PathParam("csid") String csid,
187             MultipartInput theUpdate) {
188         if (logger.isDebugEnabled()) {
189             logger.debug("updateDimension with csid=" + csid);
190         }
191         if (csid == null || "".equals(csid)) {
192             logger.error("updateDimension: missing csid!");
193             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
194                     "update failed on Dimension csid=" + csid).type(
195                     "text/plain").build();
196             throw new WebApplicationException(response);
197         }
198         MultipartOutput result = null;
199         try {
200             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
201             DocumentHandler handler = createDocumentHandler(ctx);
202             getRepositoryClient(ctx).update(ctx, csid, handler);
203             result = (MultipartOutput) ctx.getOutput();
204         } catch (DocumentNotFoundException dnfe) {
205             if (logger.isDebugEnabled()) {
206                 logger.debug("caugth exception in updateDimension", dnfe);
207             }
208             Response response = Response.status(Response.Status.NOT_FOUND).entity(
209                     "Update failed on Dimension csid=" + csid).type(
210                     "text/plain").build();
211             throw new WebApplicationException(response);
212         } catch (Exception e) {
213             Response response = Response.status(
214                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
215             throw new WebApplicationException(response);
216         }
217         return result;
218     }
219
220     @DELETE
221     @Path("{csid}")
222     public Response deleteDimension(@PathParam("csid") String csid) {
223
224         if (logger.isDebugEnabled()) {
225             logger.debug("deleteDimension with csid=" + csid);
226         }
227         if (csid == null || "".equals(csid)) {
228             logger.error("deleteDimension: missing csid!");
229             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
230                     "delete failed on Dimension csid=" + csid).type(
231                     "text/plain").build();
232             throw new WebApplicationException(response);
233         }
234         try {
235             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
236             getRepositoryClient(ctx).delete(ctx, csid);
237             return Response.status(HttpResponseCodes.SC_OK).build();
238         } catch (DocumentNotFoundException dnfe) {
239             if (logger.isDebugEnabled()) {
240                 logger.debug("caught exception in deleteDimension", dnfe);
241             }
242             Response response = Response.status(Response.Status.NOT_FOUND).entity(
243                     "Delete failed on Dimension csid=" + csid).type(
244                     "text/plain").build();
245             throw new WebApplicationException(response);
246         } catch (Exception e) {
247             Response response = Response.status(
248                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
249             throw new WebApplicationException(response);
250         }
251
252     }
253 }