227 lines
7.9 KiB
C#
227 lines
7.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Newtonsoft.Json;
|
|
using Serilog;
|
|
using LCU.WebApps.Internal.DispatcherManager.Helpers;
|
|
using LCU.WebApps.Internal.DispatcherManager.Models;
|
|
using LCU.Library.Internal.DispatcherModels.Models;
|
|
using LCU.Library.Internal.DispatcherModels.Entities;
|
|
using LCU.Library.Internal.DispatcherModels.Enums;
|
|
using LCU.Api.Internal.DispatcherSharedLib.BuilderServices;
|
|
using System.Threading.Tasks;
|
|
using LCU.Library.Internal.Security.Service;
|
|
namespace LCU.WebApps.Internal.DispatcherManager.Services;
|
|
|
|
public class JobStateService
|
|
{
|
|
private readonly IRequestBuilderService _requestBuilderService;
|
|
private readonly IAzureAdAuthorizationService AzureAdAuthorizationService;
|
|
private const string DispatcherManager = "Dispatcher Manager";
|
|
private int KeyCount;
|
|
public event Action OnChange;
|
|
public JobDetails JobDetails { get; set; }
|
|
public bool Scheduled { get; set; }
|
|
public bool SkipHolidays { get; set; }
|
|
public string ActionTypeSelected { get; set; }
|
|
public IList<Tag> TagSelectedList { get; set; }
|
|
public List<WorkItemComponent> WorkItemComponents { get; set; }
|
|
public Job EditingJob { get; set; }
|
|
|
|
public JobStateService(IRequestBuilderService requestBuilderService,
|
|
IAzureAdAuthorizationService azureAdAuthorizationService)
|
|
{
|
|
WorkItemComponents = new List<WorkItemComponent>();
|
|
JobDetails = new JobDetails();
|
|
KeyCount = 0;
|
|
ActionTypeSelected = "";
|
|
Scheduled = false;
|
|
EditingJob = null;
|
|
_requestBuilderService = requestBuilderService;
|
|
AzureAdAuthorizationService = azureAdAuthorizationService;
|
|
}
|
|
|
|
private void NotifyStateChanged() => OnChange?.Invoke();
|
|
|
|
public void Reset()
|
|
{
|
|
JobDetails = new JobDetails
|
|
{
|
|
ErrorRecipients = new List<string>()
|
|
};
|
|
WorkItemComponents = new List<WorkItemComponent>();
|
|
TagSelectedList = new List<Tag>();
|
|
ActionTypeSelected = "";
|
|
Scheduled = false;
|
|
SkipHolidays = false;
|
|
KeyCount = 0;
|
|
EditingJob = null;
|
|
NotifyStateChanged();
|
|
}
|
|
|
|
public void MapJobToJobState()
|
|
{
|
|
if (EditingJob == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
MapEditingJobToJobDetails();
|
|
|
|
foreach (var workItem in EditingJob.WorkItems.OrderBy(wi => wi.Order))
|
|
{
|
|
AddExistingWorkItem(workItem);
|
|
}
|
|
|
|
ActionTypeSelected = "";
|
|
Scheduled = !string.IsNullOrEmpty(JobDetails.CronSchedule);
|
|
SkipHolidays = JobDetails.SkipHolidays;
|
|
NotifyStateChanged();
|
|
}
|
|
|
|
public void MapEditingJobToJobDetails()
|
|
{
|
|
JobDetails.JobId = EditingJob.JobId;
|
|
JobDetails.Name = EditingJob.Name;
|
|
JobDetails.RequestingApplication = EditingJob.RequestingApplication;
|
|
JobDetails.Description = EditingJob.Description;
|
|
JobDetails.CronSchedule = EditingJob.CronSchedule;
|
|
JobDetails.NextRunTime = EditingJob.NextRunTime;
|
|
JobDetails.SkipHolidays = EditingJob.SkipHolidays;
|
|
JobDetails.CreatedBy = EditingJob.CreatedBy;
|
|
JobDetails.CreatedAt = EditingJob.CreatedAt;
|
|
JobDetails.ModifiedBy = EditingJob.ModifiedBy;
|
|
JobDetails.ModifiedDate = EditingJob.ModifiedDate;
|
|
JobDetails.ErrorRecipients = EditingJob.JobErrorRecipients.Select(jer => jer.Recipient).ToList();
|
|
JobDetails.JobTags = EditingJob.JobTags.ToList();
|
|
}
|
|
|
|
public void AddExistingWorkItem(WorkItem workItem)
|
|
{
|
|
ActionTypeSelected = Enum.GetName(typeof(ActionType), workItem.ActionType);
|
|
var request = JsonConvert.DeserializeObject(workItem.SerializedPayload,
|
|
_requestBuilderService.GetDispatcherTypeByString(ActionTypeSelected));
|
|
|
|
var workItemComponent = new WorkItemComponent(KeyCount, ActionTypeSelected, request)
|
|
{
|
|
WorkItemDetails = new WorkItemDetails()
|
|
};
|
|
workItemComponent.WorkItemDetails.WorkItemId = workItem.WorkItemId;
|
|
workItemComponent.WorkItemDetails.Name = workItem.Name;
|
|
workItemComponent.WorkItemDetails.Description = workItem.Description;
|
|
workItemComponent.WorkItemDetails.CancelJobOnFailure = workItem.CancelJobOnFailure;
|
|
workItemComponent.WorkItemDetails.CancelJobLimit = workItem.CancelJobLimit;
|
|
WorkItemComponents.Add(workItemComponent);
|
|
KeyCount++;
|
|
}
|
|
|
|
public void Add()
|
|
{
|
|
var type = _requestBuilderService.GetDispatcherTypeByString(ActionTypeSelected);
|
|
var request = Activator.CreateInstance(type);
|
|
AddWorkItemState(request);
|
|
}
|
|
|
|
private void AddWorkItemState(object item)
|
|
{
|
|
var workItemComponent = new WorkItemComponent(KeyCount, ActionTypeSelected, item)
|
|
{
|
|
WorkItemDetails = new WorkItemDetails()
|
|
};
|
|
WorkItemComponents.Add(workItemComponent);
|
|
KeyCount++;
|
|
NotifyStateChanged();
|
|
}
|
|
|
|
private List<JobTag> BuildJobTags()
|
|
{
|
|
var jobTagList = new List<JobTag>();
|
|
foreach (var tag in TagSelectedList)
|
|
{
|
|
var jobTag = new JobTag
|
|
{
|
|
Tag = null,
|
|
TagId = tag.TagId,
|
|
JobId = JobDetails.JobId
|
|
};
|
|
jobTagList.Add(jobTag);
|
|
}
|
|
return jobTagList;
|
|
}
|
|
|
|
public void Remove(WorkItemComponent toRemove)
|
|
{
|
|
WorkItemComponents.Remove(toRemove);
|
|
NotifyStateChanged();
|
|
}
|
|
|
|
public int GetIndex(WorkItemComponent item)
|
|
{
|
|
return WorkItemComponents.FindIndex(a => a == item);
|
|
}
|
|
|
|
public bool ValidateWorkItems()
|
|
{
|
|
bool itemsAreValid = WorkItemComponents.Any();
|
|
foreach (var component in WorkItemComponents)
|
|
{
|
|
component.IsValid = component.EditContext.Validate() && component.WorkItemDetailsEditContext.Validate();
|
|
if (!component.IsValid)
|
|
{
|
|
itemsAreValid = component.IsValid;
|
|
component.IsCollapsed = false;
|
|
}
|
|
}
|
|
|
|
return itemsAreValid;
|
|
}
|
|
|
|
public async Task<bool> SendJob()
|
|
{
|
|
try
|
|
{
|
|
UpdateJobDetails();
|
|
_requestBuilderService.CreateJob(JobDetails);
|
|
|
|
foreach (var component in WorkItemComponents)
|
|
{
|
|
AddWorkitem(component);
|
|
}
|
|
|
|
//bool result = Task.Run(() => _requestBuilderService.SendDispatcherRequest()).Result;
|
|
bool result = await _requestBuilderService.SendDispatcherRequest();
|
|
|
|
return result;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Failure sending job to dispatcher.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void UpdateJobDetails()
|
|
{
|
|
if (Scheduled)
|
|
{
|
|
JobDetails.NextRunTime = null;
|
|
}
|
|
|
|
var isNewJob = EditingJob == null;
|
|
JobDetails.RequestingApplication = isNewJob ? DispatcherManager : EditingJob.RequestingApplication;
|
|
JobDetails.CreatedBy = isNewJob ? AzureAdAuthorizationService.GetUserName() : EditingJob.CreatedBy;
|
|
JobDetails.CreatedAt = isNewJob ? DateTime.Now : EditingJob.CreatedAt;
|
|
JobDetails.ModifiedBy = isNewJob ? JobDetails.ModifiedBy : AzureAdAuthorizationService.GetUserName();
|
|
JobDetails.ModifiedDate = isNewJob ? JobDetails.ModifiedDate : DateTime.Now;
|
|
JobDetails.SkipHolidays = isNewJob ? JobDetails.SkipHolidays : SkipHolidays;
|
|
JobDetails.JobTags = BuildJobTags();
|
|
}
|
|
|
|
public void AddWorkitem(WorkItemComponent component)
|
|
{
|
|
component.WorkItemDetails.Name = string.IsNullOrEmpty(component.WorkItemDetails.Name) ? component.Type : component.WorkItemDetails.Name;
|
|
component.WorkItemDetails.JobId = EditingJob == null ? 0 : EditingJob.JobId;
|
|
component.WorkItemDetails.Order = GetIndex(component);
|
|
_requestBuilderService.AddWorkItem(component.Item, component.WorkItemDetails, component.Type);
|
|
}
|
|
} |