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.List;
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.QueryParam;
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;
44 import org.collectionspace.services.client.IQueryManager;
45 import org.collectionspace.services.client.PoxPayloadIn;
46 import org.collectionspace.services.client.PoxPayloadOut;
47 import org.collectionspace.services.common.vocabulary.AuthorityResource;
48 //import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
49 //import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
50 //import org.collectionspace.services.common.vocabulary.AuthorityResource.Specifier;
51 //import org.collectionspace.services.common.vocabulary.AuthorityResource.SpecifierForm;
52 //import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
53 //import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
54 //import org.collectionspace.services.common.ClientType;
55 //import org.collectionspace.services.common.ServiceMain;
56 //import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
57 //import org.collectionspace.services.common.authorityref.AuthorityRefList;
58 //import org.collectionspace.services.common.context.MultipartServiceContextImpl;
59 //import org.collectionspace.services.common.context.ServiceBindingUtils;
60 import org.collectionspace.services.common.context.ServiceContext;
61 import org.collectionspace.services.common.document.BadRequestException;
62 import org.collectionspace.services.common.document.DocumentFilter;
63 import org.collectionspace.services.common.document.DocumentHandler;
64 import org.collectionspace.services.common.document.DocumentNotFoundException;
65 //import org.collectionspace.services.common.document.DocumentWrapper;
66 import org.collectionspace.services.contact.ContactResource;
67 import org.collectionspace.services.contact.ContactsCommon;
68 import org.collectionspace.services.contact.ContactsCommonList;
69 import org.collectionspace.services.contact.ContactJAXBSchema;
70 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
71 //import org.collectionspace.services.common.repository.RepositoryClient;
72 import org.collectionspace.services.common.security.UnauthorizedException;
73 //import org.collectionspace.services.common.query.IQueryManager;
74 //import org.collectionspace.services.common.query.QueryManager;
75 //import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
76 //import org.jboss.remoting.samples.chat.exceptions.InvalidArgumentException;
77 import org.jboss.resteasy.util.HttpResponseCodes;
78 //import org.nuxeo.ecm.core.api.DocumentModel;
79 import org.slf4j.Logger;
80 import org.slf4j.LoggerFactory;
83 * The Class AuthorityResourceWithContacts.
85 @Consumes("application/xml")
86 @Produces("application/xml")
87 public abstract class AuthorityResourceWithContacts<AuthCommon, AuthCommonList, AuthItemCommonList, AuthItemHandler> extends //FIXME: REM - Why is this resource in this package instead of somewhere in 'common'?
88 AuthorityResource<AuthCommon, AuthCommonList, AuthItemCommonList, AuthItemHandler> {
90 /** The contact resource. */
91 private ContactResource contactResource = new ContactResource(); // Warning: ContactResource is a singleton.
93 final Logger logger = LoggerFactory.getLogger(AuthorityResourceWithContacts.class);
96 * Instantiates a new Authority resource.
98 public AuthorityResourceWithContacts(
99 Class<AuthCommon> authCommonClass, Class<?> resourceClass,
100 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
101 super(authCommonClass, resourceClass,
102 authorityCommonSchemaName, authorityItemCommonSchemaName);
105 public abstract String getItemServiceName();
108 * Gets the contact service name.
110 * @return the contact service name
112 public String getContactServiceName() {
113 return contactResource.getServiceName();
117 * Creates the contact document handler.
120 * @param inAuthority the in authority
121 * @param inItem the in item
123 * @return the document handler
125 * @throws Exception the exception
127 private DocumentHandler createContactDocumentHandler(
128 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String inAuthority,
129 String inItem) throws Exception {
131 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(
133 ctx.getCommonPartLabel(getContactServiceName()),
134 ContactsCommon.class);
135 docHandler.setInAuthority(inAuthority);
136 docHandler.setInItem(inItem);
141 /*************************************************************************
142 * Contact parts - this is a sub-resource of the AuthorityItem
143 * @param parentspecifier either a CSID or one of the urn forms
144 * @param itemspecifier either a CSID or one of the urn forms
147 *************************************************************************/
149 @Path("{parentcsid}/items/{itemcsid}/contacts")
150 public Response createContact(
151 @PathParam("parentcsid") String parentspecifier,
152 @PathParam("itemcsid") String itemspecifier,
155 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
156 Specifier parentSpec = getSpecifier(parentspecifier,
157 "createContact(authority)", "CREATE_ITEM_CONTACT");
158 Specifier itemSpec = getSpecifier(itemspecifier,
159 "createContact(item)", "CREATE_ITEM_CONTACT");
160 // Note that we have to create the service context for the Items, not the main service
161 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
163 if(parentSpec.form==SpecifierForm.CSID) {
164 parentcsid = parentSpec.value;
166 String whereClause = buildWhereForAuthByName(parentSpec.value);
167 ctx = createServiceContext(getServiceName());
168 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
171 if(itemSpec.form==SpecifierForm.CSID) {
172 itemcsid = itemSpec.value;
174 String itemWhereClause =
175 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
176 ctx = createServiceContext(getItemServiceName());
177 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
179 // Note that we have to create the service context and document
180 // handler for the Contact service, not the main service.
181 ctx = createServiceContext(getContactServiceName(), input);
182 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
183 String csid = getRepositoryClient(ctx).create(ctx, handler);
184 UriBuilder path = UriBuilder.fromResource(resourceClass);
185 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
186 Response response = Response.created(path.build()).build();
188 } catch (BadRequestException bre) {
189 Response response = Response.status(
190 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
191 throw new WebApplicationException(response);
192 } catch (UnauthorizedException ue) {
193 Response response = Response.status(
194 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
195 throw new WebApplicationException(response);
196 } catch (DocumentNotFoundException dnfe) {
197 if (logger.isDebugEnabled()) {
198 logger.debug("createContact", dnfe);
200 Response response = Response.status(Response.Status.NOT_FOUND)
201 .entity("Create Contact failed; one of the requested specifiers for authority:"
202 +parentspecifier+": and item:"+itemspecifier+": was not found.")
203 .type("text/plain").build();
204 throw new WebApplicationException(response);
205 } catch (Exception e) {
206 if (logger.isDebugEnabled()) {
207 logger.debug("Caught exception in createContact", e);
209 Response response = Response.status(
210 Response.Status.INTERNAL_SERVER_ERROR)
211 .entity("Attempt to create Contact failed.")
212 .type("text/plain").build();
213 throw new WebApplicationException(response);
219 * Gets the contact list.
221 * @param parentspecifier either a CSID or one of the urn forms
222 * @param itemspecifier either a CSID or one of the urn forms
225 * @return the contact list
228 @Produces({"application/xml"})
229 @Path("{parentcsid}/items/{itemcsid}/contacts/")
230 public ContactsCommonList getContactList(
231 @PathParam("parentcsid") String parentspecifier,
232 @PathParam("itemcsid") String itemspecifier,
233 @Context UriInfo ui) {
234 ContactsCommonList contactObjectList = new ContactsCommonList();
236 Specifier parentSpec = getSpecifier(parentspecifier,
237 "getContactList(parent)", "GET_CONTACT_LIST");
238 Specifier itemSpec = getSpecifier(itemspecifier,
239 "getContactList(item)", "GET_CONTACT_LIST");
240 // Note that we have to create the service context for the Items, not the main service
241 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
243 if(parentSpec.form==SpecifierForm.CSID) {
244 parentcsid = parentSpec.value;
246 String whereClause = buildWhereForAuthByName(parentSpec.value);
247 ctx = createServiceContext(getServiceName());
248 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
251 if(itemSpec.form==SpecifierForm.CSID) {
252 itemcsid = itemSpec.value;
254 String itemWhereClause =
255 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
256 ctx = createServiceContext(getItemServiceName());
257 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
259 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
260 ctx = createServiceContext(getContactServiceName(), queryParams);
261 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
262 DocumentFilter myFilter = handler.getDocumentFilter(); //new DocumentFilter();
263 myFilter.appendWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
264 ContactJAXBSchema.IN_AUTHORITY +
265 "='" + parentcsid + "'" +
266 IQueryManager.SEARCH_QUALIFIER_AND +
267 ContactJAXBSchema.CONTACTS_COMMON + ":" +
268 ContactJAXBSchema.IN_ITEM +
269 "='" + itemcsid + "'",
270 IQueryManager.SEARCH_QUALIFIER_AND); // "AND" this clause to any existing
271 getRepositoryClient(ctx).getFiltered(ctx, handler);
272 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
273 } catch (UnauthorizedException ue) {
274 Response response = Response.status(
275 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
276 throw new WebApplicationException(response);
277 } catch (DocumentNotFoundException dnfe) {
278 if (logger.isDebugEnabled()) {
279 logger.debug("getContactList", dnfe);
281 Response response = Response.status(Response.Status.NOT_FOUND)
282 .entity("Get ContactList failed; one of the requested specifiers for authority:"
283 +parentspecifier+": and item:"+itemspecifier+": was not found.")
284 .type("text/plain").build();
285 throw new WebApplicationException(response);
286 } catch (Exception e) {
287 if (logger.isDebugEnabled()) {
288 logger.debug("Caught exception in getContactsList", e);
290 Response response = Response.status(
291 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
292 throw new WebApplicationException(response);
294 return contactObjectList;
300 * @param parentspecifier either a CSID or one of the urn forms
301 * @param itemspecifier either a CSID or one of the urn forms
302 * @param csid the csid
304 * @return the contact
307 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
308 public String getContact(
309 @PathParam("parentcsid") String parentspecifier,
310 @PathParam("itemcsid") String itemspecifier,
311 @PathParam("csid") String csid) {
312 PoxPayloadOut result = null;
314 Specifier parentSpec = getSpecifier(parentspecifier,
315 "getContact(parent)", "GET_ITEM_CONTACT");
316 Specifier itemSpec = getSpecifier(itemspecifier,
317 "getContact(item)", "GET_ITEM_CONTACT");
318 // Note that we have to create the service context for the Items, not the main service
319 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
321 if(parentSpec.form==SpecifierForm.CSID) {
322 parentcsid = parentSpec.value;
324 String whereClause = buildWhereForAuthByName(parentSpec.value);
325 ctx = createServiceContext(getServiceName());
326 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
329 if(itemSpec.form==SpecifierForm.CSID) {
330 itemcsid = itemSpec.value;
332 String itemWhereClause =
333 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
334 ctx = createServiceContext(getItemServiceName());
335 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
337 // Note that we have to create the service context and document
338 // handler for the Contact service, not the main service.
339 ctx = createServiceContext(getContactServiceName());
340 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
341 getRepositoryClient(ctx).get(ctx, csid, handler);
342 result = ctx.getOutput();
343 } catch (UnauthorizedException ue) {
344 Response response = Response.status(
345 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
346 throw new WebApplicationException(response);
347 } catch (DocumentNotFoundException dnfe) {
348 if (logger.isDebugEnabled()) {
349 logger.debug("getContact", dnfe);
351 Response response = Response.status(Response.Status.NOT_FOUND)
352 .entity("Get failed, the requested Contact CSID:" + csid +
353 ": or one of the specifiers for authority:"+parentspecifier+
354 ": and item:"+itemspecifier+": was not found.")
355 .type("text/plain").build();
356 throw new WebApplicationException(response);
357 } catch (Exception e) {
358 if (logger.isDebugEnabled()) {
359 logger.debug("getContact", e);
361 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
362 .entity("Get contact failed")
363 .type("text/plain").build();
364 throw new WebApplicationException(response);
366 if (result == null) {
367 Response response = Response.status(Response.Status.NOT_FOUND)
368 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
369 .type("text/plain").build();
370 throw new WebApplicationException(response);
372 return result.toXML();
379 * @param parentspecifier either a CSID or one of the urn forms
380 * @param itemspecifier either a CSID or one of the urn forms
381 * @param csid the csid
382 * @param theUpdate the the update
384 * @return the multipart output
387 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
388 public String updateContact(
389 @PathParam("parentcsid") String parentspecifier,
390 @PathParam("itemcsid") String itemspecifier,
391 @PathParam("csid") String csid,
393 PoxPayloadOut result = null;
395 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
396 Specifier parentSpec = getSpecifier(parentspecifier,
397 "updateContact(authority)", "UPDATE_CONTACT");
398 Specifier itemSpec = getSpecifier(itemspecifier,
399 "updateContact(item)", "UPDATE_CONTACT");
400 // Note that we have to create the service context for the Items, not the main service
401 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
403 if(parentSpec.form==SpecifierForm.CSID) {
404 parentcsid = parentSpec.value;
406 String whereClause = buildWhereForAuthByName(parentSpec.value);
407 ctx = createServiceContext(getServiceName());
408 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
411 if(itemSpec.form==SpecifierForm.CSID) {
412 itemcsid = itemSpec.value;
414 String itemWhereClause =
415 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
416 ctx = createServiceContext(getItemServiceName());
417 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
419 // Note that we have to create the service context and document
420 // handler for the Contact service, not the main service.
421 ctx = createServiceContext(getContactServiceName(), theUpdate);
422 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
423 getRepositoryClient(ctx).update(ctx, csid, handler);
424 result = ctx.getOutput();
425 } catch (BadRequestException bre) {
426 Response response = Response.status(
427 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
428 throw new WebApplicationException(response);
429 } catch (UnauthorizedException ue) {
430 Response response = Response.status(
431 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
432 throw new WebApplicationException(response);
433 } catch (DocumentNotFoundException dnfe) {
434 if (logger.isDebugEnabled()) {
435 logger.debug("caught exception in updateContact", dnfe);
437 Response response = Response.status(Response.Status.NOT_FOUND)
438 .entity("Update failed, the requested Contact CSID:" + csid +
439 ": or one of the specifiers for authority:"+parentspecifier+
440 ": and item:"+itemspecifier+": was not found.")
441 .type("text/plain").build();
442 throw new WebApplicationException(response);
443 } catch (Exception e) {
444 Response response = Response.status(
445 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
446 throw new WebApplicationException(response);
448 return result.toXML();
454 * @param parentspecifier either a CSID or one of the urn forms
455 * @param itemspecifier either a CSID or one of the urn forms
456 * @param csid the csid
458 * @return the response
461 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
462 public Response deleteContact(
463 @PathParam("parentcsid") String parentspecifier,
464 @PathParam("itemcsid") String itemspecifier,
465 @PathParam("csid") String csid) {
467 Specifier parentSpec = getSpecifier(parentspecifier,
468 "deleteContact(authority)", "DELETE_CONTACT");
469 Specifier itemSpec = getSpecifier(itemspecifier,
470 "deleteContact(item)", "DELETE_CONTACT");
471 // Note that we have to create the service context for the Items, not the main service
472 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
474 if(parentSpec.form==SpecifierForm.CSID) {
475 parentcsid = parentSpec.value;
477 String whereClause = buildWhereForAuthByName(parentSpec.value);
478 ctx = createServiceContext(getServiceName());
479 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
482 if(itemSpec.form==SpecifierForm.CSID) {
483 itemcsid = itemSpec.value;
485 String itemWhereClause =
486 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
487 ctx = createServiceContext(getItemServiceName());
488 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
490 // Note that we have to create the service context for the
491 // Contact service, not the main service.
492 ctx = createServiceContext(getContactServiceName());
493 getRepositoryClient(ctx).delete(ctx, csid);
494 return Response.status(HttpResponseCodes.SC_OK).build();
495 } catch (UnauthorizedException ue) {
496 Response response = Response.status(
497 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
498 throw new WebApplicationException(response);
499 } catch (DocumentNotFoundException dnfe) {
500 if (logger.isDebugEnabled()) {
501 logger.debug("Caught exception in deleteContact", dnfe);
503 Response response = Response.status(Response.Status.NOT_FOUND)
504 .entity("Delete failed, the requested Contact CSID:" + csid +
505 ": or one of the specifiers for authority:"+parentspecifier+
506 ": and item:"+itemspecifier+": was not found.")
507 .type("text/plain").build();
508 throw new WebApplicationException(response);
509 } catch (Exception e) {
510 Response response = Response.status(
511 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
512 throw new WebApplicationException(response);