AnsweredAssumed Answered

Issue with Alfresco Behavior: BeforeCreateNodePolicy & BeforeSetNodeTypePolicy

Question asked by hiten.rastogi on Jan 21, 2016
Latest reply on Jan 27, 2016 by hiten.rastogi
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.

Outcomes