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;
30 import javax.ws.rs.Consumes;
31 import javax.ws.rs.DELETE;
32 import javax.ws.rs.GET;
33 import javax.ws.rs.POST;
34 import javax.ws.rs.PUT;
35 import javax.ws.rs.Path;
36 import javax.ws.rs.PathParam;
37 import javax.ws.rs.Produces;
38 import javax.ws.rs.WebApplicationException;
39 import javax.ws.rs.core.Context;
40 import javax.ws.rs.core.MultivaluedMap;
41 import javax.ws.rs.core.Response;
42 import javax.ws.rs.core.UriBuilder;
43 import javax.ws.rs.core.UriInfo;
45 import org.collectionspace.services.client.*;
46 import org.collectionspace.services.common.CSWebApplicationException;
47 import org.collectionspace.services.common.StoredValuesUriTemplate;
48 import org.collectionspace.services.common.UriTemplateFactory;
49 import org.collectionspace.services.common.UriTemplateRegistryKey;
50 import org.collectionspace.services.common.vocabulary.AuthorityResource;
51 import org.collectionspace.services.common.context.ServiceContext;
52 import org.collectionspace.services.common.document.DocumentFilter;
53 import org.collectionspace.services.common.document.DocumentHandler;
54 import org.collectionspace.services.contact.ContactResource;
55 import org.collectionspace.services.contact.ContactsCommon;
56 import org.collectionspace.services.contact.ContactJAXBSchema;
57 import org.collectionspace.services.contact.nuxeo.ContactConstants;
58 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
59 import org.collectionspace.services.jaxb.AbstractCommonList;
60 import org.jboss.resteasy.util.HttpResponseCodes;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
65 * The Class AuthorityResourceWithContacts.
67 @Consumes("application/xml")
68 @Produces("application/xml")
69 public abstract class AuthorityResourceWithContacts<AuthCommon, AuthItemHandler> extends //FIXME: REM - Why is this resource in this package instead of somewhere in 'common'?
70 AuthorityResource<AuthCommon, AuthItemHandler> {
72 private ContactResource contactResource = new ContactResource(); // Warning: ContactResource is a singleton.
73 final Logger logger = LoggerFactory.getLogger(AuthorityResourceWithContacts.class);
75 public AuthorityResourceWithContacts(
76 Class<AuthCommon> authCommonClass, Class<?> resourceClass,
77 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
78 super(authCommonClass, resourceClass,
79 authorityCommonSchemaName, authorityItemCommonSchemaName);
82 public abstract String getItemServiceName();
84 public String getContactServiceName() {
85 return contactResource.getServiceName();
88 private DocumentHandler createContactDocumentHandler(
89 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
90 String inItem) throws Exception {
92 return createContactDocumentHandler(ctx, inAuthority, inItem, ui);
95 private DocumentHandler createContactDocumentHandler(
96 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
97 String inItem, UriInfo ui) throws Exception {
98 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler) createDocumentHandler(
100 ctx.getCommonPartLabel(getContactServiceName()),
101 ContactsCommon.class);
102 docHandler.setInAuthority(inAuthority);
103 docHandler.setInItem(inItem);
104 docHandler.getServiceContext().setUriInfo(ui);
108 /*************************************************************************
109 * Contact parts - this is a sub-resource of the AuthorityItem
110 * @param parentspecifier either a CSID or one of the urn forms
111 * @param itemspecifier either a CSID or one of the urn forms
113 *************************************************************************/
115 @Path("{parentcsid}/items/{itemcsid}/contacts")
116 public Response createContact(
117 @PathParam("parentcsid") String parentspecifier,
118 @PathParam("itemcsid") String itemspecifier,
120 @Context UriInfo ui) {
122 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
123 String parentcsid = lookupParentCSID(parentspecifier, "createContact(authority)", "CREATE_ITEM_CONTACT", null);
125 ServiceContext itemCtx = createServiceContext(getItemServiceName());
126 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "createContact(item)", "CREATE_ITEM_CONTACT");
128 // Note that we have to create the service context and document
129 // handler for the Contact service, not the main service.
130 ServiceContext ctx = createServiceContext(getContactServiceName(), input);
131 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, ui);
132 String csid = getRepositoryClient(ctx).create(ctx, handler);
133 UriBuilder path = UriBuilder.fromResource(resourceClass);
134 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
135 Response response = Response.created(path.build()).build();
137 } catch (Exception e) {
139 "Create Contact failed; one of the requested specifiers for authority:"
140 + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
146 * Gets the contact list.
148 * @param parentspecifier either a CSID or one of the urn forms
149 * @param itemspecifier either a CSID or one of the urn forms
152 * @return the contact list
155 @Produces({"application/xml"})
156 @Path("{parentcsid}/items/{itemcsid}/contacts/")
157 public AbstractCommonList getContactList(
158 @PathParam("parentcsid") String parentspecifier,
159 @PathParam("itemcsid") String itemspecifier,
160 @Context UriInfo uriInfo) {
161 AbstractCommonList contactObjectList = new AbstractCommonList();
164 ServiceContext ctx = createServiceContext(getContactServiceName(), uriInfo);
165 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
167 String parentcsid = lookupParentCSID(parentspecifier, "getContactList(parent)", "GET_CONTACT_LIST", null);
168 ServiceContext itemCtx = createServiceContext(getItemServiceName());
169 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "getContactList(item)", "GET_CONTACT_LIST");
171 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, uriInfo);
172 DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
173 myFilter.appendWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":"
174 + ContactJAXBSchema.IN_AUTHORITY
175 + "='" + parentcsid + "'"
176 + IQueryManager.SEARCH_QUALIFIER_AND
177 + ContactJAXBSchema.CONTACTS_COMMON + ":"
178 + ContactJAXBSchema.IN_ITEM
179 + "='" + itemcsid + "'",
180 IQueryManager.SEARCH_QUALIFIER_AND); // "AND" this clause to any existing
181 getRepositoryClient(ctx).getFiltered(ctx, handler);
182 contactObjectList = (AbstractCommonList) handler.getCommonPartList();
183 } catch (Exception e) {
185 "Get ContactList failed; one of the requested specifiers for authority:"
186 + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
190 return contactObjectList;
196 * @param parentspecifier either a CSID or one of the urn forms
197 * @param itemspecifier either a CSID or one of the urn forms
198 * @param csid the csid
200 * @return the contact
203 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
204 public String getContact(
205 @PathParam("parentcsid") String parentspecifier,
206 @PathParam("itemcsid") String itemspecifier,
207 @PathParam("csid") String csid) {
208 PoxPayloadOut result = null;
210 String parentcsid = lookupParentCSID(parentspecifier, "getContact(parent)", "GET_ITEM_CONTACT", null);
212 ServiceContext itemCtx = createServiceContext(getItemServiceName());
213 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "getContact(item)", "GET_ITEM_CONTACT");
215 // Note that we have to create the service context and document handler for the Contact service, not the main service.
216 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getContactServiceName());
217 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
218 getRepositoryClient(ctx).get(ctx, csid, handler);
219 result = ctx.getOutput();
220 } catch (Exception e) {
221 throw bigReThrow(e, "Get failed, the requested Contact CSID:" + csid
222 + ": or one of the specifiers for authority:" + parentspecifier
223 + ": and item:" + itemspecifier + ": was not found.",
226 if (result == null) {
227 Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed, the requested Contact CSID:" + csid + ": was not found.").type("text/plain").build();
228 throw new CSWebApplicationException(response);
230 return result.toXML();
236 * @param parentspecifier either a CSID or one of the urn forms
237 * @param itemspecifier either a CSID or one of the urn forms
238 * @param csid the csid
240 * @return the multipart output
243 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
244 public String updateContact(
245 @PathParam("parentcsid") String parentspecifier,
246 @PathParam("itemcsid") String itemspecifier,
247 @PathParam("csid") String csid,
249 PoxPayloadOut result = null;
251 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
252 String parentcsid = lookupParentCSID(parentspecifier, "updateContact(authority)", "UPDATE_CONTACT", null);
254 ServiceContext itemCtx = createServiceContext(getItemServiceName());
255 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "updateContact(item)", "UPDATE_CONTACT");
257 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
258 // Note that we have to create the service context and document handler for the Contact service, not the main service.
259 ctx = createServiceContext(getContactServiceName(), theUpdate);
260 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
261 getRepositoryClient(ctx).update(ctx, csid, handler);
262 result = ctx.getOutput();
263 } catch (Exception e) {
264 throw bigReThrow(e, "Update failed, the requested Contact CSID:" + csid
265 + ": or one of the specifiers for authority:" + parentspecifier
266 + ": and item:" + itemspecifier + ": was not found.",
269 return result.toXML();
275 * @param parentspecifier either a CSID or one of the urn forms
276 * @param itemspecifier either a CSID or one of the urn forms
277 * @param csid the csid
279 * @return the response
282 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
283 public Response deleteContact(
284 @PathParam("parentcsid") String parentspecifier,
285 @PathParam("itemcsid") String itemspecifier,
286 @PathParam("csid") String csid) {
288 String parentcsid = lookupParentCSID(parentspecifier, "deleteContact(authority)", "DELETE_CONTACT", null);
290 ServiceContext itemCtx = createServiceContext(getItemServiceName());
291 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "deleteContact(item)", "DELETE_CONTACT");
292 //NOTE: itemcsid is not used below. Leaving the above call in for possible side effects??? CSPACE-3175
294 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
295 // Note that we have to create the service context for the Contact service, not the main service.
296 ctx = createServiceContext(getContactServiceName());
297 DocumentHandler handler = createDocumentHandler(ctx);
298 getRepositoryClient(ctx).delete(ctx, csid, handler);
299 return Response.status(HttpResponseCodes.SC_OK).build();
300 } catch (Exception e) {
301 throw bigReThrow(e, "DELETE failed, the requested Contact CSID:" + csid
302 + ": or one of the specifiers for authority:" + parentspecifier
303 + ": and item:" + itemspecifier + ": was not found.", csid);
307 protected String getContactDocType() {
308 return ContactConstants.NUXEO_DOCTYPE;
312 * Returns a UriRegistry entry: a map of tenant-qualified URI templates
313 * for the current resource, for all tenants
315 * @return a map of URI templates for the current resource, for all tenants
318 public Map<UriTemplateRegistryKey,StoredValuesUriTemplate> getUriRegistryEntries() {
319 Map<UriTemplateRegistryKey,StoredValuesUriTemplate> uriRegistryEntriesMap =
320 super.getUriRegistryEntries();
321 List<String> tenantIds = getTenantBindingsReader().getTenantIds();
322 for (String tenantId : tenantIds) {
323 uriRegistryEntriesMap.putAll(getUriRegistryEntries(tenantId, getContactDocType(), UriTemplateFactory.CONTACT));
325 return uriRegistryEntriesMap;