import org.collectionspace.services.common.query.QueryManager;
import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityDocumentModelHandler;
import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityItemDocumentModelHandler;
-import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityServiceUtils;
import org.collectionspace.services.common.workflow.service.nuxeo.WorkflowDocumentModelHandler;
import org.collectionspace.services.config.ClientType;
import org.collectionspace.services.jaxb.AbstractCommonList;
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);
}
return result;
}
-
- /**
- * We override the base method, so we can decide if we need to update the rev number. We won't
- * if we are performing a synchronization with the SAS.
- * @param csid
- * @param theUpdate
- * @param ctx
- * @return
- * @throws Exception
- */
- @Override
- protected PoxPayloadOut update(String csid,
- PoxPayloadIn theUpdate, // not used in this method, but could be used by an overriding method
- ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
- throws Exception {
- AuthorityDocumentModelHandler handler = (AuthorityDocumentModelHandler) createDocumentHandler(ctx);
- handler.setShouldUpdateRevNumber(this.shouldUpdateRevNumber(ctx));
- getRepositoryClient(ctx).update(ctx, csid, handler);
- return ctx.getOutput();
- }
-
- /**
- * Look for a property in the current context to determine if we're handling a sync request.
- * @param ctx
- * @return
- */
- private boolean shouldUpdateRevNumber(ServiceContext ctx) {
- boolean result = true;
-
- Boolean flag = (Boolean) ctx.getProperty(AuthorityServiceUtils.SHOULD_UPDATE_REV_PROPERTY);
- if (flag != null) {
- result = flag.booleanValue();
- }
-
- return result;
- }
-
+
/**
* Gets the authority.
*
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.
+ */
+ @Override
+ protected PoxPayloadOut update(String csid,
+ PoxPayloadIn theUpdate, // not used in this method, but could be used by an overriding method
+ ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx)
+ throws Exception {
+ AuthorityDocumentModelHandler handler = (AuthorityDocumentModelHandler) createDocumentHandler(ctx);
+ Boolean shouldUpdateRev = (Boolean) ctx.getProperty(AuthorityServiceUtils.SHOULD_UPDATE_REV_PROPERTY);
+ if (shouldUpdateRev != null && shouldUpdateRev == true) {
+ handler.setShouldUpdateRevNumber(true);
+ }
+ getRepositoryClient(ctx).update(ctx, csid, handler);
+ return ctx.getOutput();
+ }
+
/**
* Update authority.
*
-package org.collectionspace.services.common.vocabulary.nuxeo;
+package org.collectionspace.services.common.vocabulary;
import javax.ws.rs.core.Response;
import org.collectionspace.services.common.context.MultipartServiceContextImpl;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
+import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
+import org.collectionspace.services.common.vocabulary.nuxeo.AuthorityIdentifierUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public static final boolean DONT_UPDATE_REV = false;
public static final boolean UPDATE_REV = true;
- static public PoxPayloadIn getPayloadIn(AuthorityItemSpecifier specifier, String serviceName, Class responseType) throws Exception {
+ static public PoxPayloadIn requestPayloadIn(ServiceContext ctx, Specifier specifier, Class responseType) throws Exception {
+ PoxPayloadIn result = null;
+
+ AuthorityClient client = (AuthorityClient) ctx.getClient();
+ Response res = client.read(specifier.value);
+ try {
+ int statusCode = res.getStatus();
+
+ // Check the status code of the response: does it match
+ // the expected response(s)?
+ if (logger.isDebugEnabled()) {
+ logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
+ }
+
+ result = new PoxPayloadIn((String)res.readEntity(responseType)); // Get the entire response!
+ } finally {
+ res.close();
+ }
+
+ return result;
+ }
+
+ static public PoxPayloadIn requestPayloadIn(AuthorityItemSpecifier specifier, String serviceName, Class responseType) throws Exception {
PoxPayloadIn result = null;
ServiceContext parentCtx = new MultipartServiceContextImpl(serviceName);
import org.collectionspace.services.common.api.Tools;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.document.DocumentException;
+import org.collectionspace.services.common.document.DocumentHandler;
import org.collectionspace.services.common.document.DocumentNotFoundException;
import org.collectionspace.services.common.document.DocumentWrapper;
import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
import org.collectionspace.services.common.vocabulary.AuthorityResource;
+import org.collectionspace.services.common.vocabulary.AuthorityServiceUtils;
import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.SpecifierForm;
protected String authorityCommonSchemaName;
protected String authorityItemCommonSchemaName;
- protected boolean shouldUpdateRevNumber;
+ protected boolean shouldUpdateRevNumber = true; // default to updating the revision number
public AuthorityDocumentModelHandler(String authorityCommonSchemaName, String authorityItemCommonSchemaName) {
this.authorityCommonSchemaName = authorityCommonSchemaName;
public Class<String> getEntityResponseType() {
return String.class;
}
-
+
+ @Override
+ public void prepareSync() throws Exception {
+ this.setShouldUpdateRevNumber(AuthorityServiceUtils.DONT_UPDATE_REV); // Never update rev nums on sync operations
+ }
+
protected PayloadInputPart extractPart(Response res, String partLabel)
throws Exception {
PoxPayloadIn input = new PoxPayloadIn((String)res.readEntity(getEntityResponseType()));
// Get the rev number of the authority so we can compare with rev number of shared authority
//
DocumentModel docModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), authorityCommonSchemaName, specifier);
- Long rev = (Long) NuxeoUtils.getProperyValue(docModel, AuthorityJAXBSchema.REV);
+ Long localRev = (Long) NuxeoUtils.getProperyValue(docModel, AuthorityJAXBSchema.REV);
String shortId = (String) NuxeoUtils.getProperyValue(docModel, AuthorityJAXBSchema.SHORT_IDENTIFIER);
- String refName = (String) NuxeoUtils.getProperyValue(docModel, AuthorityJAXBSchema.REF_NAME);
//
// Using the short ID of the local authority, create a URN specifier to retrieve the SAS authority
//
Specifier sasSpecifier = new Specifier(SpecifierForm.URN_NAME, RefNameUtils.createShortIdRefName(shortId));
- PoxPayloadIn sasPayloadIn = getPayloadIn(ctx, sasSpecifier);
-
+ PoxPayloadIn sasPayloadIn = AuthorityServiceUtils.requestPayloadIn(ctx, sasSpecifier, getEntityResponseType());
+ //
+ // If the authority on the SAS is newer, synch all the items and then the authority record as well
+ //
Long sasRev = getRevision(sasPayloadIn);
- if (sasRev > rev) {
+ if (sasRev > localRev) {
//
// First, sync all the authority items
//
ResourceMap resourceMap = ctx.getResourceMap();
String resourceName = ctx.getClient().getServiceName();
AuthorityResource authorityResource = (AuthorityResource) resourceMap.get(resourceName);
- ctx.setProperty(AuthorityServiceUtils.SHOULD_UPDATE_REV_PROPERTY, // Since it is a sync, don't update the rev. Instead use the rev from the SAS
- new Boolean(AuthorityServiceUtils.DONT_UPDATE_REV));
PoxPayloadOut payloadOut = authorityResource.update(ctx, resourceMap, ctx.getUriInfo(), docModel.getName(),
sasPayloadIn);
if (payloadOut != null) {
int synched = 0;
int alreadySynched = 0;
int totalItemsProcessed = 0;
-
+ //
+ // Iterate over the list of items/terms in the remote authority
+ //
PoxPayloadIn sasPayloadInItemList = getPayloadInItemList(ctx, sasSpecifier);
List<Element> itemList = getItemList(sasPayloadInItemList);
if (itemList != null) {
}
/**
- *
+ * This is a sync method.
* @param ctx
* @param parentIdentifier - Must be in short-id-refname form -i.e., urn:cspace:name(shortid)
* @param itemIdentifier - Must be in short-id-refname form -i.e., urn:cspace:name(shortid)
*/
protected void createLocalItem(ServiceContext ctx, String parentIdentifier, String itemIdentifier) throws Exception {
//
- // Create a URN short ID specifier for the getting to the remote item payload
+ // Create a URN short ID specifier for the getting a copy of the remote authority item
+ //
Specifier authoritySpecifier = new Specifier(SpecifierForm.URN_NAME, parentIdentifier);
Specifier itemSpecifier = new Specifier(SpecifierForm.URN_NAME, itemIdentifier);
AuthorityItemSpecifier sasAuthorityItemSpecifier = new AuthorityItemSpecifier(authoritySpecifier, itemSpecifier);
//
// Get the remote payload
//
- PoxPayloadIn sasPayloadIn = AuthorityServiceUtils.getPayloadIn(sasAuthorityItemSpecifier,
+ PoxPayloadIn sasPayloadIn = AuthorityServiceUtils.requestPayloadIn(sasAuthorityItemSpecifier,
ctx.getServiceName(), getEntityResponseType());
//
// Using the payload from the remote server, create a local copy of the item
AuthorityResource authorityResource = (AuthorityResource) resourceMap.get(resourceName);
Response response = authorityResource.createAuthorityItemWithParentContext(ctx, authoritySpecifier.value,
sasPayloadIn, AuthorityServiceUtils.DONT_UPDATE_REV);
+ //
+ // Check the response for successful POST result
+ //
if (response.getStatus() != Response.Status.CREATED.getStatusCode()) {
throw new DocumentException(String.format("Could not create new authority item '%s' during synchronization of the '%s' authority.",
itemIdentifier, parentIdentifier));
*/
protected long syncRemoteItemWithLocalItem(ServiceContext ctx, String remoteRefName) throws Exception {
long result = -1;
-
+ //
+ // Using the remote refname, create specifiers that we'll use to find the local versions
+ //
AuthorityTermInfo authorityTermInfo = RefNameUtils.parseAuthorityTermInfo(remoteRefName);
String parentIdentifier = RefNameUtils.createShortIdRefName(authorityTermInfo.inAuthority.name);
String itemIdentifier = RefNameUtils.createShortIdRefName(authorityTermInfo.name);
-
+ //
+ // We'll use the Authority JAX-RS resource to peform sync operations (creates and updates)
+ //
ResourceMap resourceMap = ctx.getResourceMap();
String resourceName = ctx.getClient().getServiceName();
AuthorityResource authorityResource = (AuthorityResource) resourceMap.get(resourceName);
try {
localItemPayloadOut = authorityResource.getAuthorityItemWithParentContext(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.", remoteRefName));
createLocalItem(ctx, parentIdentifier, itemIdentifier);
return 1; // exit with status of 1 means we created a new authority item
return result;
}
- private PoxPayloadIn getPayloadIn(ServiceContext ctx, Specifier specifier) throws Exception {
- PoxPayloadIn result = null;
-
- AuthorityClient client = (AuthorityClient) ctx.getClient();
- Response res = client.read(specifier.value);
- try {
- int statusCode = res.getStatus();
-
- // Check the status code of the response: does it match
- // the expected response(s)?
- if (logger.isDebugEnabled()) {
- logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
- }
-
- result = new PoxPayloadIn((String)res.readEntity(getEntityResponseType())); // Get the entire response!
- } finally {
- res.close();
- }
-
- return result;
- }
/*
* Non standard injection of CSID into common part, since caller may access through
import org.collectionspace.services.common.UriTemplateRegistry;
import org.collectionspace.services.common.api.RefName;
import org.collectionspace.services.common.api.RefNameUtils;
-import org.collectionspace.services.common.api.RefNameUtils.AuthorityTermInfo;
import org.collectionspace.services.common.api.Tools;
import org.collectionspace.services.common.api.RefNameUtils.AuthorityInfo;
import org.collectionspace.services.common.authorityref.AuthorityRefDocList;
import org.collectionspace.services.common.context.MultipartServiceContext;
-import org.collectionspace.services.common.context.MultipartServiceContextImpl;
import org.collectionspace.services.common.context.ServiceContext;
import org.collectionspace.services.common.document.DocumentException;
import org.collectionspace.services.common.document.DocumentFilter;
import org.collectionspace.services.common.document.DocumentNotFoundException;
import org.collectionspace.services.common.document.DocumentWrapper;
-import org.collectionspace.services.common.document.DocumentHandler.Action;
import org.collectionspace.services.common.repository.RepositoryClient;
import org.collectionspace.services.common.vocabulary.AuthorityJAXBSchema;
import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
import org.collectionspace.services.common.vocabulary.AuthorityResource;
+import org.collectionspace.services.common.vocabulary.AuthorityServiceUtils;
import org.collectionspace.services.common.vocabulary.RefNameServiceUtils;
import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.AuthorityItemSpecifier;
import org.collectionspace.services.common.vocabulary.RefNameServiceUtils.Specifier;
import org.collectionspace.services.nuxeo.util.NuxeoUtils;
import org.collectionspace.services.relation.RelationsCommonList;
import org.collectionspace.services.vocabulary.VocabularyItemJAXBSchema;
+
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.api.model.PropertyException;
+
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-
import javax.ws.rs.core.MultivaluedMap;
-import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Collections;
protected String authorityCommonSchemaName;
protected String authorityItemCommonSchemaName;
private String authorityItemTermGroupXPathBase;
- private boolean shouldUpdateRevNumber = true;
+ private boolean shouldUpdateRevNumber = true; // by default we should update the revision number -not true on synchronization with SAS
/**
* inVocabulary is the parent Authority for this context
*/
this.shouldUpdateRevNumber = flag;
}
+ @Override
+ public void prepareSync() throws Exception {
+ this.setShouldUpdateRevNumber(AuthorityServiceUtils.DONT_UPDATE_REV); // Never update rev nums on sync operations
+ }
+
@Override
protected String getRefnameDisplayName(DocumentWrapper<DocumentModel> docWrapper) {
String result = null;
}
}
-
+
setListItemArrayExtended(true);
} // end of synchronized block
}
return list;
}
-// private PoxPayloadIn getPayloadIn(AuthorityItemSpecifier specifier) throws Exception {
-// PoxPayloadIn result = null;
-//
-// ServiceContext parentCtx = new MultipartServiceContextImpl(this.getAuthorityServicePath());
-// AuthorityClient client = (AuthorityClient) parentCtx.getClient();
-// Response res = client.readItem(specifier.getParentSpecifier().value, specifier.getItemSpecifier().value);
-// try {
-// int statusCode = res.getStatus();
-//
-// // Check the status code of the response: does it match
-// // the expected response(s)?
-// if (logger.isDebugEnabled()) {
-// logger.debug(client.getClass().getCanonicalName() + ": status = " + statusCode);
-// }
-//
-// result = new PoxPayloadIn((String)res.readEntity(getEntityResponseType())); // Get the entire response!
-// } finally {
-// res.close();
-// }
-//
-// return result;
-// }
-
+ /**
+ * This method synchronizes/updates a single authority item resource.
+ */
@Override
public boolean handleSync(DocumentWrapper<Object> wrapDoc) throws Exception {
boolean result = false;
ServiceContext<PoxPayloadIn, PoxPayloadOut> ctx = getServiceContext();
- AuthorityItemSpecifier authorityItemSpecifier = (AuthorityItemSpecifier) wrapDoc.getWrappedObject();
//
- // Get the rev number of the authority item so we can compare with rev number of shared authority
+ // Get the rev number of the local authority item so we can compare with rev number of shared authority
//
+ AuthorityItemSpecifier authorityItemSpecifier = (AuthorityItemSpecifier) wrapDoc.getWrappedObject();
DocumentModel itemDocModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), getAuthorityItemCommonSchemaName(),
authorityItemSpecifier);
if (itemDocModel == null) {
throw new DocumentNotFoundException(String.format("Could not find authority item resource with CSID='%s'",
authorityItemSpecifier.getItemSpecifier().value));
}
- Long itemRev = (Long) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.REV);
+ Long localItemRev = (Long) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.REV);
String itemShortId = (String) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.SHORT_IDENTIFIER);
- String itemRefName = (String) NuxeoUtils.getProperyValue(itemDocModel, AuthorityItemJAXBSchema.REF_NAME);
//
// Now get the Authority (the parent) information
//
DocumentModel authorityDocModel = NuxeoUtils.getDocFromSpecifier(ctx, getRepositorySession(), authorityCommonSchemaName,
authorityItemSpecifier.getParentSpecifier());
- Long authorityRev = (Long) NuxeoUtils.getProperyValue(authorityDocModel, AuthorityJAXBSchema.REV);
String authorityShortId = (String) NuxeoUtils.getProperyValue(authorityDocModel, AuthorityJAXBSchema.SHORT_IDENTIFIER);
- String authorityRefName = (String) NuxeoUtils.getProperyValue(authorityDocModel, AuthorityJAXBSchema.REF_NAME);
- AuthorityInfo authorityInfo = RefNameUtils.parseAuthorityInfo(authorityRefName);
//
// Using the short IDs of the local authority and item, create URN specifiers to retrieve the SAS authority item
//
Specifier sasItemSpecifier = new Specifier(SpecifierForm.URN_NAME, RefNameUtils.createShortIdRefName(itemShortId));
AuthorityItemSpecifier sasAuthorityItemSpecifier = new AuthorityItemSpecifier(sasAuthoritySpecifier, sasItemSpecifier);
// Get the shared authority server's copy
- PoxPayloadIn sasPayloadIn = AuthorityServiceUtils.getPayloadIn(sasAuthorityItemSpecifier,
+ PoxPayloadIn sasPayloadIn = AuthorityServiceUtils.requestPayloadIn(sasAuthorityItemSpecifier,
getAuthorityServicePath(), getEntityResponseType());
-
Long sasRev = getRevision(sasPayloadIn);
- if (sasRev > itemRev) {
+ //
+ // If the shared authority item is newer, update our local copy
+ //
+ if (sasRev > localItemRev) {
ResourceMap resourceMap = ctx.getResourceMap();
String resourceName = this.getAuthorityServicePath();
AuthorityResource authorityResource = (AuthorityResource) resourceMap.get(resourceName);
authorityDocModel.getName(), // parent's CSID
itemDocModel.getName(), // item's CSID
sasPayloadIn, // the payload from the SAS
- false); // don't update the parent's revision number
+ AuthorityServiceUtils.DONT_UPDATE_REV); // don't update the parent's revision number
if (payloadOut != null) {
ctx.setOutput(payloadOut);
result = true;
return this.update(null, resourceMap, uriInfo, csid, xmlPayload);
}
+ /**
+ *
+ * @param parentCtx
+ * @param resourceMap
+ * @param uriInfo
+ * @param csid
+ * @param xmlPayload
+ * @return
+ */
public byte[] update(ServiceContext<PoxPayloadIn, PoxPayloadOut> parentCtx, // REM: 8/13/2012 - Some sub-classes will override this method -e.g., MediaResource does.
@Context ResourceMap resourceMap,
@Context UriInfo uriInfo,