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