MNT-22780: fix rule behavior (#950)

* MNT-22780 Remove properties if node has aspect

* MNT-22780 Add remove features test

* MNT-22780 Remove wildcard imports + fix test

* MNT-22780 Remove aspect when only node has aspect

* MNT-22780 Fix if statement
This commit is contained in:
Damian Ujma
2022-02-17 15:06:28 +01:00
committed by GitHub
parent 2177ffcea8
commit 3b65b30bd5
2 changed files with 173 additions and 127 deletions

View File

@@ -1,106 +1,106 @@
/* /*
* #%L * #%L
* Alfresco Repository * Alfresco Repository
* %% * %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited * Copyright (C) 2005 - 2022 Alfresco Software Limited
* %% * %%
* This file is part of the Alfresco software. * This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of * If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is * the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms: * provided under the following open source license terms:
* *
* Alfresco is free software: you can redistribute it and/or modify * Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* Alfresco is distributed in the hope that it will be useful, * Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L% * #L%
*/ */
package org.alfresco.repo.node.db; package org.alfresco.repo.node.db;
import java.io.Serializable; import java.io.Serializable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import org.alfresco.error.AlfrescoRuntimeException; import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel; import org.alfresco.model.ContentModel;
import org.alfresco.repo.domain.node.ChildAssocEntity; import org.alfresco.repo.domain.node.ChildAssocEntity;
import org.alfresco.repo.domain.node.Node; import org.alfresco.repo.domain.node.Node;
import org.alfresco.repo.domain.node.NodeDAO; import org.alfresco.repo.domain.node.NodeDAO;
import org.alfresco.repo.domain.node.NodeDAO.ChildAssocRefQueryCallback; import org.alfresco.repo.domain.node.NodeDAO.ChildAssocRefQueryCallback;
import org.alfresco.repo.domain.node.NodeExistsException; import org.alfresco.repo.domain.node.NodeExistsException;
import org.alfresco.repo.domain.qname.QNameDAO; import org.alfresco.repo.domain.qname.QNameDAO;
import org.alfresco.repo.node.AbstractNodeServiceImpl; import org.alfresco.repo.node.AbstractNodeServiceImpl;
import org.alfresco.repo.node.StoreArchiveMap; import org.alfresco.repo.node.StoreArchiveMap;
import org.alfresco.repo.node.archive.NodeArchiveService; import org.alfresco.repo.node.archive.NodeArchiveService;
import org.alfresco.repo.node.db.NodeHierarchyWalker.VisitedNode; import org.alfresco.repo.node.db.NodeHierarchyWalker.VisitedNode;
import org.alfresco.repo.node.db.traitextender.NodeServiceExtension; import org.alfresco.repo.node.db.traitextender.NodeServiceExtension;
import org.alfresco.repo.node.db.traitextender.NodeServiceTrait; import org.alfresco.repo.node.db.traitextender.NodeServiceTrait;
import org.alfresco.repo.policy.BehaviourFilter; import org.alfresco.repo.policy.BehaviourFilter;
import org.alfresco.repo.security.authentication.AuthenticationUtil; import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport; import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport.TxnReadState; import org.alfresco.repo.transaction.AlfrescoTransactionSupport.TxnReadState;
import org.alfresco.repo.transaction.RetryingTransactionHelper; import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.transaction.TransactionalResourceHelper; import org.alfresco.repo.transaction.TransactionalResourceHelper;
import org.alfresco.service.cmr.dictionary.AspectDefinition; import org.alfresco.service.cmr.dictionary.AspectDefinition;
import org.alfresco.service.cmr.dictionary.AssociationDefinition; import org.alfresco.service.cmr.dictionary.AssociationDefinition;
import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition; import org.alfresco.service.cmr.dictionary.ChildAssociationDefinition;
import org.alfresco.service.cmr.dictionary.ClassDefinition; import org.alfresco.service.cmr.dictionary.ClassDefinition;
import org.alfresco.service.cmr.dictionary.InvalidAspectException; import org.alfresco.service.cmr.dictionary.InvalidAspectException;
import org.alfresco.service.cmr.dictionary.InvalidTypeException; import org.alfresco.service.cmr.dictionary.InvalidTypeException;
import org.alfresco.service.cmr.dictionary.PropertyDefinition; import org.alfresco.service.cmr.dictionary.PropertyDefinition;
import org.alfresco.service.cmr.dictionary.TypeDefinition; import org.alfresco.service.cmr.dictionary.TypeDefinition;
import org.alfresco.service.cmr.repository.AssociationExistsException; import org.alfresco.service.cmr.repository.AssociationExistsException;
import org.alfresco.service.cmr.repository.AssociationRef; import org.alfresco.service.cmr.repository.AssociationRef;
import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.InvalidChildAssociationRefException; import org.alfresco.service.cmr.repository.InvalidChildAssociationRefException;
import org.alfresco.service.cmr.repository.InvalidNodeRefException; import org.alfresco.service.cmr.repository.InvalidNodeRefException;
import org.alfresco.service.cmr.repository.InvalidStoreRefException; import org.alfresco.service.cmr.repository.InvalidStoreRefException;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeRef.Status; import org.alfresco.service.cmr.repository.NodeRef.Status;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.Path; import org.alfresco.service.cmr.repository.Path;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter; import org.alfresco.service.cmr.repository.datatype.DefaultTypeConverter;
import org.alfresco.service.cmr.security.AccessPermission; import org.alfresco.service.cmr.security.AccessPermission;
import org.alfresco.service.cmr.security.AccessStatus; import org.alfresco.service.cmr.security.AccessStatus;
import org.alfresco.service.cmr.security.OwnableService; import org.alfresco.service.cmr.security.OwnableService;
import org.alfresco.service.cmr.security.PermissionService; import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.QNamePattern; import org.alfresco.service.namespace.QNamePattern;
import org.alfresco.service.namespace.RegexQNamePattern; import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.traitextender.AJProxyTrait; import org.alfresco.traitextender.AJProxyTrait;
import org.alfresco.traitextender.Extend; import org.alfresco.traitextender.Extend;
import org.alfresco.traitextender.ExtendedTrait; import org.alfresco.traitextender.ExtendedTrait;
import org.alfresco.traitextender.Extensible; import org.alfresco.traitextender.Extensible;
import org.alfresco.traitextender.Trait; import org.alfresco.traitextender.Trait;
import org.alfresco.util.EqualsHelper; import org.alfresco.util.EqualsHelper;
import org.alfresco.util.GUID; import org.alfresco.util.GUID;
import org.alfresco.util.Pair; import org.alfresco.util.Pair;
import org.alfresco.util.ParameterCheck; import org.alfresco.util.ParameterCheck;
import org.alfresco.util.PropertyMap; import org.alfresco.util.PropertyMap;
import org.alfresco.util.transaction.TransactionListenerAdapter; import org.alfresco.util.transaction.TransactionListenerAdapter;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.extensions.surf.util.I18NUtil; import org.springframework.extensions.surf.util.I18NUtil;
/** /**
@@ -844,26 +844,25 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl implements Extens
// get the node // get the node
final Pair<Long, NodeRef> nodePair = getNodePairNotNull(nodeRef); final Pair<Long, NodeRef> nodePair = getNodePairNotNull(nodeRef);
final Long nodeId = nodePair.getFirst(); final Long nodeId = nodePair.getFirst();
boolean hadAspect = nodeDAO.hasNodeAspect(nodeId, aspectTypeQName); if (!nodeDAO.hasNodeAspect(nodeId, aspectTypeQName))
{
return;
}
// Invoke policy behaviours // Invoke policy behaviours
invokeBeforeUpdateNode(nodeRef); invokeBeforeUpdateNode(nodeRef);
if (hadAspect) invokeBeforeRemoveAspect(nodeRef, aspectTypeQName);
{ nodeDAO.removeNodeAspects(nodeId, Collections.singleton(aspectTypeQName));
invokeBeforeRemoveAspect(nodeRef, aspectTypeQName);
nodeDAO.removeNodeAspects(nodeId, Collections.singleton(aspectTypeQName));
}
AspectDefinition aspectDef = dictionaryService.getAspect(aspectTypeQName); AspectDefinition aspectDef = dictionaryService.getAspect(aspectTypeQName);
boolean updated = false; boolean updated = false;
if (aspectDef != null) if (aspectDef != null)
{ {
// Remove default properties // Remove default properties
Map<QName,PropertyDefinition> propertyDefs = aspectDef.getProperties(); Map<QName, PropertyDefinition> propertyDefs = aspectDef.getProperties();
Set<QName> propertyToRemoveQNames = propertyDefs.keySet(); Set<QName> propertyToRemoveQNames = propertyDefs.keySet();
nodeDAO.removeNodeProperties(nodeId, propertyToRemoveQNames); nodeDAO.removeNodeProperties(nodeId, propertyToRemoveQNames);
// Remove child associations // Remove child associations
// We have to iterate over the associations and remove all those between the parent and child // We have to iterate over the associations and remove all those between the parent and child
final List<Pair<Long, ChildAssociationRef>> assocsToDelete = new ArrayList<Pair<Long, ChildAssociationRef>>(5); final List<Pair<Long, ChildAssociationRef>> assocsToDelete = new ArrayList<Pair<Long, ChildAssociationRef>>(5);
@@ -875,29 +874,24 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl implements Extens
return true; return true;
} }
@Override @Override public boolean orderResults()
public boolean orderResults()
{ {
return false; return false;
} }
public boolean handle( public boolean handle(Pair<Long, ChildAssociationRef> childAssocPair, Pair<Long, NodeRef> parentNodePair,
Pair<Long, ChildAssociationRef> childAssocPair, Pair<Long, NodeRef> childNodePair)
Pair<Long, NodeRef> parentNodePair,
Pair<Long, NodeRef> childNodePair
)
{ {
if (isPendingDelete(parentNodePair.getSecond()) || isPendingDelete(childNodePair.getSecond())) if (isPendingDelete(parentNodePair.getSecond()) || isPendingDelete(childNodePair.getSecond()))
{ {
if (logger.isTraceEnabled()) if (logger.isTraceEnabled())
{ {
logger.trace( logger.trace("Aspect-triggered association removal: "
"Aspect-triggered association removal: " + + "Ignoring child associations where one of the nodes is pending delete: " + childAssocPair);
"Ignoring child associations where one of the nodes is pending delete: " + childAssocPair);
} }
return true; return true;
} }
// Double check that it's not a primary association. If so, we can't delete it and // Double check that it's not a primary association. If so, we can't delete it and
// have to delete the child node directly and with full archival. // have to delete the child node directly and with full archival.
if (childAssocPair.getSecond().isPrimary()) if (childAssocPair.getSecond().isPrimary())
@@ -914,7 +908,7 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl implements Extens
public void done() public void done()
{ {
} }
}; };
// Get all the QNames to remove // Get all the QNames to remove
Set<QName> assocTypeQNamesToRemove = new HashSet<QName>(aspectDef.getChildAssociations().keySet()); Set<QName> assocTypeQNamesToRemove = new HashSet<QName>(aspectDef.getChildAssociations().keySet());
@@ -930,14 +924,14 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl implements Extens
nodeDAO.deleteChildAssoc(assocId); nodeDAO.deleteChildAssoc(assocId);
invokeOnDeleteChildAssociation(assocRef); invokeOnDeleteChildAssociation(assocRef);
} }
// Cascade-delete any nodes that were attached to primary associations // Cascade-delete any nodes that were attached to primary associations
for (Pair<Long, NodeRef> childNodePair : nodesToDelete) for (Pair<Long, NodeRef> childNodePair : nodesToDelete)
{ {
NodeRef childNodeRef = childNodePair.getSecond(); NodeRef childNodeRef = childNodePair.getSecond();
this.deleteNode(childNodeRef); this.deleteNode(childNodeRef);
} }
// Gather peer associations to delete // Gather peer associations to delete
Map<QName, AssociationDefinition> nodeAssocDefs = aspectDef.getAssociations(); Map<QName, AssociationDefinition> nodeAssocDefs = aspectDef.getAssociations();
List<Long> nodeAssocIdsToRemove = new ArrayList<Long>(13); List<Long> nodeAssocIdsToRemove = new ArrayList<Long>(13);
@@ -949,8 +943,8 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl implements Extens
if (logger.isTraceEnabled()) if (logger.isTraceEnabled())
{ {
logger.trace( logger.trace(
"Aspect-triggered association removal: " + "Aspect-triggered association removal: " + "Ignoring peer associations where one of the nodes is pending delete: "
"Ignoring peer associations where one of the nodes is pending delete: " + nodeRef); + nodeRef);
} }
continue; continue;
} }
@@ -967,9 +961,8 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl implements Extens
{ {
if (logger.isTraceEnabled()) if (logger.isTraceEnabled())
{ {
logger.trace( logger.trace("Aspect-triggered association removal: "
"Aspect-triggered association removal: " + + "Ignoring peer associations where one of the nodes is pending delete: " + assocPair);
"Ignoring peer associations where one of the nodes is pending delete: " + assocPair);
} }
continue; continue;
} }
@@ -990,16 +983,15 @@ public class DbNodeServiceImpl extends AbstractNodeServiceImpl implements Extens
} }
updated = updated || assocsDeleted > 0; updated = updated || assocsDeleted > 0;
} }
// Invoke policy behaviours // Invoke policy behaviours
if (updated) if (updated)
{ {
invokeOnUpdateNode(nodeRef); invokeOnUpdateNode(nodeRef);
} }
if (hadAspect)
{ invokeOnRemoveAspect(nodeRef, aspectTypeQName);
invokeOnRemoveAspect(nodeRef, aspectTypeQName);
}
} }
/** /**

View File

@@ -31,10 +31,12 @@ import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.StoreRef; import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName; import org.alfresco.service.namespace.QName;
import org.alfresco.test_category.BaseSpringTestsCategory; import org.alfresco.test_category.BaseSpringTestsCategory;
import org.alfresco.util.BaseSpringTest; import org.alfresco.util.BaseSpringTest;
import org.alfresco.util.GUID; import org.alfresco.util.GUID;
import org.alfresco.util.PropertyMap;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.experimental.categories.Category; import org.junit.experimental.categories.Category;
@@ -130,4 +132,56 @@ public class RemoveFeaturesActionExecuterTest extends BaseSpringTest
action2.setParameterValue(RemoveFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE); action2.setParameterValue(RemoveFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_VERSIONABLE);
this.executer.execute(action2, this.nodeRef); this.executer.execute(action2, this.nodeRef);
} }
/**
* Test removing aspect properties
*/
@Test
public void testRemovingAspectPropertiesAfterExecution()
{
QName QNAME_PUBLISHER = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "publisher");
QName QNAME_SUBJECT = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "subject");
// Execute the action
PropertyMap dublinCoreProperties = new PropertyMap(2);
dublinCoreProperties.put(QNAME_PUBLISHER, "publisher");
dublinCoreProperties.put(QNAME_SUBJECT, "subject");
nodeService.addAspect(nodeRef, ContentModel.ASPECT_DUBLINCORE, dublinCoreProperties);
// Check that the node has aspect properties
assertTrue(this.nodeService.hasAspect(this.nodeRef, ContentModel.ASPECT_DUBLINCORE));
assertTrue(this.nodeService.getProperties(this.nodeRef).containsKey(QNAME_PUBLISHER));
assertTrue(this.nodeService.getProperties(this.nodeRef).containsKey(QNAME_SUBJECT));
// Remove the aspect
ActionImpl action = new ActionImpl(null, ID, RemoveFeaturesActionExecuter.NAME, null);
action.setParameterValue(RemoveFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_DUBLINCORE);
this.executer.execute(action, this.nodeRef);
// Check that the node now no longer has aspect properties
assertFalse(this.nodeService.getProperties(this.nodeRef).containsKey(QNAME_PUBLISHER));
assertFalse(this.nodeService.getProperties(this.nodeRef).containsKey(QNAME_SUBJECT));
}
/**
* Test removing not added child aspect
*/
@Test
public void testRemovingNotAddedChildAspect()
{
QName QNAME_TITLE = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "title");
// Execute the action
PropertyMap titledProperties = new PropertyMap(1);
titledProperties.put(QNAME_TITLE, "title");
nodeService.addAspect(nodeRef, ContentModel.ASPECT_TITLED, titledProperties);
// Remove the child aspect which has not been added to the node
ActionImpl action = new ActionImpl(null, ID, RemoveFeaturesActionExecuter.NAME, null);
action.setParameterValue(RemoveFeaturesActionExecuter.PARAM_ASPECT_NAME, ContentModel.ASPECT_DUBLINCORE);
this.executer.execute(action, this.nodeRef);
// Now check that the node has parent aspect properties
assertTrue(this.nodeService.getProperties(this.nodeRef).containsKey(QNAME_TITLE));
}
} }