AnsweredAssumed Answered

add the behavior in the aspect

Question asked by forrest43 on Nov 22, 2012
Latest reply on Nov 22, 2012 by erikwinlof
I am looking for a function that when user add/modify the content in some folders, it can auto select the type according to the data and type of the content, and fullfill the corresponding properties.

So I created an aspect, add the behavior in the aspect, and add this aspect to the specific folder to  achieve the funciton.

I defined behavior as followed,


public class ScanContentBehavior implements NodeServicePolicies.BeforeCreateStorePolicy,
                                 NodeServicePolicies.OnCreateStorePolicy,
                                 NodeServicePolicies.BeforeCreateNodePolicy,
                                 NodeServicePolicies.OnCreateNodePolicy,
                                 NodeServicePolicies.OnMoveNodePolicy,
                                 NodeServicePolicies.BeforeUpdateNodePolicy,
                                 NodeServicePolicies.OnUpdateNodePolicy,
                                 NodeServicePolicies.OnUpdatePropertiesPolicy,
                                 NodeServicePolicies.BeforeDeleteNodePolicy,
                                 NodeServicePolicies.OnDeleteNodePolicy,
                                 NodeServicePolicies.BeforeAddAspectPolicy,
                                 NodeServicePolicies.OnAddAspectPolicy,
                                 NodeServicePolicies.BeforeRemoveAspectPolicy,
                                 NodeServicePolicies.OnRemoveAspectPolicy,
                                 NodeServicePolicies.BeforeCreateNodeAssociationPolicy,
                                 NodeServicePolicies.OnCreateNodeAssociationPolicy,
                                 NodeServicePolicies.OnCreateChildAssociationPolicy,
                                 NodeServicePolicies.BeforeDeleteChildAssociationPolicy,
                                 NodeServicePolicies.OnDeleteChildAssociationPolicy,
                                 NodeServicePolicies.OnCreateAssociationPolicy,
                                 NodeServicePolicies.OnDeleteAssociationPolicy{

    private NodeService nodeService;
    private PolicyComponent policyComponent;
    private  FileFolderService fileFolderService;
   
    // Behaviours 
    private Behaviour beforeCreateStorePolicy;
    private Behaviour onCreateStorePolicy;
    private Behaviour beforeCreateNodePolicy;
    private Behaviour onCreateNodePolicy;
    private Behaviour onMoveNodePolicy;
    private Behaviour beforeUpdateNodePolicy;
    private Behaviour onUpdateNodePolicy;
    private Behaviour onUpdatePropertiesPolicy;
    private Behaviour beforeDeleteNodePolicy;
    private Behaviour onDeleteNodePolicy;
    private Behaviour beforeAddAspectPolicy;
    private Behaviour onAddAspectPolicy;
    private Behaviour beforeRemoveAspectPolicy;
    private Behaviour onRemoveAspectPolicy;
    private Behaviour beforeCreateNodeAssociationPolicy;
    private Behaviour onCreateNodeAssociationPolicy;
    private Behaviour onCreateChildAssociationPolicy;
    private Behaviour beforeDeleteChildAssociationPolicy;
    private Behaviour onDeleteChildAssociationPolicy;
    private Behaviour onCreateAssociationPolicy;
    private Behaviour onDeleteAssociationPolicy;
   
   
   
    private Logger logger = Logger.getLogger(ScanContentBehavior.class);
   
    public void init() {
       if (logger.isDebugEnabled()) logger.debug("Initializing scan content behaviors");
        // Create behaviours
       this.beforeCreateStorePolicy  = new JavaBehaviour(this, "beforeCreateStore", NotificationFrequency.TRANSACTION_COMMIT);
       this.onCreateStorePolicy  = new JavaBehaviour(this, "onCreateStore", NotificationFrequency.TRANSACTION_COMMIT);
       this.beforeCreateNodePolicy  = new JavaBehaviour(this, "beforeCreateNode", NotificationFrequency.TRANSACTION_COMMIT);
       this.onCreateNodePolicy  = new JavaBehaviour(this, "onCreateNode", NotificationFrequency.TRANSACTION_COMMIT);
       this.onMoveNodePolicy  = new JavaBehaviour(this, "onMoveNode", NotificationFrequency.TRANSACTION_COMMIT);
       this.beforeUpdateNodePolicy  = new JavaBehaviour(this, "beforeUpdateNode", NotificationFrequency.TRANSACTION_COMMIT);
       this.onUpdateNodePolicy  = new JavaBehaviour(this, "onUpdateNode", NotificationFrequency.TRANSACTION_COMMIT);
       this.onUpdatePropertiesPolicy  = new JavaBehaviour(this, "onUpdateProperties", NotificationFrequency.TRANSACTION_COMMIT);
       this.beforeDeleteNodePolicy  = new JavaBehaviour(this, "beforeDeleteNode", NotificationFrequency.TRANSACTION_COMMIT);
       this.onDeleteNodePolicy  = new JavaBehaviour(this, "onDeleteNode", NotificationFrequency.TRANSACTION_COMMIT);
       this.beforeAddAspectPolicy  = new JavaBehaviour(this, "beforeAddAspect", NotificationFrequency.TRANSACTION_COMMIT);
       this.onAddAspectPolicy  = new JavaBehaviour(this, "onAddAspect", NotificationFrequency.TRANSACTION_COMMIT);
       this.beforeRemoveAspectPolicy  = new JavaBehaviour(this, "beforeRemoveAspect", NotificationFrequency.TRANSACTION_COMMIT);
       this.onRemoveAspectPolicy  = new JavaBehaviour(this, "onRemoveAspect", NotificationFrequency.TRANSACTION_COMMIT);
       this.beforeCreateNodeAssociationPolicy  = new JavaBehaviour(this, "beforeCreateNodeAssociation", NotificationFrequency.TRANSACTION_COMMIT);
       this.onCreateNodeAssociationPolicy  = new JavaBehaviour(this, "onCreateNodeAssociation", NotificationFrequency.TRANSACTION_COMMIT);
       this.onCreateChildAssociationPolicy  = new JavaBehaviour(this, "onCreateChildAssociation", NotificationFrequency.TRANSACTION_COMMIT);
       this.beforeDeleteChildAssociationPolicy  = new JavaBehaviour(this, "beforeDeleteChildAssociation", NotificationFrequency.TRANSACTION_COMMIT);
       this.onDeleteChildAssociationPolicy  = new JavaBehaviour(this, "onDeleteChildAssociation", NotificationFrequency.TRANSACTION_COMMIT);
       this.onCreateAssociationPolicy  = new JavaBehaviour(this, "onCreateAssociation", NotificationFrequency.TRANSACTION_COMMIT);
       this.onDeleteAssociationPolicy  = new JavaBehaviour(this, "onDeleteAssociation", NotificationFrequency.TRANSACTION_COMMIT);
       
        // Bind behaviours to node policies
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeCreateStorePolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.beforeCreateStorePolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnCreateStorePolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onCreateStorePolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeCreateNodePolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.beforeCreateNodePolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnCreateNodePolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onCreateNodePolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnMoveNodePolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onMoveNodePolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeUpdateNodePolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.beforeUpdateNodePolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnUpdateNodePolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onUpdateNodePolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnUpdatePropertiesPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onUpdatePropertiesPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeDeleteNodePolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.beforeDeleteNodePolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnDeleteNodePolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onDeleteNodePolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeAddAspectPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.beforeAddAspectPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnAddAspectPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onAddAspectPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeRemoveAspectPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.beforeRemoveAspectPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnRemoveAspectPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onRemoveAspectPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeCreateNodeAssociationPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.beforeCreateNodeAssociationPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnCreateNodeAssociationPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onCreateNodeAssociationPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnCreateChildAssociationPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onCreateChildAssociationPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.BeforeDeleteChildAssociationPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.beforeDeleteChildAssociationPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnDeleteChildAssociationPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onDeleteChildAssociationPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnCreateAssociationPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onCreateAssociationPolicy);
        this.policyComponent.bindClassBehaviour(NodeServicePolicies.OnDeleteAssociationPolicy.QNAME, ErryContentModel.NAMESPACE_ERRY_CONTENT_SCAN, this.onDeleteAssociationPolicy);
   
   
   
   
   
   
   
   
    }
   
   @Override
   public void onCreateChildAssociation(ChildAssociationRef childAssocRef,
         boolean isNewNode) {
       NodeRef childNodeRef = childAssocRef.getChildRef();
       String fileName = (String) nodeService.getProperty(childAssocRef.getChildRef(), ContentModel.PROP_NAME);
       Map<QName, Serializable> map = nodeService.getProperties(childAssocRef.getChildRef());
      
         ContentDataWithId cdi = (ContentDataWithId)nodeService.getProperty(childNodeRef, ContentModel.PROP_CONTENT);
         nodeService.getProperty(childNodeRef, ContentModel.TYPE_SYSTEM_FOLDER);
         nodeService.getProperty(childNodeRef, ContentModel.TYPE_FOLDER);
         ByteArrayOutputStream ops = new ByteArrayOutputStream();
         fileFolderService.getReader(childNodeRef).getContent(ops);
         byte b[] = ops.toByteArray();
         System.out.println(fileName);
         System.out.println(new String(b));
      
   }
   
   
   @Override
   public void onCreateNodeAssociation(ChildAssociationRef childAssocRef) {
      
       NodeRef childNodeRef = childAssocRef.getChildRef();
       String fileName = (String) nodeService.getProperty(childAssocRef.getChildRef(), ContentModel.PROP_NAME);
       Map<QName, Serializable> map = nodeService.getProperties(childAssocRef.getChildRef());
      
         ContentDataWithId cdi = (ContentDataWithId)nodeService.getProperty(childNodeRef, ContentModel.PROP_CONTENT);
         nodeService.getProperty(childNodeRef, ContentModel.TYPE_SYSTEM_FOLDER);
         nodeService.getProperty(childNodeRef, ContentModel.TYPE_FOLDER);
         ByteArrayOutputStream ops = new ByteArrayOutputStream();
         fileFolderService.getReader(childNodeRef).getContent(ops);
         byte b[] = ops.toByteArray();
         System.out.println(fileName);
         System.out.println(new String(b));
      
   }

   @Override
   public void onUpdateNode(NodeRef childNodeRef) {
      if(!fileFolderService.getFileInfo(childNodeRef).isFolder()){
         ContentDataWithId cdi = (ContentDataWithId)nodeService.getProperty(childNodeRef, ContentModel.PROP_CONTENT);
           nodeService.getProperty(childNodeRef, ContentModel.TYPE_SYSTEM_FOLDER);
           nodeService.getProperty(childNodeRef, ContentModel.TYPE_FOLDER);
           ByteArrayOutputStream ops = new ByteArrayOutputStream();
           fileFolderService.getReader(childNodeRef).getContent(ops);
           byte b[] = ops.toByteArray();
           System.out.println(new String(b));
      }
   }

   
   public NodeService getNodeService() {
      return nodeService;
   }


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


   public PolicyComponent getPolicyComponent() {
      return policyComponent;
   }


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

   public FileFolderService getFileFolderService() {
      return fileFolderService;
   }

   public void setFileFolderService(FileFolderService fileFolderService) {
      this.fileFolderService = fileFolderService;
   }

   @Override
   public void onDeleteAssociation(AssociationRef nodeAssocRef) {
      // TODO Auto-generated method stub
   }


   @Override
   public void onCreateAssociation(AssociationRef nodeAssocRef) {
      // TODO Auto-generated method stub
   }

   @Override
   public void onDeleteChildAssociation(ChildAssociationRef childAssocRef) {
      // TODO Auto-generated method stub
   }

   @Override
   public void beforeDeleteChildAssociation(ChildAssociationRef childAssocRef) {
      // TODO Auto-generated method stub
   }

   @Override
   public void beforeCreateNodeAssociation(NodeRef parentNodeRef,
         QName assocTypeQName, QName assocQName) {
      // TODO Auto-generated method stub
   }

   @Override
   public void onRemoveAspect(NodeRef nodeRef, QName aspectTypeQName) {
      // TODO Auto-generated method stub
   }

   @Override
   public void beforeRemoveAspect(NodeRef nodeRef, QName aspectTypeQName) {
      // TODO Auto-generated method stub
   }

   @Override
   public void onAddAspect(NodeRef nodeRef, QName aspectTypeQName) {
      // TODO Auto-generated method stub
   }

   @Override
   public void beforeAddAspect(NodeRef nodeRef, QName aspectTypeQName) {
      // TODO Auto-generated method stub
   }

   @Override
   public void onDeleteNode(ChildAssociationRef childAssocRef,
         boolean isNodeArchived) {
      // TODO Auto-generated method stub
   }

   @Override
   public void beforeDeleteNode(NodeRef nodeRef) {
      // TODO Auto-generated method stub
   }

   @Override
   public void onUpdateProperties(NodeRef nodeRef,
         Map<QName, Serializable> before, Map<QName, Serializable> after) {
      // TODO Auto-generated method stub
   }

   @Override
   public void beforeUpdateNode(NodeRef nodeRef) {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void onMoveNode(ChildAssociationRef oldChildAssocRef,
         ChildAssociationRef newChildAssocRef) {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void onCreateNode(ChildAssociationRef childAssocRef) {
      // TODO Auto-generated method stub
      
   }

   @Override
   public void beforeCreateNode(NodeRef parentRef, QName assocTypeQName,
         QName assocQName, QName nodeTypeQName) {
      // TODO Auto-generated method stub
      
      
   }

   @Override
   public void onCreateStore(NodeRef rootNodeRef) {
      
      
   }

   @Override
   public void beforeCreateStore(QName nodeTypeQName, StoreRef storeRef) {
      
      
   }
}


What confused me is that, when I added/modified the content in the folder, only the beforeUpdateNode was triggered, I could only get the nodeRef of the folder, but I was unable to get the nodeRef of the content that I added/modified.

An other question is why OnCreateChildAssociationPolicy hasn’t been triggered when the file being created?

Outcomes