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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
24 package org.collectionspace.services.contact;
26 import java.util.HashMap;
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;
59 * The Class AuthorityResourceWithContacts.
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> {
66 private ContactResource contactResource = new ContactResource(); // Warning: ContactResource is a singleton.
67 final Logger logger = LoggerFactory.getLogger(AuthorityResourceWithContacts.class);
69 public AuthorityResourceWithContacts(
70 Class<AuthCommon> authCommonClass, Class<?> resourceClass,
71 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
72 super(authCommonClass, resourceClass,
73 authorityCommonSchemaName, authorityItemCommonSchemaName);
76 public abstract String getItemServiceName();
78 public String getContactServiceName() {
79 return contactResource.getServiceName();
82 private DocumentHandler createContactDocumentHandler(
83 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
84 String inItem) throws Exception {
86 return createContactDocumentHandler(ctx, inAuthority, inItem, ui);
89 private DocumentHandler createContactDocumentHandler(
90 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
91 String inItem, UriInfo ui) throws Exception {
92 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler) createDocumentHandler(
94 ctx.getCommonPartLabel(getContactServiceName()),
95 ContactsCommon.class);
96 docHandler.setInAuthority(inAuthority);
97 docHandler.setInItem(inItem);
98 docHandler.getServiceContext().setUriInfo(ui);
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
107 *************************************************************************/
109 @Path("{parentcsid}/items/{itemcsid}/contacts")
110 public Response createContact(
111 @PathParam("parentcsid") String parentspecifier,
112 @PathParam("itemcsid") String itemspecifier,
114 @Context UriInfo ui) {
116 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
117 String parentcsid = lookupParentCSID(parentspecifier, "createContact(authority)", "CREATE_ITEM_CONTACT", null);
119 ServiceContext itemCtx = createServiceContext(getItemServiceName());
120 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "createContact(item)", "CREATE_ITEM_CONTACT", itemCtx);
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();
131 } catch (Exception e) {
133 "Create Contact failed; one of the requested specifiers for authority:"
134 + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
140 * Gets the contact list.
142 * @param parentspecifier either a CSID or one of the urn forms
143 * @param itemspecifier either a CSID or one of the urn forms
146 * @return the contact list
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();
157 String parentcsid = lookupParentCSID(parentspecifier, "getContactList(parent)", "GET_CONTACT_LIST", null);
159 ServiceContext itemCtx = createServiceContext(getItemServiceName());
160 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContactList(item)", "GET_CONTACT_LIST", itemCtx);
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) {
178 "Get ContactList failed; one of the requested specifiers for authority:"
179 + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
182 return contactObjectList;
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
192 * @return the contact
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;
202 String parentcsid = lookupParentCSID(parentspecifier, "getContact(parent)", "GET_ITEM_CONTACT", null);
204 ServiceContext itemCtx = createServiceContext(getItemServiceName());
205 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContact(item)", "GET_ITEM_CONTACT", itemCtx);
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.",
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);
222 return result.toXML();
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
232 * @return the multipart output
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,
241 PoxPayloadOut result = null;
243 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
244 String parentcsid = lookupParentCSID(parentspecifier, "updateContact(authority)", "UPDATE_CONTACT", null);
246 ServiceContext itemCtx = createServiceContext(getItemServiceName());
247 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "updateContact(item)", "UPDATE_CONTACT", itemCtx);
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.",
261 return result.toXML();
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
271 * @return the response
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) {
280 String parentcsid = lookupParentCSID(parentspecifier, "deleteContact(authority)", "DELETE_CONTACT", null);
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
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);
299 public String getContactDocType() {
300 // FIXME: Proof of concept placeholder
301 return getServiceName();
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
314 if (contactUriTemplate == null) {
315 return uriTemplateMap; // return map as obtained from superclass
317 uriTemplateMap.put(contactDocType, contactUriTemplate);
318 return uriTemplateMap;