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