@Produces("application/xml")
public abstract class AuthorityResource<AuthCommon, AuthItemHandler>
extends NuxeoBasedResource {
-
+
final Logger logger = LoggerFactory.getLogger(AuthorityResource.class);
final static String SEARCH_TYPE_TERMSTATUS = "ts";
public final static String hierarchy = "hierarchy";
-
+
private static final Integer PAGE_NUM_FROM_QUERYPARAMS = null;
private static final Integer PAGE_SIZE_FROM_QUERYPARAMS = null;
protected String authorityCommonSchemaName;
protected String authorityItemCommonSchemaName;
final static ClientType CLIENT_TYPE = ServiceMain.getInstance().getClientType(); //FIXME: REM - 3 Why is this field needed? I see no references to it.
-
+
final static String FETCH_SHORT_ID = "_fetch_";
public final static String PARENT_WILDCARD = "_ALL_";
protected static final boolean DONT_INCLUDE_ITEMS = false;
protected static final boolean INCLUDE_ITEMS = true;
-
+
/**
* Instantiates a new Authority resource.
*/
}
public abstract String getItemServiceName();
-
+
public abstract String getItemTermInfoGroupXPathBase();
@Override
/**
* Creates the item document handler.
- *
+ *
* @param ctx the ctx
* @param inAuthority the in vocabulary
- *
+ *
* @return the document handler
- *
+ *
* @throws Exception the exception
*/
protected DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> createItemDocumentHandler(
public String getAuthShortIdentifier(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String authCSID)
throws DocumentNotFoundException, DocumentException {
String shortIdentifier = null;
-
+
try {
AuthorityDocumentModelHandler<?> handler = (AuthorityDocumentModelHandler<?>) createDocumentHandler(ctx);
shortIdentifier = handler.getShortIdentifier(ctx, authCSID, authorityCommonSchemaName);
}
throw new DocumentException(e);
}
-
+
return shortIdentifier;
}
protected String buildAuthorityRefNameBase(
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String shortIdentifier) {
RefName.Authority authority = RefName.Authority.buildAuthority(ctx.getTenantName(),
- ctx.getServiceName(),
+ ctx.getServiceName(),
null, // Only use shortId form!!!
shortIdentifier, null);
return authority.toString();
parentspecifier, method, op, uriInfo);
return tempResult.CSID;
}
-
+
protected String lookupParentCSID(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, String parentspecifier, String method,
String op, UriInfo uriInfo) throws Exception {
CsidAndShortIdentifier tempResult = lookupParentCSIDAndShortIdentifer(ctx,
throws Exception {
CsidAndShortIdentifier result = new CsidAndShortIdentifier();
Specifier parentSpec = Specifier.getSpecifier(parentIdentifier, method, op);
-
+
String parentcsid;
String parentShortIdentifier;
if (parentSpec.form == SpecifierForm.CSID) {
}
parentcsid = getRepositoryClient(ctx).findDocCSID(repoSession, ctx, whereClause); //FIXME: REM - If the parent has been soft-deleted, should we be looking for the item?
}
-
+
result.CSID = parentcsid;
result.shortIdentifier = parentShortIdentifier;
-
+
return result;
}
- public String lookupItemCSID(ServiceContext<PoxPayloadIn, PoxPayloadOut> existingContext, String itemspecifier, String parentcsid, String method, String op)
+ public String lookupItemCSID(ServiceContext<PoxPayloadIn, PoxPayloadOut> existingContext, String itemSpecifier, String parentCsid, String method, String op)
throws Exception {
- String itemcsid;
-
- Specifier itemSpec = Specifier.getSpecifier(itemspecifier, method, op);
+ String itemCsid;
+ Specifier itemSpec = Specifier.getSpecifier(itemSpecifier, method, op);
+
if (itemSpec.form == SpecifierForm.CSID) {
- itemcsid = itemSpec.value;
+ itemCsid = itemSpec.value;
} else {
- String itemWhereClause = RefNameServiceUtils.buildWhereForAuthItemByName(authorityItemCommonSchemaName, itemSpec.value, parentcsid);
- MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(getItemServiceName());
CoreSessionInterface repoSession = null;
+ MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(getItemServiceName());
+
if (existingContext != null) {
repoSession = (CoreSessionInterface) existingContext.getCurrentRepositorySession(); // We want to use the thread's current repo session
}
- itemcsid = getRepositoryClient(ctx).findDocCSID(repoSession, ctx, itemWhereClause); //FIXME: REM - Should we be looking for the 'wf_deleted' query param and filtering on it?
+
+ String itemWhereClause = RefNameServiceUtils.buildWhereForAuthItemByName(authorityItemCommonSchemaName, itemSpec.value, parentCsid);
+ itemCsid = getRepositoryClient(ctx).findDocCSID(repoSession, ctx, itemWhereClause); //FIXME: REM - Should we be looking for the 'wf_deleted' query param and filtering on it?
}
-
- return itemcsid;
+
+ return itemCsid;
}
/*
- * Generally, callers will first call RefName.AuthorityItem.parse with a refName, and then
+ * Generally, callers will first call RefName.AuthorityItem.parse with a refName, and then
* use the returned item.inAuthority.resource and a resourceMap to get a service-specific
* Resource. They then call this method on that resource.
*/
@Override
- public DocumentModel getDocModelForAuthorityItem(CoreSessionInterface repoSession, RefName.AuthorityItem item)
+ public DocumentModel getDocModelForAuthorityItem(CoreSessionInterface repoSession, RefName.AuthorityItem item)
throws Exception, DocumentNotFoundException {
if (item == null) {
return null;
String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, item.getParentShortIdentifier());
// Ensure we have the right context.
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(item.inAuthority.resource);
-
+
// HACK - this really must be moved to the doc handler, not here. No Nuxeo specific stuff here!
NuxeoRepositoryClientImpl client = (NuxeoRepositoryClientImpl)getRepositoryClient(ctx);
String parentcsid = client.findDocCSID(repoSession, ctx, whereClause);
PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(input);
DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
-
+
String csid = getRepositoryClient(ctx).create(ctx, handler);
UriBuilder path = UriBuilder.fromResource(resourceClass);
path.path("" + csid);
protected boolean supportsReplicating(String tenantId, String serviceName) {
boolean result = false;
-
+
ServiceBindingType sb = getTenantBindingsReader().getServiceBinding(tenantId, getServiceName());
result = sb.isSupportsReplicating();
-
+
return result;
}
/**
* Synchronizes the authority and its items/terms with a Shared Authority Server.
- *
+ *
* @param specifier either a CSID or one of the urn forms
- *
+ *
* @return the authority
*/
@POST
boolean neededSync = false;
PoxPayloadOut payloadOut = null;
Specifier specifier;
-
+
//
// Prevent multiple SAS synchronizations from occurring simultaneously by synchronizing this method.
//
- synchronized(AuthorityResource.class) {
+ synchronized(AuthorityResource.class) {
try {
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(uriInfo);
/*
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.SYNC_FAILED, identifier);
}
-
+
//
// If a sync was needed and was successful, return a copy of the updated resource. Acts like an UPDATE.
//
throw new CSWebApplicationException(response);
}
}
-
+
return result;
}
-
+
/*
* Builds a cached JAX-RS response.
*/
protected Response buildResponse(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, PoxPayloadOut payloadOut) {
Response result = null;
-
+
ResponseBuilder responseBuilder = Response.ok(payloadOut.getBytes());
this.setCacheControl(ctx, responseBuilder);
- result = responseBuilder.build();
+ result = responseBuilder.build();
return result;
}
/**
* Gets the authority.
- *
+ *
* @param specifier either a CSID or one of the urn forms
- *
+ *
* @return the authority
*/
@GET
uriInfo = new UriInfoWrapper(uriInfo);
PoxPayloadOut payloadout = null;
- try {
+ try {
//
// If the specifier is a fully qualified authority term refname, then return the term payload in the response
//
return result;
}
-
+
protected PoxPayloadOut getAuthority(
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
Request request,
boolean includeItems) throws Exception {
uriInfo = new UriInfoWrapper(uriInfo);
PoxPayloadOut payloadout = null;
-
+
DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> docHandler = createDocumentHandler(ctx);
Specifier spec = Specifier.getSpecifier(specifier, "getAuthority", "GET");
if (spec.form == SpecifierForm.CSID) {
}
return payloadout;
- }
+ }
/**
* Finds and populates the authority list.
- *
+ *
* @param ui the ui
- *
+ *
* @return the authority list
*/
@GET
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.
uriInfo = new UriInfoWrapper(uriInfo);
AbstractCommonList result = null;
-
+
try {
MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(uriInfo);
-
+
DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
DocumentFilter myFilter = handler.getDocumentFilter();
// Need to make the default sort order for authority items
myFilter.setWhereClause(authorityCommonSchemaName + ":refName='" + nameQ + "'");
}
//getRepositoryClient(ctx).getFiltered(ctx, handler); # Something here?
- String advancedSearch = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_AS);
+ String advancedSearch = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_AS);
result = search(ctx, handler, uriInfo, orderBy, null, advancedSearch, null);
result = handler.getCommonPartList();
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.GET_FAILED);
}
-
+
return result;
}
-
+
/**
* Overriding this methods to see if we should update the revision number during the update. We don't
* want to update the rev number of synchronization operations.
getRepositoryClient(ctx).update(ctx, csid, handler);
return ctx.getOutput();
}
-
+
/**
* Update authority.
*
public byte[] updateAuthority(
@Context Request request,
@Context ResourceMap resourceMap,
- @Context UriInfo uriInfo,
+ @Context UriInfo uriInfo,
@PathParam("csid") String specifier,
String xmlPayload) {
PoxPayloadOut result = null;
}
return result.getBytes();
}
-
+
/**
* Delete all the items in an authority list.
- *
+ *
* @param specifier
* @param uriInfo
* @return
try {
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(uriInfo);
RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient = this.getRepositoryClient(ctx);
-
+
CoreSessionInterface repoSession = repoClient.getRepositorySession(ctx);
try {
DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
throw bigReThrow(e, ServiceMessages.DELETE_FAILED, specifier);
}
}
-
+
/**
* Delete authority
- *
+ *
* @param csid the csid or a URN specifier form -e.g., urn:cspace:name(OurMuseumPersonAuthority)
- *
+ *
* @return the response
*/
@DELETE
if (logger.isDebugEnabled()) {
logger.debug("deleteAuthority with specifier=" + specifier);
}
-
+
try {
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(uriInfo);
Specifier spec = Specifier.getSpecifier(specifier, "getAuthority", "GET");
RepositoryClient<PoxPayloadIn, PoxPayloadOut> repoClient = this.getRepositoryClient(ctx);
-
+
CoreSessionInterface repoSession = repoClient.getRepositorySession(ctx);
try {
DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createDocumentHandler(ctx);
} else {
if (logger.isDebugEnabled()) {
logger.debug("deleteAuthority with specifier=" + spec.value);
- }
+ }
String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, spec.value);
getRepositoryClient(ctx).deleteWithWhereClause(ctx, whereClause, handler);
}
throw bigReThrow(e, ServiceMessages.DELETE_FAILED, specifier);
}
}
-
+
protected String getCsid(ListItem item) {
String result = null;
-
+
for (Element ele : item.getAny()) {
String elementName = ele.getTagName().toLowerCase();
if (elementName.equals("csid")) {
break;
}
}
-
+
return result;
}
/**
- *
+ *
* @param ctx
* @param parentspecifier - ID of the container. Can be URN or CSID form
* @param shouldUpdateRevNumber - Indicates if the revision number should be updated on create -won't do this when synching with SAS
boolean isProposed,
boolean isSasItem) throws Exception {
Response result = null;
-
+
// Note: must have the parentShortId, to do the create.
CsidAndShortIdentifier parent = lookupParentCSIDAndShortIdentifer(ctx, parentIdentifier, "createAuthorityItem", "CREATE_ITEM", null);
- AuthorityItemDocumentModelHandler handler =
+ AuthorityItemDocumentModelHandler handler =
(AuthorityItemDocumentModelHandler) createItemDocumentHandler(ctx, parent.CSID, parent.shortIdentifier);
handler.setShouldUpdateRevNumber(shouldUpdateRevNumber);
handler.setIsProposed(isProposed);
handler.setIsSASItem(isSasItem);
// Make the client call
String itemcsid = getRepositoryClient(ctx).create(ctx, handler);
-
+
// Build the JAX-RS response
UriBuilder path = UriBuilder.fromResource(resourceClass);
path.path(parent.CSID + "/items/" + itemcsid);
return result;
}
-
+
public PoxPayloadOut updateAuthorityItem(
ServiceContext<PoxPayloadIn, PoxPayloadOut> itemServiceCtx, // Ok to be null. Will be null on PUT calls, but not on sync calls
- ResourceMap resourceMap,
+ ResourceMap resourceMap,
UriInfo uriInfo,
String parentspecifier,
String itemspecifier,
Boolean isSASItem
) throws Exception {
PoxPayloadOut result = null;
-
+
CsidAndShortIdentifier csidAndShortId = lookupParentCSIDAndShortIdentifer(itemServiceCtx, parentspecifier, "updateAuthorityItem(parent)", "UPDATE_ITEM", null);
String parentcsid = csidAndShortId.CSID;
String parentShortId = csidAndShortId.shortIdentifier;
} else {
ctx.setInput(theUpdate); // the update payload
}
-
+
String itemcsid = lookupItemCSID(ctx, itemspecifier, parentcsid, "updateAuthorityItem(item)", "UPDATE_ITEM"); //use itemServiceCtx if it is not null
// We omit the parentShortId, only needed when doing a create...
handler.setIsSASItem(isSASItem);
}
}
-
+
getRepositoryClient(ctx).update(ctx, itemcsid, handler);
result = ctx.getOutput();
return result;
- }
+ }
/**
* Called with an existing context.
boolean isProposed,
boolean isSASItem) throws Exception {
Response result = null;
-
+
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), input,
parentCtx.getResourceMap(), parentCtx.getUriInfo());
if (parentCtx.getCurrentRepositorySession() != null) {
return result;
}
-
+
/*************************************************************************
* Create an AuthorityItem - this is a sub-resource of Authority
* @param specifier either a CSID or one of the urn forms
String xmlPayload) {
uriInfo = new UriInfoWrapper(uriInfo);
Response result = null;
-
+
try {
PoxPayloadIn input = new PoxPayloadIn(xmlPayload);
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), input, resourceMap, uriInfo);
@PathParam("csid") String specifier,
@PathParam("transition") String transition) {
PoxPayloadOut result = null;
-
+
Specifier spec = Specifier.getSpecifier(specifier, "updateAuthority", "UPDATE");
String csid = null;
try {
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, csid);
}
-
+
return result.getBytes();
}
-
+
//FIXME: This method is almost identical to the method org.collectionspace.services.common.updateWorkflowWithTransition() so
// they should be consolidated -be DRY (D)on't (R)epeat (Y)ourself.
@PUT
@PathParam("transition") String transition) {
uriInfo = new UriInfoWrapper(uriInfo);
PoxPayloadOut result = null;
-
+
try {
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
- result = updateItemWorkflowWithTransition(ctx,
+ result = updateItemWorkflowWithTransition(ctx,
parentIdentifier, itemIdentifier, transition, AuthorityServiceUtils.UPDATE_REV);
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, parentIdentifier);
}
-
+
return result.getBytes();
}
-
+
/**
* Update an authority item's workflow state.
* @param existingContext
* @param itemcsid
* @param transition
* @return
- * @throws DocumentReferenceException
+ * @throws DocumentReferenceException
*/
public PoxPayloadOut updateItemWorkflowWithTransition(ServiceContext<PoxPayloadIn, PoxPayloadOut> existingContext,
String parentIdentifier,
String transition,
boolean updateRevNumber) throws DocumentReferenceException {
PoxPayloadOut result = null;
-
+
try {
//
// We need CSIDs for both the parent authority and the authority item
//
// Create an empty workflow_commons input part and set it into a new "workflow" sub-resource context
//
- PoxPayloadIn input = new PoxPayloadIn(WorkflowClient.SERVICE_PAYLOAD_NAME, new WorkflowCommon(),
+ PoxPayloadIn input = new PoxPayloadIn(WorkflowClient.SERVICE_PAYLOAD_NAME, new WorkflowCommon(),
WorkflowClient.SERVICE_COMMONPART_NAME);
MultipartServiceContext ctx = (MultipartServiceContext) createServiceContext(WorkflowClient.SERVICE_NAME, input);
if (existingContext != null && existingContext.getCurrentRepositorySession() != null) {
//
String targetWorkspaceName = targetCtx.getRepositoryWorkspaceName();
ctx.setRespositoryWorkspaceName(targetWorkspaceName); //find the document in the parent's workspace
-
+
// Get the type of transition we're being asked to make and store it as a context parameter -used by the workflow document handler
TransitionDef transitionDef = getTransitionDef(targetCtx, transition);
if (transitionDef == null) {
itemIdentifier, transition));
}
ctx.setProperty(WorkflowClient.TRANSITION_ID, transitionDef);
-
+
WorkflowDocumentModelHandler handler = createWorkflowDocumentHandler(ctx);
getRepositoryClient(ctx).update(ctx, itemCsid, handler);
result = ctx.getOutput();
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.UPDATE_FAILED + WorkflowClient.SERVICE_PAYLOAD_NAME, itemIdentifier);
}
-
+
return result;
}
-
+
protected PoxPayloadOut getAuthorityItem(
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx,
String parentIdentifier,
String itemIdentifier) throws Exception {
PoxPayloadOut result = null;
-
+
String parentcsid = lookupParentCSID(ctx, parentIdentifier, "getAuthorityItem(parent)", "GET_ITEM", null);
// We omit the parentShortId, only needed when doing a create...
DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler = createItemDocumentHandler(ctx, parentcsid, null);
handler.setDocumentFilter(myFilter);
getRepositoryClient(ctx).get(ctx, handler);
}
-
+
result = (PoxPayloadOut) ctx.getOutput();
if (result != null) {
String inAuthority = XmlTools.getElementValue(result.getDOMDocument(), "//" + AuthorityItemJAXBSchema.IN_AUTHORITY);
itemSpec.value, inAuthority, parentcsid));
}
}
-
+
return result;
}
String parentIdentifier,
String itemIdentifier) throws Exception {
PoxPayloadOut result = null;
-
+
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), existingCtx.getResourceMap(), existingCtx.getUriInfo());
if (existingCtx.getCurrentRepositorySession() != null) {
ctx.setCurrentRepositorySession(existingCtx.getCurrentRepositorySession()); // Reuse the current repo session if one exists
ctx.setProperties(existingCtx.getProperties());
}
result = getAuthorityItem(ctx, parentIdentifier, itemIdentifier);
-
+
return result;
}
-
+
/**
* Gets the authority item.
- *
+ *
* @param parentspecifier either a CSID or one of the urn forms
* @param itemspecifier either a CSID or one of the urn forms
- *
+ *
* @return the authority item
*/
@GET
public byte[] getAuthorityItem(
@Context Request request,
@Context UriInfo uriInfo,
- @Context ResourceMap resourceMap,
+ @Context ResourceMap resourceMap,
@PathParam("csid") String parentIdentifier,
@PathParam("itemcsid") String itemIdentifier) {
uriInfo = new UriInfoWrapper(uriInfo);
PoxPayloadOut result = null;
result = this.getAuthorityItemPayload(request, uriInfo, resourceMap, parentIdentifier, itemIdentifier);
-
+
return result.getBytes();
}
-
-
+
+
public PoxPayloadOut getAuthorityItemPayload(
@Context Request request,
@Context UriInfo uriInfo,
- @Context ResourceMap resourceMap,
+ @Context ResourceMap resourceMap,
@PathParam("csid") String parentIdentifier,
@PathParam("itemcsid") String itemIdentifier) {
uriInfo = new UriInfoWrapper(uriInfo);
PoxPayloadOut result = null;
try {
- RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx =
+ RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx =
(RemoteServiceContext<PoxPayloadIn, PoxPayloadOut>) createServiceContext(getItemServiceName(), resourceMap, uriInfo);
JaxRsContext jaxRsContext = new JaxRsContext(request, uriInfo); // Needed for getting account permissions part of the resource
ctx.setJaxRsContext(jaxRsContext);
-
+
result = getAuthorityItem(ctx, parentIdentifier, itemIdentifier);
} catch (DocumentNotFoundException dnf) {
throw bigReThrow(dnf, ServiceMessages.resourceNotFoundMsg(itemIdentifier));
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.GET_FAILED);
}
-
+
return result;
}
-
+
public Response getAuthorityItemResponse(
@Context Request request,
@Context UriInfo uriInfo,
- @Context ResourceMap resourceMap,
+ @Context ResourceMap resourceMap,
@PathParam("csid") String parentIdentifier,
@PathParam("itemcsid") String itemIdentifier) {
uriInfo = new UriInfoWrapper(uriInfo);
PoxPayloadOut payloadout = null;
RemoteServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = null;
-
+
try {
ctx = (RemoteServiceContext<PoxPayloadIn, PoxPayloadOut>) createServiceContext(getItemServiceName(), resourceMap, uriInfo);
JaxRsContext jaxRsContext = new JaxRsContext(request, uriInfo); // Needed for getting account permissions part of the resource
ctx.setJaxRsContext(jaxRsContext);
-
+
payloadout = getAuthorityItem(ctx, parentIdentifier, itemIdentifier);
} catch (DocumentNotFoundException dnf) {
throw bigReThrow(dnf, ServiceMessages.resourceNotFoundMsg(itemIdentifier));
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.GET_FAILED);
}
-
+
return buildResponse(ctx, payloadout);
}
return result;
}
-
+
@Override
protected String getPartialTermMatchField(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
String result = null;
-
+
result = NuxeoUtils.getMultiElPathPropertyName(
authorityItemCommonSchemaName, getItemTermInfoGroupXPathBase(),
AuthorityItemJAXBSchema.TERM_DISPLAY_NAME);
return result;
}
-
+
/**
* Gets the authorityItem list for the specified authority
* If partialPerm is specified, keywords will be ignored.
- *
+ *
* @param authorityIdentifier either a CSID or one of the urn forms
* @param partialTerm if non-null, matches partial terms
* @param keywords if non-null, matches terms in the keyword index for items
String authorityIdentifier,
UriInfo uriInfo) throws Exception {
AbstractCommonList result = null;
-
+
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
if (existingContext != null && existingContext.getCurrentRepositorySession() != null) { // Merge some of the existing context properties with our new context
ctx.setCurrentRepositorySession(existingContext.getCurrentRepositorySession());
ctx.setProperties(existingContext.getProperties());
}
-
+
String orderBy = queryParams.getFirst(IClientQueryParams.ORDER_BY_PARAM);
String termStatus = queryParams.getFirst(SEARCH_TYPE_TERMSTATUS);
String keywords = queryParams.getFirst(IQueryManager.SEARCH_TYPE_KEYWORDS_KW);
lookupParentCSID(ctx, authorityIdentifier, "getAuthorityItemList", "LIST", uriInfo);
DocumentHandler<?, AbstractCommonList, DocumentModel, DocumentModelList> handler =
createItemDocumentHandler(ctx, parentcsid, null);
-
+
DocumentFilter myFilter = handler.getDocumentFilter();
// If we are not wildcarding the parent, add a restriction
if (parentcsid != null) {
myFilter.appendWhereClause(tsClause, IQueryManager.SEARCH_QUALIFIER_AND);
}
- result = search(ctx, handler, uriInfo, orderBy, keywords, advancedSearch, partialTerm);
-
+ result = search(ctx, handler, uriInfo, orderBy, keywords, advancedSearch, partialTerm);
+
return result;
}
-
+
/**
* Gets the authorityItem list for the specified authority
* If partialPerm is specified, keywords will be ignored.
- *
+ *
* @param authorityIdentifier either a CSID or one of the urn forms
* @param partialTerm if non-null, matches partial terms
* @param keywords if non-null, matches terms in the keyword index for items
* @param ui passed to include additional parameters, like pagination controls
- *
+ *
* @return the authorityItem list
*/
@GET
@Context UriInfo uriInfo) {
uriInfo = new UriInfoWrapper(uriInfo);
AbstractCommonList result = null;
-
+
try {
- result = getAuthorityItemList(NULL_CONTEXT, authorityIdentifier, uriInfo);
+ result = getAuthorityItemList(NULL_CONTEXT, authorityIdentifier, uriInfo);
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.LIST_FAILED);
}
-
+
return result;
}
protected String getRefPropName() {
return ServiceBindingUtils.AUTH_REF_PROP;
}
-
+
/**
* Gets the entities referencing this Authority item instance. The service type
* can be passed as a query param "type", and must match a configured type
* @param parentspecifier either a CSID or one of the urn forms
* @param itemspecifier either a CSID or one of the urn forms
* @param ui the ui
- *
+ *
* @return the info for the referencing objects
*/
@GET
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.GET_FAILED);
}
-
+
if (authRefDocList == null) {
Response response = Response.status(Response.Status.NOT_FOUND).entity(
"Get failed, the requested Item CSID:" + itemSpecifier + ": was not found.").type(
}
return authRefDocList;
}
-
+
public AuthorityRefDocList getReferencingObjects(
ServiceContext<PoxPayloadIn, PoxPayloadOut> existingContext,
String parentspecifier,
boolean useDefaultOrderByClause,
boolean computeTotal) throws Exception {
AuthorityRefDocList authRefDocList = null;
-
+
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
MultivaluedMap<String, String> queryParams = ctx.getQueryParams();
//
String parentcsid = lookupParentCSID(ctx, parentspecifier, "getReferencingObjects(parent)", "GET_ITEM_REF_OBJS", uriInfo);
String itemcsid = lookupItemCSID(ctx, itemspecifier, parentcsid, "getReferencingObjects(item)", "GET_ITEM_REF_OBJS");
-
+
// Remove the "type" property from the query params
- List<String> serviceTypes = queryParams.remove(ServiceBindingUtils.SERVICE_TYPE_PROP);
+ List<String> serviceTypes = queryParams.remove(ServiceBindingUtils.SERVICE_TYPE_PROP);
if (serviceTypes == null || serviceTypes.isEmpty()) {
serviceTypes = ServiceBindingUtils.getCommonServiceTypes(true); //CSPACE-5359: Should now include objects, procedures, and authorities
}
-
+
AuthorityItemDocumentModelHandler handler = (AuthorityItemDocumentModelHandler)createItemDocumentHandler(ctx, parentcsid, null);
authRefDocList = handler.getReferencingObjects(ctx, serviceTypes, getRefPropName(), itemcsid, pageNum, pageSize, useDefaultOrderByClause, computeTotal);
@Context UriInfo uriInfo) {
uriInfo = new UriInfoWrapper(uriInfo);
AuthorityRefList authRefList = null;
-
+
try {
// Note that we have to create the service context for the Items, not the main service
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.GET_FAILED + " parentspecifier: " + parentspecifier + " itemspecifier:" + itemspecifier);
}
-
+
return authRefList;
}
-
+
/**
* Synchronizes a local authority item with a share authority server (SAS) item.
* @param ctx
if (neededSync == true) {
result = (PoxPayloadOut) ctx.getOutput();
}
-
+
return result;
}
boolean syncHierarchicalRelationships
) throws Exception {
PoxPayloadOut result = null;
-
+
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(),
existingCtx.getResourceMap(),
existingCtx.getUriInfo());
if (existingCtx.getCurrentRepositorySession() != null) {
ctx.setCurrentRepositorySession(existingCtx.getCurrentRepositorySession());
-
+
}
result = synchronizeItem(ctx, parentIdentifier, itemIdentifier, syncHierarchicalRelationships);
-
+
return result;
}
-
+
/**
* Synchronizes an authority item and with a Shared Authority Server (SAS) item.
- *
+ *
* @param specifier either CSIDs and/or one of the urn forms
- *
+ *
* @return the authority item if it was updated/synchronized with SAS item; otherwise empty
*/
@POST
byte[] result;
boolean neededSync = false;
PoxPayloadOut payloadOut = null;
-
+
try {
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), null, resourceMap, uriInfo);
payloadOut = this.synchronizeItem(ctx, parentIdentifier, itemIdentifier, true);
Response response = Response.status(Response.Status.NOT_MODIFIED).entity(result).type("text/plain").build();
throw new CSWebApplicationException(response);
}
-
+
return result;
}
-
+
/**
* Update authorityItem.
- *
+ *
* @param parentspecifier either a CSID or one of the urn forms
* @param itemspecifier either a CSID or one of the urn forms
*
@PUT
@Path("{csid}/items/{itemcsid}")
public byte[] updateAuthorityItem(
- @Context ResourceMap resourceMap,
+ @Context ResourceMap resourceMap,
@Context UriInfo uriInfo,
@PathParam("csid") String parentSpecifier,
@PathParam("itemcsid") String itemSpecifier,
String xmlPayload) {
uriInfo = new UriInfoWrapper(uriInfo);
PoxPayloadOut result = null;
-
+
try {
PoxPayloadIn theUpdate = new PoxPayloadIn(xmlPayload);
result = updateAuthorityItem(null, resourceMap, uriInfo, parentSpecifier, itemSpecifier, theUpdate,
} catch (Exception e) {
throw bigReThrow(e, ServiceMessages.UPDATE_FAILED);
}
-
+
return result.getBytes();
}
-
+
/**
* Delete authorityItem.
- *
+ *
* @param parentIdentifier the parentcsid
* @param itemIdentifier the itemcsid
- *
+ *
* @return the response
*/
@DELETE
if (logger.isDebugEnabled()) {
logger.debug("deleteAuthorityItem with parentcsid=" + parentIdentifier + " and itemcsid=" + itemIdentifier);
}
-
+
try {
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
deleteAuthorityItem(ctx, parentIdentifier, itemIdentifier, AuthorityServiceUtils.UPDATE_REV);
}
/**
- *
+ *
* @param existingCtx
* @param parentIdentifier
* @param itemIdentifier
boolean shouldUpdateRevNumber
) throws Exception {
boolean result = true;
-
+
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), existingCtx.getUriInfo());
if (existingCtx != null && existingCtx.getCurrentRepositorySession() != null) {
ctx.setCurrentRepositorySession(existingCtx.getCurrentRepositorySession());
ctx.setProperties(existingCtx.getProperties());
}
-
+
String parentcsid = null;
try {
parentcsid = lookupParentCSID(ctx, parentIdentifier, "deleteAuthorityItem(parent)", "DELETE_ITEM", null);
throw de;
}
String itemCsid = lookupItemCSID(ctx, itemIdentifier, parentcsid, "deleteAuthorityItem(item)", "DELETE_ITEM"); //use itemServiceCtx if it is not null
-
+
AuthorityItemDocumentModelHandler handler = (AuthorityItemDocumentModelHandler) createDocumentHandler(ctx);
handler.setShouldUpdateRevNumber(shouldUpdateRevNumber);
result = getRepositoryClient(ctx).delete(ctx, itemCsid, handler);
-
+
return result;
}
String calledUri = uriInfo.getPath();
String uri = "/" + calledUri.substring(0, (calledUri.length() - ("/" + hierarchy).length()));
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = createServiceContext(getItemServiceName(), uriInfo);
-
+
String parentcsid = lookupParentCSID(ctx, parentIdentifier, "deleteAuthorityItem(parent)", "DELETE_ITEM", null);
String itemcsid = lookupItemCSID(ctx, itemIdentifier, parentcsid, "deleteAuthorityItem(item)", "DELETE_ITEM"); //use itemServiceCtx if it is not null
-
+
String direction = uriInfo.getQueryParameters().getFirst(Hierarchy.directionQP);
if (Tools.notBlank(direction) && Hierarchy.direction_parents.equals(direction)) {
result = Hierarchy.surface(ctx, itemcsid, uri);
} else {
result = Hierarchy.dive(ctx, itemcsid, uri);
- }
+ }
} catch (Exception e) {
throw bigReThrow(e, "Error showing hierarchy for authority item: ", itemIdentifier);
}
-
+
return result;
}
-
+
/**
- *
+ *
* @param tenantId
* @return
*/
public String getItemDocType(String tenantId) {
return getDocType(tenantId, getItemServiceName());
}
-
+
/**
* Returns a UriRegistry entry: a map of tenant-qualified URI templates
* for the current resource, for all tenants
- *
+ *
* @return a map of URI templates for the current resource, for all tenants
*/
@Override
}
return uriRegistryEntriesMap;
}
-
+
/**
- *
+ *
*/
@Override
public ServiceDescription getDescription(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx) {
public Response createAuthority(String xmlPayload) {
return this.createAuthority(null, null, xmlPayload);
}
-
+
protected String getCsid(ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx, Specifier specifier) throws Exception {
String csid;
-
+
if (ctx == null) {
ctx = createServiceContext(getServiceName());
}
-
+
if (specifier.form == SpecifierForm.CSID) {
csid = specifier.value;
} else {
String whereClause = RefNameServiceUtils.buildWhereForAuthByName(authorityCommonSchemaName, specifier.value);
csid = getRepositoryClient(ctx).findDocCSID(null, ctx, whereClause);
}
-
+
return csid;
}
-
+
}
*/
package org.collectionspace.services.common.vocabulary.nuxeo;
-import java.util.ArrayList;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import org.collectionspace.services.client.PayloadInputPart;
import org.collectionspace.services.client.PoxPayloadIn;
import org.collectionspace.services.client.PoxPayloadOut;
+import org.collectionspace.services.client.RelationClient;
import org.collectionspace.services.client.XmlTools;
import org.collectionspace.services.client.workflow.WorkflowClient;
import org.collectionspace.services.common.ResourceMap;
import org.collectionspace.services.nuxeo.client.java.CoreSessionInterface;
import org.collectionspace.services.nuxeo.client.java.NuxeoRepositoryClientImpl;
import org.collectionspace.services.nuxeo.util.NuxeoUtils;
+import org.collectionspace.services.relation.RelationsCommonList;
import org.dom4j.Element;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
//
DocumentModel docModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), authorityCommonSchemaName, specifier);
if (docModel != null) {
+ String authorityCsid = docModel.getName();
Long localRev = (Long) NuxeoUtils.getProperyValue(docModel, AuthorityJAXBSchema.REV);
String shortId = (String) NuxeoUtils.getProperyValue(docModel, AuthorityJAXBSchema.SHORT_IDENTIFIER);
String remoteClientConfigName = (String) NuxeoUtils.getProperyValue(docModel, AuthorityJAXBSchema.REMOTECLIENT_CONFIG_NAME); // If set, contains the name of the remote client configuration (remoteClientConfigName) from the tenant bindings
//
// First, sync all the authority items
//
- syncAllItems(ctx, sasSpecifier); // FIXME: We probably want to consider "paging" this instead of handling the entire set of items.
+ syncAllItems(ctx, authorityCsid, sasSpecifier); // FIXME: We probably want to consider "paging" this instead of handling the entire set of items.
//
// Next, sync the authority resource/record itself
//
//
String workflowState = docModel.getCurrentLifeCycleState();
if (workflowState.contains(WorkflowClient.WORKFLOWSTATE_REPLICATED) == false) {
- String authorityCsid = docModel.getName();
authorityResource.updateWorkflowWithTransition(ctx, ctx.getUriInfo(), authorityCsid, WorkflowClient.WORKFLOWTRANSITION_REPLICATE);
}
}
}
/*
- * Get the list of authority items from the remote shared authority server (SAS) and try
- * to synchronize them with the local items. If items exist on the remote but not the local, we'll create them.
+ * Get the list of authority items from the remote shared authority server (SAS) and
+ * synchronize them with the local authority items. If items exist on the remote but not the local,
+ * create them.
*/
- protected int syncAllItems(ServiceContext ctx, Specifier sasAuthoritySpecifier) throws Exception {
- int result = -1;
- int created = 0;
- int synched = 0;
- int alreadySynched = 0;
- int deprecated = 0;
- int totalItemsProcessed = 0;
- ArrayList<String> itemsInRemoteAuthority = new ArrayList<String>();
- //
- // Iterate over the list of items/terms in the remote authority
- //
+ protected void syncAllItems(ServiceContext ctx, String parentCsid, Specifier sasAuthoritySpecifier) throws Exception {
+ int createdCount = 0;
+ int syncedCount = 0;
+ int alreadySyncedCount = 0;
+ int deletedCount = 0;
+ int totalProcessedCount = 0;
+
+ Set<String> remoteShortIds = new HashSet<String>();
+
+ // Iterate over the list of items in the remote authority.
+
PoxPayloadIn itemListPayload = requestItemList(ctx, sasAuthoritySpecifier);
- List<Element> itemList = getItemList(itemListPayload);
- if (itemList != null) {
- for (Element e:itemList) {
+ List<Element> itemElements = getItemList(itemListPayload);
+
+ if (itemElements != null) {
+ for (Element e : itemElements) {
String remoteRefName = XmlTools.getElementValue(e, AuthorityItemJAXBSchema.REF_NAME);
- itemsInRemoteAuthority.add(XmlTools.getElementValue(e, AuthorityItemJAXBSchema.SHORT_IDENTIFIER));
- long status = syncRemoteItemWithLocalItem(ctx, remoteRefName);
+ String remoteShortId = XmlTools.getElementValue(e, AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
+
+ remoteShortIds.add(remoteShortId);
+
+ long status = syncRemoteItem(ctx, parentCsid, remoteRefName);
+
if (status == 1) {
- created++;
+ createdCount++;
} else if (status == 0) {
- synched++;
+ syncedCount++;
} else {
- alreadySynched++;
+ alreadySyncedCount++;
}
- totalItemsProcessed++;
+
+ totalProcessedCount++;
}
}
- //
- // Now see if we need to deprecate or delete items that have been hard-deleted from the SAS but still exist
- // locally. Subtract (remove) the list of remote items from the list of local items to determine which
+
+ // Deprecate or delete items that have been hard-deleted from the SAS but still exist locally.
+ // Subtract (remove) the set of remote items from the set of local items to determine which
// of the remote items have been hard deleted.
- //
- ArrayList<String> itemsInLocalAuthority = getItemsInLocalAuthority(ctx, sasAuthoritySpecifier);
- itemsInLocalAuthority.removeAll(itemsInRemoteAuthority);
- if (itemsInLocalAuthority.size() > 0) {
- ArrayList<String> remainingItems = itemsInLocalAuthority; // now a subset of local items that no longer exist on the SAS, so we need to try to delete them (or mark them as deprecated if they still have records referencing them)
- //
- // We now need to either hard-delete or deprecate the remaining authorities
- //
- long processed = deleteOrDeprecateItems(ctx, sasAuthoritySpecifier, remainingItems);
- if (processed != remainingItems.size()) {
- throw new Exception("Encountered unexpected exception trying to delete or deprecated authority items during synchronization.");
- }
- }
- //
- // Now that we've sync'd all the items, we need to synchronize the hierarchy relationships
- //
- for (String itemShortId:itemsInRemoteAuthority) {
- long status = syncRemoteItemRelationshipsWithLocalItem(ctx, sasAuthoritySpecifier, itemShortId);
- if (status == 1) {
- created++;
- } else if (status == 0) {
- synched++;
- } else {
- alreadySynched++;
+
+ Set<String> localShortIds = getItemsInLocalAuthority(ctx, sasAuthoritySpecifier);
+
+ localShortIds.removeAll(remoteShortIds);
+
+ if (localShortIds.size() > 0) {
+ // Delete the remaining items (or mark them as deprecated if they still have records referencing them).
+
+ deletedCount = deleteOrDeprecateItems(ctx, sasAuthoritySpecifier, localShortIds);
+
+ if (deletedCount != localShortIds.size()) {
+ throw new Exception("Error deleting or deprecating authority items during synchronization.");
}
- totalItemsProcessed++;
}
- logger.info(String.format("Total number of items processed during sync: %d", totalItemsProcessed));
- logger.info(String.format("Number of items synchronized: %d", synched));
- logger.info(String.format("Number of items created during sync: %d", created));
- logger.info(String.format("Number not needing synchronization: %d", alreadySynched));
-
- return result;
+ logger.info(String.format("Total number of items processed during sync: %d", totalProcessedCount));
+ logger.info(String.format("Number of items synchronized: %d", syncedCount));
+ logger.info(String.format("Number of items created during sync: %d", createdCount));
+ logger.info(String.format("Number of items not needing synchronization: %d", alreadySyncedCount));
+ logger.info(String.format("Number of items hard deleted on remote: %d", deletedCount));
}
/**
* @return
* @throws Exception
*/
- private long deleteOrDeprecateItems(ServiceContext ctx, Specifier authoritySpecifier, ArrayList<String> itemShortIdList) throws Exception {
- long result = 0;
+ private int deleteOrDeprecateItems(ServiceContext ctx, Specifier authoritySpecifier, Set<String> itemShortIds) throws Exception {
+ int result = 0;
AuthorityItemSpecifier authorityItemSpecificer = null;
ctx.setProperty(AuthorityServiceUtils.SHOULD_UPDATE_REV_PROPERTY, false); // Don't update the revision number when we delete or deprecate the item
- for (String itemShortId:itemShortIdList) {
+ for (String itemShortId:itemShortIds) {
AuthorityResource authorityResource = (AuthorityResource) ctx.getResource();
try {
authorityItemSpecificer = new AuthorityItemSpecifier(SpecifierForm.URN_NAME, authoritySpecifier.value,
}
if (logger.isWarnEnabled() == true) {
- if (result != itemShortIdList.size()) {
+ if (result != itemShortIds.size()) {
logger.warn(String.format("Unable to delete or deprecate some authority items during synchronization with SAS. Deleted or deprecated %d of %d. See the services log file for details.",
- result, itemShortIdList.size()));
+ result, itemShortIds.size()));
}
}
}
/**
- * Gets the list of SAS related items in the local authority. We exlude items with the "proposed" flags because
- * we want a list with only SAS created items.
+ * Gets the list of SAS related items in the local authority. Exludes items with the "proposed" flag to
+ * include only SAS created items.
*
- * We need to add pagination support to this call!!!
+ * FIXME: Add pagination support to this call!!!
*
* @param ctx
* @param authoritySpecifier
* @return
* @throws Exception
*/
- private ArrayList<String> getItemsInLocalAuthority(ServiceContext ctx, Specifier authoritySpecifier) throws Exception {
- ArrayList<String> result = new ArrayList<String>();
+ private Set<String> getItemsInLocalAuthority(ServiceContext ctx, Specifier authoritySpecifier) throws Exception {
+ Set<String> result = new HashSet<String>();
ResourceMap resourceMap = ctx.getResourceMap();
String resourceName = ctx.getClient().getServiceName();
AuthorityResource authorityResource = (AuthorityResource) resourceMap.get(resourceName);
AbstractCommonList acl = authorityResource.getAuthorityItemList(ctx, authoritySpecifier.getURNValue(), ctx.getUriInfo());
- List<ListItem> listItemList = acl.getListItem();
- for (ListItem listItem:listItemList) {
+ List<ListItem> listItems = acl.getListItem();
+
+ for (ListItem listItem : listItems) {
Boolean proposed = getBooleanValue(listItem, AuthorityItemJAXBSchema.PROPOSED);
+
if (proposed == false) { // exclude "proposed" (i.e., local-only items)
result.add(AbstractCommonListUtils.ListItemGetElementValue(listItem, AuthorityItemJAXBSchema.SHORT_IDENTIFIER));
}
* @param itemIdentifier - Must be in short-id-refname form -i.e., urn:cspace:name(shortid)
* @throws Exception
*/
- protected void createLocalItem(ServiceContext ctx, String parentIdentifier, String itemIdentifier, Boolean syncHierarchicalRelationships) throws Exception {
+ protected void createLocalItem(ServiceContext ctx, String parentCsid, String parentIdentifier, String itemIdentifier, Boolean syncHierarchicalRelationships) throws Exception {
//
// Create a URN short ID specifier for the getting a copy of the remote authority item
//
- Specifier authoritySpecifier = Specifier.getSpecifier(parentIdentifier);
+ Specifier parentSpecifier = Specifier.getSpecifier(parentIdentifier);
Specifier itemSpecifier = Specifier.getSpecifier(itemIdentifier);
- AuthorityItemSpecifier sasAuthorityItemSpecifier = new AuthorityItemSpecifier(authoritySpecifier, itemSpecifier);
+ AuthorityItemSpecifier sasAuthorityItemSpecifier = new AuthorityItemSpecifier(parentSpecifier, itemSpecifier);
//
// Get the remote client configuration name
//
- DocumentModel docModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), authorityCommonSchemaName, authoritySpecifier);
+ DocumentModel docModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), authorityCommonSchemaName, parentSpecifier);
String remoteClientConfigName = (String) NuxeoUtils.getProperyValue(docModel, AuthorityJAXBSchema.REMOTECLIENT_CONFIG_NAME); // If set, contains the name of the remote client configuration (remoteClientConfigName) from the tenant bindings
//
// Get the remote payload
//
PoxPayloadIn sasPayloadIn = AuthorityServiceUtils.requestPayloadInFromRemoteServer(sasAuthorityItemSpecifier, remoteClientConfigName,
ctx.getServiceName(), getEntityResponseType(), syncHierarchicalRelationships);
- sasPayloadIn = AuthorityServiceUtils.localizeRefNameDomains(ctx, sasPayloadIn); // We need to filter domain name part of any and all refnames in the payload
+
+ AuthorityResource authorityResource = (AuthorityResource) ctx.getResource();
+
+ // Remove remote uris and csids from relations, and remove relations to items that don't exist locally.
+ sasPayloadIn = AuthorityServiceUtils.localizeRelations(ctx, authorityResource, parentCsid, itemSpecifier, sasPayloadIn);
+
+ // Localize domain name parts of refnames in the payload.
+ sasPayloadIn = AuthorityServiceUtils.localizeRefNameDomains(ctx, sasPayloadIn);
+
//
// Using the payload from the remote server, create a local copy of the item
//
- AuthorityResource authorityResource = (AuthorityResource) ctx.getResource();
- Response response = authorityResource.createAuthorityItemWithParentContext(ctx, authoritySpecifier.getURNValue(),
+ Response response = authorityResource.createAuthorityItemWithParentContext(ctx, parentSpecifier.getURNValue(),
sasPayloadIn, AuthorityServiceUtils.DONT_UPDATE_REV, AuthorityServiceUtils.NOT_PROPOSED, AuthorityServiceUtils.SAS_ITEM);
//
// Check the response for successful POST result
}
/**
- * Try to synchronize a remote item (using its refName) with a local item. If the local doesn't yet
- * exist, we'll create it.
+ * Synchronize a remote item (using its refName) with a local item. If the local doesn't yet
+ * exist, create it.
* Result values:
* -1 = sync not needed; i.e., already in sync
* 0 = sync succeeded
* @return
* @throws Exception
*/
- protected long syncRemoteItemWithLocalItem(ServiceContext ctx, String itemRefName) throws Exception {
- long result = -1;
- //
- // Using the item refname (with no local CSID), create specifiers that we'll use to find the local versions
- //
+ protected long syncRemoteItem(ServiceContext ctx, String parentCsid, String itemRefName) throws Exception {
+ if (logger.isInfoEnabled()) {
+ logger.info(String.format("Syncing remote item %s", itemRefName));
+ }
+
+ // Create specifiers to find the local item corresponding to the remote refname.
+
AuthorityTermInfo authorityTermInfo = RefNameUtils.parseAuthorityTermInfo(itemRefName);
String parentIdentifier = Specifier.createShortIdURNValue(authorityTermInfo.inAuthority.name);
String itemIdentifier = Specifier.createShortIdURNValue(authorityTermInfo.name);
- //
- // We'll use the Authority JAX-RS resource to peform sync operations (creates and updates)
- //
+
+ // Use the Authority JAX-RS resource to peform sync operations (creates and updates).
+
AuthorityResource authorityResource = (AuthorityResource) ctx.getResource();
- PoxPayloadOut localItemPayloadOut;
+ PoxPayloadOut localItemPayload = null;
+
+ // Find the local item.
+
try {
- localItemPayloadOut = authorityResource.getAuthorityItemWithExistingContext(ctx, parentIdentifier, itemIdentifier);
+ localItemPayload = authorityResource.getAuthorityItemWithExistingContext(ctx, parentIdentifier, itemIdentifier);
} catch (DocumentNotFoundException dnf) {
- //
- // Document not found, means we need to create an item/term that exists only on the SAS
- //
- logger.info(String.format("Remote item with refname='%s' doesn't exist locally, so we'll create it.", itemRefName));
- createLocalItem(ctx, parentIdentifier, itemIdentifier, AuthorityClient.DONT_INCLUDE_RELATIONS);
- return 1; // exit with status of 1 means we created a new authority item
- }
- //
- // If we get here, we know the item exists both locally and remotely, so we need to synchronize them.
- //
- //
- try {
- PoxPayloadOut theUpdate = authorityResource.synchronizeItemWithExistingContext(ctx, parentIdentifier, itemIdentifier, false);
- if (theUpdate != null) {
- result = 0; // means we needed to sync this item with SAS
- logger.debug(String.format("Synced authority item %s in authority %s",
- itemIdentifier, parentIdentifier));
- }
- } catch (DocumentReferenceException de) { // Exception for items that still have records/resource referencing them.
- result = -1;
- logger.error(String.format("Could not sync authority item = '%s' because it has existing records referencing it.",
- itemIdentifier));
+ localItemPayload = null;
}
- return result; // -1 = no sync needed/possible, 0 = sync'd, 1 = created new item
- }
+ // If no local item exists, create one.
- /**
- * Ensure the local items relationships look the same as the remote items' by synchronizing the hierarchy relationship records
- * of the SAS item with the local item.
- *
- * @param ctx
- * @param refName
- * @return
- * @throws Exception
- */
- protected long syncRemoteItemRelationshipsWithLocalItem(ServiceContext ctx, Specifier authoritySpecifier, String itemShortId) throws Exception {
- long result = -1;
+ if (localItemPayload == null) {
+ createLocalItem(ctx, parentCsid, parentIdentifier, itemIdentifier, AuthorityClient.INCLUDE_RELATIONS);
- String parentIdentifier = authoritySpecifier.getURNValue();
- String itemIdentifier = Specifier.createShortIdURNValue(itemShortId);
- //
- // We'll use the Authority JAX-RS resource to peform sync operations (creates and updates)
- //
- AuthorityResource authorityResource = (AuthorityResource) ctx.getResource();
- PoxPayloadOut localItemPayloadOut;
- try {
- MultivaluedMap queryParams = ctx.getQueryParams();
- localItemPayloadOut = authorityResource.getAuthorityItemWithExistingContext(ctx, parentIdentifier, itemIdentifier);
- } catch (DocumentNotFoundException dnf) {
- //
- // Document not found, means we need to create an item/term that exists only on the SAS
- //
- logger.info(String.format("Remote item with short ID ='%s' doesn't exist locally, so we can't synchronize its relationships.", itemShortId));
- return result;
+ return 1;
}
- //
- // If we get here, we know the item exists both locally and remotely, so we need to synchronize the hierarchy relationships.
- //
- //
+
+ // Sync the local item with the remote item.
+
+ PoxPayloadOut updatePayload = null;
+
try {
- PoxPayloadOut theUpdate = authorityResource.synchronizeItemWithExistingContext(ctx, parentIdentifier, itemIdentifier, AuthorityClient.INCLUDE_RELATIONS);
- if (theUpdate != null) {
- result = 0; // means we needed to sync this item with SAS
- logger.debug(String.format("Synced authority item %s in authority %s",
- itemIdentifier, parentIdentifier));
- }
- } catch (DocumentReferenceException de) { // Exception for items that still have records/resource referencing them.
- result = -1;
- logger.error(String.format("Could not sync authority item = '%s' because it has existing records referencing it.",
- itemIdentifier));
+ updatePayload = authorityResource.synchronizeItemWithExistingContext(ctx, parentIdentifier, itemIdentifier, AuthorityClient.INCLUDE_RELATIONS);
+ } catch (DocumentReferenceException de) {
+ logger.error(String.format("Could not sync item %s because it is referenced by other records", itemIdentifier));
}
- return result; // -1 = no sync needed/possible, 0 = sync'd, 1 = created new item
+ if (updatePayload != null) {
+ logger.info(String.format("Synced item %s in authority %s", itemIdentifier, parentIdentifier));
+ return 0;
+ }
+ return -1;
}
private void assertStatusCode(Response res, Specifier specifier, AuthorityClient client) throws Exception {