Sample Automation Scripts

Creates a new file on the basis of another existing document

# if the user is not available, take it from the event
#if(!$user)
    #set($user = $event.user)
#end

# if the current folder is not available, take it from the event
#if(!$folder)
    #set($folder = $event.folder)    
#end

# get the document referenced by the current event
#set($document = $DocTool.findById($event.docId)) 

#if($user.isMemberOf("admin"))
    $log.info("user is admin");
    
    #set ($ctime = $DateTool.currentTime())
    $log.info("current time is $ctime");
    
    $DateTool.setDateFormatShort('yyyy-MM-dd')
    #set ($fdate = $DateTool.format($ctime,false))
    $log.info("fdate $fdate");
    
    # get the custom attribute 'output' of the folder
    #set ($val1 = $folder.getValue('output'))
    $log.info("val1 is $val1");
    #if( !$val1 )
        #set ($val1 = '')
    #end       
    
    # get the custom attribute 'attr1' of the document and update val1
    #set($val1 = $val1 + $document.getValue('attr1') +" - " + $fdate)

    # update the custom attribute 'output' of the fodler with the new val1
    $folder.setValue('output', $val1);
    
    # instantiate a new event for the folder(class FolderHistory) and prepare it
    #set ( $history = $ClassTool.newInstance("core.folder.FolderHistory") )
    $history.setUser($user);
    
    # retrieve the FolderDAO and use it to save the folder with it's event
    #set($folderDAO = $ContextTool.getBean("FolderDAO"))     
    $folderDAO.store($folder, $history);        
    
    # retrieve the custom attribute 'attr2' of the document
    #set($attr2Value = $document.getValue("attr2"))
    
    # instantiate the 3rd party library class IOUtils 
    #set($IOUtils = $ClassTool.newInstance("org.apache.commons.io.IOUtils"))
    
    #set ($current_time = $DateTool.currentTime())
    #set ($java_year = $current_time.getYear())
    #set ($current_year = $java_year + 1900)    
    #set($docAttrs = $document.getAttributeNames())
    
    # iterate over the custom attribute of the document
    #foreach( $attrn in $docAttrs )
        # if we find the existence of the 'attr3' attribute we have to elaborate it
        #if($attrn.toString().contains("attr3"))
            #set($attr3Value = $document.getValue($attrn.toString()))
            
            # define a new name for the document
            #set($newFileName = $attr2Value + "-" + $attr3Value + "-" +$fdate + ".txt")
            
            # prepare the content of the new document as a string and convert it into an InputStream with IOUtils
            #set ($docContent =  $document.getFileName() + ", " + $attr3Value)        
            #set ($docIS = $IOUtils.toInputStream($docContent))                
            
            # create a new document from scratch
            #set ( $documentVO = $ClassTool.newInstance("core.document.Document") )
            $documentVO.setFileName($newFileName);
            
            # get the 'prot' attribute and if the case initialize it
            #set($protValue = $document.getValue('prot'))
            #if(!$protValue)
                #set($protValue = 'NA');
            #end
            
            # define the path of folder that will receive the new document and create it 
            #set($targetFldPath = "/Received Emails/Sent/" + $current_year + "/" + $protValue)
            #set($docTargetFld = $DocTool.createPath($document, $targetFldPath, $user.username))
            
            $log.info("docTargetFld: $docTargetFld.id");    
            
            # Set the folder and fill the other metadata for the new file
            $documentVO.setFolder($docTargetFld);
            $documentVO.setLanguage('en');
            
            #set($templateDAO = $ContextTool.getBean('TemplateDAO'));
            #set($sendingsTemp = $templateDAO.findByName('sendings',1)); 
            $documentVO.setTemplate($sendingsTemp);
            $documentVO.setValue('prot',$protValue);
            $documentVO.setValue('dest-ind',$attr3Value);
            $documentVO.setValue('fecha-env',$fdate);
                        
            $log.info("content: $docIS"); 
            $log.info("document: $documentVO");             
            
            #set($dmanager = $ContextTool.getBean("DocumentManager") )  
            
            #set ( $dhistory = $ClassTool.newInstance("core.document.DocumentHistory") )
            $dhistory.setUser($user);
            $log.info("history: $dhistory");                        
            
            #set($newDocument = $dmanager.create($docIS, $documentVO, $dhistory) )
            $log.info("created new document with ID: $newDocument.id");            
        #end
    #end
#end

Updates the output attribute of the folder by appending the string attr1 - date (where attr1 is an attribute, and date is the current date e.g. 2020-02-18).
Creates a text file for each attr3 metadata value (attr3 is an attribute with multiple values) of the original document whose content will be a single line string.
The characteristics of the text file are:

  • Filename: attr2 - attr3 - date (where attr2 is the value of the attr2 attribute, attr3 is one of the multiple values ​​of the attr3 attribute)
  • Contents of the text file: filename.ext, attr3 (where filename.ext is the name of the document with its extension (.doc, .docx, etc.), attr3 is one of the multiple values ​​of the attr3 attribute)
  • Target folder: /Received Emails/Sent/prot (prot is the prot attribute)

Updates the metadata of the new text file.
Applies the template sendings and sets some metadata:

  • prot (copying the prot attribute of the original document)
  • dest-ind (copying the attr3 attribute that has multiple values)
  • date-sent