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