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 javax.ws.rs.Consumes;
27 import javax.ws.rs.DELETE;
28 import javax.ws.rs.GET;
29 import javax.ws.rs.POST;
30 import javax.ws.rs.PUT;
31 import javax.ws.rs.Path;
32 import javax.ws.rs.PathParam;
33 import javax.ws.rs.Produces;
34 import javax.ws.rs.QueryParam;
35 import javax.ws.rs.WebApplicationException;
36 import javax.ws.rs.core.Context;
37 import javax.ws.rs.core.MultivaluedMap;
38 import javax.ws.rs.core.Response;
39 import javax.ws.rs.core.UriBuilder;
40 import javax.ws.rs.core.UriInfo;
42 import org.collectionspace.services.OrgAuthorityJAXBSchema;
43 import org.collectionspace.services.OrganizationJAXBSchema;
44 import org.collectionspace.services.common.AbstractMultiPartCollectionSpaceResourceImpl;
45 import org.collectionspace.services.common.ClientType;
46 import org.collectionspace.services.common.ServiceMain;
47 import org.collectionspace.services.common.context.MultipartServiceContext;
48 //import org.collectionspace.services.common.context.MultipartServiceContextFactory;
49 import org.collectionspace.services.common.context.ServiceContext;
50 import org.collectionspace.services.common.document.BadRequestException;
51 import org.collectionspace.services.common.document.DocumentFilter;
52 import org.collectionspace.services.common.document.DocumentHandler;
53 import org.collectionspace.services.common.document.DocumentNotFoundException;
54 import org.collectionspace.services.common.query.IQueryManager;
55 import org.collectionspace.services.contact.ContactResource;
56 import org.collectionspace.services.contact.ContactsCommon;
57 import org.collectionspace.services.contact.ContactsCommonList;
58 import org.collectionspace.services.contact.ContactJAXBSchema;
59 import org.collectionspace.services.contact.nuxeo.ContactDocumentModelHandler;
60 import org.collectionspace.services.common.security.UnauthorizedException;
61 import org.collectionspace.services.organization.nuxeo.OrganizationDocumentModelHandler;
62 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
63 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
64 import org.jboss.resteasy.util.HttpResponseCodes;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
69 * The Class OrgAuthorityResource.
71 @Path("/orgauthorities")
72 @Consumes("multipart/mixed")
73 @Produces("multipart/mixed")
74 public class OrgAuthorityResource extends
75 AbstractMultiPartCollectionSpaceResourceImpl {
77 /** The Constant orgAuthorityServiceName. */
78 private final static String orgAuthorityServiceName = "orgauthorities";
80 /** The Constant organizationServiceName. */
81 private final static String organizationServiceName = "organizations";
84 final Logger logger = LoggerFactory.getLogger(OrgAuthorityResource.class);
85 //FIXME retrieve client type from configuration
86 /** The Constant CLIENT_TYPE. */
87 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
89 /** The contact resource. */
90 private ContactResource contactResource = new ContactResource();
93 * Instantiates a new org authority resource.
95 public OrgAuthorityResource() {
100 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getVersionString()
103 protected String getVersionString() {
104 /** The last change revision. */
105 final String lastChangeRevision = "$LastChangedRevision$";
106 return lastChangeRevision;
110 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#getServiceName()
113 public String getServiceName() {
114 return orgAuthorityServiceName;
118 * @see org.collectionspace.services.common.CollectionSpaceResource#getCommonPartClass()
121 public Class<OrgauthoritiesCommon> getCommonPartClass() {
122 return OrgauthoritiesCommon.class;
126 * Gets the item service name.
128 * @return the item service name
130 public String getItemServiceName() {
131 return organizationServiceName;
135 * Gets the contact service name.
137 * @return the contact service name
139 public String getContactServiceName() {
140 return contactResource.getServiceName();
144 public RemoteServiceContext createItemServiceContext(MultipartInput input) throws Exception {
145 RemoteServiceContext ctx = new RemoteServiceContextImpl(getItemServiceName());
151 * @see org.collectionspace.services.common.AbstractCollectionSpaceResourceImpl#createDocumentHandler(org.collectionspace.services.common.context.ServiceContext)
154 // public DocumentHandler createDocumentHandler(ServiceContext ctx) throws Exception {
155 // DocumentHandler docHandler =ctx.getDocumentHandler();
156 // if (ctx.getInput() != null) {
157 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(), OrgauthoritiesCommon.class);
158 // if (obj != null) {
159 // docHandler.setCommonPart((OrgauthoritiesCommon) obj);
162 // return docHandler;
166 * Creates the item document handler.
169 * @param inAuthority the in authority
171 * @return the document handler
173 * @throws Exception the exception
175 private DocumentHandler createItemDocumentHandler(
176 ServiceContext<MultipartInput, MultipartOutput> ctx,
177 String inAuthority) throws Exception {
178 OrganizationDocumentModelHandler docHandler = (OrganizationDocumentModelHandler)createDocumentHandler(
180 ctx.getCommonPartLabel(getItemServiceName()),
181 OrganizationsCommon.class);
182 docHandler.setInAuthority(inAuthority);
185 // ((OrganizationDocumentModelHandler) docHandler).setInAuthority(inAuthority);
186 // if (ctx.getInput() != null) {
187 // Object obj = ((MultipartServiceContext) ctx).getInputPart(ctx.getCommonPartLabel(getItemServiceName()),
188 // OrganizationsCommon.class);
189 // if (obj != null) {
190 // docHandler.setCommonPart((OrganizationsCommon) obj);
198 * Creates the contact document handler.
201 * @param inAuthority the in authority
202 * @param inItem the in item
204 * @return the document handler
206 * @throws Exception the exception
208 private DocumentHandler createContactDocumentHandler(
209 ServiceContext<MultipartInput, MultipartOutput> ctx, String inAuthority,
210 String inItem) throws Exception {
212 ContactDocumentModelHandler docHandler = (ContactDocumentModelHandler)createDocumentHandler(
214 ctx.getCommonPartLabel(getContactServiceName()),
215 ContactsCommon.class);
216 docHandler.setInAuthority(inAuthority);
217 docHandler.setInItem(inItem);
219 // DocumentHandler docHandler = ctx.getDocumentHandler();
220 // // Set the inAuthority and inItem values, which specify the
221 // // parent authority (e.g. PersonAuthority, OrgAuthority) and the item
222 // // (e.g. Person, Organization) with which the Contact is associated.
223 // ((ContactDocumentModelHandler) docHandler).setInAuthority(inAuthority);
224 // ((ContactDocumentModelHandler) docHandler).setInItem(inItem);
225 // if (ctx.getInput() != null) {
226 // Object obj = ((MultipartServiceContext) ctx)
227 // .getInputPart(ctx.getCommonPartLabel(getContactServiceName()),
228 // ContactsCommon.class);
229 // if (obj != null) {
230 // docHandler.setCommonPart((ContactsCommon) obj);
237 * Creates the org authority.
239 * @param input the input
241 * @return the response
244 public Response createOrgAuthority(MultipartInput input) {
246 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(input);
247 DocumentHandler handler = createDocumentHandler(ctx);
248 String csid = getRepositoryClient(ctx).create(ctx, handler);
249 //orgAuthorityObject.setCsid(csid);
250 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
251 path.path("" + csid);
252 Response response = Response.created(path.build()).build();
254 } catch (BadRequestException bre) {
255 Response response = Response.status(
256 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
257 throw new WebApplicationException(response);
258 } catch (UnauthorizedException ue) {
259 Response response = Response.status(
260 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
261 throw new WebApplicationException(response);
262 } catch (Exception e) {
263 if (logger.isDebugEnabled()) {
264 logger.debug("Caught exception in createOrgAuthority", e);
266 Response response = Response.status(
267 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
268 throw new WebApplicationException(response);
273 * Gets the org authority by name.
275 * @param specifier the specifier
277 * @return the org authority by name
280 @Path("urn:cspace:name({specifier})")
281 public MultipartOutput getOrgAuthorityByName(@PathParam("specifier") String specifier) {
282 String idValue = null;
283 if (specifier == null) {
284 logger.error("getOrgAuthority: missing name!");
285 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
286 "get failed on OrgAuthority (missing specifier)").type(
287 "text/plain").build();
288 throw new WebApplicationException(response);
291 OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+
292 ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+
294 // We only get a single doc - if there are multiple,
295 // it is an error in use.
297 if (logger.isDebugEnabled()) {
298 logger.debug("getOrgAuthority with name=" + specifier);
300 MultipartOutput result = null;
302 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
303 DocumentHandler handler = createDocumentHandler(ctx);
304 DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
305 handler.setDocumentFilter(myFilter);
306 getRepositoryClient(ctx).get(ctx, handler);
307 result = (MultipartOutput) ctx.getOutput();
308 } catch (UnauthorizedException ue) {
309 Response response = Response.status(
310 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
311 throw new WebApplicationException(response);
312 } catch (DocumentNotFoundException dnfe) {
313 if (logger.isDebugEnabled()) {
314 logger.debug("getOrgAuthority", dnfe);
316 Response response = Response.status(Response.Status.NOT_FOUND).entity(
317 "Get failed on OrgAuthority spec=" + specifier).type(
318 "text/plain").build();
319 throw new WebApplicationException(response);
320 } catch (Exception e) {
321 if (logger.isDebugEnabled()) {
322 logger.debug("getOrgAuthority", e);
324 Response response = Response.status(
325 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
326 throw new WebApplicationException(response);
328 if (result == null) {
329 Response response = Response.status(Response.Status.NOT_FOUND).entity(
330 "Get failed, the requested OrgAuthority spec:" + specifier + ": was not found.").type(
331 "text/plain").build();
332 throw new WebApplicationException(response);
338 * Gets the org authority.
340 * @param csid the csid
342 * @return the org authority
346 public MultipartOutput getOrgAuthority(@PathParam("csid") String csid) {
347 String idValue = null;
349 logger.error("getOrgAuthority: missing csid!");
350 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
351 "get failed on OrgAuthority csid=" + csid).type(
352 "text/plain").build();
353 throw new WebApplicationException(response);
355 if (logger.isDebugEnabled()) {
356 logger.debug("getOrgAuthority with path(id)=" + csid);
358 MultipartOutput result = null;
360 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
361 DocumentHandler handler = createDocumentHandler(ctx);
362 getRepositoryClient(ctx).get(ctx, csid, handler);
363 result = (MultipartOutput) ctx.getOutput();
364 } catch (UnauthorizedException ue) {
365 Response response = Response.status(
366 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
367 throw new WebApplicationException(response);
368 } catch (DocumentNotFoundException dnfe) {
369 if (logger.isDebugEnabled()) {
370 logger.debug("getOrgAuthority", dnfe);
372 Response response = Response.status(Response.Status.NOT_FOUND).entity(
373 "Get failed on OrgAuthority csid=" + csid).type(
374 "text/plain").build();
375 throw new WebApplicationException(response);
376 } catch (Exception e) {
377 if (logger.isDebugEnabled()) {
378 logger.debug("getOrgAuthority", e);
380 Response response = Response.status(
381 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
382 throw new WebApplicationException(response);
384 if (result == null) {
385 Response response = Response.status(Response.Status.NOT_FOUND).entity(
386 "Get failed, the requested OrgAuthority CSID:" + csid + ": was not found.").type(
387 "text/plain").build();
388 throw new WebApplicationException(response);
394 * Gets the org authority list.
398 * @return the org authority list
401 @Produces("application/xml")
402 public OrgauthoritiesCommonList getOrgAuthorityList(@Context UriInfo ui) {
403 OrgauthoritiesCommonList orgAuthorityObjectList = new OrgauthoritiesCommonList();
405 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
406 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
407 DocumentHandler handler = createDocumentHandler(ctx);
408 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
409 myFilter.setPagination(queryParams);
410 String nameQ = queryParams.getFirst("refName");
412 myFilter.setWhereClause("orgauthorities_common:refName='" + nameQ + "'");
414 handler.setDocumentFilter(myFilter);
415 getRepositoryClient(ctx).getFiltered(ctx, handler);
416 orgAuthorityObjectList = (OrgauthoritiesCommonList) handler.getCommonPartList();
417 } catch (UnauthorizedException ue) {
418 Response response = Response.status(
419 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
420 throw new WebApplicationException(response);
421 } catch (Exception e) {
422 if (logger.isDebugEnabled()) {
423 logger.debug("Caught exception in getOrgAuthorityList", e);
425 Response response = Response.status(
426 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
427 throw new WebApplicationException(response);
429 return orgAuthorityObjectList;
433 * Update org authority.
435 * @param csid the csid
436 * @param theUpdate the the update
438 * @return the multipart output
442 public MultipartOutput updateOrgAuthority(
443 @PathParam("csid") String csid,
444 MultipartInput theUpdate) {
445 if (logger.isDebugEnabled()) {
446 logger.debug("updateOrgAuthority with csid=" + csid);
448 if (csid == null || "".equals(csid)) {
449 logger.error("updateOrgAuthority: missing csid!");
450 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
451 "update failed on OrgAuthority csid=" + csid).type(
452 "text/plain").build();
453 throw new WebApplicationException(response);
455 MultipartOutput result = null;
457 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(theUpdate);
458 DocumentHandler handler = createDocumentHandler(ctx);
459 getRepositoryClient(ctx).update(ctx, csid, handler);
460 result = (MultipartOutput) ctx.getOutput();
461 } catch (BadRequestException bre) {
462 Response response = Response.status(
463 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
464 throw new WebApplicationException(response);
465 } catch (UnauthorizedException ue) {
466 Response response = Response.status(
467 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
468 throw new WebApplicationException(response);
469 } catch (DocumentNotFoundException dnfe) {
470 if (logger.isDebugEnabled()) {
471 logger.debug("caught exception in updateOrgAuthority", dnfe);
473 Response response = Response.status(Response.Status.NOT_FOUND).entity(
474 "Update failed on OrgAuthority csid=" + csid).type(
475 "text/plain").build();
476 throw new WebApplicationException(response);
477 } catch (Exception e) {
478 Response response = Response.status(
479 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
480 throw new WebApplicationException(response);
486 * Delete org authority.
488 * @param csid the csid
490 * @return the response
494 public Response deleteOrgAuthority(@PathParam("csid") String csid) {
496 if (logger.isDebugEnabled()) {
497 logger.debug("deleteOrgAuthority with csid=" + csid);
499 if (csid == null || "".equals(csid)) {
500 logger.error("deleteOrgAuthority: missing csid!");
501 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
502 "delete failed on OrgAuthority csid=" + csid).type(
503 "text/plain").build();
504 throw new WebApplicationException(response);
507 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext();
508 getRepositoryClient(ctx).delete(ctx, csid);
509 return Response.status(HttpResponseCodes.SC_OK).build();
510 } catch (UnauthorizedException ue) {
511 Response response = Response.status(
512 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
513 throw new WebApplicationException(response);
514 } catch (DocumentNotFoundException dnfe) {
515 if (logger.isDebugEnabled()) {
516 logger.debug("caught exception in deleteOrgAuthority", dnfe);
518 Response response = Response.status(Response.Status.NOT_FOUND).entity(
519 "Delete failed on OrgAuthority csid=" + csid).type(
520 "text/plain").build();
521 throw new WebApplicationException(response);
522 } catch (Exception e) {
523 Response response = Response.status(
524 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
525 throw new WebApplicationException(response);
530 /*************************************************************************
531 * Organization parts - this is a sub-resource of OrgAuthority
532 *************************************************************************/
534 @Path("{csid}/items")
535 public Response createOrganization(@PathParam("csid") String parentcsid, MultipartInput input) {
537 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
539 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
540 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
541 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
542 path.path(parentcsid + "/items/" + itemcsid);
543 Response response = Response.created(path.build()).build();
545 } catch (BadRequestException bre) {
546 Response response = Response.status(
547 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
548 throw new WebApplicationException(response);
549 } catch (UnauthorizedException ue) {
550 Response response = Response.status(
551 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
552 throw new WebApplicationException(response);
553 } catch (Exception e) {
554 if (logger.isDebugEnabled()) {
555 logger.debug("Caught exception in createOrganization", e);
557 Response response = Response.status(
558 Response.Status.INTERNAL_SERVER_ERROR).entity("Create failed").type("text/plain").build();
559 throw new WebApplicationException(response);
564 * Gets the organization.
566 * @param parentcsid the parentcsid
567 * @param itemcsid the itemcsid
569 * @return the organization
572 @Path("{csid}/items/{itemcsid}")
573 public MultipartOutput getOrganization(
574 @PathParam("csid") String parentcsid,
575 @PathParam("itemcsid") String itemcsid) {
576 if (logger.isDebugEnabled()) {
577 logger.debug("getOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
579 if (parentcsid == null || "".equals(parentcsid)) {
580 logger.error("getOrganization: missing csid!");
581 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
582 "get failed on Organization csid=" + parentcsid).type(
583 "text/plain").build();
584 throw new WebApplicationException(response);
586 if (itemcsid == null || "".equals(itemcsid)) {
587 logger.error("getOrganization: missing itemcsid!");
588 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
589 "get failed on Organization itemcsid=" + itemcsid).type(
590 "text/plain").build();
591 throw new WebApplicationException(response);
593 MultipartOutput result = null;
595 // Note that we have to create the service context for the Items, not the main service
596 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
597 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
598 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
599 // TODO should we assert that the item is in the passed orgAuthority?
600 result = (MultipartOutput) ctx.getOutput();
601 } catch (UnauthorizedException ue) {
602 Response response = Response.status(
603 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
604 throw new WebApplicationException(response);
605 } catch (DocumentNotFoundException dnfe) {
606 if (logger.isDebugEnabled()) {
607 logger.debug("getOrganization", dnfe);
609 Response response = Response.status(Response.Status.NOT_FOUND).entity(
610 "Get failed on Organization csid=" + itemcsid).type(
611 "text/plain").build();
612 throw new WebApplicationException(response);
613 } catch (Exception e) {
614 if (logger.isDebugEnabled()) {
615 logger.debug("getOrganization", e);
617 Response response = Response.status(
618 Response.Status.INTERNAL_SERVER_ERROR).entity("Get failed").type("text/plain").build();
619 throw new WebApplicationException(response);
621 if (result == null) {
622 Response response = Response.status(Response.Status.NOT_FOUND).entity(
623 "Get failed, the requested Organization CSID:" + itemcsid + ": was not found.").type(
624 "text/plain").build();
625 throw new WebApplicationException(response);
631 * Gets the organization list.
633 * @param parentcsid the parentcsid
634 * @param partialTerm the partial term
637 * @return the organization list
640 @Path("{csid}/items")
641 @Produces("application/xml")
642 public OrganizationsCommonList getOrganizationList(
643 @PathParam("csid") String parentcsid,
644 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
645 @Context UriInfo ui) {
646 OrganizationsCommonList organizationObjectList = new OrganizationsCommonList();
648 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
649 // Note that docType defaults to the ServiceName, so we're fine with that.
650 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
652 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
653 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
654 myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
655 myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
656 ":" + OrganizationJAXBSchema.IN_AUTHORITY + "=" +
657 "'" + parentcsid + "'");
659 // AND organizations_common:displayName LIKE '%partialTerm%'
660 if (partialTerm != null && !partialTerm.isEmpty()) {
661 String ptClause = "AND " + OrganizationJAXBSchema.ORGANIZATIONS_COMMON +
662 ":" + OrganizationJAXBSchema.DISPLAY_NAME +
663 " LIKE " + "'%" + partialTerm + "%'";
664 myFilter.appendWhereClause(ptClause);
666 handler.setDocumentFilter(myFilter);
667 getRepositoryClient(ctx).getFiltered(ctx, handler);
668 organizationObjectList = (OrganizationsCommonList) handler.getCommonPartList();
669 } catch (UnauthorizedException ue) {
670 Response response = Response.status(
671 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
672 throw new WebApplicationException(response);
673 } catch (Exception e) {
674 if (logger.isDebugEnabled()) {
675 logger.debug("Caught exception in getOrganizationList", e);
677 Response response = Response.status(
678 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
679 throw new WebApplicationException(response);
681 return organizationObjectList;
685 * Gets the organization list by auth name.
687 * @param parentSpecifier the parent specifier
688 * @param partialTerm the partial term
691 * @return the organization list by auth name
694 @Path("urn:cspace:name({specifier})/items")
695 @Produces("application/xml")
696 public OrganizationsCommonList getOrganizationListByAuthName(
697 @PathParam("specifier") String parentSpecifier,
698 @QueryParam (IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
699 @Context UriInfo ui) {
700 OrganizationsCommonList personObjectList = new OrganizationsCommonList();
702 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
704 OrgAuthorityJAXBSchema.ORGAUTHORITIES_COMMON+
705 ":"+OrgAuthorityJAXBSchema.DISPLAY_NAME+
706 "='"+parentSpecifier+"'";
707 // Need to get an Authority by name
708 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(queryParams);
709 String parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
711 ctx = createServiceContext(getItemServiceName(), queryParams);
712 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
713 DocumentFilter myFilter = handler.createDocumentFilter();// new DocumentFilter();
714 myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
716 // Add the where clause "organizations_common:inAuthority='" + parentcsid + "'"
717 myFilter.setWhereClause(OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" +
718 OrganizationJAXBSchema.IN_AUTHORITY + "='" + parentcsid + "'");
720 // AND organizations_common:displayName LIKE '%partialTerm%'
721 if (partialTerm != null && !partialTerm.isEmpty()) {
722 String ptClause = "AND " +
723 OrganizationJAXBSchema.ORGANIZATIONS_COMMON + ":" +
724 OrganizationJAXBSchema.DISPLAY_NAME +
726 "'%" + partialTerm + "%'";
727 myFilter.appendWhereClause(ptClause);
730 handler.setDocumentFilter(myFilter);
731 getRepositoryClient(ctx).getFiltered(ctx, handler);
732 personObjectList = (OrganizationsCommonList) handler.getCommonPartList();
733 } catch (UnauthorizedException ue) {
734 Response response = Response.status(
735 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
736 throw new WebApplicationException(response);
737 } catch (Exception e) {
738 if (logger.isDebugEnabled()) {
739 logger.debug("Caught exception in getOrganizationListByAuthName", e);
741 Response response = Response.status(
742 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
743 throw new WebApplicationException(response);
745 return personObjectList;
749 * Update organization.
751 * @param parentcsid the parentcsid
752 * @param itemcsid the itemcsid
753 * @param theUpdate the the update
755 * @return the multipart output
758 @Path("{csid}/items/{itemcsid}")
759 public MultipartOutput updateOrganization(
760 @PathParam("csid") String parentcsid,
761 @PathParam("itemcsid") String itemcsid,
762 MultipartInput theUpdate) {
763 if (logger.isDebugEnabled()) {
764 logger.debug("updateOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
766 if (parentcsid == null || "".equals(parentcsid)) {
767 logger.error("updateOrganization: missing csid!");
768 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
769 "update failed on Organization parentcsid=" + parentcsid).type(
770 "text/plain").build();
771 throw new WebApplicationException(response);
773 if (itemcsid == null || "".equals(itemcsid)) {
774 logger.error("updateOrganization: missing itemcsid!");
775 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
776 "update failed on Organization=" + itemcsid).type(
777 "text/plain").build();
778 throw new WebApplicationException(response);
780 MultipartOutput result = null;
782 // Note that we have to create the service context for the Items, not the main service
783 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName(),
785 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid);
786 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
787 result = (MultipartOutput) ctx.getOutput();
788 } catch (BadRequestException bre) {
789 Response response = Response.status(
790 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
791 throw new WebApplicationException(response);
792 } catch (UnauthorizedException ue) {
793 Response response = Response.status(
794 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
795 throw new WebApplicationException(response);
796 } catch (DocumentNotFoundException dnfe) {
797 if (logger.isDebugEnabled()) {
798 logger.debug("caught exception in updateOrganization", dnfe);
800 Response response = Response.status(Response.Status.NOT_FOUND).entity(
801 "Update failed on Organization csid=" + itemcsid).type(
802 "text/plain").build();
803 throw new WebApplicationException(response);
804 } catch (Exception e) {
805 Response response = Response.status(
806 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
807 throw new WebApplicationException(response);
813 * Delete organization.
815 * @param parentcsid the parentcsid
816 * @param itemcsid the itemcsid
818 * @return the response
821 @Path("{csid}/items/{itemcsid}")
822 public Response deleteOrganization(
823 @PathParam("csid") String parentcsid,
824 @PathParam("itemcsid") String itemcsid) {
825 if (logger.isDebugEnabled()) {
826 logger.debug("deleteOrganization with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
828 if (parentcsid == null || "".equals(parentcsid)) {
829 logger.error("deleteOrganization: missing csid!");
830 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
831 "delete failed on Organization parentcsid=" + parentcsid).type(
832 "text/plain").build();
833 throw new WebApplicationException(response);
835 if (itemcsid == null || "".equals(itemcsid)) {
836 logger.error("deleteOrganization: missing itemcsid!");
837 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
838 "delete failed on Organization=" + itemcsid).type(
839 "text/plain").build();
840 throw new WebApplicationException(response);
843 // Note that we have to create the service context for the Items, not the main service
844 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getItemServiceName());
845 getRepositoryClient(ctx).delete(ctx, itemcsid);
846 return Response.status(HttpResponseCodes.SC_OK).build();
847 } catch (UnauthorizedException ue) {
848 Response response = Response.status(
849 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
850 throw new WebApplicationException(response);
851 } catch (DocumentNotFoundException dnfe) {
852 if (logger.isDebugEnabled()) {
853 logger.debug("caught exception in deleteOrganization", dnfe);
855 Response response = Response.status(Response.Status.NOT_FOUND).entity(
856 "Delete failed on Organization itemcsid=" + itemcsid).type(
857 "text/plain").build();
858 throw new WebApplicationException(response);
859 } catch (Exception e) {
860 Response response = Response.status(
861 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
862 throw new WebApplicationException(response);
867 /*************************************************************************
868 * Contact parts - this is a sub-resource of Organization (or "item")
869 *************************************************************************/
871 @Path("{parentcsid}/items/{itemcsid}/contacts")
872 public Response createContact(
873 @PathParam("parentcsid") String parentcsid,
874 @PathParam("itemcsid") String itemcsid,
875 MultipartInput input) {
877 // Note that we have to create the service context and document
878 // handler for the Contact service, not the main service.
879 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), input);
880 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
881 String csid = getRepositoryClient(ctx).create(ctx, handler);
882 UriBuilder path = UriBuilder.fromResource(OrgAuthorityResource.class);
883 path.path("" + parentcsid + "/items/" + itemcsid + "/contacts/" + csid);
884 Response response = Response.created(path.build()).build();
886 } catch (BadRequestException bre) {
887 Response response = Response.status(
888 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
889 throw new WebApplicationException(response);
890 } catch (UnauthorizedException ue) {
891 Response response = Response.status(
892 Response.Status.UNAUTHORIZED).entity("Create failed reason " + ue.getErrorReason()).type("text/plain").build();
893 throw new WebApplicationException(response);
894 } catch (Exception e) {
895 if (logger.isDebugEnabled()) {
896 logger.debug("Caught exception in createContact", e);
898 Response response = Response.status(
899 Response.Status.INTERNAL_SERVER_ERROR)
900 .entity("Attempt to create Contact failed.")
901 .type("text/plain").build();
902 throw new WebApplicationException(response);
908 * Gets the contact list.
910 * @param parentcsid the parentcsid
911 * @param itemcsid the itemcsid
914 * @return the contact list
917 @Produces({"application/xml"})
918 @Path("{parentcsid}/items/{itemcsid}/contacts/")
919 public ContactsCommonList getContactList(
920 @PathParam("parentcsid") String parentcsid,
921 @PathParam("itemcsid") String itemcsid,
922 @Context UriInfo ui) {
923 ContactsCommonList contactObjectList = new ContactsCommonList();
925 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
926 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(),
928 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
929 DocumentFilter myFilter = handler.createDocumentFilter(); //new DocumentFilter();
930 myFilter.setPagination(queryParams); //FIXME (this is unnecessary since it is already set by "createContactDocumentHandler" method
931 myFilter.setWhereClause(ContactJAXBSchema.CONTACTS_COMMON + ":" +
932 ContactJAXBSchema.IN_AUTHORITY +
933 "='" + parentcsid + "'" +
935 ContactJAXBSchema.CONTACTS_COMMON + ":" +
936 ContactJAXBSchema.IN_ITEM +
937 "='" + itemcsid + "'" +
938 " AND ecm:isProxy = 0");
939 handler.setDocumentFilter(myFilter);
940 getRepositoryClient(ctx).getFiltered(ctx, handler);
941 contactObjectList = (ContactsCommonList) handler.getCommonPartList();
942 } catch (UnauthorizedException ue) {
943 Response response = Response.status(
944 Response.Status.UNAUTHORIZED).entity("Index failed reason " + ue.getErrorReason()).type("text/plain").build();
945 throw new WebApplicationException(response);
946 } catch (Exception e) {
947 if (logger.isDebugEnabled()) {
948 logger.debug("Caught exception in getContactsList", e);
950 Response response = Response.status(
951 Response.Status.INTERNAL_SERVER_ERROR).entity("Index failed").type("text/plain").build();
952 throw new WebApplicationException(response);
954 return contactObjectList;
960 * @param parentcsid the parentcsid
961 * @param itemcsid the itemcsid
962 * @param csid the csid
964 * @return the contact
967 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
968 public MultipartOutput getContact(
969 @PathParam("parentcsid") String parentcsid,
970 @PathParam("itemcsid") String itemcsid,
971 @PathParam("csid") String csid) {
972 MultipartOutput result = null;
973 if (logger.isDebugEnabled()) {
974 logger.debug("getContact with parentCsid=" + parentcsid +
975 " itemcsid=" + itemcsid + " csid=" + csid);
978 // Note that we have to create the service context and document
979 // handler for the Contact service, not the main service.
980 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
981 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
982 getRepositoryClient(ctx).get(ctx, csid, handler);
983 result = (MultipartOutput) ctx.getOutput();
984 } catch (UnauthorizedException ue) {
985 Response response = Response.status(
986 Response.Status.UNAUTHORIZED).entity("Get failed reason " + ue.getErrorReason()).type("text/plain").build();
987 throw new WebApplicationException(response);
988 } catch (DocumentNotFoundException dnfe) {
989 if (logger.isDebugEnabled()) {
990 logger.debug("getContact", dnfe);
992 Response response = Response.status(Response.Status.NOT_FOUND)
993 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
994 .type("text/plain").build();
995 throw new WebApplicationException(response);
996 } catch (Exception e) {
997 if (logger.isDebugEnabled()) {
998 logger.debug("getContact", e);
1000 Response response = Response.status(Response.Status.INTERNAL_SERVER_ERROR)
1001 .entity("Get contact failed")
1002 .type("text/plain").build();
1003 throw new WebApplicationException(response);
1005 if (result == null) {
1006 Response response = Response.status(Response.Status.NOT_FOUND)
1007 .entity("Get failed, the requested Contact CSID:" + csid + ": was not found.")
1008 .type("text/plain").build();
1009 throw new WebApplicationException(response);
1018 * @param parentcsid the parentcsid
1019 * @param itemcsid the itemcsid
1020 * @param csid the csid
1021 * @param theUpdate the the update
1023 * @return the multipart output
1026 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1027 public MultipartOutput updateContact(
1028 @PathParam("parentcsid") String parentcsid,
1029 @PathParam("itemcsid") String itemcsid,
1030 @PathParam("csid") String csid,
1031 MultipartInput theUpdate) {
1032 if (logger.isDebugEnabled()) {
1033 logger.debug("updateContact with parentcsid=" + parentcsid +
1034 " itemcsid=" + itemcsid + " csid=" + csid);
1036 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1037 logger.error("updateContact: missing csid!");
1038 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1039 "update failed on Contact parentcsid=" + parentcsid).type(
1040 "text/plain").build();
1041 throw new WebApplicationException(response);
1043 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1044 logger.error("updateContact: missing itemcsid!");
1045 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1046 "update failed on Contact=" + itemcsid).type(
1047 "text/plain").build();
1048 throw new WebApplicationException(response);
1050 if (csid == null || csid.trim().isEmpty()) {
1051 logger.error("updateContact: missing csid!");
1052 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1053 "update failed on Contact=" + csid).type(
1054 "text/plain").build();
1055 throw new WebApplicationException(response);
1057 MultipartOutput result = null;
1059 // Note that we have to create the service context and document
1060 // handler for the Contact service, not the main service.
1061 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName(), theUpdate);
1062 DocumentHandler handler = createContactDocumentHandler(ctx, parentcsid, itemcsid);
1063 getRepositoryClient(ctx).update(ctx, csid, handler);
1064 result = (MultipartOutput) ctx.getOutput();
1065 } catch (BadRequestException bre) {
1066 Response response = Response.status(
1067 Response.Status.BAD_REQUEST).entity("Create failed reason " + bre.getErrorReason()).type("text/plain").build();
1068 throw new WebApplicationException(response);
1069 } catch (UnauthorizedException ue) {
1070 Response response = Response.status(
1071 Response.Status.UNAUTHORIZED).entity("Update failed reason " + ue.getErrorReason()).type("text/plain").build();
1072 throw new WebApplicationException(response);
1073 } catch (DocumentNotFoundException dnfe) {
1074 if (logger.isDebugEnabled()) {
1075 logger.debug("caught exception in updateContact", dnfe);
1077 Response response = Response.status(Response.Status.NOT_FOUND).entity(
1078 "Update failed on Contact csid=" + itemcsid).type(
1079 "text/plain").build();
1080 throw new WebApplicationException(response);
1081 } catch (Exception e) {
1082 Response response = Response.status(
1083 Response.Status.INTERNAL_SERVER_ERROR).entity("Update failed").type("text/plain").build();
1084 throw new WebApplicationException(response);
1092 * @param parentcsid the parentcsid
1093 * @param itemcsid the itemcsid
1094 * @param csid the csid
1096 * @return the response
1099 @Path("{parentcsid}/items/{itemcsid}/contacts/{csid}")
1100 public Response deleteContact(
1101 @PathParam("parentcsid") String parentcsid,
1102 @PathParam("itemcsid") String itemcsid,
1103 @PathParam("csid") String csid) {
1104 if (logger.isDebugEnabled()) {
1105 logger.debug("deleteContact with parentCsid=" + parentcsid +
1106 " itemcsid=" + itemcsid + " csid=" + csid);
1108 if (parentcsid == null || parentcsid.trim().isEmpty()) {
1109 logger.error("deleteContact: missing parentcsid!");
1110 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1111 "delete contact failed on parentcsid=" + parentcsid).type(
1112 "text/plain").build();
1113 throw new WebApplicationException(response);
1115 if (itemcsid == null || itemcsid.trim().isEmpty()) {
1116 logger.error("deleteContact: missing itemcsid!");
1117 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1118 "delete contact failed on itemcsid=" + itemcsid).type(
1119 "text/plain").build();
1120 throw new WebApplicationException(response);
1122 if (csid == null || csid.trim().isEmpty()) {
1123 logger.error("deleteContact: missing csid!");
1124 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
1125 "delete contact failed on csid=" + csid).type(
1126 "text/plain").build();
1127 throw new WebApplicationException(response);
1130 // Note that we have to create the service context for the
1131 // Contact service, not the main service.
1132 ServiceContext<MultipartInput, MultipartOutput> ctx = createServiceContext(getContactServiceName());
1133 getRepositoryClient(ctx).delete(ctx, csid);
1134 return Response.status(HttpResponseCodes.SC_OK).build();
1135 } catch (UnauthorizedException ue) {
1136 Response response = Response.status(
1137 Response.Status.UNAUTHORIZED).entity("Delete failed reason " + ue.getErrorReason()).type("text/plain").build();
1138 throw new WebApplicationException(response);
1139 } catch (DocumentNotFoundException dnfe) {
1140 if (logger.isDebugEnabled()) {
1141 logger.debug("Caught exception in deleteContact", dnfe);
1143 Response response = Response.status(Response.Status.NOT_FOUND)
1144 .entity("Delete failed, the requested Contact CSID:" + csid + ": was not found.")
1145 .type("text/plain").build();
1146 throw new WebApplicationException(response);
1147 } catch (Exception e) {
1148 Response response = Response.status(
1149 Response.Status.INTERNAL_SERVER_ERROR).entity("Delete failed").type("text/plain").build();
1150 throw new WebApplicationException(response);