From af449962c458891f096167c950311607b5b15f95 Mon Sep 17 00:00:00 2001 From: Roy Wetherall Date: Thu, 13 May 2010 13:10:01 +0000 Subject: [PATCH] Google docs delete reliability improved - Google doc remove now happens at the end of the transaction when its commited to help ensure the Alfresco state and google doc state are more likely insync - Changed behaviour to remove content when object is deleted. Ensures correct behaviour on check-in, cancel check-in and if a object (inlcuding folder) is deleted. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@20223 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- .../googledocs/GoogleDocsServiceImpl.java | 107 ++++++++++++------ .../repo/googledocs/GoogleEditableAspect.java | 39 ++++--- 2 files changed, 98 insertions(+), 48 deletions(-) diff --git a/source/java/org/alfresco/repo/googledocs/GoogleDocsServiceImpl.java b/source/java/org/alfresco/repo/googledocs/GoogleDocsServiceImpl.java index a882885599..4686431f9d 100755 --- a/source/java/org/alfresco/repo/googledocs/GoogleDocsServiceImpl.java +++ b/source/java/org/alfresco/repo/googledocs/GoogleDocsServiceImpl.java @@ -85,6 +85,10 @@ public class GoogleDocsServiceImpl extends TransactionListenerAdapter public static final String TYPE_SPREADSHEET = "spreadsheet"; public static final String TYPE_PRESENTATION = "presentation"; public static final String TYPE_PDF = "pdf"; + + /** Transaction resource keys */ + private final static String KEY_MARKED_CREATE = "google_doc_service.marked_resources"; + private final static String KEY_MARKED_DELETE = "google_doc_service.marked_delete"; /** Services */ private DocsService googleDocumentService; @@ -326,33 +330,22 @@ public class GoogleDocsServiceImpl extends TransactionListenerAdapter { throw new AlfrescoRuntimeException("Unable to create google doc, because service could not be initialised.", e); } - - try + + if (nodeService.hasAspect(nodeRef, ASPECT_GOOGLERESOURCE) == true) { - if (nodeService.hasAspect(nodeRef, ASPECT_GOOGLERESOURCE) == true) + // Get the entry + DocumentListEntry entry = getDocumentListEntry(nodeRef); + if (entry == null) { - // Get the entry - DocumentListEntry entry = getDocumentListEntry(nodeRef); - if (entry == null) - { - throw new AlfrescoRuntimeException("Unable to find google resource to delete for node " + nodeRef.toString()); - } - - // Perminantly delete the entry - googleDocumentService.delete(new URL(entry.getEditLink().getHref() + "?delete=true"), entry.getEtag()); - - // Remove the aspect from the node - nodeService.removeAspect(nodeRef, ASPECT_GOOGLERESOURCE); + throw new AlfrescoRuntimeException("Unable to find google resource to delete for node " + nodeRef.toString()); } - } - catch (ServiceException e) - { - throw new AlfrescoRuntimeException("Unable to delete google resource for the node "+ nodeRef.toString()); - } - catch (IOException e) - { - throw new AlfrescoRuntimeException("Unable to delete google resource for the node "+ nodeRef.toString()); - } + + // Mark the resource for deletion upon completion of the transaction + markResource(KEY_MARKED_DELETE, entry.getResourceId()); + + // Remove the aspect from the node + nodeService.removeAspect(nodeRef, ASPECT_GOOGLERESOURCE); + } } /** @@ -743,7 +736,7 @@ public class GoogleDocsServiceImpl extends TransactionListenerAdapter docEntry); // Mark create entry - markCreated(document.getResourceId()); + markResource(KEY_MARKED_CREATE, document.getResourceId()); } catch (IOException e) { @@ -854,7 +847,7 @@ public class GoogleDocsServiceImpl extends TransactionListenerAdapter folder); // Mark create entry - markCreated(folderEntry.getResourceId()); + markResource(KEY_MARKED_CREATE, folderEntry.getResourceId()); } catch (IOException e) { @@ -979,17 +972,20 @@ public class GoogleDocsServiceImpl extends TransactionListenerAdapter } } - private final static String KEY_MARKED_RESOURCES = "GoogleDocService.marked_resources"; - + /** + * Marks a resource as created in this transaction + * + * @param resourceId resource id of created resource + */ @SuppressWarnings("unchecked") - private void markCreated(String resourceId) + private void markResource(String key, String resourceId) { - List resources = (List)AlfrescoTransactionSupport.getResource(KEY_MARKED_RESOURCES); + List resources = (List)AlfrescoTransactionSupport.getResource(key); if (resources == null) { // bind pending rules to the current transaction resources = new ArrayList(); - AlfrescoTransactionSupport.bindResource(KEY_MARKED_RESOURCES, resources); + AlfrescoTransactionSupport.bindResource(key, resources); // bind the rule transaction listener AlfrescoTransactionSupport.bindListener(this); } @@ -998,24 +994,61 @@ public class GoogleDocsServiceImpl extends TransactionListenerAdapter { if (logger.isDebugEnabled() == true) { - logger.debug("Marking created resource " + resourceId); + logger.debug("Marking resource " + resourceId + " with key " + key); } resources.add(resourceId); } } + /** + * @see org.alfresco.repo.transaction.TransactionListenerAdapter#afterCommit() + */ + @SuppressWarnings("unchecked") @Override public void afterCommit() { - // TODO go ahead and delete any resources that have be queued up for deletion .... + List resources = (List)AlfrescoTransactionSupport.getResource(KEY_MARKED_DELETE); + if (resources != null) + { + if (logger.isDebugEnabled() == true) + { + logger.debug("Transaction commited, deleting Google resources"); + } + + for (String resourceId : resources) + { + if (logger.isDebugEnabled() == true) + { + logger.debug("Deleting resource " + resourceId); + } + + // Delete resource + try + { + DocumentListEntry entry = getDocumentListEntry(resourceId); + googleDocumentService.delete(new URL(entry.getEditLink().getHref() + "?delete=true"), entry.getEtag()); + } + catch (Throwable e) + { + // Ignore, but log + if (logger.isDebugEnabled() == true) + { + logger.debug("Unable to delete resource " + resourceId + " during commit.", e); + } + } + } + } } + /** + * @see org.alfresco.repo.transaction.TransactionListenerAdapter#afterRollback() + */ @SuppressWarnings("unchecked") @Override public void afterRollback() { - List resources = (List)AlfrescoTransactionSupport.getResource(KEY_MARKED_RESOURCES); + List resources = (List)AlfrescoTransactionSupport.getResource(KEY_MARKED_CREATE); if (resources != null) { if (logger.isDebugEnabled() == true) @@ -1038,7 +1071,11 @@ public class GoogleDocsServiceImpl extends TransactionListenerAdapter } catch (Throwable e) { - // Ignore + // Ignore, but log + if (logger.isDebugEnabled() == true) + { + logger.debug("Unable to delete resource " + resourceId + " during rollback.", e); + } } } } diff --git a/source/java/org/alfresco/repo/googledocs/GoogleEditableAspect.java b/source/java/org/alfresco/repo/googledocs/GoogleEditableAspect.java index ecc69f5327..2416dc4545 100755 --- a/source/java/org/alfresco/repo/googledocs/GoogleEditableAspect.java +++ b/source/java/org/alfresco/repo/googledocs/GoogleEditableAspect.java @@ -27,6 +27,7 @@ import org.alfresco.repo.coci.CheckOutCheckInServicePolicies.BeforeCancelCheckOu import org.alfresco.repo.coci.CheckOutCheckInServicePolicies.OnCheckIn; import org.alfresco.repo.coci.CheckOutCheckInServicePolicies.OnCheckOut; import org.alfresco.repo.node.NodeServicePolicies; +import org.alfresco.repo.node.NodeServicePolicies.BeforeDeleteNodePolicy; import org.alfresco.repo.node.NodeServicePolicies.OnAddAspectPolicy; import org.alfresco.repo.policy.JavaBehaviour; import org.alfresco.repo.policy.PolicyComponent; @@ -45,7 +46,8 @@ import org.alfresco.service.namespace.QName; public class GoogleEditableAspect implements NodeServicePolicies.OnAddAspectPolicy, CheckOutCheckInServicePolicies.OnCheckOut, CheckOutCheckInServicePolicies.OnCheckIn, - CheckOutCheckInServicePolicies.BeforeCancelCheckOut + NodeServicePolicies.BeforeDeleteNodePolicy + //CheckOutCheckInServicePolicies.BeforeCancelCheckOut { /** Indicates whether behaviour is enabled or not */ boolean enabled = false; @@ -130,9 +132,9 @@ public class GoogleEditableAspect implements NodeServicePolicies.OnAddAspectPoli policyComponent.bindClassBehaviour(OnCheckIn.QNAME, GoogleDocsModel.ASPECT_GOOGLEEDITABLE, new JavaBehaviour(this, "onCheckIn", NotificationFrequency.FIRST_EVENT)); - policyComponent.bindClassBehaviour(BeforeCancelCheckOut.QNAME, - GoogleDocsModel.ASPECT_GOOGLEEDITABLE, - new JavaBehaviour(this, "beforeCancelCheckOut", NotificationFrequency.FIRST_EVENT)); + policyComponent.bindClassBehaviour(BeforeDeleteNodePolicy.QNAME, + GoogleDocsModel.ASPECT_GOOGLERESOURCE, + new JavaBehaviour(this, "beforeDeleteNode", NotificationFrequency.FIRST_EVENT)); } } @@ -188,19 +190,30 @@ public class GoogleEditableAspect implements NodeServicePolicies.OnAddAspectPoli writer.putContent(is); // Delete the associated google resource - googleDocsService.deleteGoogleResource(nodeRef); + //googleDocsService.deleteGoogleResource(nodeRef); + + nodeService.removeAspect(nodeRef, GoogleDocsModel.ASPECT_GOOGLERESOURCE); } } + public void beforeDeleteNode(NodeRef nodeRef) + { + if (nodeService.exists(nodeRef) == true) + { + // Delete the associated google resource + googleDocsService.deleteGoogleResource(nodeRef); + } + } + /** * @see org.alfresco.repo.coci.CheckOutCheckInServicePolicies.BeforeCancelCheckOut#beforeCancelCheckOut(org.alfresco.service.cmr.repository.NodeRef) */ - public void beforeCancelCheckOut(NodeRef workingCopyNodeRef) - { - if (nodeService.exists(workingCopyNodeRef) == true) - { - // Delete the associated google resource - googleDocsService.deleteGoogleResource(workingCopyNodeRef); - } - } +// public void beforeCancelCheckOut(NodeRef workingCopyNodeRef) +// { +// if (nodeService.exists(workingCopyNodeRef) == true) +// { +// // Delete the associated google resource +// googleDocsService.deleteGoogleResource(workingCopyNodeRef); +// } +// } }