import java.util.ArrayList;
import java.util.List;
+import java.util.IllegalFormatException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.collectionspace.services.client.workflow.WorkflowClient;
import org.nuxeo.ecm.core.api.ClientException;
import org.nuxeo.ecm.core.api.CoreSession;
import org.nuxeo.ecm.core.api.DocumentModel;
import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
public class UpdateRelationsOnDelete implements EventListener {
-
- // FIXME: Consider adding the following constant to
- // org.collectionspace.services.common.workflow.jaxb.WorkflowJAXBSchema
- // and referencing it from there.
- private static final String WORKFLOWTRANSITION_TO = "to";
- // FIXME: Consider substituting existing constant WorkflowClient.WORKFLOWSTATE_DELETED
- private static final String WORKFLOWSTATE_DELETED = "deleted";
- // FIXME: Consider substituting existing constant WorkflowClient.WORKFLOWSTATE_LOCKED
- private static final String WORKFLOWSTATE_LOCKED = "locked";
- // FIXME: Consider substituting existing constant WorkflowClient.WORKFLOWTRANSITION_DELETE
- private static final String WORKFLOWTRANSITION_DELETE = "delete";
+
+ // FIXME: Get these constant values from external sources rather than redeclaring here
+ final static String RELATIONS_COMMON_SUBJECT_CSID_FIELD = "relations_common:subjectCsid";
+ final static String RELATIONS_COMMON_OBJECT_CSID_FIELD = "relations_common:objectCsid";
// FIXME: We might experiment here with using log4j instead of Apache Commons Logging;
// am using the latter to follow Ray's pattern for now
@Override
public void handleEvent(Event event) throws ClientException {
logger.info("In handleEvent in UpdateRelationsOnDelete ...");
-
+
EventContext eventContext = event.getContext();
if (isDocumentSoftDeletedEvent(eventContext)) {
// Build a query string
String csid = docModel.getName();
- StringBuilder queryString = new StringBuilder("");
- queryString.append("SELECT * FROM Relation WHERE ");
- // FIXME: Obtain and add tenant ID to the query here
- // queryString.append("collectionspace_core:tenantId = 1 ");
- // queryString.append(" AND ");
- // queryString.append("ecm:currentLifeCycleState <> 'deleted' ");
- queryString.append("ecm:isProxy = 0 ");
- queryString.append(" AND ");
- queryString.append("(");
- queryString.append("relations_common:subjectCsid = ");
- queryString.append("'");
- queryString.append(csid);
- queryString.append("'");
- queryString.append(" OR ");
- queryString.append("relations_common:objectCsid = ");
- queryString.append("'");
- queryString.append(csid);
- queryString.append("'");
- queryString.append(")");
-
+
+ String queryString;
+ try {
+ queryString =
+ String.format("SELECT * FROM Relation WHERE ecm:isProxy = 0 AND (%1$s='%3$s' OR %2$s='%3$s')",
+ RELATIONS_COMMON_SUBJECT_CSID_FIELD, RELATIONS_COMMON_OBJECT_CSID_FIELD, csid);
+ logger.trace("Query string=" + queryString);
+ } catch (IllegalFormatException ife) {
+ logger.warn("Construction of formatted query string failed: ", ife);
+ return;
+ }
+
// Create a filter to exclude from the list results any records
// that have already been soft deleted or are locked
List<String> workflowStatesToFilter = new ArrayList<String>();
- workflowStatesToFilter.add(WORKFLOWSTATE_DELETED);
- workflowStatesToFilter.add(WORKFLOWSTATE_LOCKED);
+ workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_DELETED);
+ workflowStatesToFilter.add(WorkflowClient.WORKFLOWSTATE_LOCKED);
LifeCycleFilter workflowStateFilter = new LifeCycleFilter(null, workflowStatesToFilter);
// Perform the filtered query
// Cycle through the list results, soft deleting each matching relation record
logger.trace("Attempting to soft delete " + matchingDocuments.size() + " relation records.");
for (DocumentModel doc : matchingDocuments) {
- doc.followTransition(WORKFLOWTRANSITION_DELETE);
+ doc.followTransition(WorkflowClient.WORKFLOWTRANSITION_DELETE);
}
}
private boolean isDocumentSoftDeletedEvent(EventContext eventContext) {
boolean isSoftDeletedEvent = false;
if (eventContext instanceof DocumentEventContext) {
- if (eventContext.getProperties().containsKey(WORKFLOWTRANSITION_TO)
- && eventContext.getProperties().get(WORKFLOWTRANSITION_TO).equals(WORKFLOWSTATE_DELETED)) {
+ if (eventContext.getProperties().containsKey(WorkflowClient.WORKFLOWTRANSITION_TO)
+ && eventContext.getProperties().get(WorkflowClient.WORKFLOWTRANSITION_TO).equals(WorkflowClient.WORKFLOWSTATE_DELETED)) {
isSoftDeletedEvent = true;
}
}