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