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