cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Behavior isn't triggering

fergusonna
Champ in-the-making
Champ in-the-making
Hi, I'm trying to create a behavior that triggers everytime a piece of content enters in a custom folder type I've made. The Folder type simply extends the existing folder type and adds a property called expenseSum. I have a second content type defining an expenseReport, including a cost value. When a piece of content enters the folder, if the folder is of my custom type, it recalculates the expenseSum by getting all children of the folder, checking if they are expensereports, pulling the cost value, summing the value from all children, and then putting the calculated sum onto the folder properties.

I figured the best way to bind this behavior was to bind it to the onCreateChildAssociation and onDeleteChildAssociation policies, but nothing seems to activate the behavior. I have log statements that announce when each method starts, but the only one that is ever in the logs is that the init() method starts.

I've included the majority of the code, excluding setter methods and import/package information. If there is anything else needed, please just ask.


public class SumExpense implements OnCreateChildAssociationPolicy, OnDeleteChildAssociationPolicy{

   
   private NodeService nodeService;
   private PolicyComponent policyComponent;
   
   // Behaviours
    private Behaviour onCreateChildAssociation;
    private Behaviour onDeleteChildAssociation;
   
   private Logger logger = Logger.getLogger(SumExpense.class);
   
   public void init() {
      if (logger.isDebugEnabled()){
         logger.debug("Initializing SumExpense behavior");
      }
      
      //Create behaviours
      this.onCreateChildAssociation = new JavaBehaviour(this, "onCreateChildAssociation" , Behaviour.NotificationFrequency.TRANSACTION_COMMIT);
      this.onDeleteChildAssociation = new JavaBehaviour(this, "onDeleteChildAssociation" , Behaviour.NotificationFrequency.TRANSACTION_COMMIT);
      
      //Bind behaviours to node policies
      this.policyComponent.bindAssociationBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onCreateChildAssociation"),
            ContentModel.TYPE_CONTENT,
            this.onCreateChildAssociation);
      this.policyComponent.bindAssociationBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onDeleteChildAssociation"),
            ContentModel.TYPE_CONTENT,
            this.onDeleteChildAssociation);
   }
   
   @Override
   public void onCreateChildAssociation(ChildAssociationRef childAssocRef, boolean arg1) {
      if (logger.isDebugEnabled()){
         logger.debug("Inside onCreateChildAssociation");
      }
      computeSum(childAssocRef);
   }
   
   @Override
   public void onDeleteChildAssociation(ChildAssociationRef childAssocRef) {
      if (logger.isDebugEnabled()){
         logger.debug("Inside onDeleteChildAssociation");
      }
      computeSum(childAssocRef);
   }
            
   public void computeSum(ChildAssociationRef childAssocRef){
      if (logger.isDebugEnabled()){
         logger.debug("Inside computeSum");
      }
      
      //get the parent node
      NodeRef parentRef = childAssocRef.getParentRef();
      QName parentType = nodeService.getType(parentRef);
      
      QName expenseFolder = QName.createQName(MyCoBehaviorModel.NAMESPACE_MYCO_BEHAVIOR_MODEL, MyCoBehaviorModel.TYPE_MYB_EXPENSE_FOLDER);
      QName expenseReport = QName.createQName(MyCoModel.NAMESPACE_MYCO_CONTENT_MODEL, MyCoModel.TYPE_MY_EXPENSE_REPORT); 
      QName totalAmount = QName.createQName(MyCoModel.NAMESPACE_MYCO_CONTENT_MODEL, MyCoModel.PROP_MY_TOTAL_AMOUNT);
      QName expenseSum = QName.createQName(MyCoBehaviorModel.NAMESPACE_MYCO_BEHAVIOR_MODEL, MyCoBehaviorModel.PROP_MYB_EXPENSE_SUM);
      
      if (parentType.isMatch(parentType)){
         if (logger.isDebugEnabled()){
            logger.debug("Parent is an expense folder");
         }
         
         logger.debug("getting parent's children");
         
         List<ChildAssociationRef> children = nodeService.getChildAssocs(parentRef);
         
         Double expenseTotal = 0.0;
         logger.debug("expense sum: should be 0.0: " + expenseSum);
         
         if (children.size() == 0){
            if (logger.isDebugEnabled()){
               logger.debug("No children found.");
            }
         } else {
            if (logger.isDebugEnabled()){
               logger.debug("Children found.");
            }
            for (ChildAssociationRef child : children){
               logger.debug("In children loop");
               
               NodeRef childRef = child.getChildRef();
               QName childType = nodeService.getType(childRef);
               
               if (childType.isMatch(expenseReport)){
                  logger.debug("child is expense report");
                  Double expenseVal = (Double) nodeService.getProperty(childRef, totalAmount);
                  expenseTotal += expenseVal;
                  logger.debug("ExpenseSum: " + expenseTotal + " ExpenseVal: " + expenseVal);
               }
            }
            
            Map<QName, Serializable> parentProps = nodeService.getProperties(parentRef);
            parentProps.put(expenseSum, expenseTotal);
            
            nodeService.setProperties(parentRef, parentProps);
            
         }
         
         
      }
      
      return;
   }
      
2 REPLIES 2

douglascrp
World-Class Innovator
World-Class Innovator
Try to change your bind code to use TYPE_FOLDER instead of TYPE_CONTENT

//Bind behaviours to node policies
      this.policyComponent.bindAssociationBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onCreateChildAssociation"),
            ContentModel.TYPE_FOLDER,
            this.onCreateChildAssociation);
      this.policyComponent.bindAssociationBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onDeleteChildAssociation"),
            ContentModel.TYPE_FOLDER,
            this.onDeleteChildAssociation);

fergusonna
Champ in-the-making
Champ in-the-making
That worked! Thanks.