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