]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
74947ec458952c4b0ccf34b55865cedc0b516087
[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.HashMap;
27 import java.util.List;
28 import java.util.Map;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
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 import org.collectionspace.services.client.*;
44 import org.collectionspace.services.common.StoredValuesUriTemplate;
45 import org.collectionspace.services.common.UriTemplateFactory;
46 import org.collectionspace.services.common.UriTemplateRegistryKey;
47 import org.collectionspace.services.common.vocabulary.AuthorityResource;
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.contact.ContactResource;
52 import org.collectionspace.services.contact.ContactsCommon;
53 import org.collectionspace.services.contact.ContactJAXBSchema;
54 import org.collectionspace.services.contact.nuxeo.ContactConstants;
55 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
56 import org.collectionspace.services.jaxb.AbstractCommonList;
57 import org.jboss.resteasy.util.HttpResponseCodes;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
60
61 /**
62  * The Class AuthorityResourceWithContacts.
63  */
64 @Consumes("application/xml")
65 @Produces("application/xml")
66 public abstract class AuthorityResourceWithContacts<AuthCommon, AuthItemHandler> extends //FIXME: REM - Why is this resource in this package instead of somewhere in 'common'?
67         AuthorityResource<AuthCommon, AuthItemHandler> {
68
69     private ContactResource contactResource = new ContactResource(); // Warning: ContactResource is a singleton.
70     final Logger logger = LoggerFactory.getLogger(AuthorityResourceWithContacts.class);
71
72     public AuthorityResourceWithContacts(
73             Class<AuthCommon> authCommonClass, Class<?> resourceClass,
74             String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
75         super(authCommonClass, resourceClass,
76                 authorityCommonSchemaName, authorityItemCommonSchemaName);
77     }
78
79     public abstract String getItemServiceName();
80
81     public String getContactServiceName() {
82         return contactResource.getServiceName();
83     }
84     
85     private DocumentHandler createContactDocumentHandler(
86             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
87             String inItem) throws Exception {
88         UriInfo ui = null;
89         return createContactDocumentHandler(ctx, inAuthority, inItem, ui);
90     }
91
92     private DocumentHandler createContactDocumentHandler(
93             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
94             String inItem, UriInfo ui) throws Exception {
95         ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler) createDocumentHandler(
96                 ctx,
97                 ctx.getCommonPartLabel(getContactServiceName()),
98                 ContactsCommon.class);
99         docHandler.setInAuthority(inAuthority);
100         docHandler.setInItem(inItem);
101         docHandler.getServiceContext().setUriInfo(ui);
102         return docHandler;
103     }
104
105     /*************************************************************************
106      * Contact parts - this is a sub-resource of the AuthorityItem
107      * @param parentspecifier either a CSID or one of the urn forms
108      * @param itemspecifier either a CSID or one of the urn forms
109      * @return contact
110      *************************************************************************/
111     @POST
112     @Path("{parentcsid}/items/{itemcsid}/contacts")
113     public Response createContact(
114             @PathParam("parentcsid") String parentspecifier,
115             @PathParam("itemcsid") String itemspecifier,
116             String xmlPayload,
117             @Context UriInfo ui) {
118         try {
119             PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
120             String parentcsid = lookupParentCSID(parentspecifier, "createContact(authority)", "CREATE_ITEM_CONTACT", null);
121
122             ServiceContext itemCtx = createServiceContext(getItemServiceName());
123             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "createContact(item)", "CREATE_ITEM_CONTACT", itemCtx);
124
125             // Note that we have to create the service context and document
126             // handler for the Contact service, not the main service.
127             ServiceContext ctx = createServiceContext(getContactServiceName(), input);
128             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, ui);
129             String csid = getRepositoryClient(ctx).create(ctx, handler);
130             UriBuilder path = UriBuilder.fromResource(resourceClass);
131             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
132             Response response = Response.created(path.build()).build();
133             return response;
134         } catch (Exception e) {
135             throw bigReThrow(e,
136                     "Create Contact failed; one of the requested specifiers for authority:"
137                     + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
138                     itemspecifier);
139         }
140     }
141
142     /**
143      * Gets the contact list.
144      * 
145      * @param parentspecifier either a CSID or one of the urn forms
146      * @param itemspecifier either a CSID or one of the urn forms
147      * @param ui the ui
148      * 
149      * @return the contact list
150      */
151     @GET
152     @Produces({"application/xml"})
153     @Path("{parentcsid}/items/{itemcsid}/contacts/")
154     public AbstractCommonList getContactList(
155             @PathParam("parentcsid") String parentspecifier,
156             @PathParam("itemcsid") String itemspecifier,
157             @Context UriInfo uriInfo) {
158         AbstractCommonList contactObjectList = new AbstractCommonList();
159
160         try {
161             ServiceContext ctx = createServiceContext(getContactServiceName(), uriInfo);
162             MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
163                 
164             String parentcsid = lookupParentCSID(parentspecifier, "getContactList(parent)", "GET_CONTACT_LIST", null);
165             ServiceContext itemCtx = createServiceContext(getItemServiceName());
166             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContactList(item)", "GET_CONTACT_LIST", itemCtx);
167
168             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, uriInfo);
169             DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
170             myFilter.appendWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":"
171                     + ContactJAXBSchema.IN_AUTHORITY
172                     + "='" + parentcsid + "'"
173                     + IQueryManager.SEARCH_QUALIFIER_AND
174                     + ContactJAXBSchema.CONTACTS_COMMON + ":"
175                     + ContactJAXBSchema.IN_ITEM
176                     + "='" + itemcsid + "'",
177                     IQueryManager.SEARCH_QUALIFIER_AND);  // "AND" this clause to any existing
178             getRepositoryClient(ctx).getFiltered(ctx, handler);
179             contactObjectList = (AbstractCommonList) handler.getCommonPartList();
180         } catch (Exception e) {
181             throw bigReThrow(e,
182                     "Get ContactList failed; one of the requested specifiers for authority:"
183                     + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
184                     itemspecifier);
185         }
186         
187         return contactObjectList;
188     }
189
190     /**
191      * Gets the contact.
192      * 
193      * @param parentspecifier either a CSID or one of the urn forms
194      * @param itemspecifier either a CSID or one of the urn forms
195      * @param csid the csid
196      * 
197      * @return the contact
198      */
199     @GET
200     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
201     public String getContact(
202             @PathParam("parentcsid") String parentspecifier,
203             @PathParam("itemcsid") String itemspecifier,
204             @PathParam("csid") String csid) {
205         PoxPayloadOut result = null;
206         try {
207             String parentcsid = lookupParentCSID(parentspecifier, "getContact(parent)", "GET_ITEM_CONTACT", null);
208
209             ServiceContext itemCtx = createServiceContext(getItemServiceName());
210             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContact(item)", "GET_ITEM_CONTACT", itemCtx);
211
212             // Note that we have to create the service context and document handler for the Contact service, not the main service.
213             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getContactServiceName());
214             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
215             getRepositoryClient(ctx).get(ctx, csid, handler);
216             result = ctx.getOutput();
217         } catch (Exception e) {
218             throw bigReThrow(e, "Get failed, the requested Contact CSID:" + csid
219                     + ": or one of the specifiers for authority:" + parentspecifier
220                     + ": and item:" + itemspecifier + ": was not found.",
221                     csid);
222         }
223         if (result == null) {
224             Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed, the requested Contact CSID:" + csid + ": was not found.").type("text/plain").build();
225             throw new WebApplicationException(response);
226         }
227         return result.toXML();
228     }
229
230     /**
231      * Update contact.
232      * 
233      * @param parentspecifier either a CSID or one of the urn forms
234      * @param itemspecifier either a CSID or one of the urn forms
235      * @param csid the csid
236      *
237      * @return the multipart output
238      */
239     @PUT
240     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
241     public String updateContact(
242             @PathParam("parentcsid") String parentspecifier,
243             @PathParam("itemcsid") String itemspecifier,
244             @PathParam("csid") String csid,
245             String xmlPayload) {
246         PoxPayloadOut result = null;
247         try {
248             PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
249             String parentcsid = lookupParentCSID(parentspecifier, "updateContact(authority)", "UPDATE_CONTACT", null);
250
251             ServiceContext itemCtx = createServiceContext(getItemServiceName());
252             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "updateContact(item)", "UPDATE_CONTACT", itemCtx);
253
254             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
255             // Note that we have to create the service context and document handler for the Contact service, not the main service.
256             ctx = createServiceContext(getContactServiceName(), theUpdate);
257             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
258             getRepositoryClient(ctx).update(ctx, csid, handler);
259             result = ctx.getOutput();
260         } catch (Exception e) {
261             throw bigReThrow(e, "Update failed, the requested Contact CSID:" + csid
262                     + ": or one of the specifiers for authority:" + parentspecifier
263                     + ": and item:" + itemspecifier + ": was not found.",
264                     csid);
265         }
266         return result.toXML();
267     }
268
269     /**
270      * Delete contact.
271      * 
272      * @param parentspecifier either a CSID or one of the urn forms
273      * @param itemspecifier either a CSID or one of the urn forms
274      * @param csid the csid
275      * 
276      * @return the response
277      */
278     @DELETE
279     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
280     public Response deleteContact(
281             @PathParam("parentcsid") String parentspecifier,
282             @PathParam("itemcsid") String itemspecifier,
283             @PathParam("csid") String csid) {
284         try {
285             String parentcsid = lookupParentCSID(parentspecifier, "deleteContact(authority)", "DELETE_CONTACT", null);
286
287             ServiceContext itemCtx = createServiceContext(getItemServiceName());
288             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "deleteContact(item)", "DELETE_CONTACT", itemCtx);
289             //NOTE: itemcsid is not used below.  Leaving the above call in for possible side effects???       CSPACE-3175
290
291             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
292             // Note that we have to create the service context for the Contact service, not the main service.
293             ctx = createServiceContext(getContactServiceName());
294             DocumentHandler handler = createDocumentHandler(ctx);
295             getRepositoryClient(ctx).delete(ctx, csid, handler);
296             return Response.status(HttpResponseCodes.SC_OK).build();
297         } catch (Exception e) {
298             throw bigReThrow(e, "DELETE failed, the requested Contact CSID:" + csid
299                     + ": or one of the specifiers for authority:" + parentspecifier
300                     + ": and item:" + itemspecifier + ": was not found.", csid);
301         }
302     }
303     
304     protected String getContactDocType() {
305         return ContactConstants.NUXEO_DOCTYPE;
306     }
307     
308     /**
309      * Returns a UriRegistry entry: a map of tenant-qualified URI templates
310      * for the current resource, for all tenants
311      * 
312      * @return a map of URI templates for the current resource, for all tenants
313      */
314     @Override
315     public Map<UriTemplateRegistryKey,StoredValuesUriTemplate> getUriRegistryEntries() {
316         Map<UriTemplateRegistryKey,StoredValuesUriTemplate> uriRegistryEntriesMap =
317                 super.getUriRegistryEntries();
318         List<String> tenantIds = getTenantBindingsReader().getTenantIds();
319         for (String tenantId : tenantIds) {
320                 uriRegistryEntriesMap.putAll(getUriRegistryEntries(tenantId, getContactDocType(), UriTemplateFactory.CONTACT));
321         }
322         return uriRegistryEntriesMap;
323     }
324
325 }