23 de abril de 2018

:: Alfresco. Programación de tareas

Una prueba de concepto para comprobar el funcionamiento de las tareas programadas en Alfresco, podría ser: recibir un correo electrónico con los documentos (por ejemplo: contratos) que caducarán en un determinado periodo de tiempo.

Para esta prueba haremos uso del aspecto: "Efectividad" descrito en otro post.

Los pasos a seguir para crear la tarea programada son los siguientes:

.- Editamos el fichero que nos permite la definición de tareas programadas: scheduled-action-services-context.xml ubicado en la ruta: tomcat/shared/classes/alfresco/extension

.- Añadimos los detalles de la tarea programada, la cual ejecutará un script cada cierto tiempo


<bean class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition" id="runScriptAction">
  <property name="actionName">
   <value>script</value>
  </property>
  <property name="parameterTemplates">
  
   <map>
    <entry>
     <key>
      <value>script-ref</value>
     </key>
     <!-- Note that as of Alfresco 4.0, due to a  Spring upgrade, the FreeMarker ${foo} entries must be escaped -->
     <value>${selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:busqueda_lucene.js"' )}</value>
    </entry>
   </map>
  
  </property>
  <property name="templateActionModelFactory">
   <ref bean="templateActionModelFactory">
  </ref></property>
  <property name="dictionaryService">
   <ref bean="DictionaryService">
  </ref></property>
  <property name="actionService">
   <ref bean="ActionService">
  </ref></property>
  <property name="templateService">
   <ref bean="TemplateService">
  </ref></property>
 </bean>
 
 <bean class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition" id="runScript">
  <property name="transactionMode">
   <value>UNTIL_FIRST_FAILURE</value>
  </property>
  <property name="compensatingActionMode">
   <value>IGNORE</value>
  </property>
  <property name="searchService">
   <ref bean="SearchService">
  </ref></property>
  <property name="templateService">
   <ref bean="TemplateService">
  </ref></property>
  <property name="queryLanguage">
   <value>lucene</value>
  </property>
  <property name="stores">
   <list>
    <value>workspace://SpacesStore</value>
   </list>
  </property>
  <property name="queryTemplate">
   <value>PATH:"/app:company_home"</value>
  </property>
  <property name="cronExpression">
   <value>0/30 * * * * ?</value>
  </property>
  <property name="jobName">
   <value>jobC</value>
  </property>
  <property name="jobGroup">
   <value>jobGroupC</value>
  </property>
  <property name="triggerName">
   <value>triggerC</value>
  </property>
  <property name="triggerGroup">
   <value>triggerGroupC</value>
  </property>
  <property name="scheduler">
   <ref bean="schedulerFactory">
  </ref></property>
  <property name="actionService">
   <ref bean="ActionService">
  </ref></property>
  <property name="templateActionModelFactory">
   <ref bean="templateActionModelFactory">
  </ref></property>
  <property name="templateActionDefinition">
   <ref bean="runScriptAction">
  <!-- This is name of the action (bean) that gets run -->
  </ref></property>
  <property name="transactionService">
   <ref bean="TransactionService">
  </ref></property>
  <property name="runAsUser">
   <value>System</value>
  </property>
 </bean>

Como podemos ver, el script "busqueda_lucene.js" se ejecutará cada 30 segundos.
El contenido del script -almacenado en: "Repositorio" > "Diccionario de datos" > "Scripts" - podría ser algo similar a esto:


var nombredoc = document.properties["{http://www.alfresco.org/model/content/1.0}name"];
var hoy = new Date();
var dia = hoy.getDate();
var mes = hoy.getMonth()+1;
var anyo = hoy.getFullYear();
var siguiente = new Date();
siguiente.setMonth(siguiente.getMonth() + 1);
var dia_sig = siguiente.getDate();
var mes_sig = siguiente.getMonth()+1;
var anyo_sig = siguiente.getFullYear();
var finicio = anyo +"\-" + mes+ "\-" + dia + "T00:00:00.000Z";
var ffin = anyo_sig + "\-" + mes_sig + "\-" + dia_sig + "T23:59:59.000Z";
var query = "@cm\\:to:[" + finicio + " TO " + ffin +"]";
var documentos = search.luceneSearch(query);
var log = "";
for (var i=0; i < documentos.length(); i++)
{
   log += "Nombre: " + documentos[i].name + "\tRuta: " + documentos[i].displayPath + "\r\n";
}
var mail = actions.create("mail");
mail.parameters.html = "html";
mail.parameters.from = "asacinternet@gmail.com";
mail.parameters.to = "gis@asac.as";
mail.parameters.subject = "Alfresco :: Documentos que caducan mañana";
mail.parameters.text = "Consulta: " + query + " --- Listado: " + "\r\n" + log;
mail.execute(document);

No hay comentarios:

Publicar un comentario

Déjanos tu comentario

:: Liferay. Script para ir al inicio de la página

En diversos temas de apariencia es necesario contar un enlace que permita al usuario realizar un scroll al inicio de la página. Este comport...