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 java.util.List;
29 import javax.ws.rs.Consumes;
30 import javax.ws.rs.DELETE;
31 import javax.ws.rs.GET;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Request;
40 import javax.ws.rs.core.Response;
41 import javax.ws.rs.core.UriBuilder;
42 import javax.ws.rs.core.UriInfo;
43 import javax.ws.rs.core.MultivaluedHashMap;
45 import org.collectionspace.services.client.IClientQueryParams;
46 import org.collectionspace.services.client.IQueryManager;
47 import org.collectionspace.services.client.PoxPayloadIn;
48 import org.collectionspace.services.client.PoxPayloadOut;
49 import org.collectionspace.services.client.XmlTools;
50 import org.collectionspace.services.client.workflow.WorkflowClient;
51 import org.collectionspace.services.common.CSWebApplicationException;
52 import org.collectionspace.services.common.NuxeoBasedResource;
53 import org.collectionspace.services.common.ResourceMap;
54 import org.collectionspace.services.common.ServiceMain;
55 import org.collectionspace.services.common.ServiceMessages;
56 import org.collectionspace.services.common.StoredValuesUriTemplate;
57 import org.collectionspace.services.common.UriTemplateFactory;
58 import org.collectionspace.services.common.UriTemplateRegistry;
59 import org.collectionspace.services.common.UriTemplateRegistryKey;
60 import org.collectionspace.services.common.api.RefName;
61 import org.collectionspace.services.common.api.Tools;
62 import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
63 import org.collectionspace.services.common.authorityref.AuthorityRefList;
64 import org.collectionspace.services.common.context.JaxRsContext;
65 import org.collectionspace.services.common.context.MultipartServiceContext;
66 import org.collectionspace.services.common.context.RemoteServiceContext;
67 import org.collectionspace.services.common.context.ServiceBindingUtils;
68 import org.collectionspace.services.common.context.ServiceContext;
69 import org.collectionspace.services.common.document.DocumentException;
70 import org.collectionspace.services.common.document.DocumentFilter;
71 import org.collectionspace.services.common.document.DocumentHandler;
72 import org.collectionspace.services.common.document.DocumentNotFoundException;
73 import org.collectionspace.services.common.document.DocumentReferenceException;
74 import org.collectionspace.services.common.document.DocumentWrapper;
75 import org.collectionspace.services.common.document.Hierarchy;
76 import org.collectionspace.services.common.query.QueryManager;
77 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityDocumentModelHandler;
78 import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
79 import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;
80 import org.collectionspace.services.config.ClientType;
81 import org.collectionspace.services.config.service.ServiceBindingType;
82 import org.collectionspace.services.jaxb.AbstractCommonList;
83 import org.collectionspace.services.lifecycle.TransitionDef;
84 import org.collectionspace.services.nuxeo.client.java.DocumentModelHandler;
85 import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
86 import org.collectionspace.services.nuxeo.client.java.NuxeoDocumentFilter;
87 import org.collectionspace.services.nuxeo.client.java.RepositoryClientImpl;
88 import org.collectionspace.services.nuxeo.util.NuxeoUtils;
89 import org.collectionspace.services.workflow.WorkflowCommon;
90 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
91 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.SpecifierForm;
92 import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
93 import org.collectionspace.services.description.ServiceDescription;
94 import org.jboss.resteasy.util.HttpResponseCodes;
95 import org.nuxeo.ecm.core.api.DocumentModel;
96 import org.nuxeo.ecm.core.api.DocumentModelList;
97 import org.slf4j.Logger;
98 import org.slf4j.LoggerFactory;
101 * The Class AuthorityResource.
104 @Consumes("application/xml")
105 @Produces("application/xml")
106 public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
107 extends NuxeoBasedResource {
109 final static String SEARCH_TYPE_TERMSTATUS = "ts";
110 public final static String hierarchy = "hierarchy";
112 protected Class<AuthCommon> authCommonClass;
113 protected Class<?> resourceClass;
114 protected String authorityCommonSchemaName;
115 protected String authorityItemCommonSchemaName;
116 final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType(); //FIXME: REM - 3 Why is this field needed? I see no references to it.
118 final static String FETCH_SHORT_ID = "_fetch_";
119 public final static String PARENT_WILDCARD = "_ALL_";
121 final Logger logger = LoggerFactory.getLogger(AuthorityResource.class);
124 * Instantiates a new Authority resource.
126 public AuthorityResource(Class<AuthCommon> authCommonClass, Class<?> resourceClass,
127 String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
128 this.authCommonClass = authCommonClass;
129 this.resourceClass = resourceClass;
130 this.authorityCommonSchemaName = authorityCommonSchemaName;
131 this.authorityItemCommonSchemaName = authorityItemCommonSchemaName;
134 public abstract String getItemServiceName();
136 public abstract String getItemTermInfoGroupXPathBase();
139 protected String getVersionString() {
140 return "$LastChangedRevision: 2617 $";
144 public Class<AuthCommon> getCommonPartClass() {
145 return authCommonClass;
149 * Creates the item document handler.
152 * @param inAuthority the in vocabulary
154 * @return the document handler
156 * @throws Exception the exception
158 protected DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> createItemDocumentHandler(
159 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
160 String inAuthority, String parentShortIdentifier)
162 String authorityRefNameBase;
163 AuthorityItemDocumentModelHandler<?> docHandler;
165 if (parentShortIdentifier == null) {
166 authorityRefNameBase = null;
168 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getServiceName());
169 if (parentShortIdentifier.equals(FETCH_SHORT_ID)) { // We need to fetch this from the repo
170 if (ctx.getCurrentRepositorySession() != null) {
171 parentCtx.setCurrentRepositorySession(ctx.getCurrentRepositorySession()); // We need to use the current repo session if one exists
173 // Get from parent document
174 parentShortIdentifier = getAuthShortIdentifier(parentCtx, inAuthority);
176 authorityRefNameBase = buildAuthorityRefNameBase(parentCtx, parentShortIdentifier);
179 docHandler = (AuthorityItemDocumentModelHandler<?>) createDocumentHandler(ctx,
180 ctx.getCommonPartLabel(getItemServiceName()),
182 // FIXME - Richard and Aron think the following three lines should
183 // be in the constructor for the AuthorityItemDocumentModelHandler
184 // because all three are required fields.
185 docHandler.setInAuthority(inAuthority);
186 docHandler.setAuthorityRefNameBase(authorityRefNameBase);
187 docHandler.setItemTermInfoGroupXPathBase(getItemTermInfoGroupXPathBase());
191 public String getAuthShortIdentifier(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String authCSID)
192 throws DocumentNotFoundException, DocumentException {
193 String shortIdentifier = null;
196 AuthorityDocumentModelHandler<?> handler = (AuthorityDocumentModelHandler<?>) createDocumentHandler(ctx);
197 shortIdentifier = handler.getShortIdentifier(ctx, authCSID, authorityCommonSchemaName);
198 } catch (Exception e) {
199 if (logger.isDebugEnabled()) {
200 logger.debug("Caught exception ", e);
202 throw new DocumentException(e);
205 return shortIdentifier;
208 protected String buildAuthorityRefNameBase(
209 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String shortIdentifier) {
210 RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
211 ctx.getServiceName(),
212 null, // Only use shortId form!!!
213 shortIdentifier, null);
214 return authority.toString();
217 public static class CsidAndShortIdentifier {
219 String shortIdentifier;
222 protected String lookupParentCSID(String parentspecifier, String method,
223 String op, UriInfo uriInfo) throws Exception {
224 CsidAndShortIdentifier tempResult = lookupParentCSIDAndShortIdentifer(NULL_CONTEXT,
225 parentspecifier, method, op, uriInfo);
226 return tempResult.CSID;
229 protected String lookupParentCSID(ServiceContext ctx, String parentspecifier, String method,
230 String op, UriInfo uriInfo) throws Exception {
231 CsidAndShortIdentifier tempResult = lookupParentCSIDAndShortIdentifer(ctx,
232 parentspecifier, method, op, uriInfo);
233 return tempResult.CSID;
237 private CsidAndShortIdentifier lookupParentCSIDAndShortIdentifer(
238 ServiceContext existingCtx, // Ok to be null
239 String parentIdentifier,
244 CsidAndShortIdentifier result = new CsidAndShortIdentifier();
245 Specifier parentSpec = Specifier.getSpecifier(parentIdentifier, method, op);
248 String parentShortIdentifier;
249 if (parentSpec.form == SpecifierForm.CSID) {
250 parentShortIdentifier = null;
251 parentcsid = parentSpec.value;
252 // Uncomment when app layer is ready to integrate
253 // Uncommented since refNames are currently only generated if not present - ADR CSPACE-3178
254 parentShortIdentifier = FETCH_SHORT_ID;
256 parentShortIdentifier = parentSpec.value;
257 String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, parentShortIdentifier);
258 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getServiceName(), uriInfo);
259 CoreSessionInterface repoSession = null;
260 if (existingCtx != null) {
261 repoSession = (CoreSessionInterface) existingCtx.getCurrentRepositorySession(); // We want to use the thread's current repo session
263 parentcsid = getRepositoryClient(ctx).findDocCSID(repoSession, ctx, whereClause); //FIXME: REM - If the parent has been soft-deleted, should we be looking for the item?
266 result.CSID = parentcsid;
267 result.shortIdentifier = parentShortIdentifier;
272 public String lookupItemCSID(ServiceContext<PoxPayloadIn, PoxPayloadOut> existingContext, String itemspecifier, String parentcsid, String method, String op)
276 Specifier itemSpec = Specifier.getSpecifier(itemspecifier, method, op);
277 if (itemSpec.form == SpecifierForm.CSID) {
278 itemcsid = itemSpec.value;
280 String itemWhereClause = RefNameServiceUtils.buildWhereForAuthItemByName(authorityItemCommonSchemaName, itemSpec.value, parentcsid);
281 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(getItemServiceName());
282 CoreSessionInterface repoSession = null;
283 if (existingContext != null) {
284 repoSession = (CoreSessionInterface) existingContext.getCurrentRepositorySession(); // We want to use the thread's current repo session
286 itemcsid = getRepositoryClient(ctx).findDocCSID(repoSession, ctx, itemWhereClause); //FIXME: REM - Should we be looking for the 'wf_deleted' query param and filtering on it?
293 * Generally, callers will first call RefName.AuthorityItem.parse with a refName, and then
294 * use the returned item.inAuthority.resource and a resourceMap to get a service-specific
295 * Resource. They then call this method on that resource.
298 public DocumentModel getDocModelForAuthorityItem(CoreSessionInterface repoSession, RefName.AuthorityItem item)
299 throws Exception, DocumentNotFoundException {
303 String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, item.getParentShortIdentifier());
304 // Ensure we have the right context.
305 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(item.inAuthority.resource);
307 // HACK - this really must be moved to the doc handler, not here. No Nuxeo specific stuff here!
308 RepositoryClientImpl client = (RepositoryClientImpl)getRepositoryClient(ctx);
309 String parentcsid = client.findDocCSID(repoSession, ctx, whereClause);
311 String itemWhereClause = RefNameServiceUtils.buildWhereForAuthItemByName(authorityItemCommonSchemaName, item.getShortIdentifier(), parentcsid);
312 ctx = createServiceContext(getItemServiceName());
313 DocumentWrapper<DocumentModel> docWrapper = client.findDoc(repoSession, ctx, itemWhereClause);
314 DocumentModel docModel = docWrapper.getWrappedObject();
320 public Response createAuthority(String xmlPayload) {
322 // Requests to create new authorities come in on new threads. Unfortunately, we need to synchronize those threads on this block because, as of 8/27/2015, we can't seem to get Nuxeo
323 // transaction code to deal with a database level UNIQUE constraint violations on the 'shortidentifier' column of the vocabularies_common table.
324 // Therefore, to prevent having multiple authorities with the same shortid, we need to synchronize
325 // the code that creates new authorities. The authority document model handler will first check for authorities with the same short id before
326 // trying to create a new authority.
328 synchronized(AuthorityResource.class) {
330 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
331 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(input);
332 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
334 String csid = getRepositoryClient(ctx).create(ctx, handler);
335 UriBuilder path = UriBuilder.fromResource(resourceClass);
336 path.path("" + csid);
337 Response response = Response.created(path.build()).build();
339 } catch (Exception e) {
340 throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
345 protected boolean supportsReplicating(String tenantId, String serviceName) {
346 boolean result = false;
348 ServiceBindingType sb = getTenantBindingsReader().getServiceBinding(tenantId, getServiceName());
349 result = sb.isSupportsReplicating();
355 * Synchronizes the authority and its items/terms with a Shared Authority Server.
357 * @param specifier either a CSID or one of the urn forms
359 * @return the authority
363 public byte[] synchronize(
364 @Context Request request,
366 @PathParam("csid") String identifier) {
368 boolean neededSync = false;
369 PoxPayloadOut payloadOut = null;
373 // Prevent multiple SAS synchronizations from occurring simultaneously by synchronizing this method.
375 synchronized(AuthorityResource.class) {
377 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
379 * Make sure this authority service supports synchronization
381 if (supportsReplicating(ctx.getTenantId(), ctx.getServiceName()) == false) {
382 throw new DocumentException(Response.Status.FORBIDDEN.getStatusCode());
384 AuthorityDocumentModelHandler handler = (AuthorityDocumentModelHandler)createDocumentHandler(ctx);
385 specifier = Specifier.getSpecifier(identifier, "getAuthority", "GET");
386 handler.setShouldUpdateRevNumber(AuthorityServiceUtils.DONT_UPDATE_REV); // Never update rev number on sync calls
387 neededSync = getRepositoryClient(ctx).synchronize(ctx, specifier, handler);
388 payloadOut = ctx.getOutput();
389 } catch (Exception e) {
390 throw bigReThrow(e, ServiceMessages.SYNC_FAILED, identifier);
394 // If a sync was needed and was successful, return a copy of the updated resource. Acts like an UPDATE.
396 if (neededSync == true) {
397 result = payloadOut.getBytes();
399 result = String.format("Authority resource '%s' was already in sync with shared authority server.",
400 specifier.value).getBytes();
401 Response response = Response.status(Response.Status.NOT_MODIFIED).entity(result).type("text/plain").build();
402 throw new CSWebApplicationException(response);
410 * Gets the authority.
412 * @param specifier either a CSID or one of the urn forms
414 * @return the authority
420 @Context Request request,
422 @PathParam("csid") String specifier) {
423 PoxPayloadOut result = null;
425 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
426 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
428 Specifier spec = Specifier.getSpecifier(specifier, "getAuthority", "GET");
429 if (spec.form == SpecifierForm.CSID) {
430 if (logger.isDebugEnabled()) {
431 logger.debug("getAuthority with csid=" + spec.value);
433 getRepositoryClient(ctx).get(ctx, spec.value, handler);
435 String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, spec.value);
436 DocumentFilter myFilter = new NuxeoDocumentFilter(whereClause, 0, 1);
437 handler.setDocumentFilter(myFilter);
438 getRepositoryClient(ctx).get(ctx, handler);
440 result = ctx.getOutput();
442 } catch (Exception e) {
443 throw bigReThrow(e, ServiceMessages.GET_FAILED, specifier);
446 if (result == null) {
447 Response response = Response.status(Response.Status.NOT_FOUND).entity(
448 "Get failed, the requested Authority specifier:" + specifier + ": was not found.").type(
449 "text/plain").build();
450 throw new CSWebApplicationException(response);
453 return result.getBytes();
457 * Finds and populates the authority list.
461 * @return the authority list
464 @Produces("application/xml")
465 public AbstractCommonList getAuthorityList(@Context UriInfo uriInfo) { //FIXME - REM 5/3/2012 - This is not reachable from the JAX-RS dispatcher. Instead the equivalent method in ResourceBase is getting called.
466 AbstractCommonList result = null;
469 MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
470 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(uriInfo);
472 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
473 DocumentFilter myFilter = handler.getDocumentFilter();
474 // Need to make the default sort order for authority items
475 // be on the displayName field
476 String sortBy = queryParams.getFirst(IClientQueryParams.ORDER_BY_PARAM);
477 if (sortBy == null || sortBy.isEmpty()) {
478 String qualifiedDisplayNameField = authorityCommonSchemaName + ":"
479 + AuthorityItemJAXBSchema.DISPLAY_NAME;
480 myFilter.setOrderByClause(qualifiedDisplayNameField);
482 String nameQ = queryParams.getFirst("refName");
484 myFilter.setWhereClause(authorityCommonSchemaName + ":refName='" + nameQ + "'");
486 getRepositoryClient(ctx).getFiltered(ctx, handler);
487 result = handler.getCommonPartList();
488 } catch (Exception e) {
489 throw bigReThrow(e, ServiceMessages.GET_FAILED);
496 * Overriding this methods to see if we should update the revision number during the update. We don't
497 * want to update the rev number of synchronization operations.
500 protected PoxPayloadOut update(String csid,
501 PoxPayloadIn theUpdate, // not used in this method, but could be used by an overriding method
502 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
504 AuthorityDocumentModelHandler handler = (AuthorityDocumentModelHandler) createDocumentHandler(ctx);
505 Boolean shouldUpdateRev = (Boolean) ctx.getProperty(AuthorityServiceUtils.SHOULD_UPDATE_REV_PROPERTY);
506 if (shouldUpdateRev != null) {
507 handler.setShouldUpdateRevNumber(shouldUpdateRev);
509 getRepositoryClient(ctx).update(ctx, csid, handler);
510 return ctx.getOutput();
516 * @param specifier the csid or id
518 * @return the multipart output
522 public byte[] updateAuthority(
523 @PathParam("csid") String specifier,
525 PoxPayloadOut result = null;
527 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
528 Specifier spec = Specifier.getSpecifier(specifier, "updateAuthority", "UPDATE");
529 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(theUpdate);
530 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
532 if (spec.form == SpecifierForm.CSID) {
535 String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, spec.value);
536 csid = getRepositoryClient(ctx).findDocCSID(null, ctx, whereClause);
538 getRepositoryClient(ctx).update(ctx, csid, handler);
539 result = ctx.getOutput();
540 } catch (Exception e) {
541 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED);
543 return result.getBytes();
549 * @param csid the csid
551 * @return the response
556 public Response old_deleteAuthority(@PathParam("csid") String csid) {
557 if (logger.isDebugEnabled()) {
558 logger.debug("deleteAuthority with csid=" + csid);
561 ensureCSID(csid, ServiceMessages.DELETE_FAILED, "Authority.csid");
562 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext();
563 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
564 getRepositoryClient(ctx).delete(ctx, csid, handler);
565 return Response.status(HttpResponseCodes.SC_OK).build();
566 } catch (Exception e) {
567 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, csid);
574 * @param csid the csid or a URN specifier form -e.g., urn:cspace:name(OurMuseumPersonAuthority)
576 * @return the response
580 public Response deleteAuthority(
581 @Context Request request,
583 @PathParam("csid") String specifier) {
584 if (logger.isDebugEnabled()) {
585 logger.debug("deleteAuthority with specifier=" + specifier);
589 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(ui);
590 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
592 Specifier spec = Specifier.getSpecifier(specifier, "getAuthority", "GET");
593 if (spec.form == SpecifierForm.CSID) {
594 if (logger.isDebugEnabled()) {
595 logger.debug("deleteAuthority with csid=" + spec.value);
597 ensureCSID(spec.value, ServiceMessages.DELETE_FAILED, "Authority.csid");
598 getRepositoryClient(ctx).delete(ctx, spec.value, handler);
600 if (logger.isDebugEnabled()) {
601 logger.debug("deleteAuthority with specifier=" + spec.value);
603 String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, spec.value);
604 getRepositoryClient(ctx).deleteWithWhereClause(ctx, whereClause, handler);
607 return Response.status(HttpResponseCodes.SC_OK).build();
608 } catch (Exception e) {
609 throw bigReThrow(e, ServiceMessages.DELETE_FAILED, specifier);
616 * @param parentspecifier - ID of the container. Can be URN or CSID form
617 * @param shouldUpdateRevNumber - Indicates if the revision number should be updated on create -won't do this when synching with SAS
618 * @param isProposed - In a shared authority context, indicates if this item just a proposed item and not yet part of the SAS authority
622 protected Response createAuthorityItem(ServiceContext ctx, String parentIdentifier,
623 boolean shouldUpdateRevNumber,
625 boolean isSasItem) throws Exception {
626 Response result = null;
628 // Note: must have the parentShortId, to do the create.
629 CsidAndShortIdentifier parent = lookupParentCSIDAndShortIdentifer(ctx, parentIdentifier, "createAuthorityItem", "CREATE_ITEM", null);
630 AuthorityItemDocumentModelHandler handler =
631 (AuthorityItemDocumentModelHandler) createItemDocumentHandler(ctx, parent.CSID, parent.shortIdentifier);
632 handler.setShouldUpdateRevNumber(shouldUpdateRevNumber);
633 handler.setIsProposed(isProposed);
634 handler.setIsSASItem(isSasItem);
635 // Make the client call
636 String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
638 // Build the JAX-RS response
639 UriBuilder path = UriBuilder.fromResource(resourceClass);
640 path.path(parent.CSID + "/items/" + itemcsid);
641 result = Response.created(path.build()).build();
647 * Called with an existing context.
649 * @param parentIdentifier
654 public Response createAuthorityItemWithParentContext(ServiceContext parentCtx,
655 String parentIdentifier,
657 boolean shouldUpdateRevNumber,
659 boolean isSASItem) throws Exception {
660 Response result = null;
662 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), input,
663 parentCtx.getResourceMap(), parentCtx.getUriInfo());
664 if (parentCtx.getCurrentRepositorySession() != null) {
665 ctx.setCurrentRepositorySession(parentCtx.getCurrentRepositorySession());
667 result = this.createAuthorityItem(ctx, parentIdentifier, shouldUpdateRevNumber, isProposed, isSASItem);
672 /*************************************************************************
673 * Create an AuthorityItem - this is a sub-resource of Authority
674 * @param specifier either a CSID or one of the urn forms
675 * @return Authority item response
676 *************************************************************************/
678 @Path("{csid}/items")
679 public Response createAuthorityItem(
680 @Context ResourceMap resourceMap,
681 @Context UriInfo uriInfo,
682 @PathParam("csid") String parentIdentifier, // Either a CSID or a URN form -e.g., a8ad38ec-1d7d-4bf2-bd31 or urn:cspace:name(bugsbunny)
684 Response result = null;
687 PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
688 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), input, resourceMap, uriInfo);
689 result = this.createAuthorityItem(ctx, parentIdentifier, AuthorityServiceUtils.UPDATE_REV,
690 AuthorityServiceUtils.PROPOSED, AuthorityServiceUtils.NOT_SAS_ITEM);
691 } catch (Exception e) {
692 throw bigReThrow(e, ServiceMessages.CREATE_FAILED);
699 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH)
700 public byte[] getItemWorkflow(
701 @PathParam("csid") String csid,
702 @PathParam("itemcsid") String itemcsid) {
703 PoxPayloadOut result = null;
706 ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx = createServiceContext(getItemServiceName());
707 String parentWorkspaceName = parentCtx.getRepositoryWorkspaceName();
709 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME);
710 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
711 ctx.setRespositoryWorkspaceName(parentWorkspaceName); //find the document in the parent's workspace
712 getRepositoryClient(ctx).get(ctx, itemcsid, handler);
713 result = ctx.getOutput();
714 } catch (Exception e) {
715 throw bigReThrow(e, ServiceMessages.READ_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
717 return result.getBytes();
720 //FIXME: This method is almost identical to the method org.collectionspace.services.common.updateWorkflowWithTransition() so
721 // they should be consolidated -be DRY (D)on't (R)epeat (Y)ourself.
723 @Path("{csid}/items/{itemcsid}" + WorkflowClient.SERVICE_PATH + "/{transition}")
724 public byte[] updateItemWorkflowWithTransition(
725 @Context UriInfo uriInfo,
726 @PathParam("csid") String parentIdentifier,
727 @PathParam("itemcsid") String itemIdentifier,
728 @PathParam("transition") String transition) {
729 PoxPayloadOut result = null;
732 ServiceContext ctx = createServiceContext(getItemServiceName(), uriInfo);
733 result = updateItemWorkflowWithTransition(ctx,
734 parentIdentifier, itemIdentifier, transition, AuthorityServiceUtils.UPDATE_REV);
735 } catch (Exception e) {
736 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, parentIdentifier);
739 return result.getBytes();
743 * Update an authority item's workflow state.
744 * @param existingContext
749 * @throws DocumentReferenceException
751 public PoxPayloadOut updateItemWorkflowWithTransition(ServiceContext existingContext,
752 String parentIdentifier,
753 String itemIdentifier,
755 boolean updateRevNumber) throws DocumentReferenceException {
756 PoxPayloadOut result = null;
760 // We need CSIDs for both the parent authority and the authority item
762 CsidAndShortIdentifier csidAndShortId = lookupParentCSIDAndShortIdentifer(existingContext, parentIdentifier, "updateItemWorkflowWithTransition(parent)", "UPDATE_ITEM", null);
763 String itemCsid = lookupItemCSID(existingContext, itemIdentifier, csidAndShortId.CSID, "updateAuthorityItem(item)", "UPDATE_ITEM");
766 // Create an empty workflow_commons input part and set it into a new "workflow" sub-resource context
768 PoxPayloadIn input = new PoxPayloadIn(WorkflowClient.SERVICE_PAYLOAD_NAME, new WorkflowCommon(),
769 WorkflowClient.SERVICE_COMMONPART_NAME);
770 MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, input);
771 if (existingContext != null && existingContext.getCurrentRepositorySession() != null) {
772 ctx.setCurrentRepositorySession(existingContext.getCurrentRepositorySession());// If a repo session is already open, we need to use it and not create a new one
775 // Create a service context and document handler for the target resource -not the workflow resource itself.
777 ServiceContext<PoxPayloadIn, PoxPayloadOut> targetCtx = createServiceContext(getItemServiceName(), existingContext.getUriInfo());
778 AuthorityItemDocumentModelHandler targetDocHandler = (AuthorityItemDocumentModelHandler) this.createDocumentHandler(targetCtx);
779 targetDocHandler.setShouldUpdateRevNumber(updateRevNumber);
780 ctx.setProperty(WorkflowClient.TARGET_DOCHANDLER, targetDocHandler); //added as a context param for the workflow document handler -it will call the parent's dochandler "prepareForWorkflowTranstion" method
782 // When looking for the document, we need to use the parent/target resource's workspace name -not the "workflow" workspace name
784 String targetWorkspaceName = targetCtx.getRepositoryWorkspaceName();
785 ctx.setRespositoryWorkspaceName(targetWorkspaceName); //find the document in the parent's workspace
787 // Get the type of transition we're being asked to make and store it as a context parameter -used by the workflow document handler
788 TransitionDef transitionDef = getTransitionDef(targetCtx, transition);
789 if (transitionDef == null) {
790 throw new DocumentException(String.format("The document with ID='%s' does not support the workflow transition '%s'.",
791 itemIdentifier, transition));
793 ctx.setProperty(WorkflowClient.TRANSITION_ID, transitionDef);
795 WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
796 getRepositoryClient(ctx).update(ctx, itemCsid, handler);
797 result = ctx.getOutput();
798 } catch (DocumentReferenceException de) {
800 } catch (Exception e) {
801 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, itemIdentifier);
807 private PoxPayloadOut getAuthorityItem(
809 String parentIdentifier,
810 String itemIdentifier) throws Exception {
811 PoxPayloadOut result = null;
813 String parentcsid = lookupParentCSID(ctx, parentIdentifier, "getAuthorityItem(parent)", "GET_ITEM", null);
814 // We omit the parentShortId, only needed when doing a create...
815 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createItemDocumentHandler(ctx, parentcsid, null);
817 Specifier itemSpec = Specifier.getSpecifier(itemIdentifier, "getAuthorityItem(item)", "GET_ITEM");
818 if (itemSpec.form == SpecifierForm.CSID) {
819 // TODO should we assert that the item is in the passed vocab?
820 getRepositoryClient(ctx).get(ctx, itemSpec.value, handler);
822 String itemWhereClause =
823 RefNameServiceUtils.buildWhereForAuthItemByName(authorityItemCommonSchemaName, itemSpec.value, parentcsid);
824 DocumentFilter myFilter = new NuxeoDocumentFilter(itemWhereClause, 0, 1); // start at page 0 and get 1 item
825 handler.setDocumentFilter(myFilter);
826 getRepositoryClient(ctx).get(ctx, handler);
829 result = (PoxPayloadOut) ctx.getOutput();
830 if (result != null) {
831 String inAuthority = XmlTools.getElementValue(result.getDOMDocument(), "//" + AuthorityItemJAXBSchema.IN_AUTHORITY);
832 if (inAuthority.equalsIgnoreCase(parentcsid) == false) {
833 throw new Exception(String.format("Looked up item = '%s' and found with inAuthority = '%s', but expected inAuthority = '%s'.",
834 itemSpec.value, inAuthority, parentcsid));
841 public PoxPayloadOut getAuthorityItemWithExistingContext(
842 ServiceContext existingCtx,
843 String parentIdentifier,
844 String itemIdentifier) throws Exception {
845 PoxPayloadOut result = null;
847 ServiceContext ctx = createServiceContext(getItemServiceName(), existingCtx.getResourceMap(), existingCtx.getUriInfo());
848 if (existingCtx.getCurrentRepositorySession() != null) {
849 ctx.setCurrentRepositorySession(existingCtx.getCurrentRepositorySession()); // Reuse the current repo session if one exists
850 ctx.setProperties(existingCtx.getProperties());
852 result = getAuthorityItem(ctx, parentIdentifier, itemIdentifier);
858 * Gets the authority item.
860 * @param parentspecifier either a CSID or one of the urn forms
861 * @param itemspecifier either a CSID or one of the urn forms
863 * @return the authority item
866 @Path("{csid}/items/{itemcsid}")
867 public byte[] getAuthorityItem(
868 @Context Request request,
869 @Context UriInfo uriInfo,
870 @Context ResourceMap resourceMap,
871 @PathParam("csid") String parentIdentifier,
872 @PathParam("itemcsid") String itemIdentifier) {
873 PoxPayloadOut result = null;
875 RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx =
876 (RemoteServiceContext<PoxPayloadIn, PoxPayloadOut>) createServiceContext(getItemServiceName(), resourceMap, uriInfo);
878 JaxRsContext jaxRsContext = new JaxRsContext(request, uriInfo); // Needed for getting account permissions part of the resource
879 ctx.setJaxRsContext(jaxRsContext);
881 result = getAuthorityItem(ctx, parentIdentifier, itemIdentifier);
882 } catch (DocumentNotFoundException dnf) {
883 throw bigReThrow(dnf, ServiceMessages.resourceNotFoundMsg(itemIdentifier));
884 } catch (Exception e) {
885 throw bigReThrow(e, ServiceMessages.GET_FAILED);
888 return result.getBytes();
892 * Most of the authority child classes will/should use this implementation. However, the Vocabulary service's item schema is
893 * different enough that it will have to override this method in it's resource class.
896 protected String getOrderByField(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
897 String result = null;
899 result = NuxeoUtils.getPrimaryElPathPropertyName(
900 authorityItemCommonSchemaName, getItemTermInfoGroupXPathBase(),
901 AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
907 protected String getPartialTermMatchField(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
908 String result = null;
910 result = NuxeoUtils.getMultiElPathPropertyName(
911 authorityItemCommonSchemaName, getItemTermInfoGroupXPathBase(),
912 AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
918 * Gets the authorityItem list for the specified authority
919 * If partialPerm is specified, keywords will be ignored.
921 * @param authorityIdentifier either a CSID or one of the urn forms
922 * @param partialTerm if non-null, matches partial terms
923 * @param keywords if non-null, matches terms in the keyword index for items
924 * @param ui passed to include additional parameters, like pagination controls
927 public AbstractCommonList getAuthorityItemList(ServiceContext existingContext,
928 String authorityIdentifier,
929 UriInfo uriInfo) throws Exception {
930 AbstractCommonList result = null;
932 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
933 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
934 if (existingContext != null && existingContext.getCurrentRepositorySession() != null) { // Merge some of the existing context properties with our new context
935 ctx.setCurrentRepositorySession(existingContext.getCurrentRepositorySession());
936 ctx.setProperties(existingContext.getProperties());
939 String orderBy = queryParams.getFirst(IClientQueryParams.ORDER_BY_PARAM);
940 String termStatus = queryParams.getFirst(SEARCH_TYPE_TERMSTATUS);
941 String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
942 String advancedSearch = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_AS);
943 String partialTerm = queryParams.getFirst(IQueryManager.SEARCH_TYPE_PARTIALTERM);
945 // For the wildcard case, parentcsid is null, but docHandler will deal with this.
946 // We omit the parentShortId, only needed when doing a create...
947 String parentcsid = PARENT_WILDCARD.equals(authorityIdentifier) ? null :
948 lookupParentCSID(ctx, authorityIdentifier, "getAuthorityItemList", "LIST", uriInfo);
949 DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler =
950 createItemDocumentHandler(ctx, parentcsid, null);
952 DocumentFilter myFilter = handler.getDocumentFilter();
953 // If we are not wildcarding the parent, add a restriction
954 if (parentcsid != null) {
955 myFilter.appendWhereClause(authorityItemCommonSchemaName + ":"
956 + AuthorityItemJAXBSchema.IN_AUTHORITY + "="
957 + "'" + parentcsid + "'",
958 IQueryManager.SEARCH_QUALIFIER_AND);
961 if (Tools.notBlank(termStatus)) {
962 // Start with the qualified termStatus field
963 String qualifiedTermStatusField = authorityItemCommonSchemaName + ":"
964 + AuthorityItemJAXBSchema.TERM_STATUS;
965 String[] filterTerms = termStatus.trim().split("\\|");
966 String tsClause = QueryManager.createWhereClauseToFilterFromStringList(qualifiedTermStatusField, filterTerms, IQueryManager.FILTER_EXCLUDE);
967 myFilter.appendWhereClause(tsClause, IQueryManager.SEARCH_QUALIFIER_AND);
970 result = search(ctx, handler, uriInfo, orderBy, keywords, advancedSearch, partialTerm);
976 * Gets the authorityItem list for the specified authority
977 * If partialPerm is specified, keywords will be ignored.
979 * @param authorityIdentifier either a CSID or one of the urn forms
980 * @param partialTerm if non-null, matches partial terms
981 * @param keywords if non-null, matches terms in the keyword index for items
982 * @param ui passed to include additional parameters, like pagination controls
984 * @return the authorityItem list
987 @Path("{csid}/items")
988 @Produces("application/xml")
989 public AbstractCommonList getAuthorityItemList(@PathParam("csid") String authorityIdentifier,
990 @Context UriInfo uriInfo) {
991 AbstractCommonList result = null;
994 result = getAuthorityItemList(NULL_CONTEXT, authorityIdentifier, uriInfo);
995 } catch (Exception e) {
996 throw bigReThrow(e, ServiceMessages.LIST_FAILED);
1003 * @return the name of the property used to specify references for items in this type of
1004 * authority. For most authorities, it is ServiceBindingUtils.AUTH_REF_PROP ("authRef").
1005 * Some types (like Vocabulary) use a separate property.
1007 protected String getRefPropName() {
1008 return ServiceBindingUtils.AUTH_REF_PROP;
1012 * Gets the entities referencing this Authority item instance. The service type
1013 * can be passed as a query param "type", and must match a configured type
1014 * for the service bindings. If not set, the type defaults to
1015 * ServiceBindingUtils.SERVICE_TYPE_PROCEDURE.
1017 * @param parentspecifier either a CSID or one of the urn forms
1018 * @param itemspecifier either a CSID or one of the urn forms
1021 * @return the info for the referencing objects
1024 @Path("{csid}/items/{itemcsid}/refObjs")
1025 @Produces("application/xml")
1026 public AuthorityRefDocList getReferencingObjects(
1027 @PathParam("csid") String parentSpecifier,
1028 @PathParam("itemcsid") String itemSpecifier,
1029 @Context UriTemplateRegistry uriTemplateRegistry,
1030 @Context UriInfo uriInfo) {
1031 AuthorityRefDocList authRefDocList = null;
1033 authRefDocList = getReferencingObjects(null, parentSpecifier, itemSpecifier, uriTemplateRegistry, uriInfo);
1034 } catch (Exception e) {
1035 throw bigReThrow(e, ServiceMessages.GET_FAILED);
1038 if (authRefDocList == null) {
1039 Response response = Response.status(Response.Status.NOT_FOUND).entity(
1040 "Get failed, the requested Item CSID:" + itemSpecifier + ": was not found.").type(
1041 "text/plain").build();
1042 throw new CSWebApplicationException(response);
1044 return authRefDocList;
1047 public AuthorityRefDocList getReferencingObjects(
1048 ServiceContext existingContext,
1049 String parentspecifier,
1050 String itemspecifier,
1051 UriTemplateRegistry uriTemplateRegistry,
1052 UriInfo uriInfo) throws Exception {
1053 AuthorityRefDocList authRefDocList = null;
1055 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
1056 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
1058 // Merge parts of existing context with our new context
1060 if (existingContext != null && existingContext.getCurrentRepositorySession() != null) {
1061 ctx.setCurrentRepositorySession(existingContext.getCurrentRepositorySession()); // If one exists, use the existing repo session
1062 ctx.setProperties(existingContext.getProperties());
1065 String parentcsid = lookupParentCSID(ctx, parentspecifier, "getReferencingObjects(parent)", "GET_ITEM_REF_OBJS", uriInfo);
1066 String itemcsid = lookupItemCSID(ctx, itemspecifier, parentcsid, "getReferencingObjects(item)", "GET_ITEM_REF_OBJS");
1068 // RESTEasy returns a read-only set of query params, so we need to make a read-write copy of them
1069 MultivaluedHashMap<String, String> tmpQueryParams = new MultivaluedHashMap<String, String>();
1070 tmpQueryParams.putAll(queryParams);
1071 // Set the original query params to the new copy
1072 queryParams = tmpQueryParams;
1073 // Update the service context with the new copy
1074 ctx.setQueryParams(queryParams);
1076 // Remove the "type" property from the query params
1077 List<String> serviceTypes = queryParams.remove(ServiceBindingUtils.SERVICE_TYPE_PROP);
1078 if (serviceTypes == null || serviceTypes.isEmpty()) {
1079 serviceTypes = ServiceBindingUtils.getCommonServiceTypes(true); //CSPACE-5359: Should now include objects, procedures, and authorities
1082 AuthorityItemDocumentModelHandler handler = (AuthorityItemDocumentModelHandler)createItemDocumentHandler(ctx, parentcsid, null);
1083 authRefDocList = handler.getReferencingObjects(ctx, uriTemplateRegistry, serviceTypes, getRefPropName(), itemcsid);
1085 return authRefDocList;
1089 * Gets the authority terms used in the indicated Authority item.
1091 * @param parentspecifier either a CSID or one of the urn forms
1092 * @param itemspecifier either a CSID or one of the urn forms
1093 * @param ui passed to include additional parameters, like pagination controls
1095 * @return the authority refs for the Authority item.
1098 @Path("{csid}/items/{itemcsid}/authorityrefs")
1099 @Produces("application/xml")
1100 public AuthorityRefList getAuthorityItemAuthorityRefs(
1101 @PathParam("csid") String parentspecifier,
1102 @PathParam("itemcsid") String itemspecifier,
1103 @Context UriInfo uriInfo) {
1104 AuthorityRefList authRefList = null;
1107 // Note that we have to create the service context for the Items, not the main service
1108 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
1109 MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
1110 String parentcsid = lookupParentCSID(parentspecifier, "getAuthorityItemAuthRefs(parent)", "GET_ITEM_AUTH_REFS", uriInfo);
1111 // We omit the parentShortId, only needed when doing a create...
1112 DocumentModelHandler<?, AbstractCommonList> handler =
1113 (DocumentModelHandler<?, AbstractCommonList>)createItemDocumentHandler(ctx, parentcsid, null /*no parent short ID*/);
1115 String itemcsid = lookupItemCSID(ctx, itemspecifier, parentcsid, "getAuthorityItemAuthRefs(item)", "GET_ITEM_AUTH_REFS");
1117 List<RefNameServiceUtils.AuthRefConfigInfo> authRefsInfo = RefNameServiceUtils.getConfiguredAuthorityRefs(ctx);
1118 authRefList = handler.getAuthorityRefs(itemcsid, authRefsInfo);
1119 } catch (Exception e) {
1120 throw bigReThrow(e, ServiceMessages.GET_FAILED + " parentspecifier: " + parentspecifier + " itemspecifier:" + itemspecifier);
1127 * Synchronizes a local authority item with a share authority server (SAS) item.
1129 * @param parentIdentifier
1130 * @param itemIdentifier
1134 @SuppressWarnings("unchecked")
1135 private PoxPayloadOut synchronizeItem(
1137 String parentIdentifier,
1138 String itemIdentifier,
1139 boolean syncHierarchicalRelationships) throws Exception {
1140 PoxPayloadOut result = null;
1141 AuthorityItemSpecifier specifier;
1142 boolean neededSync = false;
1144 CsidAndShortIdentifier parent = lookupParentCSIDAndShortIdentifer(ctx, parentIdentifier, "syncAuthorityItem(parent)", "SYNC_ITEM", null);
1145 AuthorityItemDocumentModelHandler handler = (AuthorityItemDocumentModelHandler)createItemDocumentHandler(ctx, parent.CSID, parent.shortIdentifier);
1146 handler.setIsProposed(AuthorityServiceUtils.NOT_PROPOSED); // In case it was formally locally proposed, clear the proposed flag
1147 handler.setIsSASItem(AuthorityServiceUtils.SAS_ITEM); // Since we're sync'ing, this is now a SAS controlled item
1148 handler.setShouldSyncHierarchicalRelationships(syncHierarchicalRelationships);
1149 // Create an authority item specifier
1150 Specifier parentSpecifier = Specifier.getSpecifier(parent.CSID, "getAuthority", "GET");
1151 Specifier itemSpecifier = Specifier.getSpecifier(itemIdentifier, "getAuthorityItem", "GET");
1152 specifier = new AuthorityItemSpecifier(parentSpecifier, itemSpecifier);
1154 neededSync = getRepositoryClient(ctx).synchronize(ctx, specifier, handler);
1155 if (neededSync == true) {
1156 result = (PoxPayloadOut) ctx.getOutput();
1163 * Using the parent and item ID, sync the local item with the SAS (shared authority server)
1164 * Used by the AuthorityItemDocumentModelHandler when synchronizing a list of remote authority items with a
1165 * local authority. The parent context was created for the authority (parent) because the sync started there.
1166 * @param existingCtx
1167 * @param parentIdentifier
1168 * @param itemIdentifier
1172 public PoxPayloadOut synchronizeItemWithExistingContext(
1173 ServiceContext existingCtx,
1174 String parentIdentifier,
1175 String itemIdentifier,
1176 boolean syncHierarchicalRelationships
1177 ) throws Exception {
1178 PoxPayloadOut result = null;
1180 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(),
1181 existingCtx.getResourceMap(),
1182 existingCtx.getUriInfo());
1183 if (existingCtx.getCurrentRepositorySession() != null) {
1184 ctx.setCurrentRepositorySession(existingCtx.getCurrentRepositorySession());
1187 result = synchronizeItem(ctx, parentIdentifier, itemIdentifier, syncHierarchicalRelationships);
1193 * Synchronizes an authority item and with a Shared Authority Server (SAS) item.
1195 * @param specifier either CSIDs and/or one of the urn forms
1197 * @return the authority item if it was updated/synchronized with SAS item; otherwise empty
1200 @Path("{csid}/items/{itemcsid}/sync")
1201 public byte[] synchronizeItem(
1202 @Context ResourceMap resourceMap,
1203 @Context UriInfo uriInfo,
1204 @PathParam("csid") String parentIdentifier,
1205 @PathParam("itemcsid") String itemIdentifier) {
1207 boolean neededSync = false;
1208 PoxPayloadOut payloadOut = null;
1211 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), null, resourceMap, uriInfo);
1212 payloadOut = this.synchronizeItem(ctx, parentIdentifier, itemIdentifier, true);
1213 if (payloadOut != null) {
1216 } catch (Exception e) {
1217 throw bigReThrow(e, ServiceMessages.SYNC_FAILED, itemIdentifier);
1221 // If a sync was needed and was successful, return a copy of the updated resource. Acts like an UPDATE.
1223 if (neededSync == true) {
1224 result = payloadOut.getBytes();
1226 result = String.format("Authority item resource '%s' was already in sync with shared authority server.",
1227 itemIdentifier).getBytes();
1228 Response response = Response.status(Response.Status.NOT_MODIFIED).entity(result).type("text/plain").build();
1229 throw new CSWebApplicationException(response);
1236 * Update authorityItem.
1238 * @param parentspecifier either a CSID or one of the urn forms
1239 * @param itemspecifier either a CSID or one of the urn forms
1241 * @return the multipart output
1244 @Path("{csid}/items/{itemcsid}")
1245 public byte[] updateAuthorityItem(
1246 @Context ResourceMap resourceMap,
1247 @Context UriInfo uriInfo,
1248 @PathParam("csid") String parentSpecifier,
1249 @PathParam("itemcsid") String itemSpecifier,
1250 String xmlPayload) {
1251 PoxPayloadOut result = null;
1254 PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
1255 result = updateAuthorityItem(null, resourceMap, uriInfo, parentSpecifier, itemSpecifier, theUpdate,
1256 AuthorityServiceUtils.UPDATE_REV, // passing TRUE so rev num increases, passing
1257 AuthorityServiceUtils.NO_CHANGE, // don't change the state of the "proposed" field -we could be performing a sync or just a plain update
1258 AuthorityServiceUtils.NO_CHANGE); // don't change the state of the "sas" field -we could be performing a sync or just a plain update
1259 } catch (Exception e) {
1260 throw bigReThrow(e, ServiceMessages.UPDATE_FAILED);
1263 return result.getBytes();
1266 public PoxPayloadOut updateAuthorityItem(
1267 ServiceContext itemServiceCtx, // Ok to be null. Will be null on PUT calls, but not on sync calls
1268 ResourceMap resourceMap,
1270 String parentspecifier,
1271 String itemspecifier,
1272 PoxPayloadIn theUpdate,
1273 boolean shouldUpdateRevNumber,
1276 ) throws Exception {
1277 PoxPayloadOut result = null;
1279 CsidAndShortIdentifier csidAndShortId = lookupParentCSIDAndShortIdentifer(itemServiceCtx, parentspecifier, "updateAuthorityItem(parent)", "UPDATE_ITEM", null);
1280 String parentcsid = csidAndShortId.CSID;
1281 String parentShortId = csidAndShortId.shortIdentifier;
1283 // If the itemServiceCtx context is not null, use it. Otherwise, create a new context
1285 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = itemServiceCtx;
1287 ctx = createServiceContext(getItemServiceName(), theUpdate, resourceMap, uriInfo);
1289 ctx.setInput(theUpdate); // the update payload
1292 String itemcsid = lookupItemCSID(ctx, itemspecifier, parentcsid, "updateAuthorityItem(item)", "UPDATE_ITEM"); //use itemServiceCtx if it is not null
1294 // We omit the parentShortId, only needed when doing a create...
1295 AuthorityItemDocumentModelHandler handler = (AuthorityItemDocumentModelHandler)createItemDocumentHandler(ctx, parentcsid, parentShortId);
1296 handler.setShouldUpdateRevNumber(shouldUpdateRevNumber);
1297 if (isProposed != null) {
1298 handler.setIsProposed(isProposed);
1300 if (isSASItem != null) {
1301 handler.setIsSASItem(isSASItem);
1303 getRepositoryClient(ctx).update(ctx, itemcsid, handler);
1304 result = ctx.getOutput();
1310 * Delete authorityItem.
1312 * @param parentIdentifier the parentcsid
1313 * @param itemIdentifier the itemcsid
1315 * @return the response
1318 @Path("{csid}/items/{itemcsid}")
1319 public Response deleteAuthorityItem(
1320 @Context UriInfo uriInfo,
1321 @PathParam("csid") String parentIdentifier,
1322 @PathParam("itemcsid") String itemIdentifier) {
1323 Response result = null;
1325 ensureCSID(parentIdentifier, ServiceMessages.DELETE_FAILED, "AuthorityItem.parentcsid");
1326 ensureCSID(itemIdentifier, ServiceMessages.DELETE_FAILED, "AuthorityItem.itemcsid");
1327 if (logger.isDebugEnabled()) {
1328 logger.debug("deleteAuthorityItem with parentcsid=" + parentIdentifier + " and itemcsid=" + itemIdentifier);
1332 ServiceContext ctx = createServiceContext(getItemServiceName(), uriInfo);
1333 deleteAuthorityItem(ctx, parentIdentifier, itemIdentifier, AuthorityServiceUtils.UPDATE_REV);
1334 result = Response.status(HttpResponseCodes.SC_OK).build();
1335 } catch (Exception e) {
1336 throw bigReThrow(e, ServiceMessages.DELETE_FAILED + " itemcsid: " + itemIdentifier + " parentcsid:" + parentIdentifier);
1344 * @param existingCtx
1345 * @param parentIdentifier
1346 * @param itemIdentifier
1349 @SuppressWarnings("rawtypes")
1350 public boolean deleteAuthorityItem(ServiceContext existingCtx,
1351 String parentIdentifier,
1352 String itemIdentifier,
1353 boolean shouldUpdateRevNumber
1354 ) throws Exception {
1355 boolean result = true;
1357 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), existingCtx.getUriInfo());
1358 if (existingCtx != null && existingCtx.getCurrentRepositorySession() != null) {
1359 ctx.setCurrentRepositorySession(existingCtx.getCurrentRepositorySession());
1360 ctx.setProperties(existingCtx.getProperties());
1363 String parentcsid = null;
1365 parentcsid = lookupParentCSID(ctx, parentIdentifier, "deleteAuthorityItem(parent)", "DELETE_ITEM", null);
1366 } catch (DocumentNotFoundException de) {
1367 logger.warn(String.format("Could not find parent with ID='%s' when trying to delete item ID='%s'",
1368 parentIdentifier, itemIdentifier));
1370 String itemCsid = lookupItemCSID(ctx, itemIdentifier, parentcsid, "deleteAuthorityItem(item)", "DELETE_ITEM"); //use itemServiceCtx if it is not null
1372 AuthorityItemDocumentModelHandler handler = (AuthorityItemDocumentModelHandler) createDocumentHandler(ctx);
1373 handler.setShouldUpdateRevNumber(shouldUpdateRevNumber);
1374 result = getRepositoryClient(ctx).delete(ctx, itemCsid, handler);
1380 @Path("{csid}/items/{itemcsid}/" + hierarchy)
1381 @Produces("application/xml")
1382 public String getHierarchy(
1383 @PathParam("csid") String parentIdentifier,
1384 @PathParam("itemcsid") String itemIdentifier,
1385 @Context UriInfo uriInfo) throws Exception {
1386 String result = null;
1390 // 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...?
1392 String calledUri = uriInfo.getPath();
1393 String uri = "/" + calledUri.substring(0, (calledUri.length() - ("/" + hierarchy).length()));
1394 ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
1396 String parentcsid = lookupParentCSID(ctx, parentIdentifier, "deleteAuthorityItem(parent)", "DELETE_ITEM", null);
1397 String itemcsid = lookupItemCSID(ctx, itemIdentifier, parentcsid, "deleteAuthorityItem(item)", "DELETE_ITEM"); //use itemServiceCtx if it is not null
1399 String direction = uriInfo.getQueryParameters().getFirst(Hierarchy.directionQP);
1400 if (Tools.notBlank(direction) && Hierarchy.direction_parents.equals(direction)) {
1401 result = Hierarchy.surface(ctx, itemcsid, uri);
1403 result = Hierarchy.dive(ctx, itemcsid, uri);
1405 } catch (Exception e) {
1406 throw bigReThrow(e, "Error showing hierarchy for authority item: ", itemIdentifier);
1417 protected String getItemDocType(String tenantId) {
1418 return getDocType(tenantId, getItemServiceName());
1422 * Returns a UriRegistry entry: a map of tenant-qualified URI templates
1423 * for the current resource, for all tenants
1425 * @return a map of URI templates for the current resource, for all tenants
1428 public Map<UriTemplateRegistryKey,StoredValuesUriTemplate> getUriRegistryEntries() {
1429 Map<UriTemplateRegistryKey,StoredValuesUriTemplate> uriRegistryEntriesMap =
1430 super.getUriRegistryEntries();
1431 List<String> tenantIds = getTenantBindingsReader().getTenantIds();
1432 for (String tenantId : tenantIds) {
1433 uriRegistryEntriesMap.putAll(getUriRegistryEntries(tenantId, getItemDocType(tenantId), UriTemplateFactory.ITEM));
1435 return uriRegistryEntriesMap;
1441 public ServiceDescription getDescription(ServiceContext ctx) {
1442 ServiceDescription result = super.getDescription(ctx);
1443 result.setSubresourceDocumentType(this.getItemDocType(ctx.getTenantId()));