Job Expressions
Expressions can be triggered when jobs are created and closed
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using Disco.Models.BI.Config;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Authorization;
|
||||
using Disco.Services.Logging;
|
||||
using Disco.Services.Plugins;
|
||||
using Disco.Services.Plugins.Features.RepairProvider;
|
||||
using Disco.Services.Plugins.Features.WarrantyProvider;
|
||||
@@ -491,13 +492,35 @@ namespace Disco.Services
|
||||
#endregion
|
||||
|
||||
#region Close
|
||||
public static void OnCloseNormally(this Job j, User Technician)
|
||||
public static void OnCloseNormally(this Job j, DiscoDataContext Database, User Technician)
|
||||
{
|
||||
if (!j.CanCloseNormally())
|
||||
throw new InvalidOperationException("Close was Denied");
|
||||
|
||||
j.ClosedDate = DateTime.Now;
|
||||
j.ClosedTechUserId = Technician.UserId;
|
||||
j.ClosedTechUser = Technician;
|
||||
|
||||
// Evaluate OnClose Expression
|
||||
try
|
||||
{
|
||||
var onCloseResult = j.EvaluateOnCloseExpression(Database);
|
||||
if (!string.IsNullOrWhiteSpace(onCloseResult))
|
||||
{
|
||||
var jl = new JobLog()
|
||||
{
|
||||
Job = j,
|
||||
TechUser = Technician,
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = onCloseResult
|
||||
};
|
||||
Database.JobLogs.Add(jl);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SystemLog.LogException("Job Expression - OnCloseExpression", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private static bool CanCloseNever(this Job j, JobQueueJob IgnoreJobQueueJob = null)
|
||||
@@ -645,6 +668,28 @@ namespace Disco.Services
|
||||
|
||||
j.ClosedDate = DateTime.Now;
|
||||
j.ClosedTechUserId = Technician.UserId;
|
||||
j.ClosedTechUser = Technician;
|
||||
|
||||
// Evaluate OnClose Expression
|
||||
try
|
||||
{
|
||||
var onCloseResult = j.EvaluateOnCloseExpression(Database);
|
||||
if (!string.IsNullOrWhiteSpace(onCloseResult))
|
||||
{
|
||||
var jl = new JobLog()
|
||||
{
|
||||
Job = j,
|
||||
TechUser = Technician,
|
||||
Timestamp = DateTime.Now,
|
||||
Comments = onCloseResult
|
||||
};
|
||||
Database.JobLogs.Add(jl);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SystemLog.LogException("Job Expression - OnCloseExpression", ex);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -3,9 +3,11 @@ using Disco.Models.Repository;
|
||||
using Disco.Models.Services.Documents;
|
||||
using Disco.Models.Services.Jobs.JobLists;
|
||||
using Disco.Services.Authorization;
|
||||
using Disco.Services.Expressions;
|
||||
using Disco.Services.Interop.ActiveDirectory;
|
||||
using Disco.Services.Plugins;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -383,5 +385,39 @@ namespace Disco.Services
|
||||
return JobTypes;
|
||||
}
|
||||
|
||||
#region Expressions
|
||||
|
||||
public static string EvaluateOnCreateExpression(this Job job, DiscoDataContext Database)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(Database.DiscoConfiguration.JobPreferences.OnCreateExpression))
|
||||
{
|
||||
Expression compiledExpression = Jobs.Jobs.OnCreateExpressionFromCache(Database);
|
||||
IDictionary evaluatorVariables = Expression.StandardVariables(null, Database, job.OpenedTechUser, DateTime.Now, null);
|
||||
object result = compiledExpression.EvaluateFirst<object>(job, evaluatorVariables);
|
||||
if (result == null)
|
||||
return null;
|
||||
else
|
||||
return result.ToString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string EvaluateOnCloseExpression(this Job job, DiscoDataContext Database)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(Database.DiscoConfiguration.JobPreferences.OnCloseExpression))
|
||||
{
|
||||
Expression compiledExpression = Jobs.Jobs.OnCloseExpressionFromCache(Database);
|
||||
IDictionary evaluatorVariables = Expression.StandardVariables(null, Database, job.OpenedTechUser, DateTime.Now, null);
|
||||
object result = compiledExpression.EvaluateFirst<object>(job, evaluatorVariables);
|
||||
if (result == null)
|
||||
return null;
|
||||
else
|
||||
return result.ToString();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Models.Repository;
|
||||
using Disco.Services.Expressions;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -120,5 +122,25 @@ namespace Disco.Services.Jobs
|
||||
return j;
|
||||
}
|
||||
|
||||
public static Expression OnCreateExpressionFromCache(DiscoDataContext Database)
|
||||
{
|
||||
return ExpressionCache.GetValue("Job_OnCreateExpression", string.Empty, () => { return Expression.TokenizeSingleDynamic(null, Database.DiscoConfiguration.JobPreferences.OnCreateExpression, 0); });
|
||||
}
|
||||
|
||||
public static void OnCreateExpressionInvalidateCache()
|
||||
{
|
||||
ExpressionCache.InvalidateKey("Job_OnCreateExpression", string.Empty);
|
||||
}
|
||||
|
||||
public static Expression OnCloseExpressionFromCache(DiscoDataContext Database)
|
||||
{
|
||||
return ExpressionCache.GetValue("Job_OnCloseExpression", string.Empty, () => { return Expression.TokenizeSingleDynamic(null, Database.DiscoConfiguration.JobPreferences.OnCloseExpression, 0); });
|
||||
}
|
||||
|
||||
public static void OnCloseExpressionInvalidateCache()
|
||||
{
|
||||
ExpressionCache.InvalidateKey("Job_OnCloseExpression", string.Empty);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user