]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
949299df52df8fb5d49fa7c3ac4a31e3ade946af
[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.ContactsCommonList;
51 import org.collectionspace.services.contact.ContactJAXBSchema;
52 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
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
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         ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(
86                         ctx,
87                         ctx.getCommonPartLabel(getContactServiceName()),
88                         ContactsCommon.class);          
89         docHandler.setInAuthority(inAuthority);
90         docHandler.setInItem(inItem);
91         return docHandler;
92     }
93
94     /*************************************************************************
95      * Contact parts - this is a sub-resource of the AuthorityItem
96          * @param parentspecifier either a CSID or one of the urn forms
97          * @param itemspecifier either a CSID or one of the urn forms
98      * @return contact
99      *************************************************************************/
100     @POST
101     @Path("{parentcsid}/items/{itemcsid}/contacts")
102     public Response createContact(
103             @PathParam("parentcsid") String parentspecifier,
104             @PathParam("itemcsid") String itemspecifier,
105             String xmlPayload) {
106         try {
107                 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
108             String parentcsid = lookupParentCSID(parentspecifier, "createContact(authority)", "CREATE_ITEM_CONTACT", null);
109
110             ServiceContext itemCtx = createServiceContext(getItemServiceName());
111             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "createContact(item)", "CREATE_ITEM_CONTACT", itemCtx);
112
113             // Note that we have to create the service context and document
114             // handler for the Contact service, not the main service.
115                 ServiceContext ctx = createServiceContext(getContactServiceName(), input);
116             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
117             String csid = getRepositoryClient(ctx).create(ctx, handler);
118             UriBuilder path = UriBuilder.fromResource(resourceClass);
119             path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
120             Response response = Response.created(path.build()).build();
121             return response;
122         } catch (Exception e) {
123             throw bigReThrow(e,
124                                         "Create Contact failed; one of the requested specifiers for authority:"
125                                                        +parentspecifier+": and item:"+itemspecifier+": was not found.",
126                                         itemspecifier);
127         }
128     }
129
130     /**
131      * Gets the contact list.
132      * 
133          * @param parentspecifier either a CSID or one of the urn forms
134          * @param itemspecifier either a CSID or one of the urn forms
135      * @param ui the ui
136      * 
137      * @return the contact list
138      */
139     @GET
140     @Produces({"application/xml"})
141     @Path("{parentcsid}/items/{itemcsid}/contacts/")
142     public ContactsCommonList getContactList(
143             @PathParam("parentcsid") String parentspecifier,
144             @PathParam("itemcsid") String itemspecifier,
145             @Context UriInfo ui) {
146         ContactsCommonList contactObjectList = new ContactsCommonList();
147         try {
148             String parentcsid = lookupParentCSID(parentspecifier, "getContactList(parent)", "GET_CONTACT_LIST", null);
149
150             ServiceContext itemCtx = createServiceContext(getItemServiceName());
151             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContactList(item)", "GET_CONTACT_LIST", itemCtx);
152
153             MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
154                 ServiceContext ctx = createServiceContext(getContactServiceName(), queryParams);
155             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
156             DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
157             myFilter.appendWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
158                 ContactJAXBSchema.IN_AUTHORITY +
159                 "='" + parentcsid + "'" +
160                 IQueryManager.SEARCH_QUALIFIER_AND +
161                 ContactJAXBSchema.CONTACTS_COMMON + ":" +
162                 ContactJAXBSchema.IN_ITEM +
163                 "='" + itemcsid + "'",
164                 IQueryManager.SEARCH_QUALIFIER_AND);  // "AND" this clause to any existing
165             getRepositoryClient(ctx).getFiltered(ctx, handler);
166             contactObjectList = (ContactsCommonList) handler.getCommonPartList();
167         } catch (Exception e) {
168             throw bigReThrow(e,
169                                         "Get ContactList failed; one of the requested specifiers for authority:"
170                                                     +parentspecifier+": and item:"+itemspecifier+": was not found.",
171                                         itemspecifier);
172         }
173         return contactObjectList;
174     }
175
176     /**
177      * Gets the contact.
178      * 
179          * @param parentspecifier either a CSID or one of the urn forms
180          * @param itemspecifier either a CSID or one of the urn forms
181      * @param csid the csid
182      * 
183      * @return the contact
184      */
185     @GET
186     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
187     public String getContact(
188             @PathParam("parentcsid") String parentspecifier,
189             @PathParam("itemcsid") String itemspecifier,
190             @PathParam("csid") String csid) {
191         PoxPayloadOut result = null;
192         try {
193             String parentcsid = lookupParentCSID(parentspecifier, "getContact(parent)", "GET_ITEM_CONTACT", null);
194
195             ServiceContext itemCtx = createServiceContext(getItemServiceName());
196             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContact(item)", "GET_ITEM_CONTACT", itemCtx);
197
198             // Note that we have to create the service context and document handler for the Contact service, not the main service.
199                 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getContactServiceName());
200             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
201             getRepositoryClient(ctx).get(ctx, csid, handler);
202             result = ctx.getOutput();
203         } catch (Exception e) {
204             throw bigReThrow(e, "Get failed, the requested Contact CSID:" + csid +
205                                             ": or one of the specifiers for authority:"+parentspecifier+
206                                             ": and item:"+itemspecifier+": was not found.",
207                                         csid);
208         }
209         if (result == null) {
210             Response response = Response.status(Response.Status.NOT_FOUND)
211                 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
212                 .type("text/plain").build();
213             throw new WebApplicationException(response);
214         }
215         return result.toXML();
216     }
217
218     /**
219      * Update contact.
220      * 
221          * @param parentspecifier either a CSID or one of the urn forms
222          * @param itemspecifier either a CSID or one of the urn forms
223      * @param csid the csid
224      *
225      * @return the multipart output
226      */
227     @PUT
228     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
229     public String updateContact(
230             @PathParam("parentcsid") String parentspecifier,
231             @PathParam("itemcsid") String itemspecifier,
232             @PathParam("csid") String csid,
233             String xmlPayload) {
234         PoxPayloadOut result = null;
235         try {
236                 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
237                         String parentcsid = lookupParentCSID(parentspecifier, "updateContact(authority)", "UPDATE_CONTACT", null);
238
239             ServiceContext itemCtx = createServiceContext(getItemServiceName());
240             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "updateContact(item)", "UPDATE_CONTACT", itemCtx);
241
242             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
243             // Note that we have to create the service context and document handler for the Contact service, not the main service.
244                 ctx = createServiceContext(getContactServiceName(), theUpdate);
245             DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
246             getRepositoryClient(ctx).update(ctx, csid, handler);
247             result = ctx.getOutput();
248         } catch (Exception e) {
249            throw bigReThrow(e, "Update failed, the requested Contact CSID:" + csid +
250                                 ": or one of the specifiers for authority:"+parentspecifier+
251                                 ": and item:"+itemspecifier+": was not found.",
252                             csid);
253         }
254         return result.toXML();
255     }
256
257     /**
258      * Delete contact.
259      * 
260          * @param parentspecifier either a CSID or one of the urn forms
261          * @param itemspecifier either a CSID or one of the urn forms
262      * @param csid the csid
263      * 
264      * @return the response
265      */
266     @DELETE
267     @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
268     public Response deleteContact(
269             @PathParam("parentcsid") String parentspecifier,
270             @PathParam("itemcsid") String itemspecifier,
271             @PathParam("csid") String csid) {
272         try {
273                         String parentcsid = lookupParentCSID(parentspecifier, "deleteContact(authority)", "DELETE_CONTACT", null);
274
275             ServiceContext itemCtx = createServiceContext(getItemServiceName());
276             String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "deleteContact(item)", "DELETE_CONTACT", itemCtx);
277             //NOTE: itemcsid is not used below.  Leaving the above call in for possible side effects???       CSPACE-3175
278
279             ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
280             // Note that we have to create the service context for the Contact service, not the main service.
281                 ctx = createServiceContext(getContactServiceName());
282             getRepositoryClient(ctx).delete(ctx, csid);
283             return Response.status(HttpResponseCodes.SC_OK).build();
284          } catch (Exception e) {
285             throw bigReThrow(e, "DELETE failed, the requested Contact CSID:" + csid +
286                                 ": or one of the specifiers for authority:"+parentspecifier+
287                                 ": and item:"+itemspecifier+": was not found.", csid);
288         }
289     }
290     
291 }