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.IQueryManager;
27 import org.collectionspace.services.client.PoxPayloadIn;
28 import org.collectionspace.services.client.PoxPayloadOut;
29 import org.collectionspace.services.client.workflow.WorkflowClient;
30 import org.collectionspace.services.common.ClientType;
31 import org.collectionspace.services.common.ResourceBase;
32 import org.collectionspace.services.common.ServiceMain;
33 import org.collectionspace.services.common.ServiceMessages;
34 import org.collectionspace.services.common.XmlTools;
35 import org.collectionspace.services.common.api.RefName;
36 import org.collectionspace.services.common.api.Tools;
37 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
38 import org.collectionspace.services.common.authorityref.AuthorityRefList;
39 import org.collectionspace.services.common.context.JaxRsContext;
40 import org.collectionspace.services.common.context.MultipartServiceContext;
41 import org.collectionspace.services.common.context.MultipartServiceContextImpl;
42 import org.collectionspace.services.common.context.RemoteServiceContext;
43 import org.collectionspace.services.common.context.ServiceBindingUtils;
44 import org.collectionspace.services.common.context.ServiceContext;
45 import org.collectionspace.services.common.document.DocumentException;
46 import org.collectionspace.services.common.document.DocumentFilter;
47 import org.collectionspace.services.common.document.DocumentHandler;
48 import org.collectionspace.services.common.document.DocumentNotFoundException;
49 import org.collectionspace.services.common.document.DocumentWrapper;
50 import org.collectionspace.services.common.query.QueryManager;
51 import org.collectionspace.services.common.repository.RepositoryClient;
52 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityDocumentModelHandler;
53 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
54 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;
55 import org.collectionspace.services.jaxb.AbstractCommonList;
56 import org.collectionspace.services.nuxeo.client.java.RemoteDocumentModelHandlerImpl;
57 import org.collectionspace.services.relation.RelationResource;
58 import org.collectionspace.services.relation.RelationsCommonList;
59 import org.collectionspace.services.relation.RelationshipType;
60 import org.jboss.resteasy.util.HttpResponseCodes;
61 import org.nuxeo.ecm.core.api.DocumentModel;
62 import org.slf4j.Logger;
63 import org.slf4j.LoggerFactory;
65 import javax.ws.rs.Consumes;
66 import javax.ws.rs.DELETE;
67 import javax.ws.rs.GET;
68 import javax.ws.rs.POST;
69 import javax.ws.rs.PUT;
70 import javax.ws.rs.Path;
71 import javax.ws.rs.PathParam;
72 import javax.ws.rs.Produces;
73 import javax.ws.rs.QueryParam;
74 import javax.ws.rs.WebApplicationException;
75 import javax.ws.rs.core.Context;
76 import javax.ws.rs.core.MultivaluedMap;
77 import javax.ws.rs.core.Request;
78 import javax.ws.rs.core.Response;
79 import javax.ws.rs.core.UriBuilder;
80 import javax.ws.rs.core.UriInfo;
81 import java.util.List;
84 * The Class AuthorityResource.
86 @Consumes("application/xml")
87 @Produces("application/xml")
88 public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
89 extends ResourceBase {
91 protected Class<AuthCommon> authCommonClass;
92 protected Class<?> resourceClass;
93 protected String authorityCommonSchemaName;
94 protected String authorityItemCommonSchemaName;
96 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType();
98 final static String URN_PREFIX = "urn:cspace:";
99 final static int URN_PREFIX_LEN = URN_PREFIX.length();
100 final static String URN_PREFIX_NAME = "name(";
101 final static int URN_NAME_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_NAME.length();
102 final static String URN_PREFIX_ID = "id(";
103 final static int URN_ID_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_ID.length();
104 final static String FETCH_SHORT_ID = "_fetch_";
106 final Logger logger = LoggerFactory.getLogger(AuthorityResource.class);
108 public enum SpecifierForm { CSID, URN_NAME };
110 public class Specifier {
111 public SpecifierForm form;
113 Specifier(SpecifierForm form, String value) {
119 protected Specifier getSpecifier(String specifierIn, String method, String op) throws WebApplicationException {
120 if (logger.isDebugEnabled()) {
121 logger.debug("getSpecifier called by: "+method+" with specifier: "+specifierIn);
123 if (specifierIn != null) {
124 if(!specifierIn.startsWith(URN_PREFIX)) {
125 // We'll assume it is a CSID and complain if it does not match
126 return new Specifier(SpecifierForm.CSID, specifierIn);
128 if(specifierIn.startsWith(URN_PREFIX_NAME, URN_PREFIX_LEN)) {
129 int closeParen = specifierIn.indexOf(')', URN_NAME_PREFIX_LEN);
131 return new Specifier(SpecifierForm.URN_NAME,
132 specifierIn.substring(URN_NAME_PREFIX_LEN, closeParen));
134 } else if(specifierIn.startsWith(URN_PREFIX_ID, URN_PREFIX_LEN)) {
135 int closeParen = specifierIn.indexOf(')', URN_ID_PREFIX_LEN);
137 return new Specifier(SpecifierForm.CSID,
138 specifierIn.substring(URN_ID_PREFIX_LEN, closeParen));
143 logger.error(method+": bad or missing specifier!");
144 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
145 op+" failed on bad or missing Authority specifier").type(
146 "text/plain").build();
147 throw new WebApplicationException(response);
151 * Instantiates a new Authority resource.
153 public AuthorityResource(Class<AuthCommon> authCommonClass, Class<?> resourceClass,
154 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
155 this.authCommonClass = authCommonClass;
156 this.resourceClass = resourceClass;
157 this.authorityCommonSchemaName = authorityCommonSchemaName;
158 this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
161 public abstract String getItemServiceName();
164 protected String getVersionString() {
165 return "$LastChangedRevision: 2617 $";
169 public Class<AuthCommon> getCommonPartClass() {
170 return authCommonClass;
174 * Creates the item document handler.
177 * @param inAuthority the in vocabulary
179 * @return the document handler
181 * @throws Exception the exception
183 protected DocumentHandler createItemDocumentHandler(
184 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
185 String inAuthority, String parentShortIdentifier)
187 String authorityRefNameBase;
188 AuthorityItemDocumentModelHandler<?> docHandler;
190 if(parentShortIdentifier==null) {
191 authorityRefNameBase = null;
193 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx =
194 createServiceContext(getServiceName());
195 if(parentShortIdentifier.equals(FETCH_SHORT_ID)) {
196 // Get from parent document
197 parentShortIdentifier = getAuthShortIdentifier(parentCtx, inAuthority);
199 authorityRefNameBase = buildAuthorityRefNameBase(parentCtx, parentShortIdentifier);
202 docHandler = (AuthorityItemDocumentModelHandler<?>)createDocumentHandler(ctx,
203 ctx.getCommonPartLabel(getItemServiceName()),
205 docHandler.setInAuthority(inAuthority);
206 docHandler.setAuthorityRefNameBase(authorityRefNameBase);
211 public String getAuthShortIdentifier(
212 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String authCSID)
213 throws DocumentNotFoundException, DocumentException {
214 String shortIdentifier = null;
216 DocumentWrapper<DocumentModel> wrapDoc = getRepositoryClient(ctx).getDocFromCsid(ctx, authCSID);
217 AuthorityDocumentModelHandler<?> handler =
218 (AuthorityDocumentModelHandler<?>)createDocumentHandler(ctx);
219 shortIdentifier = handler.getShortIdentifier(wrapDoc, authorityCommonSchemaName);
220 } catch (Exception e) {
221 if (logger.isDebugEnabled()) {
222 logger.debug("Caught exception ", e);
224 throw new DocumentException(e);
226 return shortIdentifier;
230 protected String buildAuthorityRefNameBase(
231 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String shortIdentifier) {
232 RefName.Authority authority = RefName.buildAuthority(ctx.getTenantName(),
233 ctx.getServiceName(), shortIdentifier, null);
234 return authority.toString();
237 public static class CsidAndShortIdentifier {
239 String shortIdentifier;
242 public String lookupParentCSID (String parentspecifier, String method, String op, MultivaluedMap<String, String> queryParams)
244 CsidAndShortIdentifier tempResult = lookupParentCSIDAndShortIdentifer(parentspecifier, method, op, queryParams);
245 return tempResult.CSID;
248 public CsidAndShortIdentifier lookupParentCSIDAndShortIdentifer (String parentspecifier, String method, String op, MultivaluedMap<String, String> queryParams)
250 CsidAndShortIdentifier result = new CsidAndShortIdentifier();
251 Specifier parentSpec = getSpecifier(parentspecifier, method, op);
252 // Note that we have to create the service context for the Items, not the main service
254 String parentShortIdentifier;
255 if(parentSpec.form==SpecifierForm.CSID) {
256 parentShortIdentifier = null;
257 parentcsid = parentSpec.value;
258 // Uncomment when app layer is ready to integrate
259 // parentShortIdentifier = FETCH_SHORT_ID;
261 parentShortIdentifier = parentSpec.value;
262 String whereClause = buildWhereForAuthByName(parentSpec.value);
263 ServiceContext ctx = createServiceContext(getServiceName(), queryParams);
264 parentcsid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause); //FIXME: REM - If the parent has been soft-deleted, should we be looking for the item?
266 result.CSID = parentcsid;
270 public String lookupItemCSID (String itemspecifier, String parentcsid, String method, String op, ServiceContext ctx)
271 throws DocumentException {
273 Specifier itemSpec = getSpecifier(itemspecifier, method, op);
274 if(itemSpec.form==SpecifierForm.CSID) {
275 itemcsid = itemSpec.value;
277 String itemWhereClause = buildWhereForAuthItemByName(itemSpec.value, parentcsid);
278 itemcsid = getRepositoryClient(ctx).findDocCSID(ctx, itemWhereClause); //FIXME: REM - Should we be looking for the 'wf_deleted' query param and filtering on it?
284 public Response createAuthority(String xmlPayload) {
286 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
287 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(input);
288 DocumentHandler handler = createDocumentHandler(ctx);
289 String csid = getRepositoryClient(ctx).create(ctx, handler);
290 UriBuilder path = UriBuilder.fromResource(resourceClass);
291 path.path("" + csid);
292 Response response = Response.created(path.build()).build();
294 } catch (Exception e) {
295 throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
299 protected String buildWhereForAuthByName(String name) {
300 return authorityCommonSchemaName+
301 ":"+AuthorityJAXBSchema.SHORT_IDENTIFIER+
305 protected String buildWhereForAuthItemByName(String name, String parentcsid) {
307 authorityItemCommonSchemaName+
308 ":"+AuthorityItemJAXBSchema.SHORT_IDENTIFIER+
310 + authorityItemCommonSchemaName + ":"
311 + AuthorityItemJAXBSchema.IN_AUTHORITY + "="
312 + "'" + parentcsid + "'";
316 * Gets the authority.
318 * @param specifier either a CSID or one of the urn forms
320 * @return the authority
325 public byte[] get( // getAuthority(
327 @PathParam("csid") String specifier) {
328 PoxPayloadOut result = null;
330 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
331 DocumentHandler handler = createDocumentHandler(ctx);
333 Specifier spec = getSpecifier(specifier, "getAuthority", "GET");
334 if(spec.form == SpecifierForm.CSID) {
335 if (logger.isDebugEnabled()) {
336 logger.debug("getAuthority with csid=" + spec.value);
338 getRepositoryClient(ctx).get(ctx, spec.value, handler);
340 String whereClause = buildWhereForAuthByName(spec.value);
341 DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
342 handler.setDocumentFilter(myFilter);
343 getRepositoryClient(ctx).get(ctx, handler);
345 result = ctx.getOutput();
347 } catch (Exception e) {
348 throw bigReThrow(e, ServiceMessages.GET_FAILED, specifier);
351 if (result == null) {
352 Response response = Response.status(Response.Status.NOT_FOUND).entity(
353 "Get failed, the requested Authority specifier:" + specifier + ": was not found.").type(
354 "text/plain").build();
355 throw new WebApplicationException(response);
358 return result.getBytes();
362 * Finds and populates the authority list.
366 * @return the authority list
369 @Produces("application/xml")
370 public AbstractCommonList getAuthorityList(@Context UriInfo ui) {
372 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
373 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(queryParams);
374 DocumentHandler handler = createDocumentHandler(ctx);
375 DocumentFilter myFilter = handler.getDocumentFilter();
376 String nameQ = queryParams.getFirst("refName");
378 myFilter.setWhereClause(authorityCommonSchemaName+":refName='" + nameQ + "'");
380 getRepositoryClient(ctx).getFiltered(ctx, handler);
381 return (AbstractCommonList) handler.getCommonPartList();
382 } catch (Exception e) {
383 throw bigReThrow(e, ServiceMessages.GET_FAILED);
390 * @param specifier the csid or id
392 * @return the multipart output
396 public byte[] updateAuthority(
397 @PathParam("csid") String specifier,
399 PoxPayloadOut result = null;
401 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
402 Specifier spec = getSpecifier(specifier, "updateAuthority", "UPDATE");
403 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(theUpdate);
404 DocumentHandler handler = createDocumentHandler(ctx);
406 if(spec.form==SpecifierForm.CSID) {
409 String whereClause = buildWhereForAuthByName(spec.value);
410 csid = getRepositoryClient(ctx).findDocCSID(ctx, whereClause);
412 getRepositoryClient(ctx).update(ctx, csid, handler);
413 result = ctx.getOutput();
414 } catch (Exception e) {
415 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED);
417 return result.getBytes();
423 * @param csid the csid
425 * @return the response
429 public Response deleteAuthority(@PathParam("csid") String csid) {
430 if (logger.isDebugEnabled()) {
431 logger.debug("deleteAuthority with csid=" + csid);
434 ensureCSID(csid, ServiceMessages.DELETE_FAILED, "Authority.csid");
435 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
436 getRepositoryClient(ctx).delete(ctx, csid);
437 return Response.status(HttpResponseCodes.SC_OK).build();
438 } catch (Exception e) {
439 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
443 /*************************************************************************
444 * Create an AuthorityItem - this is a sub-resource of Authority
445 * @param specifier either a CSID or one of the urn forms
446 * @return Authority item response
447 *************************************************************************/
449 @Path("{csid}/items")
450 public Response createAuthorityItem(@Context UriInfo ui, @PathParam("csid") String specifier, String xmlPayload) {
452 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
453 ServiceContext ctx = createServiceContext(getItemServiceName(), input);
454 ctx.setUriInfo(ui); //Laramie
456 // Note: must have the parentShortId, to do the create.
457 CsidAndShortIdentifier parent = lookupParentCSIDAndShortIdentifer(specifier,"createAuthorityItem", "CREATE_ITEM", null);
458 DocumentHandler handler = createItemDocumentHandler(ctx, parent.CSID, parent.shortIdentifier);
459 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
460 UriBuilder path = UriBuilder.fromResource(resourceClass);
461 path.path(parent.CSID + "/items/" + itemcsid);
462 Response response = Response.created(path.build()).build();
464 } catch (Exception e) {
465 throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
470 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH)
471 public byte[] getItemWorkflow(
472 @PathParam("csid") String csid,
473 @PathParam("itemcsid") String itemcsid) {
474 PoxPayloadOut result = null;
477 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getItemServiceName());
478 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
480 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
481 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
482 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
483 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
484 result = ctx.getOutput();
485 } catch (Exception e) {
486 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
488 return result.getBytes();
492 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH)
493 public byte[] updateWorkflow(
494 @PathParam("csid") String csid,
495 @PathParam("itemcsid") String itemcsid,
497 PoxPayloadOut result = null;
499 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getItemServiceName());
500 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
502 PoxPayloadIn workflowUpdate = new PoxPayloadIn(xmlPayload);
503 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, workflowUpdate);
504 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
505 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
506 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
507 result = ctx.getOutput();
508 } catch (Exception e) {
509 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
511 return result.getBytes();
516 * Gets the authority item.
518 * @param parentspecifier either a CSID or one of the urn forms
519 * @param itemspecifier either a CSID or one of the urn forms
521 * @return the authority item
524 @Path("{csid}/items/{itemcsid}")
525 public byte[] getAuthorityItem(
526 @Context Request request,
528 @PathParam("csid") String parentspecifier,
529 @PathParam("itemcsid") String itemspecifier) {
530 PoxPayloadOut result = null;
532 JaxRsContext jaxRsContext = new JaxRsContext(request, ui);
533 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
534 String parentcsid = lookupParentCSID(parentspecifier, "getAuthorityItem(parent)", "GET_ITEM", queryParams);
536 RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
537 ctx = (RemoteServiceContext)createServiceContext(getItemServiceName(), queryParams);
538 ctx.setJaxRsContext(jaxRsContext);
540 ctx.setUriInfo(ui); //ARG! must pass this or subsequent calls will not have a ui.
542 // We omit the parentShortId, only needed when doing a create...
543 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, null);
545 Specifier itemSpec = getSpecifier(itemspecifier, "getAuthorityItem(item)", "GET_ITEM");
546 if(itemSpec.form==SpecifierForm.CSID) {
547 getRepositoryClient(ctx).get(ctx, itemSpec.value, handler);
549 String itemWhereClause =
550 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
551 DocumentFilter myFilter = new DocumentFilter(itemWhereClause, 0, 1);
552 handler.setDocumentFilter(myFilter);
553 getRepositoryClient(ctx).get(ctx, handler);
555 // TODO should we assert that the item is in the passed vocab?
556 result = ctx.getOutput();
557 } catch (Exception e) {
558 throw bigReThrow(e, ServiceMessages.GET_FAILED);
560 if (result == null) {
561 Response response = Response.status(Response.Status.NOT_FOUND).entity(
562 "Get failed, the requested AuthorityItem specifier:" + itemspecifier + ": was not found.").type(
563 "text/plain").build();
564 throw new WebApplicationException(response);
566 return result.getBytes();
570 * Gets the authorityItem list for the specified authority
571 * If partialPerm is specified, keywords will be ignored.
573 * @param specifier either a CSID or one of the urn forms
574 * @param partialTerm if non-null, matches partial terms
575 * @param keywords if non-null, matches terms in the keyword index for items
576 * @param ui passed to include additional parameters, like pagination controls
578 * @return the authorityItem list
581 @Path("{csid}/items")
582 @Produces("application/xml")
583 public AbstractCommonList getAuthorityItemList(
584 @PathParam("csid") String specifier,
585 @QueryParam(IQueryManager.SEARCH_TYPE_PARTIALTERM) String partialTerm,
586 @QueryParam(IQueryManager.SEARCH_TYPE_KEYWORDS_KW) String keywords,
587 @Context UriInfo ui) {
589 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
590 // Note that docType defaults to the ServiceName, so we're fine with that.
591 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
593 String parentcsid = lookupParentCSID(specifier, "getAuthorityItemList", "LIST", queryParams);
595 ctx = createServiceContext(getItemServiceName(), queryParams);
596 // We omit the parentShortId, only needed when doing a create...
597 DocumentHandler handler = createItemDocumentHandler(ctx,
599 DocumentFilter myFilter = handler.getDocumentFilter();
600 myFilter.appendWhereClause(authorityItemCommonSchemaName + ":" +
601 AuthorityItemJAXBSchema.IN_AUTHORITY + "=" +
602 "'" + parentcsid + "'",
603 IQueryManager.SEARCH_QUALIFIER_AND);
605 // AND vocabularyitems_common:displayName LIKE '%partialTerm%'
606 if (partialTerm != null && !partialTerm.isEmpty()) {
607 String ptClause = QueryManager.createWhereClauseForPartialMatch(
608 authorityItemCommonSchemaName + ":"
609 + AuthorityItemJAXBSchema.DISPLAY_NAME, partialTerm );
610 myFilter.appendWhereClause(ptClause, IQueryManager.SEARCH_QUALIFIER_AND);
611 } else if (keywords != null) {
612 String kwdClause = QueryManager.createWhereClauseFromKeywords(keywords);
613 myFilter.appendWhereClause(kwdClause, IQueryManager.SEARCH_QUALIFIER_AND);
615 if (logger.isDebugEnabled()) {
616 logger.debug("getAuthorityItemList filtered WHERE clause: "
617 + myFilter.getWhereClause());
619 getRepositoryClient(ctx).getFiltered(ctx, handler);
620 return (AbstractCommonList) handler.getCommonPartList();
621 } catch (Exception e) {
622 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
627 * Gets the entities referencing this Authority item instance. The service type
628 * can be passed as a query param "type", and must match a configured type
629 * for the service bindings. If not set, the type defaults to
630 * ServiceBindingUtils.SERVICE_TYPE_PROCEDURE.
632 * @param parentspecifier either a CSID or one of the urn forms
633 * @param itemspecifier either a CSID or one of the urn forms
636 * @return the info for the referencing objects
639 @Path("{csid}/items/{itemcsid}/refObjs")
640 @Produces("application/xml")
641 public AuthorityRefDocList getReferencingObjects(
642 @PathParam("csid") String parentspecifier,
643 @PathParam("itemcsid") String itemspecifier,
644 @Context UriInfo ui) {
645 AuthorityRefDocList authRefDocList = null;
647 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
649 String parentcsid = lookupParentCSID(parentspecifier, "getReferencingObjects(parent)", "GET_ITEM_REF_OBJS", queryParams);
651 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), queryParams);
652 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getReferencingObjects(item)", "GET_ITEM_REF_OBJS", ctx);
654 // Note that we have to create the service context for the Items, not the main service
655 // We omit the parentShortId, only needed when doing a create...
656 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, null);
657 RepositoryClient repoClient = getRepositoryClient(ctx);
658 DocumentFilter myFilter = handler.getDocumentFilter();
659 String serviceType = ServiceBindingUtils.SERVICE_TYPE_PROCEDURE;
660 List<String> list = queryParams.remove(ServiceBindingUtils.SERVICE_TYPE_PROP);
662 serviceType = list.get(0);
664 DocumentWrapper<DocumentModel> docWrapper = repoClient.getDoc(ctx, itemcsid);
665 DocumentModel docModel = docWrapper.getWrappedObject();
666 String refName = (String)docModel.getPropertyValue(AuthorityItemJAXBSchema.REF_NAME);
668 authRefDocList = RefNameServiceUtils.getAuthorityRefDocs(ctx,
672 myFilter.getPageSize(), myFilter.getStartPage(), true /*computeTotal*/ );
673 } catch (Exception e) {
674 throw bigReThrow(e, ServiceMessages.GET_FAILED);
676 if (authRefDocList == null) {
677 Response response = Response.status(Response.Status.NOT_FOUND).entity(
678 "Get failed, the requested Item CSID:" + itemspecifier + ": was not found.").type(
679 "text/plain").build();
680 throw new WebApplicationException(response);
682 return authRefDocList;
686 * Gets the authority terms used in the indicated Authority item.
688 * @param parentspecifier either a CSID or one of the urn forms
689 * @param itemspecifier either a CSID or one of the urn forms
690 * @param ui passed to include additional parameters, like pagination controls
692 * @return the authority refs for the Authority item.
695 @Path("{csid}/items/{itemcsid}/authorityrefs")
696 @Produces("application/xml")
697 public AuthorityRefList getAuthorityItemAuthorityRefs(
698 @PathParam("csid") String parentspecifier,
699 @PathParam("itemcsid") String itemspecifier,
700 @Context UriInfo ui) {
701 AuthorityRefList authRefList = null;
703 // Note that we have to create the service context for the Items, not the main service
704 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
705 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
707 String parentcsid = lookupParentCSID(parentspecifier, "getAuthorityItemAuthRefs(parent)", "GET_ITEM_AUTH_REFS", queryParams);
709 ctx = createServiceContext(getItemServiceName(), queryParams);
710 // We omit the parentShortId, only needed when doing a create...
711 RemoteDocumentModelHandlerImpl handler =
712 (RemoteDocumentModelHandlerImpl) createItemDocumentHandler(ctx, parentcsid, null);
714 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getAuthorityItemAuthRefs(item)", "GET_ITEM_AUTH_REFS", ctx);
716 DocumentWrapper<DocumentModel> docWrapper = getRepositoryClient(ctx).getDoc(ctx, itemcsid);
717 List<String> authRefFields =
718 ((MultipartServiceContextImpl)ctx).getCommonPartPropertyValues(
719 ServiceBindingUtils.AUTH_REF_PROP, ServiceBindingUtils.QUALIFIED_PROP_NAMES);
720 authRefList = handler.getAuthorityRefs(docWrapper, authRefFields);
721 } catch (Exception e) {
722 throw bigReThrow(e, ServiceMessages.GET_FAILED + " parentspecifier: "+parentspecifier + " itemspecifier:" +itemspecifier);
727 * Update authorityItem.
729 * @param parentspecifier either a CSID or one of the urn forms
730 * @param itemspecifier either a CSID or one of the urn forms
732 * @return the multipart output
735 @Path("{csid}/items/{itemcsid}")
736 public byte[] updateAuthorityItem(
738 @PathParam("csid") String parentspecifier,
739 @PathParam("itemcsid") String itemspecifier,
741 PoxPayloadOut result = null;
743 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
744 // Note that we have to create the service context for the Items, not the main service
745 //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
746 String parentcsid = lookupParentCSID(parentspecifier, "updateAuthorityItem(parent)", "UPDATE_ITEM", null);
748 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), theUpdate);
749 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "updateAuthorityItem(item)", "UPDATE_ITEM", ctx);
751 // We omit the parentShortId, only needed when doing a create...
752 DocumentHandler handler = createItemDocumentHandler(ctx, parentcsid, null);
754 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
755 result = ctx.getOutput();
757 } catch (Exception e) {
758 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED);
760 return result.getBytes();
764 * Delete authorityItem.
766 * @param parentcsid the parentcsid
767 * @param itemcsid the itemcsid
769 * @return the response
772 @Path("{csid}/items/{itemcsid}")
773 public Response deleteAuthorityItem(
774 @PathParam("csid") String parentcsid,
775 @PathParam("itemcsid") String itemcsid) {
777 if (logger.isDebugEnabled()) {
778 logger.debug("deleteAuthorityItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
781 ensureCSID(parentcsid, ServiceMessages.DELETE_FAILED, "AuthorityItem.parentcsid");
782 ensureCSID(itemcsid, ServiceMessages.DELETE_FAILED, "AuthorityItem.itemcsid");
783 //Laramie, removing this catch, since it will surely fail below, since itemcsid or parentcsid will be null.
784 // }catch (Throwable t){
785 // System.out.println("ERROR in setting up DELETE: "+t);
788 // Note that we have to create the service context for the Items, not the main service
789 ServiceContext ctx = createServiceContext(getItemServiceName());
790 getRepositoryClient(ctx).delete(ctx, itemcsid);
791 return Response.status(HttpResponseCodes.SC_OK).build();
792 } catch (Exception e) {
793 throw bigReThrow(e, ServiceMessages.DELETE_FAILED + " itemcsid: " + itemcsid+ " parentcsid:" + parentcsid);
797 public final static String hierarchy = "hierarchy";
799 @Path("{csid}/items/{itemcsid}/"+hierarchy)
800 @Produces("application/xml")
801 public String getHierarchy(@PathParam("csid") String csid,
802 @PathParam("itemcsid") String itemcsid,
803 @Context UriInfo ui) throws Exception {
805 // 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...?
806 String calledUri = ui.getPath();
807 String uri = "/"+calledUri.substring(0, (calledUri.length()-("/"+hierarchy).length()));
808 ServiceContext ctx = createServiceContext(getItemServiceName());
810 String direction = ui.getQueryParameters().getFirst(Hierarchy.directionQP);
811 if (Tools.notBlank(direction) && Hierarchy.direction_parents.equals(direction)){
812 return Hierarchy.surface(ctx, itemcsid, uri);
814 return Hierarchy.dive(ctx, itemcsid, uri);
816 } catch (Exception e){
817 throw bigReThrow(e, "Error showing hierarchy", itemcsid);