Crea un nuovo file sulla base di un altro documento esistente
# 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
Aggiorna l'attributo di output della cartella aggiungendo la stringa attr1 - date (dove attr1 è un attributo e date è la data corrente, ad esempio 2020-02-18).
Crea un file di testo per ogni valore di metadati attr3 (attr3 è un attributo con più valori) del documento originale il cui contenuto sarà una stringa a riga singola.
Le caratteristiche del file di testo sono:
- Nome file: attr2 - attr3 - date (dove attr2 è il valore dell'attributo attr2, attr3 è uno dei valori multipli dell'attributo attr3)
- Contenuto del file di testo: nomefile.ext, attr3 (dove nomefile.ext è il nome del documento con la sua estensione (.doc, .docx, ecc.), attr3 è uno dei valori multipli dell'attributo attr3)
- Cartella di destinazione: /Received Emails/Sent/prot (prot è l'attributo prot)
Aggiorna i metadati del nuovo file di testo.
Applica la classe documentale sendings e imposta alcuni metadati:
- prot (copiando l'attributo port del documento originale)
- dest-ind (copiando l'attributo attr3 che ha più valori)
- date-sent