From b3f62c505cdb3678d4c62e296f64883a20c31386 Mon Sep 17 00:00:00 2001 From: Alex Miller Date: Thu, 31 May 2012 14:53:01 +0000 Subject: [PATCH] ALF-11318: Add policy to prevent copying of rateable aspect and associated aspects. git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@37313 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261 --- config/alfresco/rating-services-context.xml | 6 + .../alfresco/repo/rating/RateableAspect.java | 116 ++++++++++++++++++ .../rating/RatingServiceIntegrationTest.java | 57 ++++++++- 3 files changed, 178 insertions(+), 1 deletion(-) create mode 100644 source/java/org/alfresco/repo/rating/RateableAspect.java diff --git a/config/alfresco/rating-services-context.xml b/config/alfresco/rating-services-context.xml index 57c974e2ea..73239e62d7 100644 --- a/config/alfresco/rating-services-context.xml +++ b/config/alfresco/rating-services-context.xml @@ -107,6 +107,12 @@ + + + + + + . + */ +package org.alfresco.repo.rating; + +import org.alfresco.model.ContentModel; +import org.alfresco.repo.copy.CopyBehaviourCallback; +import org.alfresco.repo.copy.CopyDetails; +import org.alfresco.repo.copy.CopyServicePolicies; +import org.alfresco.repo.copy.DefaultCopyBehaviourCallback; +import org.alfresco.repo.copy.DoNothingCopyBehaviourCallback; +import org.alfresco.repo.policy.JavaBehaviour; +import org.alfresco.repo.policy.PolicyComponent; +import org.alfresco.service.cmr.rating.RatingScheme; +import org.alfresco.service.namespace.NamespaceService; +import org.alfresco.service.namespace.QName; +import org.alfresco.util.Pair; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * Rateable aspect behaviour bean. When any node with the rateable aspect is + * copied, then ensure the ratings and roll ups are not copied. + * + * @author Alex Miller + */ +public class RateableAspect implements CopyServicePolicies.OnCopyNodePolicy +{ + /** logger */ + private static final Log logger = LogFactory.getLog(RateableAspect.class); + + /** Services */ + private PolicyComponent policyComponent; + + private RatingNamingConventionsUtil ratingNamingConventions; + + private RatingSchemeRegistry ratingSchemeRegistry; + + /** + * Set the policy component + * + * @param policyComponent policy component + */ + public void setPolicyComponent(PolicyComponent policyComponent) + { + this.policyComponent = policyComponent; + } + + /** + * Set the rating scheme registry + * + * @param ratingSchemeRegistry The rating scheme registry + */ + public void setRatingSchemeRegistry(RatingSchemeRegistry ratingSchemeRegistry) + { + this.ratingSchemeRegistry = ratingSchemeRegistry; + } + + /** + * Set the rating naming conventions service. + * + * @param + */ + public void setRatingNamingConventions(RatingNamingConventionsUtil ratingNamingConventions) + { + this.ratingNamingConventions = ratingNamingConventions; + } + + /** + * Initialise method + */ + public void init() + { + // Prevent the ratebale aspect from being copied + bindNoCopyBehaviour(ContentModel.ASPECT_RATEABLE); + + // Prevent the roll up aspects from being copied + for (RatingScheme ratingScheme : ratingSchemeRegistry.getRatingSchemes().values()) + { + if (ratingScheme.getPropertyRollups() != null && ratingScheme.getPropertyRollups().size() > 0) + { + QName rollupAspectName = ratingNamingConventions.getRollupAspectNameFor(ratingScheme); + bindNoCopyBehaviour(rollupAspectName); + } + } + } + + private void bindNoCopyBehaviour(QName rollupAspectName) + { + this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "getCopyCallback"), + rollupAspectName, new JavaBehaviour(this, "getCopyCallback")); + } + + /** + * @return Returns {@link RateableAspectCopyBehaviourCallback} + */ + public CopyBehaviourCallback getCopyCallback(QName classRef, CopyDetails copyDetails) + { + return DoNothingCopyBehaviourCallback.getInstance(); + } +} diff --git a/source/java/org/alfresco/repo/rating/RatingServiceIntegrationTest.java b/source/java/org/alfresco/repo/rating/RatingServiceIntegrationTest.java index 734f5c2222..df0914fc7b 100644 --- a/source/java/org/alfresco/repo/rating/RatingServiceIntegrationTest.java +++ b/source/java/org/alfresco/repo/rating/RatingServiceIntegrationTest.java @@ -41,17 +41,19 @@ import org.alfresco.service.cmr.rating.RatingScheme; import org.alfresco.service.cmr.rating.RatingService; import org.alfresco.service.cmr.rating.RatingServiceException; import org.alfresco.service.cmr.repository.ChildAssociationRef; +import org.alfresco.service.cmr.repository.CopyService; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.ScriptLocation; import org.alfresco.service.cmr.repository.ScriptService; +import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.RegexQNamePattern; import org.alfresco.util.test.junitrules.AlfrescoPerson; import org.alfresco.util.test.junitrules.ApplicationContextInit; import org.alfresco.util.test.junitrules.RunAsFullyAuthenticatedRule; -import org.alfresco.util.test.junitrules.TemporaryNodes; import org.alfresco.util.test.junitrules.RunAsFullyAuthenticatedRule.RunAsUser; +import org.alfresco.util.test.junitrules.TemporaryNodes; import org.junit.Before; import org.junit.BeforeClass; import org.junit.ClassRule; @@ -89,6 +91,7 @@ public class RatingServiceIntegrationTest @Rule public RunAsFullyAuthenticatedRule runAsRule = new RunAsFullyAuthenticatedRule(TEST_USER1); // Various services + private static CopyService COPY_SERVICE; private static NodeService NODE_SERVICE; private static RatingService RATING_SERVICE; private static RetryingTransactionHelper TRANSACTION_HELPER; @@ -98,8 +101,10 @@ public class RatingServiceIntegrationTest private static NodeRef COMPANY_HOME; // These NodeRefs are used by the test methods. + private static NodeRef COPY_DEST_FOLDER; private static NodeRef TEST_FOLDER; private NodeRef testDoc_Admin; + private NodeRef testDoc_Copy; private NodeRef testDoc_UserOne; private NodeRef testDoc_UserTwo; @@ -109,6 +114,7 @@ public class RatingServiceIntegrationTest @BeforeClass public static void initStaticData() throws Exception { + COPY_SERVICE = (CopyService) APP_CONTEXT_INIT.getApplicationContext().getBean("copyService"); NODE_SERVICE = (NodeService) APP_CONTEXT_INIT.getApplicationContext().getBean("nodeService"); RATING_NAMING_CONVENTIONS = (RatingNamingConventionsUtil) APP_CONTEXT_INIT.getApplicationContext().getBean("rollupNamingConventions"); RATING_SERVICE = (RatingService) APP_CONTEXT_INIT.getApplicationContext().getBean("ratingService"); @@ -120,6 +126,7 @@ public class RatingServiceIntegrationTest // Create some static test content TEST_FOLDER = STATIC_TEST_NODES.createNode(COMPANY_HOME, "testFolder", ContentModel.TYPE_FOLDER, AuthenticationUtil.getAdminUserName()); + COPY_DEST_FOLDER = STATIC_TEST_NODES.createNode(COMPANY_HOME, "testCopyDestinationFolder", ContentModel.TYPE_FOLDER, AuthenticationUtil.getAdminUserName()); } @@ -432,4 +439,52 @@ public class RatingServiceIntegrationTest } }); } + + @Test public void copyNodeNotRatings() + { + TRANSACTION_HELPER.doInTransaction(new RetryingTransactionCallback() + { + public Void execute() throws Throwable + { + // Apply ratings to node + AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER1.getUsername()); + + RATING_SERVICE.applyRating(testDoc_UserTwo, 2.0f, FIVE_STAR_SCHEME_NAME); + + // A new score in a different rating scheme by the same user should not fail. + RATING_SERVICE.applyRating(testDoc_UserTwo, 1.0f, LIKES_SCHEME_NAME); + + // There should be two rating child nodes under the rated node. + assertEquals("Wrong number of child nodes", 2 , NODE_SERVICE.getChildAssocs(testDoc_UserTwo).size()); + + List ratings = RATING_SERVICE.getRatingsByCurrentUser(testDoc_UserTwo); + assertEquals(2, ratings.size()); + assertEquals(FIVE_STAR_SCHEME_NAME, ratings.get(0).getScheme().getName()); + assertEquals(LIKES_SCHEME_NAME, ratings.get(1).getScheme().getName()); + + // Copy the node + AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName()); + + final QName childName = QName.createQName(NamespaceService.APP_MODEL_1_0_URI, "userTwosDoc");; + final NodeRef testDocCopy = COPY_SERVICE.copy(testDoc_UserTwo, COPY_DEST_FOLDER, ContentModel.ASSOC_CONTAINS, childName, true); + + testNodes.addNodeRef(testDocCopy); + + // Check that the user ratings aren't copied + AuthenticationUtil.setFullyAuthenticatedUser(TEST_USER1.getUsername()); + + List copiedRatings = RATING_SERVICE.getRatingsByCurrentUser(testDocCopy); + assertEquals(0, copiedRatings.size()); + + // Check that the roll ups aren't copied + final int likesRatingCount = RATING_SERVICE.getRatingsCount(testDocCopy, LIKES_SCHEME_NAME); + final int fiveStarRatingCount = RATING_SERVICE.getRatingsCount(testDocCopy, FIVE_STAR_SCHEME_NAME); + + assertEquals(0, fiveStarRatingCount); + assertEquals(0, likesRatingCount); + + return null; + } + }); + } }