1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
|
using Google.Protobuf;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations.Schema;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
using Tango.BL.Builders;
using Tango.BL.Enumerations;
using Tango.Core;
using Tango.Core.ExtensionMethods;
using Tango.Logging;
using Tango.PMR.Exports;
namespace Tango.BL.Entities
{
public partial class Job : JobBase
{
private double _lastLength;
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Job" /> class.
/// </summary>
public Job() : base()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Job"/> class.
/// </summary>
public Job(DateTime creationDate) : this()
{
CreationDate = creationDate;
}
#endregion
#region Events
/// <summary>
/// Occurs when the job total segments length has changed.
/// </summary>
public event EventHandler LengthChanged;
#endregion
#region Properties
/// <summary>
/// Gets the total job segments length.
/// </summary>
[NotMapped]
[JsonIgnore]
public double Length
{
get
{
_lastLength = GetLength();
return _lastLength;
}
}
/// <summary>
/// Gets the total job segments length multiplied by number of units if it is an embroidery job.
/// </summary>
[NotMapped]
[JsonIgnore]
public double LengthIncludingNumberOfUnits
{
get
{
_lastLength = GetLength();
var l = _lastLength * NumberOfUnits;
if (EnableInterSegment && NumberOfUnits > 1)
{
l += ((NumberOfUnits - 1) * InterSegmentLength);
}
return l;
}
}
/// <summary>
/// Gets or sets the job <see cref="Status"/> property as <see cref="JobStatus"/> enum instead of int.
/// </summary>
[NotMapped]
[JsonIgnore]
public JobStatuses JobStatus
{
get { return (JobStatuses)Status; }
set { Status = value.ToInt32(); RaisePropertyChangedAuto(); }
}
/// <summary>
/// Gets or sets the job <see cref="Type"/> property as <see cref="JobType"/> enum instead of int.
/// </summary>
[NotMapped]
[JsonIgnore]
public JobTypes JobType
{
get { return (JobTypes)Type; }
set { Type = value.ToInt32(); RaisePropertyChangedAuto(); }
}
/// <summary>
/// Gets or sets the job <see cref="SpoolsDistribution"/> property as a <see cref="Boolean"/> property.
/// </summary>
[NotMapped]
[JsonIgnore]
public bool IsAllSegmentsPerSpool
{
get { return ((SpoolsDistributions)SpoolsDistribution) == SpoolsDistributions.AllSegments; }
set
{
SpoolsDistribution = value ? SpoolsDistributions.AllSegments.ToInt32() : SpoolsDistributions.SingleSegment.ToInt32();
RaisePropertyChangedAuto();
}
}
/// <summary>
/// Gets or sets the effective segments.
/// </summary>
[NotMapped]
[JsonIgnore]
public ObservableCollection<Segment> EffectiveSegments
{
get
{
if (EnableInterSegment && IsAllSegmentsPerSpool)
{
int max = Segments.Max(x => x.SegmentIndex);
ObservableCollection<Segment> effectiveSegments = new ObservableCollection<Segment>();
foreach (var s in Segments.ToList().OrderBy(x => x.SegmentIndex))
{
effectiveSegments.Add(s);
if (s.SegmentIndex != max)
{
effectiveSegments.Add(CreateInterSegment(InterSegmentLength));
}
}
return effectiveSegments;
}
else
{
return Segments.OrderBy(x => x.SegmentIndex).ToObservableCollection();
}
}
}
/// <summary>
/// Gets the ordered segments.
/// </summary>
[NotMapped]
[JsonIgnore]
public ObservableCollection<Segment> OrderedSegments
{
get
{
return Segments.OrderBy(x => x.SegmentIndex).ToObservableCollection();
}
}
/// <summary>
/// Gets or sets the job fine tuning status.
/// </summary>
[NotMapped]
[JsonIgnore]
public FineTuningStatuses JobFineTuningStatus
{
get { return (FineTuningStatuses)FineTuningStatus; }
set { FineTuningStatus = value.ToInt32(); RaisePropertyChangedAuto(); }
}
/// <summary>
/// Gets or sets the job sample dye status.
/// </summary>
[NotMapped]
[JsonIgnore]
public SampleDyeStatuses JobSampleDyeStatus
{
get { return (SampleDyeStatuses)SampleDyeStatus; }
set { SampleDyeStatus = value.ToInt32(); RaisePropertyChangedAuto(); }
}
private JobDesignations _designation;
/// <summary>
/// Gets or sets the designation.
/// </summary>
[NotMapped]
[JsonIgnore]
public JobDesignations Designation
{
get { return _designation; }
set { _designation = value; RaisePropertyChangedAuto(); }
}
/// <summary>
/// Gets or sets the state of the job editing.
/// </summary>
[NotMapped]
[JsonIgnore]
public EditingStates JobEditingState
{
get { return (EditingStates)EditingState; }
set { EditingState = value.ToInt32(); RaisePropertyChangedAuto(); }
}
#endregion
#region Event Handlers
/// <summary>
/// Handles the CollectionChanged event of the Segments collection.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Collections.Specialized.NotifyCollectionChangedEventArgs"/> instance containing the event data.</param>
private void Segments_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
foreach (var segment in Segments.ToList())
{
segment.PropertyChanged -= Segment_PropertyChanged;
segment.PropertyChanged += Segment_PropertyChanged;
}
OnLengthChanged();
RaisePropertyChanged(nameof(EffectiveSegments));
}
/// <summary>
/// Handles the PropertyChanged event of all job segments.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.ComponentModel.PropertyChangedEventArgs"/> instance containing the event data.</param>
private void Segment_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Segment.Length))
{
OnLengthChanged();
}
else if (e.PropertyName == nameof(Segment.SegmentIndex))
{
RaisePropertyChanged(nameof(EffectiveSegments));
}
}
#endregion
#region Virtual Methods
/// <summary>
/// Called when the <see cref="Length"/> property has been changed
/// </summary>
protected virtual void OnLengthChanged()
{
if (_lastLength != GetLength())
{
RaisePropertyChanged(nameof(Length));
RaisePropertyChanged(nameof(LengthIncludingNumberOfUnits));
LengthChanged?.Invoke(this, new EventArgs());
}
}
#endregion
#region Override Methods
/// <summary>
/// Raises the property changed event.
/// </summary>
/// <param name="propName">Name of the property.</param>
protected override void RaisePropertyChanged(string propName)
{
base.RaisePropertyChanged(propName);
if (propName == nameof(Segments))
{
if (Segments != null)
{
Segments.CollectionChanged -= Segments_CollectionChanged;
Segments.CollectionChanged += Segments_CollectionChanged;
OnLengthChanged();
RaisePropertyChanged(nameof(EffectiveSegments));
}
}
if (propName == nameof(InterSegmentLength) || propName == nameof(EnableInterSegment) || propName == nameof(SpoolsDistribution))
{
OnLengthChanged();
RaisePropertyChanged(nameof(EffectiveSegments));
}
if (propName == nameof(ColorSpace))
{
//Make all brush stops the same color space if job color space is not null!
if (ColorSpace != null)
{
Segments.SelectMany(x => x.BrushStops).ToList().ForEach(x => x.ColorSpace = ColorSpace);
}
}
if (propName == nameof(NumberOfUnits))
{
LengthChanged?.Invoke(this, new EventArgs());
}
if (propName == nameof(LengthPercentageFactor))
{
OnLengthChanged();
Segments.ToList().ForEach(x => x.RaiseLengthWithFactorChanged());
}
if (InterSegmentLength < 1)
{
InterSegmentLength = 1;
}
}
public override Job Clone()
{
Job cloned = base.Clone();
cloned.Name = Name + " - Copy";
cloned.CreationDate = DateTime.UtcNow;
cloned.LastRun = null;
cloned.ColorSpace = ColorSpace;
cloned.Customer = Customer;
cloned.Rml = Rml;
cloned.SpoolType = SpoolType;
cloned.WindingMethod = WindingMethod;
cloned.JobStatus = JobStatuses.Draft;
cloned.Segments = Segments.Select(x => x.Clone(cloned)).ToSynchronizedObservableCollection();
foreach (var segment in cloned.Segments)
{
segment.JobGuid = cloned.Guid;
segment.Job = cloned;
}
return cloned;
}
#endregion
#region Private Methods
private double GetLength()
{
if (Segments != null)
{
return Segments.Sum(x => x.LengthWithFactor) + ((EnableInterSegment && IsAllSegmentsPerSpool) ? (InterSegmentLength * (Segments.Count > 0 ? Segments.Count - 1 : Segments.Count)) : 0);
}
else
{
return 0;
}
}
#endregion
#region Public Methods
public BitmapSource CreateSegmentsPie(double width, double height)
{
Bitmap bmp = new Bitmap((int)width, (int)height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.Clear(Color.Transparent);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
int fromAngle = -90;
double totalLength = Segments.Sum(x => x.Length); //Excluding inter segment.
foreach (var segment in OrderedSegments)
{
int toAngle = (int)((segment.Length / totalLength) * 360d);
Rectangle rect = new Rectangle(0, 0, bmp.Width - 2, bmp.Height - 2);
g.FillPie(segment.CreateGdiBrush(bmp.Width - 2, bmp.Height - 2), rect, fromAngle, toAngle);
Pen pen = new Pen(Color.Gainsboro);
g.DrawEllipse(pen, rect);
pen.Dispose();
fromAngle += toAngle;
}
}
var source = bmp.ToBitmapSource();
bmp.Dispose();
return source;
}
/// <summary>
/// Adds a new solid segment.
/// </summary>
public Segment AddSolidSegment()
{
return AddSolidSegment(System.Windows.Media.Colors.Black);
}
/// <summary>
/// Adds a new solid segment.
/// </summary>
public Segment AddSolidSegment(System.Windows.Media.Color color)
{
return AddSolidSegment(color, 10);
}
/// <summary>
/// Adds a new solid segment.
/// </summary>
public Segment AddSolidSegment(double length)
{
return AddSolidSegment(System.Windows.Media.Colors.White, length);
}
/// <summary>
/// Adds a new solid segment.
/// </summary>
public Segment AddSolidSegment(System.Windows.Media.Color color, double length)
{
Segment segment = new Segment();
segment.Name = "Standard Segment";
if (Segments.Count > 0)
{
segment.SegmentIndex = Segments.Max(x => x.SegmentIndex) + 1;
}
else
{
segment.SegmentIndex = 1;
}
segment.Length = length;
segment.Job = this;
var stop = segment.AddBrushStop();
stop.Color = color;
Segments.Add(segment);
return segment;
}
/// <summary>
/// Adds a new gradient segment.
/// </summary>
public Segment AddGradientSegment()
{
return AddGradientSegment(10);
}
/// <summary>
/// Adds a new gradient segment.
/// </summary>
public Segment AddGradientSegment(double length)
{
var segment = AddSolidSegment(length);
segment.BrushStops.Last().Color = System.Windows.Media.Colors.White;
segment.AddBrushStop();
segment.BrushStops.Last().Color = System.Windows.Media.Colors.White;
return segment;
}
/// <summary>
/// Gets the duration estimation for this job.
/// </summary>
/// <param name="processParameters">The process parameters.</param>
/// <returns></returns>
public TimeSpan GetEstimatedDuration(ProcessParametersTable processParameters)
{
if (processParameters.DyeingSpeed == 0)
{
throw new ArgumentException("Process parameters dying speed cannot be zero.");
}
return TimeSpan.FromSeconds((LengthIncludingNumberOfUnits + processParameters.DryerBufferLengthMeters) / (processParameters.DyeingSpeed / 100d));
}
///// <summary>
///// Gets the duration estimation for this job.
///// </summary>
///// <returns></returns>
//public Task<TimeSpan> GetEstimatedDuration()
//{
// return Task.Factory.StartNew<TimeSpan>(() =>
// {
// var process = GetRecommendedProcessParameters().Result;
// return GetEstimatedDuration(process);
// });
//}
/// <summary>
/// Translates the job progress to time.
/// </summary>
/// <param name="progress">The progress.</param>
/// <param name="processParameters">The process parameters.</param>
/// <returns></returns>
public static TimeSpan TranslateProgressToTime(double progress, ProcessParametersTable processParameters)
{
return TimeSpan.FromSeconds(progress / (processParameters.DyeingSpeed / 100d));
}
/// <summary>
/// Creates an inter segment.
/// </summary>
/// <param name="length">The length.</param>
/// <returns></returns>
public static Segment CreateInterSegment(double length)
{
return new Segment()
{
IsInterSegment = true,
Length = length,
Name = "Inter Segment",
BrushStops = new SynchronizedObservableCollection<BrushStop>()
{
new BrushStop()
{
ColorSpace = new ColorSpace(),
Color = System.Windows.Media.Colors.White,
}
},
};
}
//public Task<ProcessParametersTable> GetRecommendedProcessParameters()
//{
// return Task.Factory.StartNew<ProcessParametersTable>(() =>
// {
// try
// {
// int index = ColorConversion.TangoColorConverter.GetLeastCommonProcessParametersTableIndex(Segments.SelectMany(x => x.BrushStops));
// if (index < Rml.GetActiveProcessGroup().ProcessParametersTables.Count)
// {
// return Rml.GetActiveProcessGroup().ProcessParametersTables[index];
// }
// else
// {
// return Rml.GetActiveProcessGroup().ProcessParametersTables[0];
// }
// }
// catch (Exception ex)
// {
// throw new InvalidOperationException("Could not calculate the recommended process parameters for the job.", ex);
// }
// });
//}
/// <summary>
/// Creates a PMR job file from the this job based on its database data.
/// </summary>
/// <returns></returns>
public Task<JobFile> ToJobFile()
{
return Task.Factory.StartNew<JobFile>(() =>
{
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
var job = new JobBuilder(db).Set(Guid).WithUser().WithRML().WithSegments().WithBrushStops().Build();
var machine = new MachineBuilder(db).Set(job.MachineGuid).WithConfiguration().Build();
return CreateJobFile(job, machine);
}
});
}
/// <summary>
/// Creates a PMR job file based on the current state of the object.
/// </summary>
/// <returns></returns>
public JobFile ToJobFileWhenLoaded()
{
return CreateJobFile(this, Machine);
}
private static JobFile CreateJobFile(Job job, Machine machine)
{
var jobFile = new JobFile();
jobFile.ColorSpaceGuid = job.ColorSpaceGuid.ToStringOrEmpty();
jobFile.Customer = job.Customer != null ? job.Customer.Name : String.Empty;
jobFile.Description = job.Description.ToStringOrEmpty();
if (job.HasEmbroideryFile)
{
jobFile.HasEmbroideryFile = job.HasEmbroideryFile;
jobFile.EmbroideryFileData = ByteString.CopyFrom(job.EmbroideryFileData);
jobFile.EmbroideryFileName = job.EmbroideryFileName;
jobFile.EmbroideryJpeg = ByteString.CopyFrom(job.EmbroideryJpeg);
}
jobFile.EnableInterSegment = job.EnableInterSegment;
jobFile.EnableLubrication = job.EnableLubrication;
jobFile.InterSegmentLength = job.InterSegmentLength;
jobFile.LengthPercentageFactor = job.LengthPercentageFactor;
jobFile.Name = job.Name.ToStringOrEmpty();
jobFile.NumberOfUnits = job.NumberOfUnits;
jobFile.RmlGuid = job.RmlGuid;
jobFile.SampleUnitsOrMeters = job.SampleUnitsOrMeters;
jobFile.SpoolsDistribution = job.SpoolsDistribution;
jobFile.SpoolTypeGuid = job.SpoolTypeGuid;
jobFile.Type = job.Type;
jobFile.WindingMethodGuid = job.WindingMethodGuid;
jobFile.ColorCatalogGuid = job.ColorCatalogGuid.ToStringOrEmpty();
foreach (var segment in job.OrderedSegments)
{
JobFileSegment s = new JobFileSegment();
s.Length = segment.Length;
s.Name = segment.Name.ToStringOrEmpty();
jobFile.Segments.Add(s);
foreach (var stop in segment.BrushStops.OrderBy(x => x.StopIndex))
{
JobFileBrushStop st = new JobFileBrushStop();
stop.MapPrimitivesWithStringsNoNullsTo(st);
st.ColorCatalogItemGuid = stop.ColorCatalogsItemGuid.ToStringOrEmpty();
foreach (var idsPack in machine.Configuration.NoneEmptyIdsPacks)
{
try
{
var volume = stop.GetVolume(idsPack.PackIndex);
st.LiquidVolumes.Add(new JobFileLiquidVolume()
{
LiquidTypeName = idsPack.LiquidType.Name,
Volume = volume,
});
}
catch (Exception ex)
{
throw new InvalidOperationException($"Error extracting liquid volume from ids pack {idsPack.PackIndex}", ex);
}
}
s.BrushStops.Add(st);
}
}
return jobFile;
}
public static Task<Job> FromJobFile(JobFile jobFile, String machineGuid, String userGuid)
{
return Task.Factory.StartNew(() =>
{
using (ObservablesContext db = ObservablesContext.CreateDefault())
{
Machine machine;
var job = new Job();
job.MachineGuid = machineGuid;
job.UserGuid = userGuid;
job.CreationDate = DateTime.UtcNow;
try
{
machine = new MachineBuilder(db).Set(machineGuid).WithConfiguration().Build();
}
catch
{
throw new ArgumentException("Error loading the machine details.");
}
if (!String.IsNullOrEmpty(jobFile.ColorSpaceGuid))
{
var job_color_space = db.ColorSpaces.SingleOrDefault(x => x.Guid == jobFile.ColorSpaceGuid);
if (job_color_space == null) throw new ArgumentException("Could not load the specified job file. Job color space could not be located on database.");
job.ColorSpaceGuid = jobFile.ColorSpaceGuid;
}
if (!String.IsNullOrEmpty(jobFile.ColorCatalogGuid))
{
var job_color_catalog = db.ColorCatalogs.SingleOrDefault(x => x.Guid == jobFile.ColorCatalogGuid);
if (job_color_catalog == null) throw new ArgumentException("Could not load the specified job file. Job color catalog could not be located on database.");
job.ColorCatalogGuid = jobFile.ColorCatalogGuid;
}
var job_customer = db.Customers.FirstOrDefault(x => x.Name == jobFile.Customer);
if (job_customer != null)
{
job.CustomerGuid = job_customer.Guid;
}
job.Description = jobFile.Description.ToNullIfEmpty();
if (jobFile.HasEmbroideryFile)
{
job.HasEmbroideryFile = jobFile.HasEmbroideryFile;
job.EmbroideryFileData = jobFile.EmbroideryFileData.ToByteArray();
job.EmbroideryFileName = jobFile.EmbroideryFileName;
job.EmbroideryJpeg = jobFile.EmbroideryJpeg.ToByteArray();
}
job.EnableInterSegment = jobFile.EnableInterSegment;
job.EnableLubrication = jobFile.EnableLubrication;
job.InterSegmentLength = jobFile.InterSegmentLength;
job.LengthPercentageFactor = jobFile.LengthPercentageFactor;
job.Name = jobFile.Name.ToNullIfEmpty();
job.NumberOfUnits = jobFile.NumberOfUnits;
var job_rml = db.Rmls.SingleOrDefault(x => x.Guid == jobFile.RmlGuid);
if (job_rml == null) throw new ArgumentException("Could not load the specified job file. Job media type could not be located on database.");
job.RmlGuid = jobFile.RmlGuid;
job.SampleUnitsOrMeters = jobFile.SampleUnitsOrMeters;
job.SpoolsDistribution = jobFile.SpoolsDistribution;
var job_spool_type = db.SpoolTypes.SingleOrDefault(x => x.Guid == jobFile.SpoolTypeGuid);
if (job_spool_type == null) throw new ArgumentException("Could not load the specified job file. Job spool type could not be located on database.");
job.SpoolTypeGuid = jobFile.SpoolTypeGuid;
job.Type = jobFile.Type;
var job_winding_method = db.WindingMethods.Single(x => x.Guid == jobFile.WindingMethodGuid);
if (job_winding_method == null) throw new ArgumentException("Could not load the specified job file. Job winding method could not be located on database.");
job.WindingMethodGuid = jobFile.WindingMethodGuid;
for (int i = 0; i < jobFile.Segments.Count; i++)
{
var segment = jobFile.Segments[i];
Segment s = new Segment();
s.JobGuid = job.Guid;
s.Name = segment.Name.ToNullIfEmpty();
s.Length = segment.Length;
s.SegmentIndex = i + 1;
job.Segments.Add(s);
for (int j = 0; j < segment.BrushStops.Count; j++)
{
var stop = segment.BrushStops[j];
var stop_color_space = db.ColorSpaces.SingleOrDefault(x => x.Guid == stop.ColorSpaceGuid);
if (stop_color_space == null) throw new ArgumentException("Could not load the specified job file. Job brush stop color space could not be located on database.");
BrushStop st = new BrushStop();
st.StopIndex = j + 1;
st.SegmentGuid = s.Guid;
stop.MapPrimitivesWithStringsNoNullsTo(st);
foreach (var volume in stop.LiquidVolumes)
{
var idsPack = machine.Configuration.NoneEmptyIdsPacks.SingleOrDefault(x => x.LiquidType.Name == volume.LiquidTypeName);
if (idsPack == null)
{
throw new InvalidOperationException($"Liquid type '{volume.LiquidTypeName}' is not configured on the target machine.");
}
st.SetVolume(idsPack.PackIndex, volume.Volume);
}
if (!String.IsNullOrWhiteSpace(stop.ColorCatalogGuid))
{
var stop_color_catalog = db.ColorCatalogs.SingleOrDefault(x => x.Guid == stop.ColorCatalogGuid);
if (stop_color_catalog == null) throw new ArgumentException("Could not load the specified job file. Job brush stop color catalog could not be located on database.");
stop.ColorCatalogGuid = stop_color_catalog.Guid;
}
if (!String.IsNullOrWhiteSpace(stop.ColorCatalogItemGuid))
{
var stop_color_catalog_item = db.ColorCatalogsItems.SingleOrDefault(x => x.Guid == stop.ColorCatalogItemGuid);
if (stop_color_catalog_item == null) throw new ArgumentException("Could not load the specified job file. Job brush stop catalog color could not be located on database.");
st.ColorCatalogsItemGuid = stop.ColorCatalogItemGuid;
}
s.BrushStops.Add(st);
}
}
return job;
}
});
}
/// <summary>
/// Removes this entity and all dependent entities from the specified db context.
/// </summary>
/// <param name="context">The context.</param>
public override void Delete(ObservablesContext context)
{
base.Delete(context);
Segments.ToList().ForEach(x => x.Delete(context));
context.Jobs.Remove(this);
}
#endregion
#region Validation
protected override void OnValidating(ObservablesContext context)
{
base.OnValidating(context);
if (String.IsNullOrWhiteSpace(Name))
{
InsertError(nameof(Name), "Job name is required");
}
}
#endregion
}
}
|