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.ResourceBase;
32 import org.collectionspace.services.common.ResourceMap;
33 import org.collectionspace.services.common.ServiceMain;
34 import org.collectionspace.services.common.ServiceMessages;
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.RemoteServiceContext;
42 import org.collectionspace.services.common.context.ServiceBindingUtils;
43 import org.collectionspace.services.common.context.ServiceContext;
44 import org.collectionspace.services.common.document.DocumentException;
45 import org.collectionspace.services.common.document.DocumentFilter;
46 import org.collectionspace.services.common.document.DocumentHandler;
47 import org.collectionspace.services.common.document.DocumentNotFoundException;
48 import org.collectionspace.services.common.document.DocumentWrapper;
49 import org.collectionspace.services.common.document.Hierarchy;
50 import org.collectionspace.services.common.query.QueryManager;
51 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
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.config.ClientType;
56 import org.collectionspace.services.jaxb.AbstractCommonList;
57 import org.collectionspace.services.lifecycle.TransitionDef;
58 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
59 import org.collectionspace.services.nuxeo.client.java.RepositoryJavaClientImpl;
60 import org.collectionspace.services.workflow.WorkflowCommon;
61 import org.jboss.resteasy.util.HttpResponseCodes;
62 import org.nuxeo.ecm.core.api.DocumentModel;
63 import org.nuxeo.ecm.core.api.DocumentModelList;
64 import org.nuxeo.ecm.core.api.repository.RepositoryInstance;
65 import org.slf4j.Logger;
66 import org.slf4j.LoggerFactory;
68 import javax.ws.rs.Consumes;
69 import javax.ws.rs.DELETE;
70 import javax.ws.rs.GET;
71 import javax.ws.rs.POST;
72 import javax.ws.rs.PUT;
73 import javax.ws.rs.Path;
74 import javax.ws.rs.PathParam;
75 import javax.ws.rs.Produces;
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.List;
85 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
88 * The Class AuthorityResource.
94 * @param <AuthItemHandler>
100 * @param <AuthItemHandler>
102 @Consumes("application/xml")
103 @Produces("application/xml")
104 public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
105 extends ResourceBase {
107 final static String SEARCH_TYPE_TERMSTATUS = "ts";
109 protected Class<AuthCommon> authCommonClass;
110 protected Class<?> resourceClass;
111 protected String authorityCommonSchemaName;
112 protected String authorityItemCommonSchemaName;
113 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType(); //FIXME: REM - 3 Why is this field needed? I see no references to it.
114 final static String URN_PREFIX = "urn:cspace:";
115 final static int URN_PREFIX_LEN = URN_PREFIX.length();
116 final static String URN_PREFIX_NAME = "name(";
117 final static int URN_NAME_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_NAME.length();
118 final static String URN_PREFIX_ID = "id(";
119 final static int URN_ID_PREFIX_LEN = URN_PREFIX_LEN + URN_PREFIX_ID.length();
120 final static String FETCH_SHORT_ID = "_fetch_";
121 final static String PARENT_WILDCARD = "_ALL_";
123 final Logger logger = LoggerFactory.getLogger(AuthorityResource.class);
125 public enum SpecifierForm {
130 public class Specifier {
132 public SpecifierForm form;
135 Specifier(SpecifierForm form, String value) {
141 protected Specifier getSpecifier(String specifierIn, String method, String op) throws WebApplicationException {
142 if (logger.isDebugEnabled()) {
143 logger.debug("getSpecifier called by: " + method + " with specifier: " + specifierIn);
145 if (specifierIn != null) {
146 if (!specifierIn.startsWith(URN_PREFIX)) {
147 // We'll assume it is a CSID and complain if it does not match
148 return new Specifier(SpecifierForm.CSID, specifierIn);
150 if (specifierIn.startsWith(URN_PREFIX_NAME, URN_PREFIX_LEN)) {
151 int closeParen = specifierIn.indexOf(')', URN_NAME_PREFIX_LEN);
152 if (closeParen >= 0) {
153 return new Specifier(SpecifierForm.URN_NAME,
154 specifierIn.substring(URN_NAME_PREFIX_LEN, closeParen));
156 } else if (specifierIn.startsWith(URN_PREFIX_ID, URN_PREFIX_LEN)) {
157 int closeParen = specifierIn.indexOf(')', URN_ID_PREFIX_LEN);
158 if (closeParen >= 0) {
159 return new Specifier(SpecifierForm.CSID,
160 specifierIn.substring(URN_ID_PREFIX_LEN, closeParen));
165 logger.error(method + ": bad or missing specifier!");
166 Response response = Response.status(Response.Status.BAD_REQUEST).entity(
167 op + " failed on bad or missing Authority specifier").type(
168 "text/plain").build();
169 throw new WebApplicationException(response);
173 * Instantiates a new Authority resource.
175 public AuthorityResource(Class<AuthCommon> authCommonClass, Class<?> resourceClass,
176 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
177 this.authCommonClass = authCommonClass;
178 this.resourceClass = resourceClass;
179 this.authorityCommonSchemaName = authorityCommonSchemaName;
180 this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
183 public abstract String getItemServiceName();
185 public abstract String getItemTermInfoGroupXPathBase();
188 protected String getVersionString() {
189 return "$LastChangedRevision: 2617 $";
193 public Class<AuthCommon> getCommonPartClass() {
194 return authCommonClass;
198 * Creates the item document handler.
201 * @param inAuthority the in vocabulary
203 * @return the document handler
205 * @throws Exception the exception
207 protected DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> createItemDocumentHandler(
208 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
209 String inAuthority, String parentShortIdentifier)
211 String authorityRefNameBase;
212 AuthorityItemDocumentModelHandler<?> docHandler;
214 if (parentShortIdentifier == null) {
215 authorityRefNameBase = null;
217 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx =
218 createServiceContext(getServiceName());
219 if (parentShortIdentifier.equals(FETCH_SHORT_ID)) {
220 // Get from parent document
221 parentShortIdentifier = getAuthShortIdentifier(parentCtx, inAuthority);
223 authorityRefNameBase = buildAuthorityRefNameBase(parentCtx, parentShortIdentifier);
226 docHandler = (AuthorityItemDocumentModelHandler<?>) createDocumentHandler(ctx,
227 ctx.getCommonPartLabel(getItemServiceName()),
229 // FIXME - Richard and Aron think the following three lines should
230 // be in the constructor for the AuthorityItemDocumentModelHandler
231 // because all three are required fields.
232 docHandler.setInAuthority(inAuthority);
233 docHandler.setAuthorityRefNameBase(authorityRefNameBase);
234 docHandler.setItemTermInfoGroupXPathBase(getItemTermInfoGroupXPathBase());
238 public String getAuthShortIdentifier(
239 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String authCSID)
240 throws DocumentNotFoundException, DocumentException {
241 String shortIdentifier = null;
243 AuthorityDocumentModelHandler<?> handler =
244 (AuthorityDocumentModelHandler<?>) createDocumentHandler(ctx);
245 shortIdentifier = handler.getShortIdentifier(authCSID, authorityCommonSchemaName);
246 } catch (Exception e) {
247 if (logger.isDebugEnabled()) {
248 logger.debug("Caught exception ", e);
250 throw new DocumentException(e);
252 return shortIdentifier;
255 protected String buildAuthorityRefNameBase(
256 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String shortIdentifier) {
257 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
258 ctx.getServiceName(), shortIdentifier, null);
259 return authority.toString();
262 public static class CsidAndShortIdentifier {
265 String shortIdentifier;
268 protected String lookupParentCSID(String parentspecifier, String method, String op, MultivaluedMap<String, String> queryParams)
270 CsidAndShortIdentifier tempResult = lookupParentCSIDAndShortIdentifer(parentspecifier, method, op, queryParams);
271 return tempResult.CSID;
274 private CsidAndShortIdentifier lookupParentCSIDAndShortIdentifer(String parentspecifier, String method, String op, MultivaluedMap<String, String> queryParams)
276 CsidAndShortIdentifier result = new CsidAndShortIdentifier();
277 Specifier parentSpec = getSpecifier(parentspecifier, method, op);
278 // Note that we have to create the service context for the Items, not the main service
280 String parentShortIdentifier;
281 if (parentSpec.form == SpecifierForm.CSID) {
282 parentShortIdentifier = null;
283 parentcsid = parentSpec.value;
284 // Uncomment when app layer is ready to integrate
285 // Uncommented since refNames are currently only generated if not present - ADR CSPACE-3178
286 parentShortIdentifier = FETCH_SHORT_ID;
288 parentShortIdentifier = parentSpec.value;
289 String whereClause = buildWhereForAuthByName(parentSpec.value);
290 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getServiceName(), queryParams);
291 parentcsid = getRepositoryClient(ctx).findDocCSID(null, ctx, whereClause); //FIXME: REM - If the parent has been soft-deleted, should we be looking for the item?
293 result.CSID = parentcsid;
294 result.shortIdentifier = parentShortIdentifier;
298 public String lookupItemCSID(String itemspecifier, String parentcsid, String method, String op, ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
299 throws DocumentException {
301 Specifier itemSpec = getSpecifier(itemspecifier, method, op);
302 if (itemSpec.form == SpecifierForm.CSID) {
303 itemcsid = itemSpec.value;
305 String itemWhereClause = buildWhereForAuthItemByName(itemSpec.value, parentcsid);
306 itemcsid = getRepositoryClient(ctx).findDocCSID(null, ctx, itemWhereClause); //FIXME: REM - Should we be looking for the 'wf_deleted' query param and filtering on it?
312 * Generally, callers will first call RefName.AuthorityItem.parse with a refName, and then
313 * use the returned item.inAuthority.resource and a resourceMap to get a service-specific
314 * Resource. They then call this method on that resource.
317 public DocumentModel getDocModelForAuthorityItem(RepositoryInstance repoSession, RefName.AuthorityItem item)
318 throws Exception, DocumentNotFoundException {
322 String whereClause = buildWhereForAuthByName(item.getParentShortIdentifier());
323 // Ensure we have the right context.
324 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(item.inAuthority.resource);
326 // HACK - this really must be moved to the doc handler, not here. No Nuxeo specific stuff here!
327 RepositoryJavaClientImpl client = (RepositoryJavaClientImpl)getRepositoryClient(ctx);
328 String parentcsid = client.findDocCSID(repoSession, ctx, whereClause);
330 String itemWhereClause = buildWhereForAuthItemByName(item.getShortIdentifier(), parentcsid);
331 ctx = createServiceContext(getItemServiceName());
332 DocumentWrapper<DocumentModel> docWrapper = client.findDoc(repoSession, ctx, itemWhereClause);
333 DocumentModel docModel = docWrapper.getWrappedObject();
338 @POST //FIXME: REM - 5/1/2012 - We can probably remove this method.
339 public Response createAuthority(String xmlPayload) { //REM - This method is never reached by the JAX-RS client -instead the "create" method in ResourceBase.java is getting called.
341 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
342 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(input);
343 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
344 String csid = getRepositoryClient(ctx).create(ctx, handler);
345 UriBuilder path = UriBuilder.fromResource(resourceClass);
346 path.path("" + csid);
347 Response response = Response.created(path.build()).build();
349 } catch (Exception e) {
350 throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
354 protected String buildWhereForAuthByName(String name) {
355 return authorityCommonSchemaName
356 + ":" + AuthorityJAXBSchema.SHORT_IDENTIFIER
360 protected String buildWhereForAuthItemByName(String name, String parentcsid) {
361 return authorityItemCommonSchemaName
362 + ":" + AuthorityItemJAXBSchema.SHORT_IDENTIFIER
363 + "='" + name + "' AND "
364 + authorityItemCommonSchemaName + ":"
365 + AuthorityItemJAXBSchema.IN_AUTHORITY + "="
366 + "'" + parentcsid + "'";
370 * Gets the authority.
372 * @param specifier either a CSID or one of the urn forms
374 * @return the authority
380 @Context Request request,
382 @PathParam("csid") String specifier) {
383 PoxPayloadOut result = null;
385 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
386 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
388 Specifier spec = getSpecifier(specifier, "getAuthority", "GET");
389 if (spec.form == SpecifierForm.CSID) {
390 if (logger.isDebugEnabled()) {
391 logger.debug("getAuthority with csid=" + spec.value);
393 getRepositoryClient(ctx).get(ctx, spec.value, handler);
395 String whereClause = buildWhereForAuthByName(spec.value);
396 DocumentFilter myFilter = new DocumentFilter(whereClause, 0, 1);
397 handler.setDocumentFilter(myFilter);
398 getRepositoryClient(ctx).get(ctx, handler);
400 result = ctx.getOutput();
402 } catch (Exception e) {
403 throw bigReThrow(e, ServiceMessages.GET_FAILED, specifier);
406 if (result == null) {
407 Response response = Response.status(Response.Status.NOT_FOUND).entity(
408 "Get failed, the requested Authority specifier:" + specifier + ": was not found.").type(
409 "text/plain").build();
410 throw new WebApplicationException(response);
413 return result.getBytes();
417 * Finds and populates the authority list.
421 * @return the authority list
424 @Produces("application/xml")
425 public AbstractCommonList getAuthorityList(@Context UriInfo ui) { //FIXME - REM 5/3/2012 - This is not reachable from the JAX-RS dispatcher. Instead the equivalent method in ResourceBase is getting called.
427 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
428 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
429 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
430 DocumentFilter myFilter = handler.getDocumentFilter();
431 // Need to make the default sort order for authority items
432 // be on the displayName field
433 String sortBy = queryParams.getFirst(IClientQueryParams.ORDER_BY_PARAM);
434 if (sortBy == null || sortBy.isEmpty()) {
435 String qualifiedDisplayNameField = authorityCommonSchemaName + ":"
436 + AuthorityItemJAXBSchema.DISPLAY_NAME;
437 myFilter.setOrderByClause(qualifiedDisplayNameField);
439 String nameQ = queryParams.getFirst("refName");
441 myFilter.setWhereClause(authorityCommonSchemaName + ":refName='" + nameQ + "'");
443 getRepositoryClient(ctx).getFiltered(ctx, handler);
444 return handler.getCommonPartList();
445 } catch (Exception e) {
446 throw bigReThrow(e, ServiceMessages.GET_FAILED);
454 * @param specifier the csid or id
456 * @return the multipart output
460 public byte[] updateAuthority(
461 @PathParam("csid") String specifier,
463 PoxPayloadOut result = null;
465 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
466 Specifier spec = getSpecifier(specifier, "updateAuthority", "UPDATE");
467 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(theUpdate);
468 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
470 if (spec.form == SpecifierForm.CSID) {
473 String whereClause = buildWhereForAuthByName(spec.value);
474 csid = getRepositoryClient(ctx).findDocCSID(null, ctx, whereClause);
476 getRepositoryClient(ctx).update(ctx, csid, handler);
477 result = ctx.getOutput();
478 } catch (Exception e) {
479 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED);
481 return result.getBytes();
487 * @param csid the csid
489 * @return the response
493 public Response deleteAuthority(@PathParam("csid") String csid) {
494 if (logger.isDebugEnabled()) {
495 logger.debug("deleteAuthority with csid=" + csid);
498 ensureCSID(csid, ServiceMessages.DELETE_FAILED, "Authority.csid");
499 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
500 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
501 getRepositoryClient(ctx).delete(ctx, csid, handler);
502 return Response.status(HttpResponseCodes.SC_OK).build();
503 } catch (Exception e) {
504 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
508 /*************************************************************************
509 * Create an AuthorityItem - this is a sub-resource of Authority
510 * @param specifier either a CSID or one of the urn forms
511 * @return Authority item response
512 *************************************************************************/
514 @Path("{csid}/items")
515 public Response createAuthorityItem(@Context ResourceMap resourceMap, @Context UriInfo ui,
516 @PathParam("csid") String specifier, String xmlPayload) {
518 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
519 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), input);
520 ctx.setResourceMap(resourceMap);
523 // Note: must have the parentShortId, to do the create.
524 CsidAndShortIdentifier parent = lookupParentCSIDAndShortIdentifer(specifier, "createAuthorityItem", "CREATE_ITEM", null);
525 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createItemDocumentHandler(ctx, parent.CSID, parent.shortIdentifier);
526 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
527 UriBuilder path = UriBuilder.fromResource(resourceClass);
528 path.path(parent.CSID + "/items/" + itemcsid);
529 Response response = Response.created(path.build()).build();
531 } catch (Exception e) {
532 throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
537 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH)
538 public byte[] getItemWorkflow(
539 @PathParam("csid") String csid,
540 @PathParam("itemcsid") String itemcsid) {
541 PoxPayloadOut result = null;
544 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getItemServiceName());
545 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
547 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
548 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
549 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
550 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
551 result = ctx.getOutput();
552 } catch (Exception e) {
553 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
555 return result.getBytes();
558 //FIXME: This method is almost identical to the method org.collectionspace.services.common.updateWorkflowWithTransition() so
559 // they should be consolidated -be DRY (don't repeat yourself).
561 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH + "/{transition}")
562 public byte[] updateItemWorkflowWithTransition(
563 @PathParam("csid") String csid,
564 @PathParam("itemcsid") String itemcsid,
565 @PathParam("transition") String transition) {
566 PoxPayloadOut result = null;
570 // Create an empty workflow_commons input part and set it into a new "workflow" sub-resource context
571 PoxPayloadIn input = new PoxPayloadIn(WorkflowClient.SERVICE_PAYLOAD_NAME, new WorkflowCommon(),
572 WorkflowClient.SERVICE_COMMONPART_NAME);
573 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, input);
575 // Create a service context and document handler for the parent resource.
576 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getItemServiceName());
577 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> parentDocHandler = this.createDocumentHandler(parentCtx);
578 ctx.setProperty(WorkflowClient.PARENT_DOCHANDLER, parentDocHandler); //added as a context param for the workflow document handler -it will call the parent's dochandler "prepareForWorkflowTranstion" method
580 // When looking for the document, we need to use the parent's workspace name -not the "workflow" workspace name
581 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
582 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
584 // Get the type of transition we're being asked to make and store it as a context parameter -used by the workflow document handler
585 TransitionDef transitionDef = getTransitionDef(parentCtx, transition);
586 ctx.setProperty(WorkflowClient.TRANSITION_ID, transitionDef);
588 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
589 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
590 result = ctx.getOutput();
591 } catch (Exception e) {
592 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
595 return result.getBytes();
599 * Gets the authority item.
601 * @param parentspecifier either a CSID or one of the urn forms
602 * @param itemspecifier either a CSID or one of the urn forms
604 * @return the authority item
607 @Path("{csid}/items/{itemcsid}")
608 public byte[] getAuthorityItem(
609 @Context Request request,
611 @Context ResourceMap resourceMap,
612 @PathParam("csid") String parentspecifier,
613 @PathParam("itemcsid") String itemspecifier) {
614 PoxPayloadOut result = null;
616 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
617 String parentcsid = lookupParentCSID(parentspecifier, "getAuthorityItem(parent)", "GET_ITEM", queryParams);
619 RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
620 ctx = (RemoteServiceContext<PoxPayloadIn, PoxPayloadOut>) createServiceContext(getItemServiceName(), resourceMap, ui);
622 JaxRsContext jaxRsContext = new JaxRsContext(request, ui); // REM - Why are we setting this? Who is using the getter?
623 ctx.setJaxRsContext(jaxRsContext);
625 // We omit the parentShortId, only needed when doing a create...
626 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createItemDocumentHandler(ctx, parentcsid, null);
628 Specifier itemSpec = getSpecifier(itemspecifier, "getAuthorityItem(item)", "GET_ITEM");
629 if (itemSpec.form == SpecifierForm.CSID) {
630 getRepositoryClient(ctx).get(ctx, itemSpec.value, handler);
632 String itemWhereClause =
633 buildWhereForAuthItemByName(itemSpec.value, parentcsid);
634 DocumentFilter myFilter = new DocumentFilter(itemWhereClause, 0, 1);
635 handler.setDocumentFilter(myFilter);
636 getRepositoryClient(ctx).get(ctx, handler);
638 // TODO should we assert that the item is in the passed vocab?
639 result = ctx.getOutput();
640 } catch (Exception e) {
641 throw bigReThrow(e, ServiceMessages.GET_FAILED);
643 if (result == null) {
644 Response response = Response.status(Response.Status.NOT_FOUND).entity(
645 "Get failed, the requested AuthorityItem specifier:" + itemspecifier + ": was not found.").type(
646 "text/plain").build();
647 throw new WebApplicationException(response);
649 return result.getBytes();
653 * Most of the authority child classes will/should use this implementation. However, the Vocabulary service's item schema is
654 * different enough that it will have to override this method in it's resource class.
657 protected String getOrderByField(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
658 String result = null;
660 result = NuxeoUtils.getPrimaryElPathPropertyName(
661 authorityItemCommonSchemaName, getItemTermInfoGroupXPathBase(),
662 AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
668 protected String getPartialTermMatchField(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
669 String result = null;
671 result = NuxeoUtils.getMultiElPathPropertyName(
672 authorityItemCommonSchemaName, getItemTermInfoGroupXPathBase(),
673 AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
679 * Gets the authorityItem list for the specified authority
680 * If partialPerm is specified, keywords will be ignored.
682 * @param specifier either a CSID or one of the urn forms
683 * @param partialTerm if non-null, matches partial terms
684 * @param keywords if non-null, matches terms in the keyword index for items
685 * @param ui passed to include additional parameters, like pagination controls
687 * @return the authorityItem list
690 @Path("{csid}/items")
691 @Produces("application/xml")
692 public AbstractCommonList getAuthorityItemList(@PathParam("csid") String specifier,
693 @Context UriInfo ui) {
694 AbstractCommonList result = null;
697 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
698 String orderBy = queryParams.getFirst(IClientQueryParams.ORDER_BY_PARAM);
699 String termStatus = queryParams.getFirst(SEARCH_TYPE_TERMSTATUS);
700 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
701 String advancedSearch = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_AS);
702 String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM);
704 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), queryParams);
705 // For the wildcard case, parentcsid is null, but docHandler will deal with this.
706 // We omit the parentShortId, only needed when doing a create...
707 String parentcsid = PARENT_WILDCARD.equals(specifier) ? null :
708 lookupParentCSID(specifier, "getAuthorityItemList", "LIST", queryParams);
709 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler =
710 createItemDocumentHandler(ctx, parentcsid, null);
712 DocumentFilter myFilter = handler.getDocumentFilter();
713 // If we are not wildcarding the parent, add a restriction
714 if (parentcsid != null) {
715 myFilter.appendWhereClause(authorityItemCommonSchemaName + ":"
716 + AuthorityItemJAXBSchema.IN_AUTHORITY + "="
717 + "'" + parentcsid + "'",
718 IQueryManager.SEARCH_QUALIFIER_AND);
721 if (Tools.notBlank(termStatus)) {
722 // Start with the qualified termStatus field
723 String qualifiedTermStatusField = authorityItemCommonSchemaName + ":"
724 + AuthorityItemJAXBSchema.TERM_STATUS;
725 String[] filterTerms = termStatus.trim().split("\\|");
726 String tsClause = QueryManager.createWhereClauseToFilterFromStringList(qualifiedTermStatusField, filterTerms, IQueryManager.FILTER_EXCLUDE);
727 myFilter.appendWhereClause(tsClause, IQueryManager.SEARCH_QUALIFIER_AND);
730 result = search(ctx, handler, queryParams, orderBy, keywords, advancedSearch, partialTerm);
731 } catch (Exception e) {
732 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
739 * @return the name of the property used to specify references for items in this type of
740 * authority. For most authorities, it is ServiceBindingUtils.AUTH_REF_PROP ("authRef").
741 * Some types (like Vocabulary) use a separate property.
743 protected String getRefPropName() {
744 return ServiceBindingUtils.AUTH_REF_PROP;
748 * Gets the entities referencing this Authority item instance. The service type
749 * can be passed as a query param "type", and must match a configured type
750 * for the service bindings. If not set, the type defaults to
751 * ServiceBindingUtils.SERVICE_TYPE_PROCEDURE.
753 * @param parentspecifier either a CSID or one of the urn forms
754 * @param itemspecifier either a CSID or one of the urn forms
757 * @return the info for the referencing objects
760 @Path("{csid}/items/{itemcsid}/refObjs")
761 @Produces("application/xml")
762 public AuthorityRefDocList getReferencingObjects(
763 @PathParam("csid") String parentspecifier,
764 @PathParam("itemcsid") String itemspecifier,
765 @Context UriInfo ui) {
766 AuthorityRefDocList authRefDocList = null;
768 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
770 String parentcsid = lookupParentCSID(parentspecifier, "getReferencingObjects(parent)", "GET_ITEM_REF_OBJS", queryParams);
772 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), queryParams);
773 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getReferencingObjects(item)", "GET_ITEM_REF_OBJS", ctx);
775 List<String> serviceTypes = queryParams.remove(ServiceBindingUtils.SERVICE_TYPE_PROP);
776 if(serviceTypes == null || serviceTypes.isEmpty()) {
777 serviceTypes = ServiceBindingUtils.getCommonServiceTypes(true); //CSPACE-5359: Should now include objects, procedures, and authorities
780 // Note that we have to create the service context for the Items, not the main service
781 // We omit the parentShortId, only needed when doing a create...
782 AuthorityItemDocumentModelHandler<?> handler = (AuthorityItemDocumentModelHandler<?>)
783 createItemDocumentHandler(ctx, parentcsid, null);
785 authRefDocList = handler.getReferencingObjects(ctx, serviceTypes, getRefPropName(), itemcsid);
786 } catch (Exception e) {
787 throw bigReThrow(e, ServiceMessages.GET_FAILED);
789 if (authRefDocList == null) {
790 Response response = Response.status(Response.Status.NOT_FOUND).entity(
791 "Get failed, the requested Item CSID:" + itemspecifier + ": was not found.").type(
792 "text/plain").build();
793 throw new WebApplicationException(response);
795 return authRefDocList;
799 * Gets the authority terms used in the indicated Authority item.
801 * @param parentspecifier either a CSID or one of the urn forms
802 * @param itemspecifier either a CSID or one of the urn forms
803 * @param ui passed to include additional parameters, like pagination controls
805 * @return the authority refs for the Authority item.
808 @Path("{csid}/items/{itemcsid}/authorityrefs")
809 @Produces("application/xml")
810 public AuthorityRefList getAuthorityItemAuthorityRefs(
811 @PathParam("csid") String parentspecifier,
812 @PathParam("itemcsid") String itemspecifier,
813 @Context UriInfo ui) {
814 AuthorityRefList authRefList = null;
816 // Note that we have to create the service context for the Items, not the main service
817 MultivaluedMap<String, String> queryParams = ui.getQueryParameters();
818 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
820 String parentcsid = lookupParentCSID(parentspecifier, "getAuthorityItemAuthRefs(parent)", "GET_ITEM_AUTH_REFS", queryParams);
822 ctx = createServiceContext(getItemServiceName(), queryParams);
823 // We omit the parentShortId, only needed when doing a create...
824 DocumentModelHandler<?, AbstractCommonList> handler =
825 (DocumentModelHandler<?, AbstractCommonList>)createItemDocumentHandler(ctx, parentcsid, null);
827 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "getAuthorityItemAuthRefs(item)", "GET_ITEM_AUTH_REFS", ctx);
829 List<RefNameServiceUtils.AuthRefConfigInfo> authRefsInfo = RefNameServiceUtils.getConfiguredAuthorityRefs(ctx);
830 authRefList = handler.getAuthorityRefs(itemcsid, authRefsInfo);
831 } catch (Exception e) {
832 throw bigReThrow(e, ServiceMessages.GET_FAILED + " parentspecifier: " + parentspecifier + " itemspecifier:" + itemspecifier);
838 * Update authorityItem.
840 * @param parentspecifier either a CSID or one of the urn forms
841 * @param itemspecifier either a CSID or one of the urn forms
843 * @return the multipart output
846 @Path("{csid}/items/{itemcsid}")
847 public byte[] updateAuthorityItem(
848 @Context ResourceMap resourceMap,
850 @PathParam("csid") String parentspecifier,
851 @PathParam("itemcsid") String itemspecifier,
853 PoxPayloadOut result = null;
855 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
856 // Note that we have to create the service context for the Items, not the main service
857 //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
858 String parentcsid = lookupParentCSID(parentspecifier, "updateAuthorityItem(parent)", "UPDATE_ITEM", null);
860 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), theUpdate);
861 ctx.setResourceMap(resourceMap);
862 String itemcsid = lookupItemCSID(itemspecifier, parentcsid, "updateAuthorityItem(item)", "UPDATE_ITEM", ctx);
864 // We omit the parentShortId, only needed when doing a create...
865 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createItemDocumentHandler(ctx, parentcsid, null);
867 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
868 result = ctx.getOutput();
870 } catch (Exception e) {
871 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED);
873 return result.getBytes();
877 * Delete authorityItem.
879 * @param parentcsid the parentcsid
880 * @param itemcsid the itemcsid
882 * @return the response
885 @Path("{csid}/items/{itemcsid}")
886 public Response deleteAuthorityItem(
887 @PathParam("csid") String parentcsid,
888 @PathParam("itemcsid") String itemcsid) {
890 if (logger.isDebugEnabled()) {
891 logger.debug("deleteAuthorityItem with parentcsid=" + parentcsid + " and itemcsid=" + itemcsid);
894 ensureCSID(parentcsid, ServiceMessages.DELETE_FAILED, "AuthorityItem.parentcsid");
895 ensureCSID(itemcsid, ServiceMessages.DELETE_FAILED, "AuthorityItem.itemcsid");
896 //Laramie, removing this catch, since it will surely fail below, since itemcsid or parentcsid will be null.
897 // }catch (Throwable t){
898 // System.out.println("ERROR in setting up DELETE: "+t);
901 // Note that we have to create the service context for the Items, not the main service
902 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName());
903 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
904 getRepositoryClient(ctx).delete(ctx, itemcsid, handler);
905 return Response.status(HttpResponseCodes.SC_OK).build();
906 } catch (Exception e) {
907 throw bigReThrow(e, ServiceMessages.DELETE_FAILED + " itemcsid: " + itemcsid + " parentcsid:" + parentcsid);
910 public final static String hierarchy = "hierarchy";
913 @Path("{csid}/items/{itemcsid}/" + hierarchy)
914 @Produces("application/xml")
915 public String getHierarchy(@PathParam("csid") String csid,
916 @PathParam("itemcsid") String itemcsid,
917 @Context UriInfo ui) throws Exception {
919 // 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...?
920 String calledUri = ui.getPath();
921 String uri = "/" + calledUri.substring(0, (calledUri.length() - ("/" + hierarchy).length()));
922 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), ui);
924 String direction = ui.getQueryParameters().getFirst(Hierarchy.directionQP);
925 if (Tools.notBlank(direction) && Hierarchy.direction_parents.equals(direction)) {
926 return Hierarchy.surface(ctx, itemcsid, uri);
928 return Hierarchy.dive(ctx, itemcsid, uri);
930 } catch (Exception e) {
931 throw bigReThrow(e, "Error showing hierarchy", itemcsid);