diff --git a/SharedDatabase/SharedDatabase.csproj b/SharedDatabase/SharedDatabase.csproj index 7114e9b56..49746eb2f 100644 --- a/SharedDatabase/SharedDatabase.csproj +++ b/SharedDatabase/SharedDatabase.csproj @@ -141,6 +141,7 @@ + True diff --git a/SharedDatabase/WorkflowSummary.cs b/SharedDatabase/WorkflowSummary.cs new file mode 100644 index 000000000..56def0138 --- /dev/null +++ b/SharedDatabase/WorkflowSummary.cs @@ -0,0 +1,129 @@ +/// +/// Copyright (c) 2022 Sensus Slovensko a.s. +/// +using System; +using System.Linq; +using log4net; +using NHibernate; +using SharedDatabase.Entities; + +namespace SharedDatabase +{ + public class WorkflowSummary + { + private static readonly ILog log = LogManager.GetLogger(typeof(WorkflowSummary)); + + public Entities.Process Workflow; /// Selected production tracing workflow + public string WorkstepName; /// Selected production tracing workstep + public string PreviousWorkstepName; /// Previous workstep name + public string Part1Name; /// 1st reference part name + public string Part2Name; /// 2nd reference part name or null + + WorkflowSummary() { } + + public WorkflowSummary(Entities.Process workflow) + { + Workflow = workflow; + } + + /// + /// Read specified workflow and workstep from the production tracing database + /// and set WorkflowSummary members accordingly. + /// + /// Workflow name + /// Part of a workstep name (not case sensitive) + /// Database session or null + /// Workflow summary or null + public static WorkflowSummary ReadFromDB(string workflowName, string workstepPattern = "test", ISession session = null) + { + if (string.IsNullOrEmpty(workflowName)) + { + log.ErrorFormat("ReadFromDB() ... workflowName argument is null or empty"); + return null; + } + + bool openAndCloseSession = (session == null) || !session.IsOpen; + + try + { + WorkflowSummary wSum = null; + + if (openAndCloseSession) session = TracingDB.SessionFactory.OpenSession(); + + var wflows = session.QueryOver() + .Where(x => x.Name == workflowName) + .And(x => (x.ReleaseStatus == ReleaseStatus.ToBeApproved || + x.ReleaseStatus == ReleaseStatus.Released || + x.ReleaseStatus == ReleaseStatus.ReleasedActive)) + .List(); + + if (wflows.Count == 1) + { + Process wf = wflows[0]; + wSum = new WorkflowSummary(wf); + var workstep = wf.Worksteps.FirstOrDefault(x => x.Name.ToLower().Contains(workstepPattern.ToLower())); + if (workstep != null) + { + wSum.WorkstepName = workstep.Name; + int previousStepNr = workstep.WorkstepNr - 1; + var previousStep = (previousStepNr < 1) ? null : wf.Worksteps.FirstOrDefault(x => x.WorkstepNr == previousStepNr); + if (previousStep != null) wSum.PreviousWorkstepName = previousStep.Name; + } + var part1 = wf.Parts.FirstOrDefault(x => x.StepNumber == 1); + var part2 = wf.Parts.FirstOrDefault(x => x.StepNumber == 2); + if (part1 != null) wSum.Part1Name = part1.Name; + if (part2 != null) wSum.Part2Name = part2.Name; + } + + if (openAndCloseSession) session.Close(); + + log.InfoFormat("ReadFromDB({0}, {1}) returns {2}", (wSum != null) ? wSum.ToString() : ""); + return wSum; + } + catch (Exception exc) + { + if (session != null && session.IsOpen) session.Close(); + log.ErrorFormat("ReadFromDB({0}, {1}) failed: {2}", workflowName, workstepPattern, exc.Message); + return null; + } + } + + + /// + /// Compares two workflows, determines whether they are compatible. + /// Minimal requirement is Part1 and Part2 names are the same (both Part2 names might be null). + /// + /// Anothe workflow summary + /// true = strict comparison + /// true when compatible + public bool IsCompatibleWith(WorkflowSummary wfs, bool strict = false) + { + if (strict) + { + return wfs != null && + Part1Name == wfs.Part1Name && + Part2Name == wfs.Part2Name && + WorkstepName == wfs.WorkstepName && + PreviousWorkstepName == wfs.PreviousWorkstepName && + Workflow.Name == wfs.Workflow.Name; + } + else + { + return wfs != null && + Part1Name == wfs.Part1Name && + Part2Name == wfs.Part2Name; + } + } + + + public override string ToString() + { + return string.Format("workflow={0} workstep={1} previousWS={2} part1={3} part2={4}", + Workflow != null ? Workflow.Name : "", + WorkstepName != null ? WorkstepName : "", + PreviousWorkstepName != null ? PreviousWorkstepName : "", + Part1Name != null ? Part1Name : "", + Part2Name != null ? Part2Name : ""); + } + } +} \ No newline at end of file