cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with Alfresco Behavior: BeforeCreateNodePolicy & BeforeSetNodeTypePolicy

hiten_rastogi1
Star Contributor
Star Contributor
Hi Folks,

I am facing issue with alfresco behaviors: <strong>BeforeCreateNodePolicy & BeforeSetNodeTypePolicy</strong>, and they are not working as I expect them to. I have bonded them to a custom folder type <strong>evgb:country</strong>. How i expect them to work is,

1. <strong>BeforeCreateNodePolicy</strong> - As soon as I create custom country folder from Create… menu option in document library alfresco should invoke the behavior and do the checking but instead it take it to a form defined for country folder type and when the user hit the create button alfresco invoked the behavior and do the checking. Please correct me if I am wrong on this and how can I achieve the functionality.

2. <strong>BeforeSetNodeTypePolicy</strong> - After creating a normal folder type when I change the type of it to <strong>evgb:country</strong> the behavior should be invoked but nothing happens though when I am applying the same behavior to my custom document type then it is working.


import org.alfresco.error.AlfrescoRuntimeException;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.node.NodeServicePolicies;
import org.alfresco.repo.policy.Behaviour;
import org.alfresco.repo.policy.Behaviour.NotificationFrequency;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.apache.log4j.Logger;

import com.models.DocModel;
import com.models.FolModel;

/**
*
* Class used for checking validity of country folder type
*
*
*/

public class CountryFolderCheckBehaviour implements
      NodeServicePolicies.BeforeCreateNodePolicy,
      NodeServicePolicies.BeforeSetNodeTypePolicy {

   // Dependencies
   private NodeService nodeService;
   private PolicyComponent policyComponent;

   // Behaviours
   private Behaviour beforeCreateNode;
   private Behaviour beforeSetNodeType;

   private Logger logger = Logger.getLogger(CountryFolderCheckBehaviour.class);

   public void init() {

      if (logger.isDebugEnabled())
         logger.debug("******************* Initializing case country folder check *******************");

      // Create behaviours
      this.beforeCreateNode = new JavaBehaviour(this, "beforeCreateNode",
            NotificationFrequency.TRANSACTION_COMMIT);

      this.beforeSetNodeType = new JavaBehaviour(this, "beforeSetNodeType",
            NotificationFrequency.TRANSACTION_COMMIT);

      // Bind behaviours to node policies
      this.policyComponent.bindClassBehaviour(QName.createQName(
            NamespaceService.ALFRESCO_URI, "beforeCreateNode"),
            FolModel.COUNTRY_FOL,
            this.beforeCreateNode);

      this.policyComponent.bindClassBehaviour(QName.createQName(
            NamespaceService.ALFRESCO_URI, "beforeSetNodeType"),
            FolModel.COUNTRY_FOL,
            this.beforeSetNodeType);

   }

   @Override
   public void beforeCreateNode(NodeRef parentRef, QName assocTypeQName,
         QName assocQName, QName nodeTypeQName) {
      if (logger.isDebugEnabled())
         logger.debug("Inside beforeCreateNode");
      checkFolderValidity(parentRef);
   }

   @Override
   public void beforeSetNodeType(NodeRef parentRef, QName oldType,
         QName newType) {
      if (logger.isDebugEnabled())
         logger.debug("Inside beforeSetNodeType");
      checkFolderValidity(parentRef);
   }

   /**
    * Method used for checking validity for country folder type
    *
    * @param parentNodeName
    *            name of the parent node
    *
    */
   private void checkFolderValidity(NodeRef parentNode) {

      String parentNodeName = (nodeService.getProperty(parentNode,
            ContentModel.PROP_NAME)).toString();
      try {
         if (!parentNodeName.equals("documentLibrary")) {
            throw new AlfrescoRuntimeException(
                  "Cannot create country type folder");
         }
      } finally {
                     if (logger.isDebugEnabled())
         logger.debug("******************* Creating country folder *******************");

      }

   }

   public void setNodeService(NodeService nodeService) {
      this.nodeService = nodeService;
   }

   public void setPolicyComponent(PolicyComponent policyComponent) {
      this.policyComponent = policyComponent;
   }

}


Please help

EDIT-1: Please let me know if anyone has any ideas on this ?? Specifically why the BeforeSetNodeTypePolicy is not working on the Folder.
3 REPLIES 3

kavilash23
Champ on-the-rise
Champ on-the-rise
Hi,
It may be other behaviours are running prior to yours.

Try putting the following before invoking your methods in the java class.
behaviourFilter.disableBehaviour();
checkFolderValidity(parentRef)
behaviourFilter.enableBehaviour();

niketapatel
Star Contributor
Star Contributor

Hi Hiten,

For your 'BeforeCreateNodePolicy', I am not sure what customization you have done but I believe when you click 'Create… menu option' at that time its NOT calling alfresco data script and so node is NOT created and so your registered policy behavior is NOT being executed. When you actually fill up form and hit create button at that time only actual Node is created and so your registered behavior is being executed.

For your query - "EDIT-1: Please let me know if anyone has any ideas on this ?? Specifically why the BeforeSetNodeTypePolicy is not working on the Folder."
you need to use - OnSetNodeTypePolicy. Check this - https://issues.alfresco.com/jira/browse/MNT-2212
I am not sure for alfresco 5.0 but I believe there is not any change in API so should be working in your case.

Hope this helps!

Thanks Niketa. Your suggestion was helpful in solving my problem.
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.