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.setWhereClause(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 getRepositoryClient(ctx).getFiltered(ctx, handler);
271 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
272 } catch (UnauthorizedException ue) {
273 Response response = Response.status(
274 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
275 throw new WebApplicationException(response);
276 } catch (DocumentNotFoundException dnfe) {
277 if (logger.isDebugEnabled()) {
278 logger.debug("getContactList", dnfe);
280 Response response = Response.status(Response.Status.NOT_FOUND)
281 .entity("Get ContactList failed; one of the requested specifiers for authority:"
282 +parentspecifier+": and item:"+itemspecifier+": was not found.")
283 .type("text/plain").build();
284 throw new WebApplicationException(response);
285 } catch (Exception e) {
286 if (logger.isDebugEnabled()) {
287 logger.debug("Caught exception in getContactsList", e);
289 Response response = Response.status(
290 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
291 throw new WebApplicationException(response);
293 return contactObjectList;
299 * @param parentspecifier either a CSID or one of the urn forms
300 * @param itemspecifier either a CSID or one of the urn forms
301 * @param csid the csid
303 * @return the contact
306 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
307 public String getContact(
308 @PathParam("parentcsid") String parentspecifier,
309 @PathParam("itemcsid") String itemspecifier,
310 @PathParam("csid") String csid) {
311 PoxPayloadOut result = null;
313 Specifier parentSpec = getSpecifier(parentspecifier,
314 "getContact(parent)", "GET_ITEM_CONTACT");
315 Specifier itemSpec = getSpecifier(itemspecifier,
316 "getContact(item)", "GET_ITEM_CONTACT");
317 // Note that we have to create the service context for the Items, not the main service
318 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
320 if(parentSpec.form==SpecifierForm.CSID) {
321 parentcsid = parentSpec.value;
323 String whereClause = buildWhereForAuthByName(parentSpec.value);
324 ctx = createServiceContext(getServiceName());
325 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
328 if(itemSpec.form==SpecifierForm.CSID) {
329 itemcsid = itemSpec.value;
331 String itemWhereClause =
332 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
333 ctx = createServiceContext(getItemServiceName());
334 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
336 // Note that we have to create the service context and document
337 // handler for the Contact service, not the main service.
338 ctx = createServiceContext(getContactServiceName());
339 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
340 getRepositoryClient(ctx).get(ctx, csid, handler);
341 result = ctx.getOutput();
342 } catch (UnauthorizedException ue) {
343 Response response = Response.status(
344 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
345 throw new WebApplicationException(response);
346 } catch (DocumentNotFoundException dnfe) {
347 if (logger.isDebugEnabled()) {
348 logger.debug("getContact", dnfe);
350 Response response = Response.status(Response.Status.NOT_FOUND)
351 .entity("Get failed, the requested Contact CSID:" + csid +
352 ": or one of the specifiers for authority:"+parentspecifier+
353 ": and item:"+itemspecifier+": was not found.")
354 .type("text/plain").build();
355 throw new WebApplicationException(response);
356 } catch (Exception e) {
357 if (logger.isDebugEnabled()) {
358 logger.debug("getContact", e);
360 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
361 .entity("Get contact failed")
362 .type("text/plain").build();
363 throw new WebApplicationException(response);
365 if (result == null) {
366 Response response = Response.status(Response.Status.NOT_FOUND)
367 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
368 .type("text/plain").build();
369 throw new WebApplicationException(response);
371 return result.toXML();
378 * @param parentspecifier either a CSID or one of the urn forms
379 * @param itemspecifier either a CSID or one of the urn forms
380 * @param csid the csid
381 * @param theUpdate the the update
383 * @return the multipart output
386 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
387 public String updateContact(
388 @PathParam("parentcsid") String parentspecifier,
389 @PathParam("itemcsid") String itemspecifier,
390 @PathParam("csid") String csid,
392 PoxPayloadOut result = null;
394 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
395 Specifier parentSpec = getSpecifier(parentspecifier,
396 "updateContact(authority)", "UPDATE_CONTACT");
397 Specifier itemSpec = getSpecifier(itemspecifier,
398 "updateContact(item)", "UPDATE_CONTACT");
399 // Note that we have to create the service context for the Items, not the main service
400 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
402 if(parentSpec.form==SpecifierForm.CSID) {
403 parentcsid = parentSpec.value;
405 String whereClause = buildWhereForAuthByName(parentSpec.value);
406 ctx = createServiceContext(getServiceName());
407 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
410 if(itemSpec.form==SpecifierForm.CSID) {
411 itemcsid = itemSpec.value;
413 String itemWhereClause =
414 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
415 ctx = createServiceContext(getItemServiceName());
416 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
418 // Note that we have to create the service context and document
419 // handler for the Contact service, not the main service.
420 ctx = createServiceContext(getContactServiceName(), theUpdate);
421 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
422 getRepositoryClient(ctx).update(ctx, csid, handler);
423 result = ctx.getOutput();
424 } catch (BadRequestException bre) {
425 Response response = Response.status(
426 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
427 throw new WebApplicationException(response);
428 } catch (UnauthorizedException ue) {
429 Response response = Response.status(
430 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
431 throw new WebApplicationException(response);
432 } catch (DocumentNotFoundException dnfe) {
433 if (logger.isDebugEnabled()) {
434 logger.debug("caught exception in updateContact", dnfe);
436 Response response = Response.status(Response.Status.NOT_FOUND)
437 .entity("Update failed, the requested Contact CSID:" + csid +
438 ": or one of the specifiers for authority:"+parentspecifier+
439 ": and item:"+itemspecifier+": was not found.")
440 .type("text/plain").build();
441 throw new WebApplicationException(response);
442 } catch (Exception e) {
443 Response response = Response.status(
444 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
445 throw new WebApplicationException(response);
447 return result.toXML();
453 * @param parentspecifier either a CSID or one of the urn forms
454 * @param itemspecifier either a CSID or one of the urn forms
455 * @param csid the csid
457 * @return the response
460 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
461 public Response deleteContact(
462 @PathParam("parentcsid") String parentspecifier,
463 @PathParam("itemcsid") String itemspecifier,
464 @PathParam("csid") String csid) {
466 Specifier parentSpec = getSpecifier(parentspecifier,
467 "deleteContact(authority)", "DELETE_CONTACT");
468 Specifier itemSpec = getSpecifier(itemspecifier,
469 "deleteContact(item)", "DELETE_CONTACT");
470 // Note that we have to create the service context for the Items, not the main service
471 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
473 if(parentSpec.form==SpecifierForm.CSID) {
474 parentcsid = parentSpec.value;
476 String whereClause = buildWhereForAuthByName(parentSpec.value);
477 ctx = createServiceContext(getServiceName());
478 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
481 if(itemSpec.form==SpecifierForm.CSID) {
482 itemcsid = itemSpec.value;
484 String itemWhereClause =
485 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
486 ctx = createServiceContext(getItemServiceName());
487 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause);
489 // Note that we have to create the service context for the
490 // Contact service, not the main service.
491 ctx = createServiceContext(getContactServiceName());
492 getRepositoryClient(ctx).delete(ctx, csid);
493 return Response.status(HttpResponseCodes.SC_OK).build();
494 } catch (UnauthorizedException ue) {
495 Response response = Response.status(
496 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
497 throw new WebApplicationException(response);
498 } catch (DocumentNotFoundException dnfe) {
499 if (logger.isDebugEnabled()) {
500 logger.debug("Caught exception in deleteContact", dnfe);
502 Response response = Response.status(Response.Status.NOT_FOUND)
503 .entity("Delete failed, the requested Contact CSID:" + csid +
504 ": or one of the specifiers for authority:"+parentspecifier+
505 ": and item:"+itemspecifier+": was not found.")
506 .type("text/plain").build();
507 throw new WebApplicationException(response);
508 } catch (Exception e) {
509 Response response = Response.status(
510 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
511 throw new WebApplicationException(response);