2 * This document is a part of the source code and related artifacts
3 * for CollectionSpace, an open source collections management system
4 * for museums and related institutions:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
24 package org.collectionspace.services.common.vocabulary;
26 import org.collectionspace.services.client.IClientQueryParams;
27 import org.collectionspace.services.client.IQueryManager;
28 import org.collectionspace.services.client.PoxPayloadIn;
29 import org.collectionspace.services.client.PoxPayloadOut;
30 import org.collectionspace.services.client.workflow.WorkflowClient;
31 import org.collectionspace.services.common.ClientType;
32 import org.collectionspace.services.common.ResourceBase;
33 import org.collectionspace.services.common.ResourceMap;
34 import org.collectionspace.services.common.ServiceMain;
35 import org.collectionspace.services.common.ServiceMessages;
36 import org.collectionspace.services.common.XmlTools;
37 import org.collectionspace.services.common.api.RefName;
38 import org.collectionspace.services.common.api.Tools;
39 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
40 import org.collectionspace.services.common.authorityref.AuthorityRefList;
41 import org.collectionspace.services.common.context.JaxRsContext;
42 import org.collectionspace.services.common.context.MultipartServiceContext;
43 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
44 import org.collectionspace.services.common.context.RemoteServiceContext;
45 import org.collectionspace.services.common.context.ServiceBindingUtils;
46 import org.collectionspace.services.common.context.ServiceContext;
47 import org.collectionspace.services.common.document.DocumentException;
48 import org.collectionspace.services.common.document.DocumentFilter;
49 import org.collectionspace.services.common.document.DocumentHandler;
50 import org.collectionspace.services.common.document.DocumentNotFoundException;
51 import org.collectionspace.services.common.document.DocumentWrapper;
52 import org.collectionspace.services.common.query.QueryManager;
53 import org.collectionspace.services.common.repository.RepositoryClient;
54 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityDocumentModelHandler;
55 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
56 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;
57 import org.collectionspace.services.jaxb.AbstractCommonList;
58 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
59 import org.collectionspace.services.relation.RelationResource;
60 import org.collectionspace.services.relation.RelationsCommonList;
61 import org.collectionspace.services.relation.RelationshipType;
62 import org.jboss.resteasy.util.HttpResponseCodes;
63 import org.nuxeo.ecm.core.api.DocumentModel;
64 import org.slf4j.Logger;
65 import org.slf4j.LoggerFactory;
67 import javax.ws.rs.Consumes;
68 import javax.ws.rs.DELETE;
69 import javax.ws.rs.GET;
70 import javax.ws.rs.POST;
71 import javax.ws.rs.PUT;
72 import javax.ws.rs.Path;
73 import javax.ws.rs.PathParam;
74 import javax.ws.rs.Produces;
75 import javax.ws.rs.QueryParam;
76 import javax.ws.rs.WebApplicationException;
77 import javax.ws.rs.core.Context;
78 import javax.ws.rs.core.MultivaluedMap;
79 import javax.ws.rs.core.Request;
80 import javax.ws.rs.core.Response;
81 import javax.ws.rs.core.UriBuilder;
82 import javax.ws.rs.core.UriInfo;
84 import java.util.ArrayList;
85 import java.util.List;
88 * The Class AuthorityResource.
90 @Consumes("application/xml")
91 @Produces("application/xml")
92 public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
93 extends ResourceBase {
95 protected Class<AuthCommon> authCommonClass;
96 protected Class<?> resourceClass;
97 protected String authorityCommonSchemaName;
98 protected String authorityItemCommonSchemaName;
99 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
100 final static String URN_PREFIX = "urn:cspace:";
101 final static int URN_PREFIX_LEN = URN_PREFIX.length();
102 final static String URN_PREFIX_NAME = "name(";
103 final static int URN_NAME_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_NAME.length();
104 final static String URN_PREFIX_ID = "id(";
105 final static int URN_ID_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_ID.length();
106 final static String FETCH_SHORT_ID = "_fetch_";
107 final Logger logger = LoggerFactory.getLogger(AuthorityResource.class);
109 public enum SpecifierForm {
114 public class Specifier {
116 public SpecifierForm form;
119 Specifier(SpecifierForm form, String value) {
125 protected Specifier getSpecifier(String specifierIn, String method, String op) throws WebApplicationException {
126 if (logger.isDebugEnabled()) {
127 logger.debug("getSpecifier called by: " + method + " with specifier: " + specifierIn);
129 if (specifierIn != null) {
130 if (!specifierIn.startsWith(URN_PREFIX)) {
131 // We'll assume it is a CSID and complain if it does not match
132 return new Specifier(SpecifierForm.CSID, specifierIn);
134 if (specifierIn.startsWith(URN_PREFIX_NAME, URN_PREFIX_LEN)) {
135 int closeParen = specifierIn.indexOf(')', URN_NAME_PREFIX_LEN);
136 if (closeParen >= 0) {
137 return new Specifier(SpecifierForm.URN_NAME,
138 specifierIn.substring(URN_NAME_PREFIX_LEN, closeParen));
140 } else if (specifierIn.startsWith(URN_PREFIX_ID, URN_PREFIX_LEN)) {
141 int closeParen = specifierIn.indexOf(')', URN_ID_PREFIX_LEN);
142 if (closeParen >= 0) {
143 return new Specifier(SpecifierForm.CSID,
144 specifierIn.substring(URN_ID_PREFIX_LEN, closeParen));
149 logger.error(method + ": bad or missing specifier!");
150 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
151 op + " failed on bad or missing Authority specifier").type(
152 "text/plain").build();
153 throw new WebApplicationException(response);
157 * Instantiates a new Authority resource.
159 public AuthorityResource(Class<AuthCommon> authCommonClass, Class<?> resourceClass,
160 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
161 this.authCommonClass = authCommonClass;
162 this.resourceClass = resourceClass;
163 this.authorityCommonSchemaName = authorityCommonSchemaName;
164 this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
167 public abstract String getItemServiceName();
170 protected String getVersionString() {
171 return "$LastChangedRevision: 2617 $";
175 public Class<AuthCommon> getCommonPartClass() {
176 return authCommonClass;
180 * Creates the item document handler.
183 * @param inAuthority the in vocabulary
185 * @return the document handler
187 * @throws Exception the exception
189 protected DocumentHandler createItemDocumentHandler(
190 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
191 String inAuthority, String parentShortIdentifier)
193 String authorityRefNameBase;
194 AuthorityItemDocumentModelHandler<?> docHandler;
196 if (parentShortIdentifier == null) {
197 authorityRefNameBase = null;
199 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx =
200 createServiceContext(getServiceName());
201 if (parentShortIdentifier.equals(FETCH_SHORT_ID)) {
202 // Get from parent document
203 parentShortIdentifier = getAuthShortIdentifier(parentCtx, inAuthority);
205 authorityRefNameBase = buildAuthorityRefNameBase(parentCtx, parentShortIdentifier);
208 docHandler = (AuthorityItemDocumentModelHandler<?>) createDocumentHandler(ctx,
209 ctx.getCommonPartLabel(getItemServiceName()),
211 docHandler.setInAuthority(inAuthority);
212 docHandler.setAuthorityRefNameBase(authorityRefNameBase);
217 public String getAuthShortIdentifier(
218 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String authCSID)
219 throws DocumentNotFoundException, DocumentException {
220 String shortIdentifier = null;
222 DocumentWrapper<DocumentModel> wrapDoc = getRepositoryClient(ctx).getDocFromCsid(ctx, authCSID);
223 AuthorityDocumentModelHandler<?> handler =
224 (AuthorityDocumentModelHandler<?>) createDocumentHandler(ctx);
225 shortIdentifier = handler.getShortIdentifier(wrapDoc, authorityCommonSchemaName);
226 } catch (Exception e) {
227 if (logger.isDebugEnabled()) {
228 logger.debug("Caught exception ", e);
230 throw new DocumentException(e);
232 return shortIdentifier;
235 protected String buildAuthorityRefNameBase(
236 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String shortIdentifier) {
237 RefName.Authority authority = RefName.buildAuthority(ctx.getTenantName(),
238 ctx.getServiceName(), shortIdentifier, null);
239 return authority.toString();
242 public static class CsidAndShortIdentifier {
245 String shortIdentifier;
248 public String lookupParentCSID(String parentspecifier, String method, String op, MultivaluedMap<String, String> queryParams)
250 CsidAndShortIdentifier tempResult = lookupParentCSIDAndShortIdentifer(parentspecifier, method, op, queryParams);
251 return tempResult.CSID;
254 public CsidAndShortIdentifier lookupParentCSIDAndShortIdentifer(String parentspecifier, String method, String op, MultivaluedMap<String, String> queryParams)
256 CsidAndShortIdentifier result = new CsidAndShortIdentifier();
257 Specifier parentSpec = getSpecifier(parentspecifier, method, op);
258 // Note that we have to create the service context for the Items, not the main service
260 String parentShortIdentifier;
261 if (parentSpec.form == SpecifierForm.CSID) {
262 parentShortIdentifier = null;
263 parentcsid = parentSpec.value;
264 // Uncomment when app layer is ready to integrate
265 // Uncommented since refNames are currently only generated if not present - ADR CSPACE-3178
266 parentShortIdentifier = FETCH_SHORT_ID;
268 parentShortIdentifier = parentSpec.value;
269 String whereClause = buildWhereForAuthByName(parentSpec.value);
270 ServiceContext ctx = createServiceContext(getServiceName(), queryParams);
271 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); //FIXME: REM - If the parent has been soft-deleted, should we be looking for the item?
273 result.CSID = parentcsid;
274 result.shortIdentifier = parentShortIdentifier;
278 public String lookupItemCSID(String itemspecifier, String parentcsid, String method, String op, ServiceContext ctx)
279 throws DocumentException {
281 Specifier itemSpec = getSpecifier(itemspecifier, method, op);
282 if (itemSpec.form == SpecifierForm.CSID) {
283 itemcsid = itemSpec.value;
285 String itemWhereClause = buildWhereForAuthItemByName(itemSpec.value, parentcsid);
286 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); //FIXME: REM - Should we be looking for the 'wf_deleted' query param and filtering on it?
292 * Generally, callers will first call RefName.AuthorityItem.parse with a refName, and then
293 * use the returned item.inAuthority.resource and a resourceMap to get a service-specific
294 * Resource. They then call this method on that resource.
297 public DocumentModel getDocModelForAuthorityItem(RefName.AuthorityItem item)
298 throws Exception, DocumentNotFoundException {
302 String whereClause = buildWhereForAuthByName(item.getParentShortIdentifier());
303 // Ensure we have the right context.
304 ServiceContext ctx = createServiceContext(item.inAuthority.resource);
306 String parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
308 String itemWhereClause = buildWhereForAuthItemByName(item.getShortIdentifier(), parentcsid);
309 ctx = createServiceContext(getItemServiceName());
310 DocumentWrapper<DocumentModel> docWrapper = getRepositoryClient(ctx).findDoc(ctx, itemWhereClause);
311 DocumentModel docModel = docWrapper.getWrappedObject();
317 public Response createAuthority(String xmlPayload) {
319 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
320 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(input);
321 DocumentHandler handler = createDocumentHandler(ctx);
322 String csid = getRepositoryClient(ctx).create(ctx, handler);
323 UriBuilder path = UriBuilder.fromResource(resourceClass);
324 path.path("" + csid);
325 Response response = Response.created(path.build()).build();
327 } catch (Exception e) {
328 throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
332 protected String buildWhereForAuthByName(String name) {
333 return authorityCommonSchemaName
334 + ":" + AuthorityJAXBSchema.SHORT_IDENTIFIER
338 protected String buildWhereForAuthItemByName(String name, String parentcsid) {
339 return authorityItemCommonSchemaName
340 + ":" + AuthorityItemJAXBSchema.SHORT_IDENTIFIER
341 + "='" + name + "' AND "
342 + authorityItemCommonSchemaName + ":"
343 + AuthorityItemJAXBSchema.IN_AUTHORITY + "="
344 + "'" + parentcsid + "'";
348 * Gets the authority.
350 * @param specifier either a CSID or one of the urn forms
352 * @return the authority
357 public byte[] get( // getAuthority(
359 @PathParam("csid") String specifier) {
360 PoxPayloadOut result = null;
362 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
363 DocumentHandler handler = createDocumentHandler(ctx);
365 Specifier spec = getSpecifier(specifier, "getAuthority", "GET");
366 if (spec.form == SpecifierForm.CSID) {
367 if (logger.isDebugEnabled()) {
368 logger.debug("getAuthority with csid=" + spec.value);
370 getRepositoryClient(ctx).get(ctx, spec.value, handler);
372 String whereClause = buildWhereForAuthByName(spec.value);
373 DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
374 handler.setDocumentFilter(myFilter);
375 getRepositoryClient(ctx).get(ctx, handler);
377 result = ctx.getOutput();
379 } catch (Exception e) {
380 throw bigReThrow(e, ServiceMessages.GET_FAILED, specifier);
383 if (result == null) {
384 Response response = Response.status(Response.Status.NOT_FOUND).entity(
385 "Get failed, the requested Authority specifier:" + specifier + ": was not found.").type(
386 "text/plain").build();
387 throw new WebApplicationException(response);
390 return result.getBytes();
394 * Finds and populates the authority list.
398 * @return the authority list
401 @Produces("application/xml")
402 public AbstractCommonList getAuthorityList(@Context UriInfo ui) {
404 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
405 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
406 DocumentHandler handler = createDocumentHandler(ctx);
407 DocumentFilter myFilter = handler.getDocumentFilter();
408 // Need to make the default sort order for authority items
409 // be on the displayName field
410 String sortBy = queryParams.getFirst(IClientQueryParams.SORT_BY_PARAM);
411 if (sortBy == null || sortBy.isEmpty()) {
412 String qualifiedDisplayNameField = authorityCommonSchemaName + ":"
413 + AuthorityItemJAXBSchema.DISPLAY_NAME;
414 myFilter.setOrderByClause(qualifiedDisplayNameField);
416 String nameQ = queryParams.getFirst("refName");
418 myFilter.setWhereClause(authorityCommonSchemaName + ":refName='" + nameQ + "'");
420 getRepositoryClient(ctx).getFiltered(ctx, handler);
421 return (AbstractCommonList) handler.getCommonPartList();
422 } catch (Exception e) {
423 throw bigReThrow(e, ServiceMessages.GET_FAILED);
430 * @param specifier the csid or id
432 * @return the multipart output
436 public byte[] updateAuthority(
437 @PathParam("csid") String specifier,
439 PoxPayloadOut result = null;
441 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
442 Specifier spec = getSpecifier(specifier, "updateAuthority", "UPDATE");
443 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(theUpdate);
444 DocumentHandler handler = createDocumentHandler(ctx);
446 if (spec.form == SpecifierForm.CSID) {
449 String whereClause = buildWhereForAuthByName(spec.value);
450 csid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
452 getRepositoryClient(ctx).update(ctx, csid, handler);
453 result = ctx.getOutput();
454 } catch (Exception e) {
455 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED);
457 return result.getBytes();
463 * @param csid the csid
465 * @return the response
469 public Response deleteAuthority(@PathParam("csid") String csid) {
470 if (logger.isDebugEnabled()) {
471 logger.debug("deleteAuthority with csid=" + csid);
474 ensureCSID(csid, ServiceMessages.DELETE_FAILED, "Authority.csid");
475 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
476 getRepositoryClient(ctx).delete(ctx, csid);
477 return Response.status(HttpResponseCodes.SC_OK).build();
478 } catch (Exception e) {
479 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
483 /*************************************************************************
484 * Create an AuthorityItem - this is a sub-resource of Authority
485 * @param specifier either a CSID or one of the urn forms
486 * @return Authority item response
487 *************************************************************************/
489 @Path("{csid}/items")
490 public Response createAuthorityItem(@Context ResourceMap resourceMap, @Context UriInfo ui,
491 @PathParam("csid") String specifier, String xmlPayload) {
493 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
494 ServiceContext ctx = createServiceContext(getItemServiceName(), input);
495 ctx.setResourceMap(resourceMap);
496 ctx.setUriInfo(ui); //Laramie
498 // Note: must have the parentShortId, to do the create.
499 CsidAndShortIdentifier parent = lookupParentCSIDAndShortIdentifer(specifier, "createAuthorityItem", "CREATE_ITEM", null);
500 DocumentHandler handler = createItemDocumentHandler(ctx, parent.CSID, parent.shortIdentifier);
501 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
502 UriBuilder path = UriBuilder.fromResource(resourceClass);
503 path.path(parent.CSID + "/items/" + itemcsid);
504 Response response = Response.created(path.build()).build();
506 } catch (Exception e) {
507 throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
512 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH)
513 public byte[] getItemWorkflow(
514 @PathParam("csid") String csid,
515 @PathParam("itemcsid") String itemcsid) {
516 PoxPayloadOut result = null;
519 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getItemServiceName());
520 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
522 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
523 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
524 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
525 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
526 result = ctx.getOutput();
527 } catch (Exception e) {
528 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
530 return result.getBytes();
534 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH)
535 public byte[] updateWorkflow(
536 @PathParam("csid") String csid,
537 @PathParam("itemcsid") String itemcsid,
539 PoxPayloadOut result = null;
541 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getItemServiceName());
542 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
544 PoxPayloadIn workflowUpdate = new PoxPayloadIn(xmlPayload);
545 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, workflowUpdate);
546 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
547 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
548 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
549 result = ctx.getOutput();
550 } catch (Exception e) {
551 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
553 return result.getBytes();
557 * Gets the authority item.
559 * @param parentspecifier either a CSID or one of the urn forms
560 * @param itemspecifier either a CSID or one of the urn forms
562 * @return the authority item
565 @Path("{csid}/items/{itemcsid}")
566 public byte[] getAuthorityItem(
567 @Context Request request,
569 @PathParam("csid") String parentspecifier,
570 @PathParam("itemcsid") String itemspecifier) {
571 PoxPayloadOut result = null;
573 JaxRsContext jaxRsContext = new JaxRsContext(request, ui);
574 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
575 String parentcsid = lookupParentCSID(parentspecifier, "getAuthorityItem(parent)", "GET_ITEM", queryParams);
577 RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
578 ctx = (RemoteServiceContext) createServiceContext(getItemServiceName(), queryParams);
579 ctx.setJaxRsContext(jaxRsContext);
581 ctx.setUriInfo(ui); //ARG! must pass this or subsequent calls will not have a ui.
583 // We omit the parentShortId, only needed when doing a create...
584 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, null);
586 Specifier itemSpec = getSpecifier(itemspecifier, "getAuthorityItem(item)", "GET_ITEM");
587 if (itemSpec.form == SpecifierForm.CSID) {
588 getRepositoryClient(ctx).get(ctx, itemSpec.value, handler);
590 String itemWhereClause =
591 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
592 DocumentFilter myFilter = new DocumentFilter(itemWhereClause, 0, 1);
593 handler.setDocumentFilter(myFilter);
594 getRepositoryClient(ctx).get(ctx, handler);
596 // TODO should we assert that the item is in the passed vocab?
597 result = ctx.getOutput();
598 } catch (Exception e) {
599 throw bigReThrow(e, ServiceMessages.GET_FAILED);
601 if (result == null) {
602 Response response = Response.status(Response.Status.NOT_FOUND).entity(
603 "Get failed, the requested AuthorityItem specifier:" + itemspecifier + ": was not found.").type(
604 "text/plain").build();
605 throw new WebApplicationException(response);
607 return result.getBytes();
611 * Gets the authorityItem list for the specified authority
612 * If partialPerm is specified, keywords will be ignored.
614 * @param specifier either a CSID or one of the urn forms
615 * @param partialTerm if non-null, matches partial terms
616 * @param keywords if non-null, matches terms in the keyword index for items
617 * @param ui passed to include additional parameters, like pagination controls
619 * @return the authorityItem list
622 @Path("{csid}/items")
623 @Produces("application/xml")
624 public AbstractCommonList getAuthorityItemList(@PathParam("csid") String specifier,
625 @Context UriInfo ui) {
627 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
628 String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM);
629 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
630 String advancedSearch = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_AS);
632 String qualifiedDisplayNameField = authorityItemCommonSchemaName + ":"
633 + AuthorityItemJAXBSchema.DISPLAY_NAME;
635 // Note that docType defaults to the ServiceName, so we're fine with that.
636 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
638 String parentcsid = lookupParentCSID(specifier, "getAuthorityItemList", "LIST", queryParams);
640 ctx = createServiceContext(getItemServiceName(), queryParams);
641 // We omit the parentShortId, only needed when doing a create...
642 DocumentHandler handler = createItemDocumentHandler(ctx,
644 DocumentFilter myFilter = handler.getDocumentFilter();
645 // Need to make the default sort order for authority items
646 // be on the displayName field
647 String sortBy = queryParams.getFirst(IClientQueryParams.SORT_BY_PARAM);
648 if (sortBy == null || sortBy.isEmpty()) {
649 myFilter.setOrderByClause(qualifiedDisplayNameField);
652 myFilter.appendWhereClause(authorityItemCommonSchemaName + ":"
653 + AuthorityItemJAXBSchema.IN_AUTHORITY + "="
654 + "'" + parentcsid + "'",
655 IQueryManager.SEARCH_QUALIFIER_AND);
657 // AND vocabularyitems_common:displayName LIKE '%partialTerm%'
658 // NOTE: Partial terms searches are mutually exclusive to keyword and advanced-search, but
659 // the PT query param trumps the KW and AS query params.
660 if (partialTerm != null && !partialTerm.isEmpty()) {
661 String ptClause = QueryManager.createWhereClauseForPartialMatch(
662 qualifiedDisplayNameField, partialTerm);
663 myFilter.appendWhereClause(ptClause, IQueryManager.SEARCH_QUALIFIER_AND);
664 } else if (keywords != null || advancedSearch != null) {
665 // String kwdClause = QueryManager.createWhereClauseFromKeywords(keywords);
666 // myFilter.appendWhereClause(kwdClause, IQueryManager.SEARCH_QUALIFIER_AND);
667 return search(ctx, handler, queryParams, keywords, advancedSearch);
669 if (logger.isDebugEnabled()) {
670 logger.debug("getAuthorityItemList filtered WHERE clause: "
671 + myFilter.getWhereClause());
673 getRepositoryClient(ctx).getFiltered(ctx, handler);
674 return (AbstractCommonList) handler.getCommonPartList();
675 } catch (Exception e) {
676 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
681 * Gets the entities referencing this Authority item instance. The service type
682 * can be passed as a query param "type", and must match a configured type
683 * for the service bindings. If not set, the type defaults to
684 * ServiceBindingUtils.SERVICE_TYPE_PROCEDURE.
686 * @param parentspecifier either a CSID or one of the urn forms
687 * @param itemspecifier either a CSID or one of the urn forms
690 * @return the info for the referencing objects
693 @Path("{csid}/items/{itemcsid}/refObjs")
694 @Produces("application/xml")
695 public AuthorityRefDocList getReferencingObjects(
696 @PathParam("csid") String parentspecifier,
697 @PathParam("itemcsid") String itemspecifier,
698 @Context UriInfo ui) {
699 AuthorityRefDocList authRefDocList = null;
701 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
703 String parentcsid = lookupParentCSID(parentspecifier, "getReferencingObjects(parent)", "GET_ITEM_REF_OBJS", queryParams);
705 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), queryParams);
706 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getReferencingObjects(item)", "GET_ITEM_REF_OBJS", ctx);
708 // Note that we have to create the service context for the Items, not the main service
709 // We omit the parentShortId, only needed when doing a create...
710 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, null);
711 RepositoryClient repoClient = getRepositoryClient(ctx);
712 DocumentFilter myFilter = handler.getDocumentFilter();
713 String serviceType = ServiceBindingUtils.SERVICE_TYPE_PROCEDURE;
714 List<String> list = queryParams.remove(ServiceBindingUtils.SERVICE_TYPE_PROP);
716 serviceType = list.get(0);
718 DocumentWrapper<DocumentModel> docWrapper = repoClient.getDoc(ctx, itemcsid);
719 DocumentModel docModel = docWrapper.getWrappedObject();
720 String refName = (String) docModel.getPropertyValue(AuthorityItemJAXBSchema.REF_NAME);
722 authRefDocList = RefNameServiceUtils.getAuthorityRefDocs(ctx,
726 myFilter.getPageSize(), myFilter.getStartPage(), true /*computeTotal*/);
727 } catch (Exception e) {
728 throw bigReThrow(e, ServiceMessages.GET_FAILED);
730 if (authRefDocList == null) {
731 Response response = Response.status(Response.Status.NOT_FOUND).entity(
732 "Get failed, the requested Item CSID:" + itemspecifier + ": was not found.").type(
733 "text/plain").build();
734 throw new WebApplicationException(response);
736 return authRefDocList;
740 * Gets the authority terms used in the indicated Authority item.
742 * @param parentspecifier either a CSID or one of the urn forms
743 * @param itemspecifier either a CSID or one of the urn forms
744 * @param ui passed to include additional parameters, like pagination controls
746 * @return the authority refs for the Authority item.
749 @Path("{csid}/items/{itemcsid}/authorityrefs")
750 @Produces("application/xml")
751 public AuthorityRefList getAuthorityItemAuthorityRefs(
752 @PathParam("csid") String parentspecifier,
753 @PathParam("itemcsid") String itemspecifier,
754 @Context UriInfo ui) {
755 AuthorityRefList authRefList = null;
757 // Note that we have to create the service context for the Items, not the main service
758 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
759 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
761 String parentcsid = lookupParentCSID(parentspecifier, "getAuthorityItemAuthRefs(parent)", "GET_ITEM_AUTH_REFS", queryParams);
763 ctx = createServiceContext(getItemServiceName(), queryParams);
764 // We omit the parentShortId, only needed when doing a create...
765 RemoteDocumentModelHandlerImpl handler =
766 (RemoteDocumentModelHandlerImpl) createItemDocumentHandler(ctx, parentcsid, null);
768 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getAuthorityItemAuthRefs(item)", "GET_ITEM_AUTH_REFS", ctx);
770 DocumentWrapper<DocumentModel> docWrapper = getRepositoryClient(ctx).getDoc(ctx, itemcsid);
771 List<String> authRefFields =
772 ((MultipartServiceContextImpl) ctx).getCommonPartPropertyValues(
773 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
774 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
775 } catch (Exception e) {
776 throw bigReThrow(e, ServiceMessages.GET_FAILED + " parentspecifier: " + parentspecifier + " itemspecifier:" + itemspecifier);
782 * Update authorityItem.
784 * @param parentspecifier either a CSID or one of the urn forms
785 * @param itemspecifier either a CSID or one of the urn forms
787 * @return the multipart output
790 @Path("{csid}/items/{itemcsid}")
791 public byte[] updateAuthorityItem(
792 @Context ResourceMap resourceMap,
794 @PathParam("csid") String parentspecifier,
795 @PathParam("itemcsid") String itemspecifier,
797 PoxPayloadOut result = null;
799 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
800 // Note that we have to create the service context for the Items, not the main service
801 //Laramie CSPACE-3175. passing null for queryParams, because prior to this refactor, the code moved to lookupParentCSID in this instance called the version of getServiceContext() that passes null
802 String parentcsid = lookupParentCSID(parentspecifier, "updateAuthorityItem(parent)", "UPDATE_ITEM", null);
804 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), theUpdate);
805 ctx.setResourceMap(resourceMap);
806 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "updateAuthorityItem(item)", "UPDATE_ITEM", ctx);
808 // We omit the parentShortId, only needed when doing a create...
809 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, null);
811 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
812 result = ctx.getOutput();
814 } catch (Exception e) {
815 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED);
817 return result.getBytes();
821 * Delete authorityItem.
823 * @param parentcsid the parentcsid
824 * @param itemcsid the itemcsid
826 * @return the response
829 @Path("{csid}/items/{itemcsid}")
830 public Response deleteAuthorityItem(
831 @PathParam("csid") String parentcsid,
832 @PathParam("itemcsid") String itemcsid) {
834 if (logger.isDebugEnabled()) {
835 logger.debug("deleteAuthorityItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
838 ensureCSID(parentcsid, ServiceMessages.DELETE_FAILED, "AuthorityItem.parentcsid");
839 ensureCSID(itemcsid, ServiceMessages.DELETE_FAILED, "AuthorityItem.itemcsid");
840 //Laramie, removing this catch, since it will surely fail below, since itemcsid or parentcsid will be null.
841 // }catch (Throwable t){
842 // System.out.println("ERROR in setting up DELETE: "+t);
845 // Note that we have to create the service context for the Items, not the main service
846 ServiceContext ctx = createServiceContext(getItemServiceName());
847 getRepositoryClient(ctx).delete(ctx, itemcsid);
848 return Response.status(HttpResponseCodes.SC_OK).build();
849 } catch (Exception e) {
850 throw bigReThrow(e, ServiceMessages.DELETE_FAILED + " itemcsid: " + itemcsid + " parentcsid:" + parentcsid);
853 public final static String hierarchy = "hierarchy";
856 @Path("{csid}/items/{itemcsid}/" + hierarchy)
857 @Produces("application/xml")
858 public String getHierarchy(@PathParam("csid") String csid,
859 @PathParam("itemcsid") String itemcsid,
860 @Context UriInfo ui) throws Exception {
862 // All items in dive can look at their child uri's to get uri. So we calculate the very first one. We could also do a GET and look at the common part uri field, but why...?
863 String calledUri = ui.getPath();
864 String uri = "/" + calledUri.substring(0, (calledUri.length() - ("/" + hierarchy).length()));
865 ServiceContext ctx = createServiceContext(getItemServiceName());
867 String direction = ui.getQueryParameters().getFirst(Hierarchy.directionQP);
868 if (Tools.notBlank(direction) && Hierarchy.direction_parents.equals(direction)) {
869 return Hierarchy.surface(ctx, itemcsid, uri);
871 return Hierarchy.dive(ctx, itemcsid, uri);
873 } catch (Exception e) {
874 throw bigReThrow(e, "Error showing hierarchy", itemcsid);