Напоминание о незавершенных задачах в Alfresco

Настройка ежедневной рассылки с напоминанием о незавершенных задачах всем пользователям:
Скрипт send_task_reminders.js:
logger.log(" begin send_task_reminder");
 
var now = new Date();
now.setHours(0,0,0,0);
 
var ticket = session.getTicket();
logger.log(" ticket "+ticket);
 
var connector = remoteService.connect("http");
var resp = eval('(' + connector.get('http://localhost/alfresco/s/api/task-instances?authority='  + encodeURIComponent(document.properties.userName) + '&alf_ticket=' + ticket) + ')');
var mail = actions.create("mail");
 
for each(task in resp.data) {
 
if(task.workflowInstance.dueDate) {
  // Получили его и конвертировали в объект типа "дата"
  var tokens = task.workflowInstance.dueDate.replace(/T.*/,'').split('-');
  var due = new Date(tokens[0], tokens[1]-1, tokens[2]);
  due.setHours(0,0,0,0);
  logger.log("due="+due);
  var diff = (due.getTime()-now.getTime())/(24*3600*1000);
  // Если со сроком все плохо - запомнили задачу
  if(diff < 0) {
   mail.parameters.to = document.properties.email;
   mail.parameters.subject = "Просроченная задача";
   mail.parameters.from = "alfresco@kristallcom.ru";
   mail.parameters.text = "Задача поставлена "+ task.properties.bpm_startDate+" "+task.owner.firstName+" "+task.owner.lastName+ task.workflowInstance.message + '. Срок: ' + due.getDate()+'.'+(due.getMonth()+1)+'.'+due.getFullYear()+ " Статус "+task.properties.bpm_status+" "+task.state;
   mail.execute(document);
 
  } else if ( diff < 1 ) {
   mail.parameters.to = document.properties.email;
   mail.parameters.subject = "Задача, срок, которой истекает сегодня";
   mail.parameters.from = "alfresco@kristallcom.ru";
   mail.parameters.text = "Задача поставлена "+ task.properties.bpm_startDate+" "+task.owner.firstName+" "+task.owner.lastName+ "  "+task.workflowInstance.message+ " Статус "+task.properties.bpm_status+" "+task.state;
   mail.execute(document);
  } else if ( diff == 1 ) {
   mail.parameters.to = document.properties.email;
   mail.parameters.subject = "Поставленная задача";
   mail.parameters.from = "alfresco@kristallcom.ru";
   mail.parameters.text = "Задача поставлена "+ task.properties.bpm_startDate+" "+task.owner.firstName+" "+task.owner.lastName+ "     "+task.workflowInstance.message+ " Статус"+task.properties.bpm_status+" "+task.state;
   mail.execute(document);
  }
 
 
};
 // Если есть контрольный срок
// logger.log(task.workflowInstance.dueDate);
// logger.log(document.properties.email);
// logger.log(task.workflowInstance.message);
// logger.log(task.title);
// logger.log(task.description);
// logger.log(document.properties.userName);
// logger.log(task.owner.userName);
// logger.log(task.owner.firstName);
// logger.log(task.owner.lastName);
// logger.log(task.properties.pm_description);
// logger.log(task.properties.bpm_startDate);
// logger.log(task.properties.bpm_status);
 
}
 
 
//var mail = actions.create("mail");
//mail.parameters.to = "boltaev@kristallcom.ru";
//mail.parameters.subject = "Hello from JavaScript";
//mail.parameters.from = "alfresco@kristallcom.ru";
//mail.parameters.text = "some text, in case template is not found";
//mail.execute(document);
 
logger.log(" end send_task_reminder");
 
Скрипт положить в Репозиторий>Словарь данных>Скрипты

В файл scheduled-action-services-context.xml, который лежит в /tomcat/shared/classes/alfresco/extension
прописать:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
 <bean id="remoteService" parent="baseJavaScriptExtension" class="com.alvexcore.repo.jscript.RemoteService">
  <property name="extensionName">
   <value>remoteService</value>
  </property>
  <property name="ScriptRemote" ref="webscripts.script.remote"/>
 </bean>
 <bean id="templateActionModelFactory" class="org.alfresco.repo.action.scheduled.FreeMarkerWithLuceneExtensionsModelFactory">
  <property name="serviceRegistry">
   <ref bean="ServiceRegistry"/>
  </property>
 </bean>
 <bean id="runScriptAction" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">
  <property name="actionName">
   <value>script</value>
  </property>
  <property name="parameterTemplates">
   <map>
    <entry>
     <key>
      <value>script-ref</value>
     </key>
     <!-- PATH points to the script to be executed -->
     <value>${selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:send_task_reminders.js"' )}</value>
    </entry>
   </map>
  </property>
  <property name="templateActionModelFactory">
   <ref bean="templateActionModelFactory"/>
  </property>
  <property name="dictionaryService">
   <ref bean="DictionaryService"/>
  </property>
  <property name="actionService">
   <ref bean="ActionService"/>
  </property>
  <property name="templateService">
   <ref bean="TemplateService"/>
  </property>
 </bean>
 <bean id="runScript" class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition">
  <property name="transactionMode">
   <value>UNTIL_FIRST_FAILURE</value>
  </property>
  <property name="compensatingActionMode">
   <value>IGNORE</value>
  </property>
  <property name="searchService">
   <ref bean="SearchService"/>
  </property>
  <property name="templateService">
   <ref bean="TemplateService"/>
  </property>
  <property name="queryLanguage">
   <value>lucene</value>
  </property>
  <property name="stores">
   <list>
    <value>workspace://SpacesStore</value>
   </list>
  </property>
  <!-- Execute for each person -->
  <property name="queryTemplate">
   <value>TYPE:"cm:person"</value>
  </property>
  <!-- Start execution at 8:50 a.m. at working days only -->
  <property name="cronExpression">
   <value>0 50 7 ? * MON-FRI</value>
  </property>
  <property name="jobName">
   <value>tasksReminder</value>
  </property>
  <property name="jobGroup">
   <value>jobGroup</value>
  </property>
  <property name="triggerName">
   <value>tasksReminderTrigger</value>
  </property>
  <property name="triggerGroup">
   <value>triggerGroup</value>
  </property>
  <property name="scheduler">
   <ref bean="schedulerFactory"/>
  </property>
  <property name="actionService">
   <ref bean="ActionService"/>
  </property>
  <property name="templateActionModelFactory">
   <ref bean="templateActionModelFactory"/>
  </property>
  <property name="templateActionDefinition">
   <ref bean="runScriptAction"/>
  </property>
  <property name="transactionService">
   <ref bean="TransactionService"/>
  </property>
  <property name="runAsUser">
   <value>System</value>
  </property>
 </bean>
</beans>
Запуск скрипта в 7:50 в рабочие дни.
Продолжение темы : http://www.ossportal.ru/forum/alfresco/1015
140

Комментарии

А этот код в джава скрипт консоли можно выполнять вообще? При попытке запуска вываливается ошибка "document" is not defined.