cancel
Showing results for 
Search instead for 
Did you mean: 

How to delete a folder using rules.

satheeshkumar
Champ in-the-making
Champ in-the-making
Hi All,

I have created a COPY Rule on one of my folder(say MyFolder), this rule will copy all the folders I create under (MyFolder) to another destination (say MyCopyFolder).
So when ever I create a folder under (MyFolder) I get a copy under another folder(MyCopyFolder). This COPY Rule works perfectly.

But now when I delete a folder from(MyFolder), copy of the same folder which is there in (MyCopyFolder) should also get deleted, but there is no straight forward rule that I can use it for, so I have decided to create a new rule and assign "Execute Script" action and referred my custom JS file.

After applying this rule I am not able to delete the folder(from MyFolder), there were no error in the logs too, so unable to triage, can someone please help me on this.

My Bean configuration,


//I have registered my Java class in the bean as shown below,

<bean id="${project.artifactId}_updateProduct" parent="baseJavaScriptExtension" class="mypackage.UpdateProduct" lazy-init="default" autowire="default" dependency-check="default">
      <property name="extensionName">
         <value>updateProduct</value>
      </property>
      <property name="serviceRegistry" ref="ServiceRegistry"/>
   </bean>




//My JS file is as shown below,
function delete_folders_main(){   
   logger.log("Execution Started!");   
   //Getting the nodeRef of a document that is getting deleted.
   var deletedNodeRef = document.getNodeRef();
   //Setting the noderef in the Java class.   
   updateProduct.setDeletedNodeRef(deletedNodeRef);   
   logger.log("Execution Completed!");
}
deletefolders_main();




My Java class as shown below,

public class UpdateProduct extends BaseProcessorExtension {
protected static Logger logger = Logger.getLogger(UpdateProduct.class);

   private static ServiceRegistry serviceRegistry;
   
   private static NodeRef deletedNodeRef;

        //Getters and setters for these.


private void doDelete() {
      
      if(nodeService != null && nodeService.exists(deletedNodeRef)){
         //Extracting the name of the deleted folder.
         deletedFolderName = serviceRegistry.getFileFolderService().getFileInfo(deletedNodeRef).getName();
      }
      else{
         throw new AlfrescoRuntimeException("Unable to Extract the Folder Name from the passed NodeRef :"+deletedNodeRef);
      }
      NodeRef stagingProductNodeRef = new NodeRef(HCNodeReferences.NODEREF_MY_COPY_FOLDER);
      
      if (nodeService != null && nodeService.exists(stagingProductNodeRef)) {
         List<ChildAssociationRef> childAssoc = nodeService.getChildAssocs(stagingProductNodeRef);         
         for(ChildAssociationRef child : childAssoc){
            NodeRef childNodeRef = child.getChildRef();
            stagingFolderToUpdate = serviceRegistry.getFileFolderService().getFileInfo(childNodeRef).getName();
            
            //Finding the ChildFolder in Staging to delete.
            if(deletedFolderName != null && stagingFolderToUpdate != null && deletedFolderName.equals(stagingFolderToUpdate)){//this check properly takes me to the exact folder which I want to delete
               try {
                  serviceRegistry.getNodeService().deleteNode(childNodeRef);//No exception thrown in this step, but still my folder in not deleted.
                  break;
               }
               catch(Exception e){
                  logger.error(e.getMessage());
               }
               }
      }   
   }
}

public static void setDeletedNodeRef(NodeRef deletedNodeRef) {
      nodeService = serviceRegistry.getNodeService();
      if (nodeService != null && nodeService.exists(deletedNodeRef)){
         UpdateProduct.deletedNodeRef = deletedNodeRef;
         new UpdateProduct().doDelete();
         logger.warn("Delete action Completed successfully for the Folder :"+serviceRegistry.getFileFolderService().getFileInfo(deletedNodeRef).getName());
         deletedNodeRef = null;
         UpdateProduct.clearFields();
      }
      else{
         logger.error("There was an error while updating the Staging Folder");
      }
      
   }
}


1 REPLY 1

muralidharand
Star Contributor
Star Contributor
Hi Satheesh,
Does the folder is not getting deleted from the original folder also?
If the destination  folder (COPY / staging folder ) is predefined, I think, we should be able to do with the Javascript only.
Let me try some example and will come back to you.