Crea un nuevo archivo sobre la base de otro documento existente
# 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
Actualiza el atributo de output de la carpeta agregando el texto attr1 - date (donde attr1 es un atributo y date es la fecha actual, por ejemplo 2020-02-18).
Crea un archivo de texto para cada valor de metadatos attr3 (attr3 es un atributo con valores múltiples) del documento original cuyo contenido será un texto de línea única.
Las características del archivo de texto son:
- Nombre de archivo: attr2 - attr3 - date (donde attr2 es el valor del atributo attr2, attr3 es uno de los múltiples valores del atributo attr3)
- Contenido del archivo de texto: filename.ext, attr3 (donde filename.ext es el nombre del documento con su extensión (.doc, .docx, etc.), attr3 es uno de los múltiples valores del atributo attr3)
- Carpeta de destino: /Received Emails/Sent/prot (prot es el atributo prot)
Actualiza los metadatos del nuevo archivo de texto.
Aplica la plantilla sendings y establece algunos metadatos:
- prot (copiando el atributo prot del documento original)
- dest-ind (copiando el atributo attr3 que tiene múltiples valores)
- date-sent