diff --git a/config/alfresco/model/contentModel.xml b/config/alfresco/model/contentModel.xml index 58bfd9768f..a4ba842360 100644 --- a/config/alfresco/model/contentModel.xml +++ b/config/alfresco/model/contentModel.xml @@ -542,6 +542,11 @@ d:text true + + Initial Version + d:boolean + true + Auto Version d:boolean diff --git a/source/java/org/alfresco/model/ContentModel.java b/source/java/org/alfresco/model/ContentModel.java index ba004acf68..c5855c46ac 100644 --- a/source/java/org/alfresco/model/ContentModel.java +++ b/source/java/org/alfresco/model/ContentModel.java @@ -106,6 +106,7 @@ public interface ContentModel // version aspect static final QName ASPECT_VERSIONABLE = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "versionable"); static final QName PROP_VERSION_LABEL = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "versionLabel"); + static final QName PROP_INITIAL_VERSION = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "initialVersion"); static final QName PROP_AUTO_VERSION = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "autoVersion"); // folders diff --git a/source/java/org/alfresco/repo/version/VersionServiceImplTest.java b/source/java/org/alfresco/repo/version/VersionServiceImplTest.java index b203751fa5..1f78d2876f 100644 --- a/source/java/org/alfresco/repo/version/VersionServiceImplTest.java +++ b/source/java/org/alfresco/repo/version/VersionServiceImplTest.java @@ -16,7 +16,9 @@ */ package org.alfresco.repo.version; +import java.io.Serializable; import java.util.Collection; +import java.util.HashMap; import java.util.Set; import org.alfresco.model.ContentModel; @@ -435,19 +437,70 @@ public class VersionServiceImplTest extends BaseVersionStoreTest } - public void testAutoVersion() + public void testAutoVersionOnInitialVersionOn() { // Create a versionable node final NodeRef versionableNode = createNewVersionableNode(); - - // Add some content - ContentWriter contentWriter = this.contentService.getWriter(versionableNode, ContentModel.PROP_CONTENT, true); - assertNotNull(contentWriter); - contentWriter.putContent(UPDATED_CONTENT_1); - // Need to commit in order to get the auto version to fire ... setComplete(); endTransaction(); + + TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork() + { + public Object doWork() throws Exception + { + // Check that the initial version has not been created + VersionHistory versionHistory = VersionServiceImplTest.this.versionService.getVersionHistory(versionableNode); + assertNotNull(versionHistory); + assertEquals(1, versionHistory.getAllVersions().size()); + + // Add some content + ContentWriter contentWriter = VersionServiceImplTest.this.contentService.getWriter(versionableNode, ContentModel.PROP_CONTENT, true); + assertNotNull(contentWriter); + contentWriter.putContent(UPDATED_CONTENT_1); + + return null; + } + }); + + // Now lets have a look and make sure we have the correct number of entries in the version history + TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork() + { + public Object doWork() throws Exception + { + VersionHistory versionHistory = VersionServiceImplTest.this.versionService.getVersionHistory(versionableNode); + assertNotNull(versionHistory); + assertEquals(2, versionHistory.getAllVersions().size()); + + return null; + } + + }); + } + + public void testAutoVersionOff() + { + // Create a versionable node + final NodeRef versionableNode = createNewVersionableNode(); + this.dbNodeService.setProperty(versionableNode, ContentModel.PROP_AUTO_VERSION, false); + + setComplete(); + endTransaction(); + + // The initial version should have been created now + + TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork() + { + public Object doWork() throws Exception + { + // Add some content + ContentWriter contentWriter = VersionServiceImplTest.this.contentService.getWriter(versionableNode, ContentModel.PROP_CONTENT, true); + assertNotNull(contentWriter); + contentWriter.putContent(UPDATED_CONTENT_1); + + return null; + } + }); // Now lets have a look and make sure we have the correct number of entries in the version history TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork() @@ -463,4 +516,37 @@ public class VersionServiceImplTest extends BaseVersionStoreTest }); } + + public void testInitialVersionOff() + { + // Create node (this node has some content) + HashMap props = new HashMap(); + props.put(ContentModel.PROP_INITIAL_VERSION, false); + HashMap props2 = new HashMap(); + props2.put(ContentModel.PROP_NAME, "test.txt"); + final NodeRef nodeRef = this.dbNodeService.createNode( + rootNodeRef, + ContentModel.ASSOC_CHILDREN, + QName.createQName("{test}MyVersionableNode2"), + TEST_TYPE_QNAME, + props2).getChildRef(); + this.dbNodeService.addAspect(nodeRef, ContentModel.ASPECT_VERSIONABLE, props); + + setComplete(); + endTransaction(); + + // The initial version should NOT have been created + + TransactionUtil.executeInUserTransaction(this.transactionService, new TransactionUtil.TransactionWork() + { + public Object doWork() throws Exception + { + VersionHistory versionHistory = VersionServiceImplTest.this.versionService.getVersionHistory(nodeRef); + assertNull(versionHistory); + + return null; + } + }); + + } } diff --git a/source/java/org/alfresco/repo/version/VersionableAspect.java b/source/java/org/alfresco/repo/version/VersionableAspect.java index 670ab40330..385ff51af6 100644 --- a/source/java/org/alfresco/repo/version/VersionableAspect.java +++ b/source/java/org/alfresco/repo/version/VersionableAspect.java @@ -20,16 +20,13 @@ import java.io.Serializable; import java.util.Map; import org.alfresco.model.ContentModel; -import org.alfresco.repo.action.evaluator.HasVersionHistoryEvaluator; import org.alfresco.repo.action.executer.CreateVersionActionExecuter; import org.alfresco.repo.policy.Behaviour; -import org.alfresco.repo.policy.BehaviourDefinition; import org.alfresco.repo.policy.JavaBehaviour; import org.alfresco.repo.policy.PolicyComponent; import org.alfresco.repo.policy.PolicyScope; import org.alfresco.repo.rule.RuntimeRuleService; import org.alfresco.service.cmr.action.Action; -import org.alfresco.service.cmr.action.ActionCondition; import org.alfresco.service.cmr.action.ActionService; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; @@ -136,12 +133,6 @@ public class VersionableAspect QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyNode"), ContentModel.ASPECT_VERSIONABLE, new JavaBehaviour(this, "onCopy")); - - // Register the onCreateVersion behavior for the version aspect - //this.policyComponent.bindClassBehaviour( - // QName.createQName(NamespaceService.ALFRESCO_URI, "onCreateVersion"), - // ContentModel.ASPECT_VERSIONABLE, - // new JavaBehaviour(this, "onCreateVersion")); } /** @@ -200,8 +191,19 @@ public class VersionableAspect { if (aspectTypeQName.equals(ContentModel.ASPECT_VERSIONABLE) == true) { - // Queue create version action - queueCreateVersionAction(nodeRef); + boolean initialVersion = true; + Boolean value = (Boolean)this.nodeService.getProperty(nodeRef, ContentModel.PROP_INITIAL_VERSION); + if (value != null) + { + initialVersion = value.booleanValue(); + } + // else this means that the default vlaue has not been set the versionable aspect we applied pre-1.2 + + if (initialVersion == true) + { + // Queue create version action + queueCreateVersionAction(nodeRef); + } } } @@ -259,15 +261,13 @@ public class VersionableAspect { if (this.rule == null) { + // Create the version action rule this.rule = this.ruleService.createRule("inbound"); Action action = this.actionService.createAction(CreateVersionActionExecuter.NAME); - ActionCondition condition = this.actionService.createActionCondition(HasVersionHistoryEvaluator.NAME); - condition.setInvertCondition(true); - // conditions are only evaluated on the parent rule - not the contained actions - rule.addActionCondition(condition); this.rule.addAction(action); } + // Stash the rule pending execution at the end of the transaction ((RuntimeRuleService)this.ruleService).addRulePendingExecution(nodeRef, nodeRef, this.rule, true); } }