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.common.vocabulary;
26 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.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;
43 import org.collectionspace.services.client.*;
44 import org.collectionspace.services.common.CSWebApplicationException;
45 import org.collectionspace.services.common.StoredValuesUriTemplate;
46 import org.collectionspace.services.common.UriTemplateFactory;
47 import org.collectionspace.services.common.UriTemplateRegistryKey;
48 import org.collectionspace.services.common.vocabulary.AuthorityResource;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.DocumentFilter;
51 import org.collectionspace.services.common.document.DocumentHandler;
52 import org.collectionspace.services.contact.ContactResource;
53 import org.collectionspace.services.contact.ContactsCommon;
54 import org.collectionspace.services.contact.ContactJAXBSchema;
55 import org.collectionspace.services.contact.nuxeo.ContactConstants;
56 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
57 import org.collectionspace.services.jaxb.AbstractCommonList;
58 import org.jboss.resteasy.util.HttpResponseCodes;
59 import org.slf4j.Logger;
60 import org.slf4j.LoggerFactory;
63 * The Class AuthorityResourceWithContacts.
65 @Consumes("application/xml")
66 @Produces("application/xml")
67 public abstract class AuthorityResourceWithContacts<AuthCommon, AuthItemHandler> extends //FIXME: REM - Why is this resource in this package instead of somewhere in 'common'?
68 AuthorityResource<AuthCommon, AuthItemHandler> {
70 private ContactResource contactResource = new ContactResource(); // Warning: ContactResource is a singleton.
71 final Logger logger = LoggerFactory.getLogger(AuthorityResourceWithContacts.class);
73 public AuthorityResourceWithContacts(
74 Class<AuthCommon> authCommonClass, Class<?> resourceClass,
75 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
76 super(authCommonClass, resourceClass,
77 authorityCommonSchemaName, authorityItemCommonSchemaName);
80 public abstract String getItemServiceName();
82 public String getContactServiceName() {
83 return contactResource.getServiceName();
86 private DocumentHandler createContactDocumentHandler(
87 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
88 String inItem) throws Exception {
90 return createContactDocumentHandler(ctx, inAuthority, inItem, ui);
93 private DocumentHandler createContactDocumentHandler(
94 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
95 String inItem, UriInfo ui) throws Exception {
96 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler) createDocumentHandler(
98 ctx.getCommonPartLabel(getContactServiceName()),
99 ContactsCommon.class);
100 docHandler.setInAuthority(inAuthority);
101 docHandler.setInItem(inItem);
102 docHandler.getServiceContext().setUriInfo(ui);
106 /*************************************************************************
107 * Contact parts - this is a sub-resource of the AuthorityItem
108 * @param parentspecifier either a CSID or one of the urn forms
109 * @param itemspecifier either a CSID or one of the urn forms
111 *************************************************************************/
113 @Path("{parentcsid}/items/{itemcsid}/contacts")
114 public Response createContact(
115 @PathParam("parentcsid") String parentspecifier,
116 @PathParam("itemcsid") String itemspecifier,
118 @Context UriInfo ui) {
120 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
121 String parentcsid = lookupParentCSID(parentspecifier, "createContact(authority)", "CREATE_ITEM_CONTACT", null);
123 ServiceContext itemCtx = createServiceContext(getItemServiceName());
124 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "createContact(item)", "CREATE_ITEM_CONTACT");
126 // Note that we have to create the service context and document
127 // handler for the Contact service, not the main service.
128 ServiceContext ctx = createServiceContext(getContactServiceName(), input);
129 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, ui);
130 String csid = getRepositoryClient(ctx).create(ctx, handler);
132 UriBuilder path = UriBuilder.fromResource(resourceClass);
133 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
134 Response response = Response.created(path.build()).build();
136 } catch (Exception e) {
138 "Create Contact failed; one of the requested specifiers for authority:"
139 + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
144 public String createContact(ServiceContext existingCtx, String parentCsid, String itemCsid, PoxPayloadIn input,
145 UriInfo ui) throws Exception {
146 ServiceContext ctx = createServiceContext(getContactServiceName(), input);
147 if (existingCtx != null) {
148 Object repoSession = existingCtx.getCurrentRepositorySession();
149 if (repoSession != null) {
150 ctx.setCurrentRepositorySession(repoSession);
151 ctx.setProperties(existingCtx.getProperties());
155 DocumentHandler handler = createContactDocumentHandler(ctx, parentCsid, itemCsid, ui);
156 String csid = getRepositoryClient(ctx).create(ctx, handler);
162 * Gets the contact list.
164 * @param parentspecifier either a CSID or one of the urn forms
165 * @param itemspecifier either a CSID or one of the urn forms
168 * @return the contact list
171 @Produces({"application/xml"})
172 @Path("{parentcsid}/items/{itemcsid}/contacts/")
173 public AbstractCommonList getContactList(
174 @PathParam("parentcsid") String parentspecifier,
175 @PathParam("itemcsid") String itemspecifier,
176 @Context UriInfo uriInfo) {
177 AbstractCommonList contactObjectList = new AbstractCommonList();
180 ServiceContext ctx = createServiceContext(getContactServiceName(), uriInfo);
181 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
183 String parentcsid = lookupParentCSID(parentspecifier, "getContactList(parent)", "GET_CONTACT_LIST", null);
184 ServiceContext itemCtx = createServiceContext(getItemServiceName());
185 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "getContactList(item)", "GET_CONTACT_LIST");
187 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid, uriInfo);
188 DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
189 myFilter.appendWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":"
190 + ContactJAXBSchema.IN_AUTHORITY
191 + "='" + parentcsid + "'"
192 + IQueryManager.SEARCH_QUALIFIER_AND
193 + ContactJAXBSchema.CONTACTS_COMMON + ":"
194 + ContactJAXBSchema.IN_ITEM
195 + "='" + itemcsid + "'",
196 IQueryManager.SEARCH_QUALIFIER_AND); // "AND" this clause to any existing
197 getRepositoryClient(ctx).getFiltered(ctx, handler);
198 contactObjectList = (AbstractCommonList) handler.getCommonPartList();
199 } catch (Exception e) {
201 "Get ContactList failed; one of the requested specifiers for authority:"
202 + parentspecifier + ": and item:" + itemspecifier + ": was not found.",
206 return contactObjectList;
212 * @param parentspecifier either a CSID or one of the urn forms
213 * @param itemspecifier either a CSID or one of the urn forms
214 * @param csid the csid
216 * @return the contact
219 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
220 public String getContact(
221 @PathParam("parentcsid") String parentspecifier,
222 @PathParam("itemcsid") String itemspecifier,
223 @PathParam("csid") String csid) {
224 PoxPayloadOut result = null;
226 String parentcsid = lookupParentCSID(parentspecifier, "getContact(parent)", "GET_ITEM_CONTACT", null);
228 ServiceContext<PoxPayloadIn, PoxPayloadOut> itemCtx = createServiceContext(getItemServiceName());
229 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "getContact(item)", "GET_ITEM_CONTACT");
231 // Note that we have to create the service context and document handler for the Contact service, not the main service.
232 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getContactServiceName());
233 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
234 getRepositoryClient(ctx).get(ctx, csid, handler);
235 result = (PoxPayloadOut) ctx.getOutput();
236 } catch (Exception e) {
237 throw bigReThrow(e, "Get failed, the requested Contact CSID:" + csid
238 + ": or one of the specifiers for authority:" + parentspecifier
239 + ": and item:" + itemspecifier + ": was not found.",
242 if (result == null) {
243 Response response = Response.status(Response.Status.NOT_FOUND).entity("Get failed, the requested Contact CSID:" + csid + ": was not found.").type("text/plain").build();
244 throw new CSWebApplicationException(response);
246 return result.toXML();
252 * @param parentspecifier either a CSID or one of the urn forms
253 * @param itemspecifier either a CSID or one of the urn forms
254 * @param csid the csid
256 * @return the multipart output
259 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
260 public String updateContact(
261 @PathParam("parentcsid") String parentspecifier,
262 @PathParam("itemcsid") String itemspecifier,
263 @PathParam("csid") String csid,
265 PoxPayloadOut result = null;
267 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
268 String parentcsid = lookupParentCSID(parentspecifier, "updateContact(authority)", "UPDATE_CONTACT", null);
270 ServiceContext<PoxPayloadIn, PoxPayloadOut> itemCtx = createServiceContext(getItemServiceName());
271 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "updateContact(item)", "UPDATE_CONTACT");
273 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
274 // Note that we have to create the service context and document handler for the Contact service, not the main service.
275 ctx = createServiceContext(getContactServiceName(), theUpdate);
276 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
277 getRepositoryClient(ctx).update(ctx, csid, handler);
278 result = (PoxPayloadOut) ctx.getOutput();
279 } catch (Exception e) {
280 throw bigReThrow(e, "Update failed, the requested Contact CSID:" + csid
281 + ": or one of the specifiers for authority:" + parentspecifier
282 + ": and item:" + itemspecifier + ": was not found.",
285 return result.toXML();
291 * @param parentspecifier either a CSID or one of the urn forms
292 * @param itemspecifier either a CSID or one of the urn forms
293 * @param csid the csid
295 * @return the response
298 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
299 public Response deleteContact(
300 @PathParam("parentcsid") String parentspecifier,
301 @PathParam("itemcsid") String itemspecifier,
302 @PathParam("csid") String csid) {
304 String parentcsid = lookupParentCSID(parentspecifier, "deleteContact(authority)", "DELETE_CONTACT", null);
306 ServiceContext<PoxPayloadIn, PoxPayloadOut> itemCtx = createServiceContext(getItemServiceName());
307 String itemcsid = lookupItemCSID(itemCtx, itemspecifier, parentcsid, "deleteContact(item)", "DELETE_CONTACT");
308 //NOTE: itemcsid is not used below. Leaving the above call in for possible side effects??? CSPACE-3175
310 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
311 // Note that we have to create the service context for the Contact service, not the main service.
312 ctx = createServiceContext(getContactServiceName());
313 DocumentHandler handler = createDocumentHandler(ctx);
314 getRepositoryClient(ctx).delete(ctx, csid, handler);
315 return Response.status(HttpResponseCodes.SC_OK).build();
316 } catch (Exception e) {
317 throw bigReThrow(e, "DELETE failed, the requested Contact CSID:" + csid
318 + ": or one of the specifiers for authority:" + parentspecifier
319 + ": and item:" + itemspecifier + ": was not found.", csid);
323 protected String getContactDocType() {
324 return ContactConstants.NUXEO_DOCTYPE;
328 * Returns a UriRegistry entry: a map of tenant-qualified URI templates
329 * for the current resource, for all tenants
331 * @return a map of URI templates for the current resource, for all tenants
334 public Map<UriTemplateRegistryKey,StoredValuesUriTemplate> getUriRegistryEntries() {
335 Map<UriTemplateRegistryKey,StoredValuesUriTemplate> uriRegistryEntriesMap =
336 super.getUriRegistryEntries();
337 List<String> tenantIds = getTenantBindingsReader().getTenantIds();
338 for (String tenantId : tenantIds) {
339 uriRegistryEntriesMap.putAll(getUriRegistryEntries(tenantId, getContactDocType(), UriTemplateFactory.CONTACT));
341 return uriRegistryEntriesMap;