]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6d5ce8108d0307ff785fd64b3e369913fc711b5c
[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.contact;
25
26 //import java.util.List;
27
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.POST;
32 import javax.ws.rs.PUT;
33 import javax.ws.rs.Path;
34 import javax.ws.rs.PathParam;
35 import javax.ws.rs.Produces;
36 //import javax.ws.rs.QueryParam;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MultivaluedMap;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriBuilder;
42 import javax.ws.rs.core.UriInfo;
43
44 import org.collectionspace.services.client.IQueryManager;
45 import org.collectionspace.services.client.PoxPayloadIn;
46 import org.collectionspace.services.client.PoxPayloadOut;
47 import org.collectionspace.services.common.vocabulary.AuthorityResource;
48 //import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
49 //import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
50 //import org.collectionspace.services.common.vocabulary.AuthorityResource.Specifier;
51 //import org.collectionspace.services.common.vocabulary.AuthorityResource.SpecifierForm;
52 //import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
53 //import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
54 //import org.collectionspace.services.common.ClientType;
55 //import org.collectionspace.services.common.ServiceMain;
56 //import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
57 //import org.collectionspace.services.common.authorityref.AuthorityRefList;
58 //import org.collectionspace.services.common.context.MultipartServiceContextImpl;
59 //import org.collectionspace.services.common.context.ServiceBindingUtils;
60 import org.collectionspace.services.common.context.ServiceContext;
61 import org.collectionspace.services.common.document.BadRequestException;
62 import org.collectionspace.services.common.document.DocumentFilter;
63 import org.collectionspace.services.common.document.DocumentHandler;
64 import org.collectionspace.services.common.document.DocumentNotFoundException;
65 //import org.collectionspace.services.common.document.DocumentWrapper;
66 import org.collectionspace.services.contact.ContactResource;
67 import org.collectionspace.services.contact.ContactsCommon;
68 import org.collectionspace.services.contact.ContactsCommonList;
69 import org.collectionspace.services.contact.ContactJAXBSchema;
70 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
71 //import org.collectionspace.services.common.repository.RepositoryClient;
72 import org.collectionspace.services.common.security.UnauthorizedException;
73 //import org.collectionspace.services.common.query.IQueryManager;
74 //import org.collectionspace.services.common.query.QueryManager;
75 //import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
76 //import org.jboss.remoting.samples.chat.exceptions.InvalidArgumentException;
77 import org.jboss.resteasy.util.HttpResponseCodes;
78 //import org.nuxeo.ecm.core.api.DocumentModel;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
81
82 /**
83  * The Class AuthorityResourceWithContacts.
84  */
85 @Consumes("application/xml")
86 @Produces("application/xml")
87 public abstract class AuthorityResourceWithContacts<AuthCommon, AuthCommonList, AuthItemCommonList, AuthItemHandler> extends //FIXME: REM - Why is this resource in this package instead of somewhere in 'common'?
88      AuthorityResource<AuthCommon, AuthCommonList, AuthItemCommonList, AuthItemHandler> {
89
90     /** The contact resource. */
91     private ContactResource contactResource = new ContactResource(); // Warning: ContactResource is a singleton.
92
93     final Logger logger = LoggerFactory.getLogger(AuthorityResourceWithContacts.class);
94
95     /**
96          * Instantiates a new Authority resource.
97          */
98         public AuthorityResourceWithContacts(
99                         Class<AuthCommon> authCommonClass, Class<?> resourceClass,
100                         String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
101                 super(authCommonClass, resourceClass,
102                         authorityCommonSchemaName, authorityItemCommonSchemaName);
103         }
104
105         public abstract String getItemServiceName();
106
107     /**
108      * Gets the contact service name.
109      * 
110      * @return the contact service name
111      */
112     public String getContactServiceName() {
113         return contactResource.getServiceName();
114     }
115
116     /**
117      * Creates the contact document handler.
118      * 
119      * @param ctx the ctx
120      * @param inAuthority the in authority
121      * @param inItem the in item
122      * 
123      * @return the document handler
124      * 
125      * @throws Exception the exception
126      */
127     private DocumentHandler createContactDocumentHandler(
128                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
129             String inItem) throws Exception {
130         
131         ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(
132                         ctx,
133                         ctx.getCommonPartLabel(getContactServiceName()),
134                         ContactsCommon.class);          
135         docHandler.setInAuthority(inAuthority);
136         docHandler.setInItem(inItem);
137         
138         return docHandler;
139     }
140
141     /*************************************************************************
142      * Contact parts - this is a sub-resource of the AuthorityItem
143          * @param parentspecifier either a CSID or one of the urn forms
144          * @param itemspecifier either a CSID or one of the urn forms
145      * @param input 
146      * @return contact
147      *************************************************************************/
148     @POST
149     @Path("{parentcsid}/items/{itemcsid}/contacts")
150     public Response createContact(
151             @PathParam("parentcsid") String parentspecifier,
152             @PathParam("itemcsid") String itemspecifier,
153             String xmlPayload) {
154         try {
155                 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
156                         Specifier parentSpec = getSpecifier(parentspecifier, 
157                                         "createContact(authority)", "CREATE_ITEM_CONTACT");
158                         Specifier itemSpec = getSpecifier(itemspecifier, 
159                                         "createContact(item)", "CREATE_ITEM_CONTACT");
160                         // Note that we have to create the service context for the Items, not the main service
161             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
162                         String parentcsid;
163                         if(parentSpec.form==SpecifierForm.CSID) {
164                                 parentcsid = parentSpec.value;
165                         } else {
166                                 String whereClause = buildWhereForAuthByName(parentSpec.value);
167                     ctx = createServiceContext(getServiceName());
168                                 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
169                         }
170             String itemcsid;
171                         if(itemSpec.form==SpecifierForm.CSID) {
172                                 itemcsid = itemSpec.value;
173                         } else {
174                                 String itemWhereClause = 
175                                         buildWhereForAuthItemByName(itemSpec.value, parentcsid);
176                     ctx = createServiceContext(getItemServiceName());
177                                 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
178                         }
179             // Note that we have to create the service context and document
180             // handler for the Contact service, not the main service.
181                 ctx = createServiceContext(getContactServiceName(), input);
182             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
183             String csid = getRepositoryClient(ctx).create(ctx, handler);
184             UriBuilder path = UriBuilder.fromResource(resourceClass);
185             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
186             Response response = Response.created(path.build()).build();
187             return response;
188         } catch (BadRequestException bre) {
189             Response response = Response.status(
190                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
191             throw new WebApplicationException(response);
192         } catch (UnauthorizedException ue) {
193             Response response = Response.status(
194                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
195             throw new WebApplicationException(response);
196         } catch (DocumentNotFoundException dnfe) {
197             if (logger.isDebugEnabled()) {
198                 logger.debug("createContact", dnfe);
199             }
200             Response response = Response.status(Response.Status.NOT_FOUND)
201                 .entity("Create Contact failed; one of the requested specifiers for authority:"
202                                 +parentspecifier+": and item:"+itemspecifier+": was not found.")
203                 .type("text/plain").build();
204             throw new WebApplicationException(response);
205         } catch (Exception e) {
206             if (logger.isDebugEnabled()) {
207                 logger.debug("Caught exception in createContact", e);
208             }
209             Response response = Response.status(
210                 Response.Status.INTERNAL_SERVER_ERROR)
211                 .entity("Attempt to create Contact failed.")
212                 .type("text/plain").build();
213             throw new WebApplicationException(response);
214         }
215
216     }
217
218     /**
219      * Gets the contact list.
220      * 
221          * @param parentspecifier either a CSID or one of the urn forms
222          * @param itemspecifier either a CSID or one of the urn forms
223      * @param ui the ui
224      * 
225      * @return the contact list
226      */
227     @GET
228     @Produces({"application/xml"})
229     @Path("{parentcsid}/items/{itemcsid}/contacts/")
230     public ContactsCommonList getContactList(
231             @PathParam("parentcsid") String parentspecifier,
232             @PathParam("itemcsid") String itemspecifier,
233             @Context UriInfo ui) {
234         ContactsCommonList contactObjectList = new ContactsCommonList();
235         try {
236                         Specifier parentSpec = getSpecifier(parentspecifier, 
237                                         "getContactList(parent)", "GET_CONTACT_LIST");
238                         Specifier itemSpec = getSpecifier(itemspecifier, 
239                                         "getContactList(item)", "GET_CONTACT_LIST");
240                         // Note that we have to create the service context for the Items, not the main service
241             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
242                         String parentcsid;
243                         if(parentSpec.form==SpecifierForm.CSID) {
244                                 parentcsid = parentSpec.value;
245                         } else {
246                                 String whereClause = buildWhereForAuthByName(parentSpec.value);
247                     ctx = createServiceContext(getServiceName());
248                                 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
249                         }
250             String itemcsid;
251                         if(itemSpec.form==SpecifierForm.CSID) {
252                                 itemcsid = itemSpec.value;
253                         } else {
254                                 String itemWhereClause = 
255                                         buildWhereForAuthItemByName(itemSpec.value, parentcsid);
256                     ctx = createServiceContext(getItemServiceName());
257                                 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
258                         }
259             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
260                 ctx = createServiceContext(getContactServiceName(), queryParams);
261             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
262             DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
263             myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
264                 ContactJAXBSchema.IN_AUTHORITY +
265                 "='" + parentcsid + "'" +
266                 IQueryManager.SEARCH_QUALIFIER_AND +
267                 ContactJAXBSchema.CONTACTS_COMMON + ":" +
268                 ContactJAXBSchema.IN_ITEM +
269                 "='" + itemcsid + "'" );
270             getRepositoryClient(ctx).getFiltered(ctx, handler);
271             contactObjectList = (ContactsCommonList) handler.getCommonPartList();
272         } catch (UnauthorizedException ue) {
273             Response response = Response.status(
274                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
275             throw new WebApplicationException(response);
276         } catch (DocumentNotFoundException dnfe) {
277             if (logger.isDebugEnabled()) {
278                 logger.debug("getContactList", dnfe);
279             }
280             Response response = Response.status(Response.Status.NOT_FOUND)
281                 .entity("Get ContactList failed; one of the requested specifiers for authority:"
282                                 +parentspecifier+": and item:"+itemspecifier+": was not found.")
283                 .type("text/plain").build();
284             throw new WebApplicationException(response);
285         } catch (Exception e) {
286             if (logger.isDebugEnabled()) {
287                 logger.debug("Caught exception in getContactsList", e);
288             }
289             Response response = Response.status(
290                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
291             throw new WebApplicationException(response);
292         }
293         return contactObjectList;
294     }
295
296     /**
297      * Gets the contact.
298      * 
299          * @param parentspecifier either a CSID or one of the urn forms
300          * @param itemspecifier either a CSID or one of the urn forms
301      * @param csid the csid
302      * 
303      * @return the contact
304      */
305     @GET
306     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
307     public String getContact(
308             @PathParam("parentcsid") String parentspecifier,
309             @PathParam("itemcsid") String itemspecifier,
310             @PathParam("csid") String csid) {
311         PoxPayloadOut result = null;
312         try {
313                         Specifier parentSpec = getSpecifier(parentspecifier, 
314                                         "getContact(parent)", "GET_ITEM_CONTACT");
315                         Specifier itemSpec = getSpecifier(itemspecifier, 
316                                         "getContact(item)", "GET_ITEM_CONTACT");
317                         // Note that we have to create the service context for the Items, not the main service
318             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
319                         String parentcsid;
320                         if(parentSpec.form==SpecifierForm.CSID) {
321                                 parentcsid = parentSpec.value;
322                         } else {
323                                 String whereClause = buildWhereForAuthByName(parentSpec.value);
324                     ctx = createServiceContext(getServiceName());
325                                 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
326                         }
327             String itemcsid;
328                         if(itemSpec.form==SpecifierForm.CSID) {
329                                 itemcsid = itemSpec.value;
330                         } else {
331                                 String itemWhereClause = 
332                                         buildWhereForAuthItemByName(itemSpec.value, parentcsid);
333                     ctx = createServiceContext(getItemServiceName());
334                                 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
335                         }
336             // Note that we have to create the service context and document
337             // handler for the Contact service, not the main service.
338                 ctx = createServiceContext(getContactServiceName());
339             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
340             getRepositoryClient(ctx).get(ctx, csid, handler);
341             result = ctx.getOutput();
342         } catch (UnauthorizedException ue) {
343             Response response = Response.status(
344                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
345             throw new WebApplicationException(response);
346         } catch (DocumentNotFoundException dnfe) {
347             if (logger.isDebugEnabled()) {
348                 logger.debug("getContact", dnfe);
349             }
350             Response response = Response.status(Response.Status.NOT_FOUND)
351                 .entity("Get failed, the requested Contact CSID:" + csid + 
352                                 ": or one of the specifiers for authority:"+parentspecifier+
353                                 ": and item:"+itemspecifier+": was not found.")
354                 .type("text/plain").build();
355             throw new WebApplicationException(response);
356         } catch (Exception e) {
357             if (logger.isDebugEnabled()) {
358                 logger.debug("getContact", e);
359             }
360             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
361                 .entity("Get contact failed")
362                 .type("text/plain").build();
363             throw new WebApplicationException(response);
364         }
365         if (result == null) {
366             Response response = Response.status(Response.Status.NOT_FOUND)
367                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
368                 .type("text/plain").build();
369             throw new WebApplicationException(response);
370         }
371         return result.toXML();
372
373     }
374
375     /**
376      * Update contact.
377      * 
378          * @param parentspecifier either a CSID or one of the urn forms
379          * @param itemspecifier either a CSID or one of the urn forms
380      * @param csid the csid
381      * @param theUpdate the the update
382      * 
383      * @return the multipart output
384      */
385     @PUT
386     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
387     public String updateContact(
388             @PathParam("parentcsid") String parentspecifier,
389             @PathParam("itemcsid") String itemspecifier,
390             @PathParam("csid") String csid,
391             String xmlPayload) {
392         PoxPayloadOut result = null;
393         try {
394                 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
395                         Specifier parentSpec = getSpecifier(parentspecifier, 
396                                         "updateContact(authority)", "UPDATE_CONTACT");
397                         Specifier itemSpec = getSpecifier(itemspecifier, 
398                                         "updateContact(item)", "UPDATE_CONTACT");
399                         // Note that we have to create the service context for the Items, not the main service
400             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
401                         String parentcsid;
402                         if(parentSpec.form==SpecifierForm.CSID) {
403                                 parentcsid = parentSpec.value;
404                         } else {
405                                 String whereClause = buildWhereForAuthByName(parentSpec.value);
406                     ctx = createServiceContext(getServiceName());
407                                 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
408                         }
409             String itemcsid;
410                         if(itemSpec.form==SpecifierForm.CSID) {
411                                 itemcsid = itemSpec.value;
412                         } else {
413                                 String itemWhereClause = 
414                                         buildWhereForAuthItemByName(itemSpec.value, parentcsid);
415                     ctx = createServiceContext(getItemServiceName());
416                                 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
417                         }
418             // Note that we have to create the service context and document
419             // handler for the Contact service, not the main service.
420                 ctx = createServiceContext(getContactServiceName(), theUpdate);
421             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
422             getRepositoryClient(ctx).update(ctx, csid, handler);
423             result = ctx.getOutput();
424         } catch (BadRequestException bre) {
425             Response response = Response.status(
426                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
427             throw new WebApplicationException(response);
428         } catch (UnauthorizedException ue) {
429             Response response = Response.status(
430                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
431             throw new WebApplicationException(response);
432         } catch (DocumentNotFoundException dnfe) {
433             if (logger.isDebugEnabled()) {
434                 logger.debug("caught exception in updateContact", dnfe);
435             }
436             Response response = Response.status(Response.Status.NOT_FOUND)
437                     .entity("Update failed, the requested Contact CSID:" + csid + 
438                                 ": or one of the specifiers for authority:"+parentspecifier+
439                                 ": and item:"+itemspecifier+": was not found.")
440                     .type("text/plain").build();
441             throw new WebApplicationException(response);
442         } catch (Exception e) {
443             Response response = Response.status(
444                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
445             throw new WebApplicationException(response);
446         }
447         return result.toXML();
448     }
449
450     /**
451      * Delete contact.
452      * 
453          * @param parentspecifier either a CSID or one of the urn forms
454          * @param itemspecifier either a CSID or one of the urn forms
455      * @param csid the csid
456      * 
457      * @return the response
458      */
459     @DELETE
460     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
461     public Response deleteContact(
462             @PathParam("parentcsid") String parentspecifier,
463             @PathParam("itemcsid") String itemspecifier,
464             @PathParam("csid") String csid) {
465         try {
466                         Specifier parentSpec = getSpecifier(parentspecifier, 
467                                         "deleteContact(authority)", "DELETE_CONTACT");
468                         Specifier itemSpec = getSpecifier(itemspecifier, 
469                                         "deleteContact(item)", "DELETE_CONTACT");
470                         // Note that we have to create the service context for the Items, not the main service
471             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
472                         String parentcsid;
473                         if(parentSpec.form==SpecifierForm.CSID) {
474                                 parentcsid = parentSpec.value;
475                         } else {
476                                 String whereClause = buildWhereForAuthByName(parentSpec.value);
477                     ctx = createServiceContext(getServiceName());
478                                 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
479                         }
480             String itemcsid;
481                         if(itemSpec.form==SpecifierForm.CSID) {
482                                 itemcsid = itemSpec.value;
483                         } else {
484                                 String itemWhereClause = 
485                                         buildWhereForAuthItemByName(itemSpec.value, parentcsid);
486                     ctx = createServiceContext(getItemServiceName());
487                                 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
488                         }
489             // Note that we have to create the service context for the
490             // Contact service, not the main service.
491                 ctx = createServiceContext(getContactServiceName());
492             getRepositoryClient(ctx).delete(ctx, csid);
493             return Response.status(HttpResponseCodes.SC_OK).build();
494          } catch (UnauthorizedException ue) {
495             Response response = Response.status(
496                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
497             throw new WebApplicationException(response);
498          } catch (DocumentNotFoundException dnfe) {
499             if (logger.isDebugEnabled()) {
500                 logger.debug("Caught exception in deleteContact", dnfe);
501             }
502             Response response = Response.status(Response.Status.NOT_FOUND)
503                 .entity("Delete failed, the requested Contact CSID:" + csid + 
504                                 ": or one of the specifiers for authority:"+parentspecifier+
505                                 ": and item:"+itemspecifier+": was not found.")
506                 .type("text/plain").build();
507             throw new WebApplicationException(response);
508        } catch (Exception e) {
509             Response response = Response.status(
510                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
511             throw new WebApplicationException(response);
512         }
513     }
514     
515 }