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.organization;
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.OrgAuthorityJAXBSchema;
45 import org.collectionspace.services.OrganizationJAXBSchema;
46 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
47 import org.collectionspace.services.common.ClientType;
48 import org.collectionspace.services.common.ServiceMain;
49 import org.collectionspace.services.common.authorityref.AuthorityRefList;
50 import org.collectionspace.services.common.context.MultipartServiceContext;
51 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
52 //import org.collectionspace.services.common.context.MultipartServiceContextFactory;
53 import org.collectionspace.services.common.context.ServiceBindingUtils;
54 import org.collectionspace.services.common.context.ServiceContext;
55 import org.collectionspace.services.common.document.BadRequestException;
56 import org.collectionspace.services.common.document.DocumentFilter;
57 import org.collectionspace.services.common.document.DocumentHandler;
58 import org.collectionspace.services.common.document.DocumentNotFoundException;
59 import org.collectionspace.services.common.document.DocumentWrapper;
60 import org.collectionspace.services.common.query.IQueryManager;
61 import org.collectionspace.services.contact.ContactResource;
62 import org.collectionspace.services.contact.ContactsCommon;
63 import org.collectionspace.services.contact.ContactsCommonList;
64 import org.collectionspace.services.contact.ContactJAXBSchema;
65 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
66 import org.collectionspace.services.common.security.UnauthorizedException;
67 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
68 import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler;
69 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
70 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
71 import org.jboss.resteasy.util.HttpResponseCodes;
72 import org.nuxeo.ecm.core.api.DocumentModel;
73 import org.slf4j.Logger;
74 import org.slf4j.LoggerFactory;
77 * The Class OrgAuthorityResource.
79 @Path("/orgauthorities")
80 @Consumes("multipart/mixed")
81 @Produces("multipart/mixed")
82 public class OrgAuthorityResource extends
83 AbstractMultiPartCollectionSpaceResourceImpl {
85 /** The Constant orgAuthorityServiceName. */
86 private final static String orgAuthorityServiceName = "orgauthorities";
88 /** The Constant organizationServiceName. */
89 private final static String organizationServiceName = "organizations";
92 final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
93 //FIXME retrieve client type from configuration
94 /** The Constant CLIENT_TYPE. */
95 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
97 /** The contact resource. */
98 private ContactResource contactResource = new ContactResource();
101 * Instantiates a new org authority resource.
103 public OrgAuthorityResource() {
108 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
111 protected String getVersionString() {
112 /** The last change revision. */
113 final String lastChangeRevision = "$LastChangedRevision$";
114 return lastChangeRevision;
118 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
121 public String getServiceName() {
122 return orgAuthorityServiceName;
126 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
129 public Class<OrgauthoritiesCommon> getCommonPartClass() {
130 return OrgauthoritiesCommon.class;
134 * Gets the item service name.
136 * @return the item service name
138 public String getItemServiceName() {
139 return organizationServiceName;
143 * Gets the contact service name.
145 * @return the contact service name
147 public String getContactServiceName() {
148 return contactResource.getServiceName();
152 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
153 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
159 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
162 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
163 // DocumentHandler docHandler =ctx.getDocumentHandler();
164 // if (ctx.getInput() != null) {
165 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), OrgauthoritiesCommon.class);
166 // if (obj != null) {
167 // docHandler.setCommonPart((OrgauthoritiesCommon) obj);
170 // return docHandler;
174 * Creates the item document handler.
177 * @param inAuthority the in authority
179 * @return the document handler
181 * @throws Exception the exception
183 private DocumentHandler createItemDocumentHandler(
184 ServiceContext<MultipartInput, MultipartOutput> ctx,
185 String inAuthority) throws Exception {
186 OrganizationDocumentModelHandler docHandler = (OrganizationDocumentModelHandler)createDocumentHandler(
188 ctx.getCommonPartLabel(getItemServiceName()),
189 OrganizationsCommon.class);
190 docHandler.setInAuthority(inAuthority);
193 // ((OrganizationDocumentModelHandler) docHandler).setInAuthority(inAuthority);
194 // if (ctx.getInput() != null) {
195 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
196 // OrganizationsCommon.class);
197 // if (obj != null) {
198 // docHandler.setCommonPart((OrganizationsCommon) obj);
206 * Creates the contact document handler.
209 * @param inAuthority the in authority
210 * @param inItem the in item
212 * @return the document handler
214 * @throws Exception the exception
216 private DocumentHandler createContactDocumentHandler(
217 ServiceContext<MultipartInput, MultipartOutput> ctx, String inAuthority,
218 String inItem) throws Exception {
220 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(
222 ctx.getCommonPartLabel(getContactServiceName()),
223 ContactsCommon.class);
224 docHandler.setInAuthority(inAuthority);
225 docHandler.setInItem(inItem);
227 // DocumentHandler docHandler = ctx.getDocumentHandler();
228 // // Set the inAuthority and inItem values, which specify the
229 // // parent authority (e.g. PersonAuthority, OrgAuthority) and the item
230 // // (e.g. Person, Organization) with which the Contact is associated.
231 // ((ContactDocumentModelHandler) docHandler).setInAuthority(inAuthority);
232 // ((ContactDocumentModelHandler) docHandler).setInItem(inItem);
233 // if (ctx.getInput() != null) {
234 // Object obj = ((MultipartServiceContext) ctx)
235 // .getInputPart(ctx.getCommonPartLabel(getContactServiceName()),
236 // ContactsCommon.class);
237 // if (obj != null) {
238 // docHandler.setCommonPart((ContactsCommon) obj);
245 * Creates the org authority.
247 * @param input the input
249 * @return the response
252 public Response createOrgAuthority(MultipartInput input) {
254 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
255 DocumentHandler handler = createDocumentHandler(ctx);
256 String csid = getRepositoryClient(ctx).create(ctx, handler);
257 //orgAuthorityObject.setCsid(csid);
258 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
259 path.path("" + csid);
260 Response response = Response.created(path.build()).build();
262 } catch (BadRequestException bre) {
263 Response response = Response.status(
264 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
265 throw new WebApplicationException(response);
266 } catch (UnauthorizedException ue) {
267 Response response = Response.status(
268 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
269 throw new WebApplicationException(response);
270 } catch (Exception e) {
271 if (logger.isDebugEnabled()) {
272 logger.debug("Caught exception in createOrgAuthority", e);
274 Response response = Response.status(
275 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
276 throw new WebApplicationException(response);
281 * Gets the org authority by name.
283 * @param specifier the specifier
285 * @return the org authority by name
288 @Path("urn:cspace:name({specifier})")
289 public MultipartOutput getOrgAuthorityByName(@PathParam("specifier") String specifier) {
290 String idValue = null;
291 if (specifier == null) {
292 logger.error("getOrgAuthority: missing name!");
293 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
294 "get failed on OrgAuthority (missing specifier)").type(
295 "text/plain").build();
296 throw new WebApplicationException(response);
299 OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+
300 ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+
302 // We only get a single doc - if there are multiple,
303 // it is an error in use.
305 if (logger.isDebugEnabled()) {
306 logger.debug("getOrgAuthority with name=" + specifier);
308 MultipartOutput result = null;
310 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
311 DocumentHandler handler = createDocumentHandler(ctx);
312 DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
313 handler.setDocumentFilter(myFilter);
314 getRepositoryClient(ctx).get(ctx, handler);
315 result = (MultipartOutput) ctx.getOutput();
316 } catch (UnauthorizedException ue) {
317 Response response = Response.status(
318 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
319 throw new WebApplicationException(response);
320 } catch (DocumentNotFoundException dnfe) {
321 if (logger.isDebugEnabled()) {
322 logger.debug("getOrgAuthority", dnfe);
324 Response response = Response.status(Response.Status.NOT_FOUND).entity(
325 "Get failed on OrgAuthority spec=" + specifier).type(
326 "text/plain").build();
327 throw new WebApplicationException(response);
328 } catch (Exception e) {
329 if (logger.isDebugEnabled()) {
330 logger.debug("getOrgAuthority", e);
332 Response response = Response.status(
333 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
334 throw new WebApplicationException(response);
336 if (result == null) {
337 Response response = Response.status(Response.Status.NOT_FOUND).entity(
338 "Get failed, the requested OrgAuthority spec:" + specifier + ": was not found.").type(
339 "text/plain").build();
340 throw new WebApplicationException(response);
346 * Gets the org authority.
348 * @param csid the csid
350 * @return the org authority
354 public MultipartOutput getOrgAuthority(@PathParam("csid") String csid) {
355 String idValue = null;
357 logger.error("getOrgAuthority: missing csid!");
358 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
359 "get failed on OrgAuthority csid=" + csid).type(
360 "text/plain").build();
361 throw new WebApplicationException(response);
363 if (logger.isDebugEnabled()) {
364 logger.debug("getOrgAuthority with path(id)=" + csid);
366 MultipartOutput result = null;
368 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
369 DocumentHandler handler = createDocumentHandler(ctx);
370 getRepositoryClient(ctx).get(ctx, csid, handler);
371 result = (MultipartOutput) ctx.getOutput();
372 } catch (UnauthorizedException ue) {
373 Response response = Response.status(
374 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
375 throw new WebApplicationException(response);
376 } catch (DocumentNotFoundException dnfe) {
377 if (logger.isDebugEnabled()) {
378 logger.debug("getOrgAuthority", dnfe);
380 Response response = Response.status(Response.Status.NOT_FOUND).entity(
381 "Get failed on OrgAuthority csid=" + csid).type(
382 "text/plain").build();
383 throw new WebApplicationException(response);
384 } catch (Exception e) {
385 if (logger.isDebugEnabled()) {
386 logger.debug("getOrgAuthority", e);
388 Response response = Response.status(
389 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
390 throw new WebApplicationException(response);
392 if (result == null) {
393 Response response = Response.status(Response.Status.NOT_FOUND).entity(
394 "Get failed, the requested OrgAuthority CSID:" + csid + ": was not found.").type(
395 "text/plain").build();
396 throw new WebApplicationException(response);
402 * Gets the org authority list.
406 * @return the org authority list
409 @Produces("application/xml")
410 public OrgauthoritiesCommonList getOrgAuthorityList(@Context UriInfo ui) {
411 OrgauthoritiesCommonList orgAuthorityObjectList = new OrgauthoritiesCommonList();
413 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
414 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
415 DocumentHandler handler = createDocumentHandler(ctx);
416 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
417 myFilter.setPagination(queryParams);
418 String nameQ = queryParams.getFirst("refName");
420 myFilter.setWhereClause("orgauthorities_common:refName='" + nameQ + "'");
422 handler.setDocumentFilter(myFilter);
423 getRepositoryClient(ctx).getFiltered(ctx, handler);
424 orgAuthorityObjectList = (OrgauthoritiesCommonList) handler.getCommonPartList();
425 } catch (UnauthorizedException ue) {
426 Response response = Response.status(
427 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
428 throw new WebApplicationException(response);
429 } catch (Exception e) {
430 if (logger.isDebugEnabled()) {
431 logger.debug("Caught exception in getOrgAuthorityList", e);
433 Response response = Response.status(
434 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
435 throw new WebApplicationException(response);
437 return orgAuthorityObjectList;
441 * Update org authority.
443 * @param csid the csid
444 * @param theUpdate the the update
446 * @return the multipart output
450 public MultipartOutput updateOrgAuthority(
451 @PathParam("csid") String csid,
452 MultipartInput theUpdate) {
453 if (logger.isDebugEnabled()) {
454 logger.debug("updateOrgAuthority with csid=" + csid);
456 if (csid == null || "".equals(csid)) {
457 logger.error("updateOrgAuthority: missing csid!");
458 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
459 "update failed on OrgAuthority csid=" + csid).type(
460 "text/plain").build();
461 throw new WebApplicationException(response);
463 MultipartOutput result = null;
465 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
466 DocumentHandler handler = createDocumentHandler(ctx);
467 getRepositoryClient(ctx).update(ctx, csid, handler);
468 result = (MultipartOutput) ctx.getOutput();
469 } catch (BadRequestException bre) {
470 Response response = Response.status(
471 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
472 throw new WebApplicationException(response);
473 } catch (UnauthorizedException ue) {
474 Response response = Response.status(
475 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
476 throw new WebApplicationException(response);
477 } catch (DocumentNotFoundException dnfe) {
478 if (logger.isDebugEnabled()) {
479 logger.debug("caught exception in updateOrgAuthority", dnfe);
481 Response response = Response.status(Response.Status.NOT_FOUND).entity(
482 "Update failed on OrgAuthority csid=" + csid).type(
483 "text/plain").build();
484 throw new WebApplicationException(response);
485 } catch (Exception e) {
486 Response response = Response.status(
487 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
488 throw new WebApplicationException(response);
494 * Delete org authority.
496 * @param csid the csid
498 * @return the response
502 public Response deleteOrgAuthority(@PathParam("csid") String csid) {
504 if (logger.isDebugEnabled()) {
505 logger.debug("deleteOrgAuthority with csid=" + csid);
507 if (csid == null || "".equals(csid)) {
508 logger.error("deleteOrgAuthority: missing csid!");
509 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
510 "delete failed on OrgAuthority csid=" + csid).type(
511 "text/plain").build();
512 throw new WebApplicationException(response);
515 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
516 getRepositoryClient(ctx).delete(ctx, csid);
517 return Response.status(HttpResponseCodes.SC_OK).build();
518 } catch (UnauthorizedException ue) {
519 Response response = Response.status(
520 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
521 throw new WebApplicationException(response);
522 } catch (DocumentNotFoundException dnfe) {
523 if (logger.isDebugEnabled()) {
524 logger.debug("caught exception in deleteOrgAuthority", dnfe);
526 Response response = Response.status(Response.Status.NOT_FOUND).entity(
527 "Delete failed on OrgAuthority csid=" + csid).type(
528 "text/plain").build();
529 throw new WebApplicationException(response);
530 } catch (Exception e) {
531 Response response = Response.status(
532 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
533 throw new WebApplicationException(response);
538 /*************************************************************************
539 * Organization parts - this is a sub-resource of OrgAuthority
540 *************************************************************************/
542 @Path("{csid}/items")
543 public Response createOrganization(@PathParam("csid") String parentcsid, MultipartInput input) {
545 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
547 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
548 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
549 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
550 path.path(parentcsid + "/items/" + itemcsid);
551 Response response = Response.created(path.build()).build();
553 } catch (BadRequestException bre) {
554 Response response = Response.status(
555 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
556 throw new WebApplicationException(response);
557 } catch (UnauthorizedException ue) {
558 Response response = Response.status(
559 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
560 throw new WebApplicationException(response);
561 } catch (Exception e) {
562 if (logger.isDebugEnabled()) {
563 logger.debug("Caught exception in createOrganization", e);
565 Response response = Response.status(
566 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
567 throw new WebApplicationException(response);
572 * Gets the organization.
574 * @param csid The organization authority (parent) CSID.
575 * @param itemcsid The organization item CSID.
577 * @return the organization.
580 @Path("{csid}/items/{itemcsid}")
581 public MultipartOutput getOrganization(
582 @PathParam("csid") String parentcsid,
583 @PathParam("itemcsid") String itemcsid) {
584 if (logger.isDebugEnabled()) {
585 logger.debug("getOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
587 if (parentcsid == null || "".equals(parentcsid)) {
588 logger.error("getOrganization: missing csid!");
589 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
590 "get failed on Organization csid=" + parentcsid).type(
591 "text/plain").build();
592 throw new WebApplicationException(response);
594 if (itemcsid == null || "".equals(itemcsid)) {
595 logger.error("getOrganization: missing itemcsid!");
596 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
597 "get failed on Organization itemcsid=" + itemcsid).type(
598 "text/plain").build();
599 throw new WebApplicationException(response);
601 MultipartOutput result = null;
603 // Note that we have to create the service context for the Items, not the main service
604 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
605 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
606 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
607 // TODO should we assert that the item is in the passed orgAuthority?
608 result = (MultipartOutput) ctx.getOutput();
609 } catch (UnauthorizedException ue) {
610 Response response = Response.status(
611 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
612 throw new WebApplicationException(response);
613 } catch (DocumentNotFoundException dnfe) {
614 if (logger.isDebugEnabled()) {
615 logger.debug("getOrganization", dnfe);
617 Response response = Response.status(Response.Status.NOT_FOUND).entity(
618 "Get failed on Organization csid=" + itemcsid).type(
619 "text/plain").build();
620 throw new WebApplicationException(response);
621 } catch (Exception e) {
622 if (logger.isDebugEnabled()) {
623 logger.debug("getOrganization", e);
625 Response response = Response.status(
626 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
627 throw new WebApplicationException(response);
629 if (result == null) {
630 Response response = Response.status(Response.Status.NOT_FOUND).entity(
631 "Get failed, the requested Organization CSID:" + itemcsid + ": was not found.").type(
632 "text/plain").build();
633 throw new WebApplicationException(response);
639 * Gets the authority refs for an Organization item.
641 * @param csid The organization authority (parent) CSID.
642 * @param itemcsid The organization item CSID.
644 * @return the authority refs for the Organization item.
647 @Path("{csid}/items/{itemcsid}/authorityrefs")
648 @Produces("application/xml")
649 public AuthorityRefList getOrganizationAuthorityRefs(
650 @PathParam("csid") String parentcsid,
651 @PathParam("itemcsid") String itemcsid,
652 @Context UriInfo ui) {
653 AuthorityRefList authRefList = null;
655 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
656 ServiceContext<MultipartInput, MultipartOutput> ctx =
657 createServiceContext(getItemServiceName(), queryParams);
658 RemoteDocumentModelHandlerImpl handler =
659 (RemoteDocumentModelHandlerImpl) createItemDocumentHandler(ctx, parentcsid);
660 DocumentWrapper<DocumentModel> docWrapper =
661 getRepositoryClient(ctx).getDoc(ctx, itemcsid);
662 List<String> authRefFields =
663 ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(
664 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
665 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
666 } catch (UnauthorizedException ue) {
667 Response response = Response.status(
668 Response.Status.UNAUTHORIZED).entity("Failed to retrieve authority references: reason " + ue.getErrorReason()).type("text/plain").build();
669 throw new WebApplicationException(response);
670 } catch (Exception e) {
671 if (logger.isDebugEnabled()) {
672 logger.debug("Caught exception in getAuthorityRefs", e);
674 Response response = Response.status(
675 Response.Status.INTERNAL_SERVER_ERROR).entity("Failed to retrieve authority references").type("text/plain").build();
676 throw new WebApplicationException(response);
682 * Gets the organization list.
684 * @param parentcsid the parentcsid
685 * @param partialTerm the partial term
688 * @return the organization list
691 @Path("{csid}/items")
692 @Produces("application/xml")
693 public OrganizationsCommonList getOrganizationList(
694 @PathParam("csid") String parentcsid,
695 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
696 @Context UriInfo ui) {
697 OrganizationsCommonList organizationObjectList = new OrganizationsCommonList();
699 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
700 // Note that docType defaults to the ServiceName, so we're fine with that.
701 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
703 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
704 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
705 myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
706 myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
707 ":" + OrganizationJAXBSchema.IN_AUTHORITY + "=" +
708 "'" + parentcsid + "'");
710 // AND organizations_common:displayName LIKE '%partialTerm%'
711 if (partialTerm != null && !partialTerm.isEmpty()) {
712 String ptClause = "AND " + OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
713 ":" + OrganizationJAXBSchema.DISPLAY_NAME +
714 " LIKE " + "'%" + partialTerm + "%'";
715 myFilter.appendWhereClause(ptClause);
717 handler.setDocumentFilter(myFilter);
718 getRepositoryClient(ctx).getFiltered(ctx, handler);
719 organizationObjectList = (OrganizationsCommonList) handler.getCommonPartList();
720 } catch (UnauthorizedException ue) {
721 Response response = Response.status(
722 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
723 throw new WebApplicationException(response);
724 } catch (Exception e) {
725 if (logger.isDebugEnabled()) {
726 logger.debug("Caught exception in getOrganizationList", e);
728 Response response = Response.status(
729 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
730 throw new WebApplicationException(response);
732 return organizationObjectList;
736 * Gets the organization list by auth name.
738 * @param parentSpecifier the parent specifier
739 * @param partialTerm the partial term
742 * @return the organization list by auth name
745 @Path("urn:cspace:name({specifier})/items")
746 @Produces("application/xml")
747 public OrganizationsCommonList getOrganizationListByAuthName(
748 @PathParam("specifier") String parentSpecifier,
749 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
750 @Context UriInfo ui) {
751 OrganizationsCommonList personObjectList = new OrganizationsCommonList();
753 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
755 OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+
756 ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+
757 "='"+parentSpecifier+"'";
758 // Need to get an Authority by name
759 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
760 String parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
762 ctx = createServiceContext(getItemServiceName(), queryParams);
763 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
764 DocumentFilter myFilter = handler.createDocumentFilter();// new DocumentFilter();
765 myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
767 // Add the where clause "organizations_common:inAuthority='" + parentcsid + "'"
768 myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" +
769 OrganizationJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
771 // AND organizations_common:displayName LIKE '%partialTerm%'
772 if (partialTerm != null && !partialTerm.isEmpty()) {
773 String ptClause = "AND " +
774 OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" +
775 OrganizationJAXBSchema.DISPLAY_NAME +
777 "'%" + partialTerm + "%'";
778 myFilter.appendWhereClause(ptClause);
781 handler.setDocumentFilter(myFilter);
782 getRepositoryClient(ctx).getFiltered(ctx, handler);
783 personObjectList = (OrganizationsCommonList) handler.getCommonPartList();
784 } catch (UnauthorizedException ue) {
785 Response response = Response.status(
786 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
787 throw new WebApplicationException(response);
788 } catch (Exception e) {
789 if (logger.isDebugEnabled()) {
790 logger.debug("Caught exception in getOrganizationListByAuthName", e);
792 Response response = Response.status(
793 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
794 throw new WebApplicationException(response);
796 return personObjectList;
800 * Update organization.
802 * @param parentcsid the parentcsid
803 * @param itemcsid the itemcsid
804 * @param theUpdate the the update
806 * @return the multipart output
809 @Path("{csid}/items/{itemcsid}")
810 public MultipartOutput updateOrganization(
811 @PathParam("csid") String parentcsid,
812 @PathParam("itemcsid") String itemcsid,
813 MultipartInput theUpdate) {
814 if (logger.isDebugEnabled()) {
815 logger.debug("updateOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
817 if (parentcsid == null || "".equals(parentcsid)) {
818 logger.error("updateOrganization: missing csid!");
819 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
820 "update failed on Organization parentcsid=" + parentcsid).type(
821 "text/plain").build();
822 throw new WebApplicationException(response);
824 if (itemcsid == null || "".equals(itemcsid)) {
825 logger.error("updateOrganization: missing itemcsid!");
826 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
827 "update failed on Organization=" + itemcsid).type(
828 "text/plain").build();
829 throw new WebApplicationException(response);
831 MultipartOutput result = null;
833 // Note that we have to create the service context for the Items, not the main service
834 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
836 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
837 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
838 result = (MultipartOutput) ctx.getOutput();
839 } catch (BadRequestException bre) {
840 Response response = Response.status(
841 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
842 throw new WebApplicationException(response);
843 } catch (UnauthorizedException ue) {
844 Response response = Response.status(
845 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
846 throw new WebApplicationException(response);
847 } catch (DocumentNotFoundException dnfe) {
848 if (logger.isDebugEnabled()) {
849 logger.debug("caught exception in updateOrganization", dnfe);
851 Response response = Response.status(Response.Status.NOT_FOUND).entity(
852 "Update failed on Organization csid=" + itemcsid).type(
853 "text/plain").build();
854 throw new WebApplicationException(response);
855 } catch (Exception e) {
856 Response response = Response.status(
857 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
858 throw new WebApplicationException(response);
864 * Delete organization.
866 * @param parentcsid the parentcsid
867 * @param itemcsid the itemcsid
869 * @return the response
872 @Path("{csid}/items/{itemcsid}")
873 public Response deleteOrganization(
874 @PathParam("csid") String parentcsid,
875 @PathParam("itemcsid") String itemcsid) {
876 if (logger.isDebugEnabled()) {
877 logger.debug("deleteOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
879 if (parentcsid == null || "".equals(parentcsid)) {
880 logger.error("deleteOrganization: missing csid!");
881 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
882 "delete failed on Organization parentcsid=" + parentcsid).type(
883 "text/plain").build();
884 throw new WebApplicationException(response);
886 if (itemcsid == null || "".equals(itemcsid)) {
887 logger.error("deleteOrganization: missing itemcsid!");
888 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
889 "delete failed on Organization=" + itemcsid).type(
890 "text/plain").build();
891 throw new WebApplicationException(response);
894 // Note that we have to create the service context for the Items, not the main service
895 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
896 getRepositoryClient(ctx).delete(ctx, itemcsid);
897 return Response.status(HttpResponseCodes.SC_OK).build();
898 } catch (UnauthorizedException ue) {
899 Response response = Response.status(
900 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
901 throw new WebApplicationException(response);
902 } catch (DocumentNotFoundException dnfe) {
903 if (logger.isDebugEnabled()) {
904 logger.debug("caught exception in deleteOrganization", dnfe);
906 Response response = Response.status(Response.Status.NOT_FOUND).entity(
907 "Delete failed on Organization itemcsid=" + itemcsid).type(
908 "text/plain").build();
909 throw new WebApplicationException(response);
910 } catch (Exception e) {
911 Response response = Response.status(
912 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
913 throw new WebApplicationException(response);
918 /*************************************************************************
919 * Contact parts - this is a sub-resource of Organization (or "item")
920 *************************************************************************/
922 @Path("{parentcsid}/items/{itemcsid}/contacts")
923 public Response createContact(
924 @PathParam("parentcsid") String parentcsid,
925 @PathParam("itemcsid") String itemcsid,
926 MultipartInput input) {
928 // Note that we have to create the service context and document
929 // handler for the Contact service, not the main service.
930 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
931 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
932 String csid = getRepositoryClient(ctx).create(ctx, handler);
933 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
934 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
935 Response response = Response.created(path.build()).build();
937 } catch (BadRequestException bre) {
938 Response response = Response.status(
939 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
940 throw new WebApplicationException(response);
941 } catch (UnauthorizedException ue) {
942 Response response = Response.status(
943 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
944 throw new WebApplicationException(response);
945 } catch (Exception e) {
946 if (logger.isDebugEnabled()) {
947 logger.debug("Caught exception in createContact", e);
949 Response response = Response.status(
950 Response.Status.INTERNAL_SERVER_ERROR)
951 .entity("Attempt to create Contact failed.")
952 .type("text/plain").build();
953 throw new WebApplicationException(response);
959 * Gets the contact list.
961 * @param parentcsid the parentcsid
962 * @param itemcsid the itemcsid
965 * @return the contact list
968 @Produces({"application/xml"})
969 @Path("{parentcsid}/items/{itemcsid}/contacts/")
970 public ContactsCommonList getContactList(
971 @PathParam("parentcsid") String parentcsid,
972 @PathParam("itemcsid") String itemcsid,
973 @Context UriInfo ui) {
974 ContactsCommonList contactObjectList = new ContactsCommonList();
976 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
977 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
979 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
980 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
981 myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
982 myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
983 ContactJAXBSchema.IN_AUTHORITY +
984 "='" + parentcsid + "'" +
986 ContactJAXBSchema.CONTACTS_COMMON + ":" +
987 ContactJAXBSchema.IN_ITEM +
988 "='" + itemcsid + "'" +
989 " AND ecm:isProxy = 0");
990 handler.setDocumentFilter(myFilter);
991 getRepositoryClient(ctx).getFiltered(ctx, handler);
992 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
993 } catch (UnauthorizedException ue) {
994 Response response = Response.status(
995 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
996 throw new WebApplicationException(response);
997 } catch (Exception e) {
998 if (logger.isDebugEnabled()) {
999 logger.debug("Caught exception in getContactsList", e);
1001 Response response = Response.status(
1002 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
1003 throw new WebApplicationException(response);
1005 return contactObjectList;
1011 * @param parentcsid the parentcsid
1012 * @param itemcsid the itemcsid
1013 * @param csid the csid
1015 * @return the contact
1018 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1019 public MultipartOutput getContact(
1020 @PathParam("parentcsid") String parentcsid,
1021 @PathParam("itemcsid") String itemcsid,
1022 @PathParam("csid") String csid) {
1023 MultipartOutput result = null;
1024 if (logger.isDebugEnabled()) {
1025 logger.debug("getContact with parentCsid=" + parentcsid +
1026 " itemcsid=" + itemcsid + " csid=" + csid);
1029 // Note that we have to create the service context and document
1030 // handler for the Contact service, not the main service.
1031 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1032 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1033 getRepositoryClient(ctx).get(ctx, csid, handler);
1034 result = (MultipartOutput) ctx.getOutput();
1035 } catch (UnauthorizedException ue) {
1036 Response response = Response.status(
1037 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
1038 throw new WebApplicationException(response);
1039 } catch (DocumentNotFoundException dnfe) {
1040 if (logger.isDebugEnabled()) {
1041 logger.debug("getContact", dnfe);
1043 Response response = Response.status(Response.Status.NOT_FOUND)
1044 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1045 .type("text/plain").build();
1046 throw new WebApplicationException(response);
1047 } catch (Exception e) {
1048 if (logger.isDebugEnabled()) {
1049 logger.debug("getContact", e);
1051 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
1052 .entity("Get contact failed")
1053 .type("text/plain").build();
1054 throw new WebApplicationException(response);
1056 if (result == null) {
1057 Response response = Response.status(Response.Status.NOT_FOUND)
1058 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1059 .type("text/plain").build();
1060 throw new WebApplicationException(response);
1069 * @param parentcsid the parentcsid
1070 * @param itemcsid the itemcsid
1071 * @param csid the csid
1072 * @param theUpdate the the update
1074 * @return the multipart output
1077 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1078 public MultipartOutput updateContact(
1079 @PathParam("parentcsid") String parentcsid,
1080 @PathParam("itemcsid") String itemcsid,
1081 @PathParam("csid") String csid,
1082 MultipartInput theUpdate) {
1083 if (logger.isDebugEnabled()) {
1084 logger.debug("updateContact with parentcsid=" + parentcsid +
1085 " itemcsid=" + itemcsid + " csid=" + csid);
1087 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1088 logger.error("updateContact: missing csid!");
1089 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1090 "update failed on Contact parentcsid=" + parentcsid).type(
1091 "text/plain").build();
1092 throw new WebApplicationException(response);
1094 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1095 logger.error("updateContact: missing itemcsid!");
1096 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1097 "update failed on Contact=" + itemcsid).type(
1098 "text/plain").build();
1099 throw new WebApplicationException(response);
1101 if (csid == null || csid.trim().isEmpty()) {
1102 logger.error("updateContact: missing csid!");
1103 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1104 "update failed on Contact=" + csid).type(
1105 "text/plain").build();
1106 throw new WebApplicationException(response);
1108 MultipartOutput result = null;
1110 // Note that we have to create the service context and document
1111 // handler for the Contact service, not the main service.
1112 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), theUpdate);
1113 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1114 getRepositoryClient(ctx).update(ctx, csid, handler);
1115 result = (MultipartOutput) ctx.getOutput();
1116 } catch (BadRequestException bre) {
1117 Response response = Response.status(
1118 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
1119 throw new WebApplicationException(response);
1120 } catch (UnauthorizedException ue) {
1121 Response response = Response.status(
1122 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
1123 throw new WebApplicationException(response);
1124 } catch (DocumentNotFoundException dnfe) {
1125 if (logger.isDebugEnabled()) {
1126 logger.debug("caught exception in updateContact", dnfe);
1128 Response response = Response.status(Response.Status.NOT_FOUND).entity(
1129 "Update failed on Contact csid=" + itemcsid).type(
1130 "text/plain").build();
1131 throw new WebApplicationException(response);
1132 } catch (Exception e) {
1133 Response response = Response.status(
1134 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
1135 throw new WebApplicationException(response);
1143 * @param parentcsid the parentcsid
1144 * @param itemcsid the itemcsid
1145 * @param csid the csid
1147 * @return the response
1150 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1151 public Response deleteContact(
1152 @PathParam("parentcsid") String parentcsid,
1153 @PathParam("itemcsid") String itemcsid,
1154 @PathParam("csid") String csid) {
1155 if (logger.isDebugEnabled()) {
1156 logger.debug("deleteContact with parentCsid=" + parentcsid +
1157 " itemcsid=" + itemcsid + " csid=" + csid);
1159 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1160 logger.error("deleteContact: missing parentcsid!");
1161 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1162 "delete contact failed on parentcsid=" + parentcsid).type(
1163 "text/plain").build();
1164 throw new WebApplicationException(response);
1166 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1167 logger.error("deleteContact: missing itemcsid!");
1168 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1169 "delete contact failed on itemcsid=" + itemcsid).type(
1170 "text/plain").build();
1171 throw new WebApplicationException(response);
1173 if (csid == null || csid.trim().isEmpty()) {
1174 logger.error("deleteContact: missing csid!");
1175 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1176 "delete contact failed on csid=" + csid).type(
1177 "text/plain").build();
1178 throw new WebApplicationException(response);
1181 // Note that we have to create the service context for the
1182 // Contact service, not the main service.
1183 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1184 getRepositoryClient(ctx).delete(ctx, csid);
1185 return Response.status(HttpResponseCodes.SC_OK).build();
1186 } catch (UnauthorizedException ue) {
1187 Response response = Response.status(
1188 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
1189 throw new WebApplicationException(response);
1190 } catch (DocumentNotFoundException dnfe) {
1191 if (logger.isDebugEnabled()) {
1192 logger.debug("Caught exception in deleteContact", dnfe);
1194 Response response = Response.status(Response.Status.NOT_FOUND)
1195 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
1196 .type("text/plain").build();
1197 throw new WebApplicationException(response);
1198 } catch (Exception e) {
1199 Response response = Response.status(
1200 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
1201 throw new WebApplicationException(response);