using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading; using System.Threading.Tasks; using Tango.BL; using Tango.BL.Builders; using Tango.Core; using Tango.PPC.Common; using Tango.PPC.Common.UpdatePackages; using Tango.PPC.Shared.Updates; using Tango.Settings; namespace Tango.PPC.Packages.JobRunsUpdater { [PPCPackage(PackageType.Pre, "JobRuns Statistics Patch", false)] public class JobRunsPatch : ExtendedObject, IPPCPackage { public Task Run(PackageContext context) { return Task.Factory.StartNew(() => { try { LogManager.Log("Applying JobRuns Statistics Patch..."); context.ReportProgress("Applying job runs statistics patch..."); Thread.Sleep(2000); //Just so we can see something happened. using (ObservablesContext db = ObservablesContext.CreateDefault()) { foreach (var jobRun in db.JobRuns.Where(x => x.JobLogicalLength == 0)) { try { var job = new JobBuilder(db).Set(jobRun.JobGuid).WithSegments().WithBrushStops().Build(); jobRun.JobLogicalLength = job.Length; jobRun.NumberOfUnits = job.NumberOfUnits; jobRun.ApplicationVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); jobRun.FirmwareVersion = SettingsManager.Default.GetOrCreate().PreviousApplicationVersion; var rml = new RmlBuilder(db).Set(job.RmlGuid).WithActiveParametersGroup().Build(); jobRun.CeVersion = rml.ColorConversionVersion.ToString(); jobRun.ProcessParametersTableGuid = rml.GetActiveProcessGroup().ProcessParametersTables[0].Guid; var jobFile = jobRun.JobFile; int segmentIndex = 0; foreach (var segment in jobFile.Segments) { segmentIndex++; var jobSegment = job.Segments.FirstOrDefault(x => x.SegmentIndex == segmentIndex); foreach (var stop in segment.BrushStops) { var jobStop = jobSegment.BrushStops.FirstOrDefault(x => x.StopIndex == stop.StopIndex); if (jobStop.BestMatchR != null) { stop.BestMatchR = jobStop.BestMatchR.Value; stop.BestMatchG = jobStop.BestMatchG.Value; stop.BestMatchB = jobStop.BestMatchB.Value; } else { stop.BestMatchR = jobStop.Red; stop.BestMatchG = jobStop.Green; stop.BestMatchB = jobStop.Blue; } } } jobRun.JobString = jobFile.ToString(); db.SaveChanges(); } catch (Exception ex) { LogManager.Log(ex, $"Error updating JobRun '{jobRun.ID}'."); } } } } catch (Exception ex) { LogManager.Log(ex, "Error occurred while trying to apply the job runs statistics patch."); } }); } } }