]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
45f9f9c23e2271cb9d53b208cf89015446368d7e
[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.person;
25
26 import javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.QueryParam;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.UriBuilder;
40 import javax.ws.rs.core.UriInfo;
41
42 import org.collectionspace.services.PersonJAXBSchema;
43 import org.collectionspace.services.common.AbstractCollectionSpaceResource;
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.DocumentFilter;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.common.document.DocumentNotFoundException;
52 import org.collectionspace.services.common.security.UnauthorizedException;
53 import org.collectionspace.services.common.query.IQueryManager;
54 import org.collectionspace.services.person.nuxeo.PersonDocumentModelHandler;
55 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
56 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
57 import org.jboss.resteasy.util.HttpResponseCodes;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 @Path("/personauthorities")
62 @Consumes("multipart/mixed")
63 @Produces("multipart/mixed")
64 public class PersonAuthorityResource extends AbstractCollectionSpaceResource {
65
66     private final static String personAuthorityServiceName = "personauthorities";
67     private final static String personServiceName = "persons";
68     final Logger logger = LoggerFactory.getLogger(PersonAuthorityResource.class);
69     //FIXME retrieve client type from configuration
70     final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
71
72     public PersonAuthorityResource() {
73         // do nothing
74     }
75
76     @Override
77     protected String getVersionString() {
78         /** The last change revision. */
79         final String lastChangeRevision = "$LastChangedRevision$";
80         return lastChangeRevision;
81     }
82     
83     @Override
84     public String getServiceName() {
85         return personAuthorityServiceName;
86     }
87
88     public String getItemServiceName() {
89         return personServiceName;
90     }
91
92     /*
93     public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
94     RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
95     ctx.setInput(input);
96     return ctx;
97     }
98      */
99     @Override
100     public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
101         DocumentHandler docHandler = ctx.getDocumentHandler();
102         if (ctx.getInput() != null) {
103             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), PersonauthoritiesCommon.class);
104             if (obj != null) {
105                 docHandler.setCommonPart((PersonauthoritiesCommon) obj);
106             }
107         }
108         return docHandler;
109     }
110
111     private DocumentHandler createItemDocumentHandler(
112             ServiceContext ctx,
113             String inAuthority) throws Exception {
114         DocumentHandler docHandler = ctx.getDocumentHandler();
115         ((PersonDocumentModelHandler) docHandler).setInAuthority(inAuthority);
116         if (ctx.getInput() != null) {
117             Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
118                     PersonsCommon.class);
119             if (obj != null) {
120                 docHandler.setCommonPart((PersonsCommon) obj);
121             }
122         }
123         return docHandler;
124     }
125
126     @POST
127     public Response createPersonAuthority(MultipartInput input) {
128         try {
129             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getServiceName());
130             DocumentHandler handler = createDocumentHandler(ctx);
131             String csid = getRepositoryClient(ctx).create(ctx, handler);
132             //personAuthorityObject.setCsid(csid);
133             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
134             path.path("" + csid);
135             Response response = Response.created(path.build()).build();
136             return response;
137         } catch (UnauthorizedException ue) {
138             Response response = Response.status(
139                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
140             throw new WebApplicationException(response);
141         } catch (Exception e) {
142             if (logger.isDebugEnabled()) {
143                 logger.debug("Caught exception in createPersonAuthority", 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     @GET
152     @Path("{csid}")
153     public MultipartOutput getPersonAuthority(@PathParam("csid") String csid) {
154         String idValue = null;
155         if (csid == null) {
156             logger.error("getPersonAuthority: missing csid!");
157             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
158                     "get failed on PersonAuthority csid=" + csid).type(
159                     "text/plain").build();
160             throw new WebApplicationException(response);
161         }
162         if (logger.isDebugEnabled()) {
163             logger.debug("getPersonAuthority with path(id)=" + csid);
164         }
165         MultipartOutput result = null;
166         try {
167             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
168             DocumentHandler handler = createDocumentHandler(ctx);
169             getRepositoryClient(ctx).get(ctx, csid, handler);
170             result = (MultipartOutput) ctx.getOutput();
171         } catch (UnauthorizedException ue) {
172             Response response = Response.status(
173                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
174             throw new WebApplicationException(response);
175         } catch (DocumentNotFoundException dnfe) {
176             if (logger.isDebugEnabled()) {
177                 logger.debug("getPersonAuthority", dnfe);
178             }
179             Response response = Response.status(Response.Status.NOT_FOUND).entity(
180                     "Get failed on PersonAuthority csid=" + csid).type(
181                     "text/plain").build();
182             throw new WebApplicationException(response);
183         } catch (Exception e) {
184             if (logger.isDebugEnabled()) {
185                 logger.debug("getPersonAuthority", 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 PersonAuthority CSID:" + csid + ": was not found.").type(
194                     "text/plain").build();
195             throw new WebApplicationException(response);
196         }
197         return result;
198     }
199
200     @GET
201     @Produces("application/xml")
202     public PersonauthoritiesCommonList getPersonAuthorityList(@Context UriInfo ui) {
203         PersonauthoritiesCommonList personAuthorityObjectList = new PersonauthoritiesCommonList();
204         try {
205             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
206             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
207             DocumentHandler handler = createDocumentHandler(ctx);
208             DocumentFilter myFilter = new DocumentFilter();
209             myFilter.setPagination(queryParams);
210             String nameQ = queryParams.getFirst("refName");
211             if (nameQ != null) {
212                 myFilter.setWhereClause("personauthorities_common:refName='" + nameQ + "'");
213             }
214             handler.setDocumentFilter(myFilter);
215             getRepositoryClient(ctx).getFiltered(ctx, handler);
216             personAuthorityObjectList = (PersonauthoritiesCommonList) handler.getCommonPartList();
217         } catch (UnauthorizedException ue) {
218             Response response = Response.status(
219                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
220             throw new WebApplicationException(response);
221         } catch (Exception e) {
222             if (logger.isDebugEnabled()) {
223                 logger.debug("Caught exception in getPersonAuthorityList", e);
224             }
225             Response response = Response.status(
226                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
227             throw new WebApplicationException(response);
228         }
229         return personAuthorityObjectList;
230     }
231
232     @PUT
233     @Path("{csid}")
234     public MultipartOutput updatePersonAuthority(
235             @PathParam("csid") String csid,
236             MultipartInput theUpdate) {
237         if (logger.isDebugEnabled()) {
238             logger.debug("updatePersonAuthority with csid=" + csid);
239         }
240         if (csid == null || "".equals(csid)) {
241             logger.error("updatePersonAuthority: missing csid!");
242             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
243                     "update failed on PersonAuthority csid=" + csid).type(
244                     "text/plain").build();
245             throw new WebApplicationException(response);
246         }
247         MultipartOutput result = null;
248         try {
249             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getServiceName());
250             DocumentHandler handler = createDocumentHandler(ctx);
251             getRepositoryClient(ctx).update(ctx, csid, handler);
252             result = (MultipartOutput) ctx.getOutput();
253         } catch (UnauthorizedException ue) {
254             Response response = Response.status(
255                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
256             throw new WebApplicationException(response);
257         } catch (DocumentNotFoundException dnfe) {
258             if (logger.isDebugEnabled()) {
259                 logger.debug("caugth exception in updatePersonAuthority", dnfe);
260             }
261             Response response = Response.status(Response.Status.NOT_FOUND).entity(
262                     "Update failed on PersonAuthority 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     @DELETE
274     @Path("{csid}")
275     public Response deletePersonAuthority(@PathParam("csid") String csid) {
276
277         if (logger.isDebugEnabled()) {
278             logger.debug("deletePersonAuthority with csid=" + csid);
279         }
280         if (csid == null || "".equals(csid)) {
281             logger.error("deletePersonAuthority: missing csid!");
282             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
283                     "delete failed on PersonAuthority csid=" + csid).type(
284                     "text/plain").build();
285             throw new WebApplicationException(response);
286         }
287         try {
288             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getServiceName());
289             getRepositoryClient(ctx).delete(ctx, csid);
290             return Response.status(HttpResponseCodes.SC_OK).build();
291         } catch (UnauthorizedException ue) {
292             Response response = Response.status(
293                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
294             throw new WebApplicationException(response);
295         } catch (DocumentNotFoundException dnfe) {
296             if (logger.isDebugEnabled()) {
297                 logger.debug("caught exception in deletePersonAuthority", dnfe);
298             }
299             Response response = Response.status(Response.Status.NOT_FOUND).entity(
300                     "Delete failed on PersonAuthority csid=" + csid).type(
301                     "text/plain").build();
302             throw new WebApplicationException(response);
303         } catch (Exception e) {
304             Response response = Response.status(
305                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
306             throw new WebApplicationException(response);
307         }
308
309     }
310
311     /*************************************************************************
312      * Person parts - this is a sub-resource of PersonAuthority
313      *************************************************************************/
314     @POST
315     @Path("{csid}/items")
316     public Response createPerson(@PathParam("csid") String parentcsid, MultipartInput input) {
317         try {
318             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(input, getItemServiceName());
319             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
320             String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
321             UriBuilder path = UriBuilder.fromResource(PersonAuthorityResource.class);
322             path.path(parentcsid + "/items/" + itemcsid);
323             Response response = Response.created(path.build()).build();
324             return response;
325         } catch (UnauthorizedException ue) {
326             Response response = Response.status(
327                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
328             throw new WebApplicationException(response);
329         } catch (Exception e) {
330             if (logger.isDebugEnabled()) {
331                 logger.debug("Caught exception in createPerson", e);
332             }
333             Response response = Response.status(
334                     Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
335             throw new WebApplicationException(response);
336         }
337     }
338
339     @GET
340     @Path("{csid}/items/{itemcsid}")
341     public MultipartOutput getPerson(
342             @PathParam("csid") String parentcsid,
343             @PathParam("itemcsid") String itemcsid) {
344         if (logger.isDebugEnabled()) {
345             logger.debug("getPerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
346         }
347         if (parentcsid == null || "".equals(parentcsid)) {
348             logger.error("getPerson: missing csid!");
349             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
350                     "get failed on Person csid=" + parentcsid).type(
351                     "text/plain").build();
352             throw new WebApplicationException(response);
353         }
354         if (itemcsid == null || "".equals(itemcsid)) {
355             logger.error("getPerson: missing itemcsid!");
356             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
357                     "get failed on Person itemcsid=" + itemcsid).type(
358                     "text/plain").build();
359             throw new WebApplicationException(response);
360         }
361         MultipartOutput result = null;
362         try {
363             // Note that we have to create the service context for the Items, not the main service
364             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
365             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
366             getRepositoryClient(ctx).get(ctx, itemcsid, handler);
367             // TODO should we assert that the item is in the passed personAuthority?
368             result = (MultipartOutput) ctx.getOutput();
369         } catch (UnauthorizedException ue) {
370             Response response = Response.status(
371                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
372             throw new WebApplicationException(response);
373         } catch (DocumentNotFoundException dnfe) {
374             if (logger.isDebugEnabled()) {
375                 logger.debug("getPerson", dnfe);
376             }
377             Response response = Response.status(Response.Status.NOT_FOUND).entity(
378                     "Get failed on Person csid=" + itemcsid).type(
379                     "text/plain").build();
380             throw new WebApplicationException(response);
381         } catch (Exception e) {
382             if (logger.isDebugEnabled()) {
383                 logger.debug("getPerson", e);
384             }
385             Response response = Response.status(
386                     Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
387             throw new WebApplicationException(response);
388         }
389         if (result == null) {
390             Response response = Response.status(Response.Status.NOT_FOUND).entity(
391                     "Get failed, the requested Person CSID:" + itemcsid + ": was not found.").type(
392                     "text/plain").build();
393             throw new WebApplicationException(response);
394         }
395         return result;
396     }
397
398     @GET
399     @Path("{csid}/items")
400     @Produces("application/xml")
401     public PersonsCommonList getPersonList(
402             @PathParam("csid") String parentcsid,
403             @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,            
404             @Context UriInfo ui) {
405         PersonsCommonList personObjectList = new PersonsCommonList();
406         try {
407             // Note that docType defaults to the ServiceName, so we're fine with that.
408             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
409             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
410             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
411             DocumentFilter myFilter = new DocumentFilter();
412             myFilter.setPagination(queryParams);
413
414             // Add the where clause "persons_common:inAuthority='" + parentcsid + "'"
415             myFilter.setWhereClause(PersonJAXBSchema.PERSONS_COMMON + ":" +
416                         PersonJAXBSchema.IN_AUTHORITY +
417                         "='" + parentcsid + "'" + "AND ecm:isProxy = 0");
418             
419             // AND persons_common:displayName LIKE '%partialTerm%'
420             if (partialTerm != null && !partialTerm.isEmpty()) {
421                 String ptClause = "AND " +
422                 PersonJAXBSchema.PERSONS_COMMON + ":" +
423                         PersonJAXBSchema.DISPLAY_NAME +
424                         " LIKE " +
425                         "'%" + partialTerm + "%'";
426                 myFilter.appendWhereClause(ptClause);
427             }
428             
429             handler.setDocumentFilter(myFilter);
430             getRepositoryClient(ctx).getFiltered(ctx, handler);
431             personObjectList = (PersonsCommonList) handler.getCommonPartList();
432         } catch (UnauthorizedException ue) {
433             Response response = Response.status(
434                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
435             throw new WebApplicationException(response);
436         } catch (Exception e) {
437             if (logger.isDebugEnabled()) {
438                 logger.debug("Caught exception in getPersonList", e);
439             }
440             Response response = Response.status(
441                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
442             throw new WebApplicationException(response);
443         }
444         return personObjectList;
445     }
446
447     @PUT
448     @Path("{csid}/items/{itemcsid}")
449     public MultipartOutput updatePerson(
450             @PathParam("csid") String parentcsid,
451             @PathParam("itemcsid") String itemcsid,
452             MultipartInput theUpdate) {
453         if (logger.isDebugEnabled()) {
454             logger.debug("updatePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
455         }
456         if (parentcsid == null || "".equals(parentcsid)) {
457             logger.error("updatePerson: missing csid!");
458             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
459                     "update failed on Person parentcsid=" + parentcsid).type(
460                     "text/plain").build();
461             throw new WebApplicationException(response);
462         }
463         if (itemcsid == null || "".equals(itemcsid)) {
464             logger.error("updatePerson: missing itemcsid!");
465             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
466                     "update failed on Person=" + itemcsid).type(
467                     "text/plain").build();
468             throw new WebApplicationException(response);
469         }
470         MultipartOutput result = null;
471         try {
472             // Note that we have to create the service context for the Items, not the main service
473             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(theUpdate, getItemServiceName());
474             DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
475             getRepositoryClient(ctx).update(ctx, itemcsid, handler);
476             result = (MultipartOutput) ctx.getOutput();
477         } catch (UnauthorizedException ue) {
478             Response response = Response.status(
479                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
480             throw new WebApplicationException(response);
481         } catch (DocumentNotFoundException dnfe) {
482             if (logger.isDebugEnabled()) {
483                 logger.debug("caugth exception in updatePerson", dnfe);
484             }
485             Response response = Response.status(Response.Status.NOT_FOUND).entity(
486                     "Update failed on Person csid=" + itemcsid).type(
487                     "text/plain").build();
488             throw new WebApplicationException(response);
489         } catch (Exception e) {
490             Response response = Response.status(
491                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
492             throw new WebApplicationException(response);
493         }
494         return result;
495     }
496
497     @DELETE
498     @Path("{csid}/items/{itemcsid}")
499     public Response deletePerson(
500             @PathParam("csid") String parentcsid,
501             @PathParam("itemcsid") String itemcsid) {
502         if (logger.isDebugEnabled()) {
503             logger.debug("deletePerson with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
504         }
505         if (parentcsid == null || "".equals(parentcsid)) {
506             logger.error("deletePerson: missing csid!");
507             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
508                     "delete failed on Person parentcsid=" + parentcsid).type(
509                     "text/plain").build();
510             throw new WebApplicationException(response);
511         }
512         if (itemcsid == null || "".equals(itemcsid)) {
513             logger.error("deletePerson: missing itemcsid!");
514             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
515                     "delete failed on Person=" + itemcsid).type(
516                     "text/plain").build();
517             throw new WebApplicationException(response);
518         }
519         try {
520             // Note that we have to create the service context for the Items, not the main service
521             ServiceContext ctx = MultipartServiceContextFactory.get().createServiceContext(null, getItemServiceName());
522             getRepositoryClient(ctx).delete(ctx, itemcsid);
523             return Response.status(HttpResponseCodes.SC_OK).build();
524         } catch (UnauthorizedException ue) {
525             Response response = Response.status(
526                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
527             throw new WebApplicationException(response);
528         } catch (DocumentNotFoundException dnfe) {
529             if (logger.isDebugEnabled()) {
530                 logger.debug("caught exception in deletePerson", dnfe);
531             }
532             Response response = Response.status(Response.Status.NOT_FOUND).entity(
533                     "Delete failed on Person itemcsid=" + itemcsid).type(
534                     "text/plain").build();
535             throw new WebApplicationException(response);
536         } catch (Exception e) {
537             Response response = Response.status(
538                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
539             throw new WebApplicationException(response);
540         }
541
542     }
543 }