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;
27 import java.util.List;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.WebApplicationException;
38 import javax.ws.rs.core.Context;
39 import javax.ws.rs.core.MultivaluedMap;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriBuilder;
42 import javax.ws.rs.core.UriInfo;
43 import org.collectionspace.services.client.*;
44 import org.collectionspace.services.common.StoredValuesUriTemplate;
45 import org.collectionspace.services.common.UriTemplateFactory;
46 import org.collectionspace.services.common.UriTemplateRegistryKey;
47 import org.collectionspace.services.common.vocabulary.AuthorityResource;
48 import org.collectionspace.services.common.context.ServiceContext;
49 import org.collectionspace.services.common.document.DocumentFilter;
50 import org.collectionspace.services.common.document.DocumentHandler;
51 import org.collectionspace.services.contact.ContactResource;
52 import org.collectionspace.services.contact.ContactsCommon;
53 import org.collectionspace.services.contact.ContactJAXBSchema;
54 import org.collectionspace.services.contact.nuxeo.ContactConstants;
55 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
56 import org.collectionspace.services.jaxb.AbstractCommonList;
57 import org.jboss.resteasy.util.HttpResponseCodes;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
62 * The Class AuthorityResourceWithContacts.
64 @Consumes("application/xml")
65 @Produces("application/xml")
66 public abstract class AuthorityResourceWithContacts<AuthCommon, AuthItemHandler> extends //FIXME: REM - Why is this resource in this package instead of somewhere in 'common'?
67 AuthorityResource<AuthCommon, AuthItemHandler> {
69 private ContactResource contactResource = new ContactResource(); // Warning: ContactResource is a singleton.
70 final Logger logger = LoggerFactory.getLogger(AuthorityResourceWithContacts.class);
72 public AuthorityResourceWithContacts(
73 Class<AuthCommon> authCommonClass, Class<?> resourceClass,
74 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
75 super(authCommonClass, resourceClass,
76 authorityCommonSchemaName, authorityItemCommonSchemaName);
79 public abstract String getItemServiceName();
81 public String getContactServiceName() {
82 return contactResource.getServiceName();
85 private DocumentHandler createContactDocumentHandler(
86 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
87 String inItem) throws Exception {
89 return createContactDocumentHandler(ctx, inAuthority, inItem, ui);
92 private DocumentHandler createContactDocumentHandler(
93 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
94 String inItem, UriInfo ui) throws Exception {
95 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler) createDocumentHandler(
97 ctx.getCommonPartLabel(getContactServiceName()),
98 ContactsCommon.class);
99 docHandler.setInAuthority(inAuthority);
100 docHandler.setInItem(inItem);
101 docHandler.getServiceContext().setUriInfo(ui);
105 /*************************************************************************
106 * Contact parts - this is a sub-resource of the AuthorityItem
107 * @param parentspecifier either a CSID or one of the urn forms
108 * @param itemspecifier either a CSID or one of the urn forms
110 *************************************************************************/
112 @Path("{parentcsid}/items/{itemcsid}/contacts")
113 public Response createContact(
114 @PathParam("parentcsid") String parentspecifier,
115 @PathParam("itemcsid") String itemspecifier,
117 @Context UriInfo ui) {
119 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
120 String parentcsid = lookupParentCSID(parentspecifier, "createContact(authority)", "CREATE_ITEM_CONTACT", null);
122 ServiceContext itemCtx = createServiceContext(getItemServiceName());
123 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "createContact(item)", "CREATE_ITEM_CONTACT", itemCtx);
125 // Note that we have to create the service context and document
126 // handler for the Contact service, not the main service.
127 ServiceContext ctx = createServiceContext(getContactServiceName(), input);
128 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, ui);
129 String csid = getRepositoryClient(ctx).create(ctx, handler);
130 UriBuilder path = UriBuilder.fromResource(resourceClass);
131 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
132 Response response = Response.created(path.build()).build();
134 } catch (Exception e) {
136 "Create Contact failed; one of the requested specifiers for authority:"
137 + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
143 * Gets the contact list.
145 * @param parentspecifier either a CSID or one of the urn forms
146 * @param itemspecifier either a CSID or one of the urn forms
149 * @return the contact list
152 @Produces({"application/xml"})
153 @Path("{parentcsid}/items/{itemcsid}/contacts/")
154 public AbstractCommonList getContactList(
155 @PathParam("parentcsid") String parentspecifier,
156 @PathParam("itemcsid") String itemspecifier,
157 @Context UriInfo uriInfo) {
158 AbstractCommonList contactObjectList = new AbstractCommonList();
161 ServiceContext ctx = createServiceContext(getContactServiceName(), uriInfo);
162 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
164 String parentcsid = lookupParentCSID(parentspecifier, "getContactList(parent)", "GET_CONTACT_LIST", null);
165 ServiceContext itemCtx = createServiceContext(getItemServiceName());
166 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContactList(item)", "GET_CONTACT_LIST", itemCtx);
168 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, uriInfo);
169 DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
170 myFilter.appendWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":"
171 + ContactJAXBSchema.IN_AUTHORITY
172 + "='" + parentcsid + "'"
173 + IQueryManager.SEARCH_QUALIFIER_AND
174 + ContactJAXBSchema.CONTACTS_COMMON + ":"
175 + ContactJAXBSchema.IN_ITEM
176 + "='" + itemcsid + "'",
177 IQueryManager.SEARCH_QUALIFIER_AND); // "AND" this clause to any existing
178 getRepositoryClient(ctx).getFiltered(ctx, handler);
179 contactObjectList = (AbstractCommonList) handler.getCommonPartList();
180 } catch (Exception e) {
182 "Get ContactList failed; one of the requested specifiers for authority:"
183 + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
187 return contactObjectList;
193 * @param parentspecifier either a CSID or one of the urn forms
194 * @param itemspecifier either a CSID or one of the urn forms
195 * @param csid the csid
197 * @return the contact
200 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
201 public String getContact(
202 @PathParam("parentcsid") String parentspecifier,
203 @PathParam("itemcsid") String itemspecifier,
204 @PathParam("csid") String csid) {
205 PoxPayloadOut result = null;
207 String parentcsid = lookupParentCSID(parentspecifier, "getContact(parent)", "GET_ITEM_CONTACT", null);
209 ServiceContext itemCtx = createServiceContext(getItemServiceName());
210 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getContact(item)", "GET_ITEM_CONTACT", itemCtx);
212 // Note that we have to create the service context and document handler for the Contact service, not the main service.
213 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getContactServiceName());
214 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
215 getRepositoryClient(ctx).get(ctx, csid, handler);
216 result = ctx.getOutput();
217 } catch (Exception e) {
218 throw bigReThrow(e, "Get failed, the requested Contact CSID:" + csid
219 + ": or one of the specifiers for authority:" + parentspecifier
220 + ": and item:" + itemspecifier + ": was not found.",
223 if (result == null) {
224 Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed, the requested Contact CSID:" + csid + ": was not found.").type("text/plain").build();
225 throw new WebApplicationException(response);
227 return result.toXML();
233 * @param parentspecifier either a CSID or one of the urn forms
234 * @param itemspecifier either a CSID or one of the urn forms
235 * @param csid the csid
237 * @return the multipart output
240 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
241 public String updateContact(
242 @PathParam("parentcsid") String parentspecifier,
243 @PathParam("itemcsid") String itemspecifier,
244 @PathParam("csid") String csid,
246 PoxPayloadOut result = null;
248 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
249 String parentcsid = lookupParentCSID(parentspecifier, "updateContact(authority)", "UPDATE_CONTACT", null);
251 ServiceContext itemCtx = createServiceContext(getItemServiceName());
252 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "updateContact(item)", "UPDATE_CONTACT", itemCtx);
254 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
255 // Note that we have to create the service context and document handler for the Contact service, not the main service.
256 ctx = createServiceContext(getContactServiceName(), theUpdate);
257 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
258 getRepositoryClient(ctx).update(ctx, csid, handler);
259 result = ctx.getOutput();
260 } catch (Exception e) {
261 throw bigReThrow(e, "Update failed, the requested Contact CSID:" + csid
262 + ": or one of the specifiers for authority:" + parentspecifier
263 + ": and item:" + itemspecifier + ": was not found.",
266 return result.toXML();
272 * @param parentspecifier either a CSID or one of the urn forms
273 * @param itemspecifier either a CSID or one of the urn forms
274 * @param csid the csid
276 * @return the response
279 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
280 public Response deleteContact(
281 @PathParam("parentcsid") String parentspecifier,
282 @PathParam("itemcsid") String itemspecifier,
283 @PathParam("csid") String csid) {
285 String parentcsid = lookupParentCSID(parentspecifier, "deleteContact(authority)", "DELETE_CONTACT", null);
287 ServiceContext itemCtx = createServiceContext(getItemServiceName());
288 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "deleteContact(item)", "DELETE_CONTACT", itemCtx);
289 //NOTE: itemcsid is not used below. Leaving the above call in for possible side effects??? CSPACE-3175
291 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
292 // Note that we have to create the service context for the Contact service, not the main service.
293 ctx = createServiceContext(getContactServiceName());
294 DocumentHandler handler = createDocumentHandler(ctx);
295 getRepositoryClient(ctx).delete(ctx, csid, handler);
296 return Response.status(HttpResponseCodes.SC_OK).build();
297 } catch (Exception e) {
298 throw bigReThrow(e, "DELETE failed, the requested Contact CSID:" + csid
299 + ": or one of the specifiers for authority:" + parentspecifier
300 + ": and item:" + itemspecifier + ": was not found.", csid);
304 protected String getContactDocType() {
305 return ContactConstants.NUXEO_DOCTYPE;
309 * Returns a UriRegistry entry: a map of tenant-qualified URI templates
310 * for the current resource, for all tenants
312 * @return a map of URI templates for the current resource, for all tenants
315 public Map<UriTemplateRegistryKey,StoredValuesUriTemplate> getUriRegistryEntries() {
316 Map<UriTemplateRegistryKey,StoredValuesUriTemplate> uriRegistryEntriesMap =
317 super.getUriRegistryEntries();
318 List<String> tenantIds = getTenantBindingsReader().getTenantIds();
319 for (String tenantId : tenantIds) {
320 uriRegistryEntriesMap.putAll(getUriRegistryEntries(tenantId, getContactDocType(), UriTemplateFactory.CONTACT));
322 return uriRegistryEntriesMap;