aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Integration/Printing/Job.cs
blob: 1cc4865f23a708410dcb4ffea2c6154912b8fb46 (plain)
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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.Integration.Observables
{
    /// <summary>
    /// Extends the standard observable Job class.
    /// </summary>
    /// <seealso cref="Tango.Integration.Observables.ObservableEntity{Tango.Integration.Observables.Job}" />
    public partial class Job
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="Job"/> class.
        /// </summary>
        public Job(DateTime creationDate)
        {
            CreationDate = creationDate;
        }

        #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 { return Segments.Sum(x => x.Length) + (EnableInterSegment ? (InterSegmentLength * (Segments.Count > 0 ? Segments.Count - 1 : Segments.Count)) : 0); }
        }

        #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)
            {
                segment.PropertyChanged += Segment_PropertyChanged;
            }

            OnLengthChanged();
        }

        /// <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();
            }
        }

        #endregion

        #region Virtual Methods

        /// <summary>
        /// Called when the <see cref="Length"/> property has been changed
        /// </summary>
        protected virtual void OnLengthChanged()
        {
            RaisePropertyChanged(nameof(Length));
            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();
                }
            }

            if (propName == nameof(InterSegmentLength) || propName == nameof(EnableInterSegment))
            {
                OnLengthChanged();
            }
        }

        /// <summary>
        /// Saves the changes on this entity to database.
        /// </summary>
        public override void Save()
        {
            for (int i = 0; i < Segments.Count; i++)
            {
                Segments[i].SegmentIndex = i;

                for (int j = 0; j < Segments[i].BrushStops.Count; j++)
                {
                    Segments[i].BrushStops[j].StopIndex = j;
                }
            }

            base.Save();
        }

        public override Job Clone()
        {
            Job cloned = base.Clone();

            cloned.CreationDate = DateTime.UtcNow;
            cloned.LastRun = null;
            cloned.Segments = Segments.Select(x => x.Clone(cloned)).ToObservableCollection();

            return cloned;
        }

        public override void DefferedDelete()
        {
            Segments.ToList().ForEach(x => x.DefferedDelete());
            Segments.Clear();
            base.DefferedDelete();
        }

        #endregion
    }
}