]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
41c8ad9c38db9bd996469abff40f314cf4d068f6
[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.organization;
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.WebApplicationException;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.UriBuilder;
41 import javax.ws.rs.core.UriInfo;
42
43 import org.collectionspace.services.common.vocabulary.AuthorityResource;
44 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
45 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
46 import org.collectionspace.services.common.authorityref.AuthorityRefList;
47 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
48 import org.collectionspace.services.common.context.ServiceBindingUtils;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.BadRequestException;
51 import org.collectionspace.services.common.document.DocumentFilter;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.document.DocumentNotFoundException;
54 import org.collectionspace.services.common.document.DocumentWrapper;
55 import org.collectionspace.services.common.query.IQueryManager;
56 import org.collectionspace.services.common.repository.RepositoryClient;
57 import org.collectionspace.services.contact.ContactResource;
58 import org.collectionspace.services.contact.ContactsCommon;
59 import org.collectionspace.services.contact.ContactsCommonList;
60 import org.collectionspace.services.contact.ContactJAXBSchema;
61 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
62 import org.collectionspace.services.common.security.UnauthorizedException;
63 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
64 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
65 import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler;
66 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
67 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
68 import org.jboss.resteasy.util.HttpResponseCodes;
69 import org.nuxeo.ecm.core.api.DocumentModel;
70 import org.slf4j.Logger;
71 import org.slf4j.LoggerFactory;
72
73 /**
74  * The Class OrgAuthorityResource.
75  */
76 @Path("/orgauthorities")
77 @Consumes("multipart/mixed")
78 @Produces("multipart/mixed")
79 public class OrgAuthorityResource extends
80         AuthorityResource<OrgauthoritiesCommon, OrgauthoritiesCommonList, OrganizationsCommon,
81         OrganizationDocumentModelHandler> {
82
83     /** The Constant orgAuthorityServiceName. */
84     private final static String orgAuthorityServiceName = "orgauthorities";
85         private final static String ORGAUTHORITIES_COMMON = "orgauthorities_common";
86     
87     /** The Constant organizationServiceName. */
88     private final static String organizationServiceName = "organizations";
89         private final static String ORGANIZATIONS_COMMON = "organizations_common";
90     
91     /** The logger. */
92     final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
93     //FIXME retrieve client type from configuration
94     /** The Constant CLIENT_TYPE. */
95     //final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
96     
97     /** The contact resource. */
98     private ContactResource contactResource = new ContactResource();
99
100     /**
101      * Instantiates a new org authority resource.
102      */
103     public OrgAuthorityResource() {
104                 super(OrgauthoritiesCommon.class, OrgAuthorityResource.class,
105                                 ORGAUTHORITIES_COMMON, ORGANIZATIONS_COMMON);
106     }
107
108     /* (non-Javadoc)
109      * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
110      */
111     @Override
112     public String getServiceName() {
113         return orgAuthorityServiceName;
114     }
115     
116     /**
117      * Gets the item service name.
118      * 
119      * @return the item service name
120      */
121     public String getItemServiceName() {
122         return organizationServiceName;
123     }
124
125     /* (non-Javadoc)
126      * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
127      */
128     @Override
129     public Class<OrgauthoritiesCommon> getCommonPartClass() {
130         return OrgauthoritiesCommon.class;
131     }    
132
133     /**
134      * Gets the contact service name.
135      * 
136      * @return the contact service name
137      */
138     public String getContactServiceName() {
139         return contactResource.getServiceName();
140     }
141
142     /**
143      * Creates the contact document handler.
144      * 
145      * @param ctx the ctx
146      * @param inAuthority the in authority
147      * @param inItem the in item
148      * 
149      * @return the document handler
150      * 
151      * @throws Exception the exception
152      */
153     private DocumentHandler createContactDocumentHandler(
154                 ServiceContext<MultipartInput, MultipartOutput> ctx, String inAuthority,
155             String inItem) throws Exception {
156         
157         ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(
158                         ctx,
159                         ctx.getCommonPartLabel(getContactServiceName()),
160                         ContactsCommon.class);          
161         docHandler.setInAuthority(inAuthority);
162         docHandler.setInItem(inItem);
163         
164         return docHandler;
165     }
166
167     /*************************************************************************
168      * Contact parts - this is a sub-resource of Organization (or "item")
169      * @param parentcsid 
170      * @param itemcsid 
171      * @param input 
172      * @return contact
173      *************************************************************************/
174     @POST
175     @Path("{parentcsid}/items/{itemcsid}/contacts")
176     public Response createContact(
177             @PathParam("parentcsid") String parentcsid,
178             @PathParam("itemcsid") String itemcsid,
179             MultipartInput input) {
180         try {
181             // Note that we have to create the service context and document
182             // handler for the Contact service, not the main service.
183                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
184             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
185             String csid = getRepositoryClient(ctx).create(ctx, handler);
186             UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
187             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
188             Response response = Response.created(path.build()).build();
189             return response;
190         } catch (BadRequestException bre) {
191             Response response = Response.status(
192                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
193             throw new WebApplicationException(response);
194         } catch (UnauthorizedException ue) {
195             Response response = Response.status(
196                     Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
197             throw new WebApplicationException(response);
198         } catch (Exception e) {
199             if (logger.isDebugEnabled()) {
200                 logger.debug("Caught exception in createContact", e);
201             }
202             Response response = Response.status(
203                 Response.Status.INTERNAL_SERVER_ERROR)
204                 .entity("Attempt to create Contact failed.")
205                 .type("text/plain").build();
206             throw new WebApplicationException(response);
207         }
208
209     }
210
211     /**
212      * Gets the contact list.
213      * 
214      * @param parentcsid the parentcsid
215      * @param itemcsid the itemcsid
216      * @param ui the ui
217      * 
218      * @return the contact list
219      */
220     @GET
221     @Produces({"application/xml"})
222     @Path("{parentcsid}/items/{itemcsid}/contacts/")
223     public ContactsCommonList getContactList(
224             @PathParam("parentcsid") String parentcsid,
225             @PathParam("itemcsid") String itemcsid,
226             @Context UriInfo ui) {
227         ContactsCommonList contactObjectList = new ContactsCommonList();
228         try {
229             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
230                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
231                                 queryParams);
232             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
233             DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
234             myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
235                 ContactJAXBSchema.IN_AUTHORITY +
236                 "='" + parentcsid + "'" +
237                 IQueryManager.SEARCH_QUALIFIER_AND +
238                 ContactJAXBSchema.CONTACTS_COMMON + ":" +
239                 ContactJAXBSchema.IN_ITEM +
240                 "='" + itemcsid + "'" +
241                 IQueryManager.SEARCH_QUALIFIER_AND +
242                 "ecm:isProxy = 0");
243             getRepositoryClient(ctx).getFiltered(ctx, handler);
244             contactObjectList = (ContactsCommonList) handler.getCommonPartList();
245         } catch (UnauthorizedException ue) {
246             Response response = Response.status(
247                     Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
248             throw new WebApplicationException(response);
249         } catch (Exception e) {
250             if (logger.isDebugEnabled()) {
251                 logger.debug("Caught exception in getContactsList", e);
252             }
253             Response response = Response.status(
254                     Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
255             throw new WebApplicationException(response);
256         }
257         return contactObjectList;
258     }
259
260     /**
261      * Gets the contact.
262      * 
263      * @param parentcsid the parentcsid
264      * @param itemcsid the itemcsid
265      * @param csid the csid
266      * 
267      * @return the contact
268      */
269     @GET
270     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
271     public MultipartOutput getContact(
272             @PathParam("parentcsid") String parentcsid,
273             @PathParam("itemcsid") String itemcsid,
274             @PathParam("csid") String csid) {
275         MultipartOutput result = null;
276        if (logger.isDebugEnabled()) {
277             logger.debug("getContact with parentCsid=" + parentcsid +
278             " itemcsid=" + itemcsid + " csid=" + csid);
279         }
280         try {
281             // Note that we have to create the service context and document
282             // handler for the Contact service, not the main service.
283                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
284             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
285             getRepositoryClient(ctx).get(ctx, csid, handler);
286             result = (MultipartOutput) ctx.getOutput();
287         } catch (UnauthorizedException ue) {
288             Response response = Response.status(
289                     Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
290             throw new WebApplicationException(response);
291         } catch (DocumentNotFoundException dnfe) {
292             if (logger.isDebugEnabled()) {
293                 logger.debug("getContact", dnfe);
294             }
295             Response response = Response.status(Response.Status.NOT_FOUND)
296                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
297                 .type("text/plain").build();
298             throw new WebApplicationException(response);
299         } catch (Exception e) {
300             if (logger.isDebugEnabled()) {
301                 logger.debug("getContact", e);
302             }
303             Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
304                 .entity("Get contact failed")
305                 .type("text/plain").build();
306             throw new WebApplicationException(response);
307         }
308         if (result == null) {
309             Response response = Response.status(Response.Status.NOT_FOUND)
310                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
311                 .type("text/plain").build();
312             throw new WebApplicationException(response);
313         }
314         return result;
315
316     }
317
318     /**
319      * Update contact.
320      * 
321      * @param parentcsid the parentcsid
322      * @param itemcsid the itemcsid
323      * @param csid the csid
324      * @param theUpdate the the update
325      * 
326      * @return the multipart output
327      */
328     @PUT
329     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
330     public MultipartOutput updateContact(
331             @PathParam("parentcsid") String parentcsid,
332             @PathParam("itemcsid") String itemcsid,
333             @PathParam("csid") String csid,
334             MultipartInput theUpdate) {
335        if (logger.isDebugEnabled()) {
336             logger.debug("updateContact with parentcsid=" + parentcsid +
337             " itemcsid=" + itemcsid + " csid=" + csid);
338         }
339        if (parentcsid == null || parentcsid.trim().isEmpty()) {
340             logger.error("updateContact: missing csid!");
341             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
342                     "update failed on Contact parentcsid=" + parentcsid).type(
343                     "text/plain").build();
344             throw new WebApplicationException(response);
345         }
346         if (itemcsid == null || itemcsid.trim().isEmpty()) {
347             logger.error("updateContact: missing itemcsid!");
348             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
349                     "update failed on Contact=" + itemcsid).type(
350                     "text/plain").build();
351             throw new WebApplicationException(response);
352         }
353         if (csid == null || csid.trim().isEmpty()) {
354             logger.error("updateContact: missing csid!");
355             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
356                     "update failed on Contact=" + csid).type(
357                     "text/plain").build();
358             throw new WebApplicationException(response);
359         }
360         MultipartOutput result = null;
361         try {
362             // Note that we have to create the service context and document
363             // handler for the Contact service, not the main service.
364                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), theUpdate);
365             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
366             getRepositoryClient(ctx).update(ctx, csid, handler);
367             result = (MultipartOutput) ctx.getOutput();
368         } catch (BadRequestException bre) {
369             Response response = Response.status(
370                     Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
371             throw new WebApplicationException(response);
372         } catch (UnauthorizedException ue) {
373             Response response = Response.status(
374                     Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
375             throw new WebApplicationException(response);
376         } catch (DocumentNotFoundException dnfe) {
377             if (logger.isDebugEnabled()) {
378                 logger.debug("caught exception in updateContact", dnfe);
379             }
380             Response response = Response.status(Response.Status.NOT_FOUND).entity(
381                     "Update failed on Contact csid=" + itemcsid).type(
382                     "text/plain").build();
383             throw new WebApplicationException(response);
384         } catch (Exception e) {
385             Response response = Response.status(
386                     Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
387             throw new WebApplicationException(response);
388         }
389         return result;
390     }
391
392     /**
393      * Delete contact.
394      * 
395      * @param parentcsid the parentcsid
396      * @param itemcsid the itemcsid
397      * @param csid the csid
398      * 
399      * @return the response
400      */
401     @DELETE
402     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
403     public Response deleteContact(
404             @PathParam("parentcsid") String parentcsid,
405             @PathParam("itemcsid") String itemcsid,
406             @PathParam("csid") String csid) {
407         if (logger.isDebugEnabled()) {
408             logger.debug("deleteContact with parentCsid=" + parentcsid +
409             " itemcsid=" + itemcsid + " csid=" + csid);
410         }
411         if (parentcsid == null || parentcsid.trim().isEmpty()) {
412             logger.error("deleteContact: missing parentcsid!");
413             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
414                     "delete contact failed on parentcsid=" + parentcsid).type(
415                     "text/plain").build();
416             throw new WebApplicationException(response);
417         }
418         if (itemcsid == null || itemcsid.trim().isEmpty()) {
419             logger.error("deleteContact: missing itemcsid!");
420             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
421                     "delete contact failed on itemcsid=" + itemcsid).type(
422                     "text/plain").build();
423             throw new WebApplicationException(response);
424         }
425         if (csid == null || csid.trim().isEmpty()) {
426             logger.error("deleteContact: missing csid!");
427             Response response = Response.status(Response.Status.BAD_REQUEST).entity(
428                     "delete contact failed on csid=" + csid).type(
429                     "text/plain").build();
430             throw new WebApplicationException(response);
431         }
432         try {
433             // Note that we have to create the service context for the
434             // Contact service, not the main service.
435                 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
436             getRepositoryClient(ctx).delete(ctx, csid);
437             return Response.status(HttpResponseCodes.SC_OK).build();
438          } catch (UnauthorizedException ue) {
439             Response response = Response.status(
440                     Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
441             throw new WebApplicationException(response);
442          } catch (DocumentNotFoundException dnfe) {
443             if (logger.isDebugEnabled()) {
444                 logger.debug("Caught exception in deleteContact", dnfe);
445             }
446             Response response = Response.status(Response.Status.NOT_FOUND)
447                 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
448                 .type("text/plain").build();
449             throw new WebApplicationException(response);
450        } catch (Exception e) {
451             Response response = Response.status(
452                     Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
453             throw new WebApplicationException(response);
454         }
455     }
456
457 }