]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0b1e1d3916c08062aff91d04e0360ac8360e1a1a
[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.collectionspace.services.common.relation.RelationJAXBSchema;
54 import org.collectionspace.services.common.relation.nuxeo.RelationConstants;
55 import org.jboss.resteasy.util.HttpResponseCodes;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 /**
60  * The Class DimensionResource.
61  */
62 @Path("/dimensions")
63 @Consumes("application/xml")
64 @Produces("application/xml")
65 public class DimensionResource extends
66                 AbstractMultiPartCollectionSpaceResourceImpl {
67
68     /** The Constant serviceName. */
69     private final static String serviceName = "dimensions";
70     
71     /** The logger. */
72     final Logger logger = LoggerFactory.getLogger(DimensionResource.class);
73     //FIXME retrieve client type from configuration
74     /** The Constant CLIENT_TYPE. */
75     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
76
77     /**
78      * Instantiates a new dimension resource.
79      */
80     public DimensionResource() {
81         // do nothing
82     }
83
84     /* (non-Javadoc)
85      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
86      */
87     @Override
88     public String getVersionString() {
89         /** The last change revision. */
90         final String lastChangeRevision = "$LastChangedRevision$";
91         return lastChangeRevision;
92     }
93     
94     /* (non-Javadoc)
95      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
96      */
97     @Override
98     public String getServiceName() {
99         return serviceName;
100     }
101
102 //    @Override
103 //    public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
104 //        DocumentHandler docHandler = ctx.getDocumentHandler();
105 //        if (ctx.getInput() != null) {
106 //            Object obj = ((MultipartServiceContext)ctx).getInputPart(ctx.getCommonPartLabel(), DimensionsCommon.class);
107 //            if (obj != null) {
108 //                docHandler.setCommonPart((DimensionsCommon) obj);
109 //            }
110 //        }
111 //        return docHandler;
112 //    }
113     
114     /* (non-Javadoc)
115  * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
116  */
117 @Override
118     public Class<DimensionsCommon> getCommonPartClass() {
119         return DimensionsCommon.class;
120     }
121
122     /**
123      * Creates the dimension.
124      * 
125      * @param xmlText an XML payload
126      * 
127      * @return the response
128      */
129     @POST
130     public Response createDimension(String xmlText) {
131         try {
132             PoxPayloadIn input = new PoxPayloadIn(xmlText);
133             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(input);
134             DocumentHandler handler = createDocumentHandler(ctx);
135             String csid = getRepositoryClient(ctx).create(ctx, handler);
136             //dimensionObject.setCsid(csid);
137             UriBuilder path = UriBuilder.fromResource(DimensionResource.class);
138             path.path("" + csid);
139             Response response = Response.created(path.build()).build();
140             return response;
141         } catch (Exception e) {
142             if (logger.isDebugEnabled()) {
143                 logger.debug("Caught exception in createDimension", e);
144             }
145             Response response = Response.status(
146                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
147             throw new WebApplicationException(response);
148         }
149     }
150
151     /**
152      * Gets the dimension.
153      * 
154      * @param csid the csid
155      * 
156      * @return the dimension
157      */
158     @GET
159     @Path("{csid}")
160     public byte[] getDimension(
161             @PathParam("csid") String csid) {
162         if (logger.isDebugEnabled()) {
163             logger.debug("getDimension with csid=" + csid);
164         }
165         if (csid == null || "".equals(csid)) {
166             logger.error("getDimension: missing csid!");
167             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
168                     "get failed on Dimension csid=" + csid).type(
169                     "text/plain").build();
170             throw new WebApplicationException(response);
171         }
172         PoxPayloadOut result = null;
173         try {
174             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
175             DocumentHandler handler = createDocumentHandler(ctx);
176             getRepositoryClient(ctx).get(ctx, csid, handler);
177             result = ctx.getOutput();
178         } catch (DocumentNotFoundException dnfe) {
179             if (logger.isDebugEnabled()) {
180                 logger.debug("getDimension", dnfe);
181             }
182             Response response = Response.status(Response.Status.NOT_FOUND).entity(
183                     "Get failed on Dimension csid=" + csid).type(
184                     "text/plain").build();
185             throw new WebApplicationException(response);
186         } catch (Exception e) {
187             if (logger.isDebugEnabled()) {
188                 logger.debug("getDimension", e);
189             }
190             Response response = Response.status(
191                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
192             throw new WebApplicationException(response);
193         }
194         if (result == null) {
195             Response response = Response.status(Response.Status.NOT_FOUND).entity(
196                     "Get failed, the requested Dimension CSID:" + csid + ": was not found.").type(
197                     "text/plain").build();
198             throw new WebApplicationException(response);
199         }
200         return result.getBytes();
201     }
202
203     /**
204      * Gets the dimension list.
205      * 
206      * @param ui the ui
207      * 
208      * @return the dimension list
209      */
210     @GET
211     @Produces("application/xml")
212     public DimensionsCommonList getDimensionList(@Context UriInfo ui) {
213         DimensionsCommonList dimensionObjectList = new DimensionsCommonList();
214         MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
215         try {
216             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
217             DocumentHandler handler = createDocumentHandler(ctx);
218             getRepositoryClient(ctx).getFiltered(ctx, handler);
219             dimensionObjectList = (DimensionsCommonList) handler.getCommonPartList();
220         } catch (Exception e) {
221             if (logger.isDebugEnabled()) {
222                 logger.debug("Caught exception in getDimensionList", e);
223             }
224             Response response = Response.status(
225                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
226             throw new WebApplicationException(response);
227         }
228         return dimensionObjectList;
229     }
230
231     /**
232      * Update dimension.
233      * 
234      * @param csid the csid
235      * @param xmlText an XML payload
236      * 
237      * @return the multipart output
238      */
239     @PUT
240     @Path("{csid}")
241     public byte[] updateDimension(
242             @PathParam("csid") String csid, String xmlText) {
243         if (logger.isDebugEnabled()) {
244             logger.debug("updateDimension with csid=" + csid);
245         }
246         if (csid == null || "".equals(csid)) {
247             logger.error("updateDimension: missing csid!");
248             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
249                     "update failed on Dimension csid=" + csid).type(
250                     "text/plain").build();
251             throw new WebApplicationException(response);
252         }
253         PoxPayloadOut result = null;
254         try {
255             PoxPayloadIn update = new PoxPayloadIn(xmlText);
256             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(update);
257             DocumentHandler handler = createDocumentHandler(ctx);
258             getRepositoryClient(ctx).update(ctx, csid, handler);
259             result = ctx.getOutput();
260         } catch (DocumentNotFoundException dnfe) {
261             if (logger.isDebugEnabled()) {
262                 logger.debug("Caught exception in updateDimension", dnfe);
263             }
264             Response response = Response.status(Response.Status.NOT_FOUND).entity(
265                     "Update failed on Dimension csid=" + csid).type(
266                     "text/plain").build();
267             throw new WebApplicationException(response);
268         } catch (Exception e) {
269             Response response = Response.status(
270                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
271             throw new WebApplicationException(response);
272         }
273         return result.getBytes();
274     }
275
276     /**
277      * Delete dimension.
278      * 
279      * @param csid the csid
280      * 
281      * @return the response
282      */
283     @DELETE
284     @Path("{csid}")
285     public Response deleteDimension(@PathParam("csid") String csid) {
286
287         if (logger.isDebugEnabled()) {
288             logger.debug("deleteDimension with csid=" + csid);
289         }
290         if (csid == null || "".equals(csid)) {
291             logger.error("deleteDimension: missing csid!");
292             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
293                     "delete failed on Dimension csid=" + csid).type(
294                     "text/plain").build();
295             throw new WebApplicationException(response);
296         }
297         try {
298             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
299             getRepositoryClient(ctx).delete(ctx, csid);
300             return Response.status(HttpResponseCodes.SC_OK).build();
301         } catch (DocumentNotFoundException dnfe) {
302             if (logger.isDebugEnabled()) {
303                 logger.debug("caught exception in deleteDimension", dnfe);
304             }
305             Response response = Response.status(Response.Status.NOT_FOUND).entity(
306                     "Delete failed on Dimension csid=" + csid).type(
307                     "text/plain").build();
308             throw new WebApplicationException(response);
309         } catch (Exception e) {
310             Response response = Response.status(
311                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
312             throw new WebApplicationException(response);
313         }
314
315     }
316 }