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