logo

Automatic reassigning of tasks in case of substitutions

This is an example of creating a listener for reassigning user tasks for existing substitutions. The standard substitution mechanism does not apply to user tasks.

Let's create a Listener object and on its Scripts tab add the following namespaces and code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EleWise.ELMA.ComponentModel;
using EleWise.ELMA.ConfigurationModel;
using EleWise.ELMA.Logging;
using EleWise.ELMA.Model.Common;
using EleWise.ELMA.Model.Entities;
using EleWise.ELMA.Model.Managers;
using EleWise.ELMA.Model.Services;
using EleWise.ELMA.Model.Types.Settings;
using EleWise.ELMA.Runtime.NH.Listeners;
using EleWise.ELMA.Security.Models;
using EleWise.ELMA.Tasks.Models;
using NHibernate.Event;

For the script to work, add the Microsoft.CSharp assembly.

namespace EleWise.ELMA.ConfigurationModel.Scripts
{
     
    /// <summary>
    /// Scripts module of the "User Task Substitution" object
    /// </summary>
    public class ForTaskReplacementScripts : EleWise.ELMA.Model.Scripts.Entities.EntityScriptModule<IForTaskReplacement>
    {
        [Component]
        public class ForTaskReplacement : PostFlushEventListener
        {
      //Create an object
            public override void OnPostInsert(PostInsertEvent @event)
            {
                //Check @event.Entity for the required type and apply the required actions
         var task = @event.Entity as ITask;
                if(task != null)
                {
             //Create a filter for searching an active substitution for the current user
                    var fl = InterfaceActivator.Create<IReplacementFilter>();
                    fl.SourceUser = task.Executor;
                    fl.Status = ReplacementStatus.Active;
             //Search a substitution
                    var rplscmnt = EntityManager<IReplacement>.Instance.Find(fl, null).FirstOrDefault();
             //Check if there is a substitution
                    if (rplscmnt != null)
                    {
                 //Compare the substitution start and end dates with the duedates of the current task
                        if (rplscmnt.StartDate < task.StartDate.Value)
                        {
                            if (rplscmnt.EndDate.AddMinutes(5) > task.StartDate.Value)
                            {
                         //Change the task executor with the subsitute user
                                task.Executor = rplscmnt.TargetUser;
                                task.Save();
                            }
                        }
                    }
                }
            }
        }
 
    }
}