aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/ObservablesContextAdapter.cs
blob: ec13b0a10dda9f9f6a13bd698a9132a1d0e87ee1 (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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using System.Data.Entity;
using System.Collections.ObjectModel;

namespace Tango.BL
{
    /// <summary>
    /// Represents an <see cref="ObservablesContext"/> adapter for retrieving aggregated entities.
    /// </summary>
    public class ObservablesContextAdapter : IDisposable
    {
        private ObservablesContext _db;
        /// <summary>
        /// Gets the underlying <see cref="ObservablesContext"/>.
        /// </summary>
        public ObservablesContext Context
        {
            get { return _db; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ObservablesContextAdapter"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public ObservablesContextAdapter(ObservablesContext context)
        {
            _db = context;
        }

        /// <summary>
        /// Creates the a new instance of <see cref="ObservablesContextAdapter"/> wrapping a default <see cref="ObservablesContext"/>.
        /// </summary>
        /// <returns></returns>
        public static ObservablesContextAdapter CreateDefault()
        {
            var context = ObservablesContext.CreateDefault();
            return new ObservablesContextAdapter(context);
        }

        /// <summary>
        /// Loads the configuration with it's Ids packs.
        /// </summary>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        public Configuration GetConfiguration(Expression<Func<Configuration, bool>> condition)
        {
            var config = _db.Configurations.SingleOrDefault(condition);

            var l = _db.IdsPacks.Where(x => x.ConfigurationGuid == config.Guid)
                .Include(x => x.LiquidType)
                .Include(x => x.MidTankType)
                .Include(x => x.CartridgeType)
                .Include(x => x.Dispenser)
                .Include(x => x.Dispenser.DispenserType)
                .Include(x => x.IdsPackFormula).OrderBy(x => x.PackIndex).ToList();

            return config;
        }

        /// <summary>
        /// Gets the hardware version by the specified condition.
        /// </summary>
        /// <param name="condition">The condition.</param>
        /// <returns></returns>
        public HardwareVersion GetHardwareVersion(Expression<Func<HardwareVersion, bool>> condition)
        {
            HardwareVersion version = _db.HardwareVersions.SingleOrDefault(condition);

            version.HardwareBlowers = _db.HardwareBlowers.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareBlowerType).ToList().OrderBy(x => x.HardwareBlowerType.Code).ToSynchronizedObservableCollection();
            version.HardwareBreakSensors = _db.HardwareBreakSensors.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareBreakSensorType).ToList().OrderBy(x => x.HardwareBreakSensorType.Code).ToSynchronizedObservableCollection();
            version.HardwareDancers = _db.HardwareDancers.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareDancerType).ToList().OrderBy(x => x.HardwareDancerType.Code).ToSynchronizedObservableCollection();
            version.HardwareMotors = _db.HardwareMotors.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareMotorType).ToList().OrderBy(x => x.HardwareMotorType.Code).ToSynchronizedObservableCollection();
            version.HardwarePidControls = _db.HardwarePidControls.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwarePidControlType).ToList().OrderBy(x => x.HardwarePidControlType.Code).ToSynchronizedObservableCollection();
            version.HardwareSpeedSensors = _db.HardwareSpeedSensors.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareSpeedSensorType).ToList().OrderBy(x => x.HardwareSpeedSensorType.Code).ToSynchronizedObservableCollection();
            version.HardwareWinders = _db.HardwareWinders.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareWinderType).ToList().OrderBy(x => x.HardwareWinderType.Code).ToSynchronizedObservableCollection();

            return version;
        }

        /// <summary>
        /// Gets the hardware version by machine.
        /// </summary>
        /// <param name="machineGuid">The machine unique identifier.</param>
        /// <returns></returns>
        public HardwareVersion GetHardwareVersionByMachine(String machineGuid)
        {
            var machine = _db.Machines.Where(x => x.Guid == machineGuid).Include(x => x.Configuration).FirstOrDefault();
            return GetHardwareVersion(x => x.Guid == machine.Configuration.HardwareVersionGuid);
        }

        /// <summary>
        /// Gets the active process parameters tables group.
        /// </summary>
        /// <param name="rmlGuid">The RML unique identifier.</param>
        /// <returns></returns>
        public ProcessParametersTablesGroup GetRmlActiveProcessParametersTablesGroup(String rmlGuid)
        {
            return _db.ProcessParametersTablesGroups.Where(x => x.RmlGuid == rmlGuid && x.Active).Include(x => x.ProcessParametersTables).FirstOrDefault();
        }

        /// <summary>
        /// Gets the RML process parameters tables groups.
        /// </summary>
        /// <param name="rmlGuid">The RML unique identifier.</param>
        /// <returns></returns>
        public List<ProcessParametersTablesGroup> GetRmlProcessParametersTablesGroups(String rmlGuid)
        {
            var groups = _db.ProcessParametersTablesGroups.Where(x => x.RmlGuid == rmlGuid).ToList();

            foreach (var group in groups)
            {
                group.ProcessParametersTables = _db.ProcessParametersTables.Where(x => x.ProcessParametersTablesGroupGuid == group.Guid).OrderBy(x => x.TableIndex).ToSynchronizedObservableCollection();
            }

            return groups;
        }

        /// <summary>
        /// Gets the RML CATS.
        /// </summary>
        /// <param name="rmlGuid">The RML unique identifier.</param>
        /// <param name="machineGuid">The machine unique identifier.</param>
        /// <returns></returns>
        public ObservableCollection<Cat> GetRmlCATs(String rmlGuid, String machineGuid)
        {
            return _db.Cats.Where(x => x.MachineGuid == machineGuid && x.RmlGuid == rmlGuid).ToObservableCollection();
        }

        /// <summary>
        /// Gets the RML liquid types factors.
        /// </summary>
        /// <param name="rmlGuid">The RML unique identifier.</param>
        /// <returns></returns>
        public ObservableCollection<LiquidTypesRml> GetRmlLiquidTypes(String rmlGuid)
        {
            return _db.LiquidTypesRmls.Where(x => x.RmlGuid == rmlGuid).ToObservableCollection();
        }

        /// <summary>
        /// Gets the job with all its segments and brush stops.
        /// </summary>
        /// <param name="jobGuid">The job unique identifier.</param>
        /// <returns></returns>
        public Job GetJob(String jobGuid)
        {
            Job job = _db.Jobs.Where(x => x.Guid == jobGuid)
                .Include(x => x.Machine)
                .Include(x => x.Rml)
                .Include(x => x.ColorSpace)
                .Include(x => x.WindingMethod)
                .Include(x => x.SpoolType).FirstOrDefault();

            job.Segments = _db.Segments.Where(x => x.JobGuid == jobGuid).OrderBy(x => x.SegmentIndex).ToSynchronizedObservableCollection();

            foreach (var segment in job.Segments)
            {
                segment.BrushStops = _db.BrushStops.Where(x => x.SegmentGuid == segment.Guid).OrderBy(x => x.StopIndex).ToSynchronizedObservableCollection();
            }

            GetMachine(job.Machine.SerialNumber);

            GetRmlActiveProcessParametersTablesGroup(job.RmlGuid);

            return job;
        }

        /// <summary>
        /// Gets the machine with its configuration and organization.
        /// </summary>
        /// <param name="serialNumber">The serial number.</param>
        /// <returns></returns>
        public Machine GetMachine(String serialNumber)
        {
            var machine = _db.Machines.SingleOrDefault(x => x.SerialNumber == serialNumber);

            machine.Organization = _db.Organizations.SingleOrDefault(x => x.Guid == machine.OrganizationGuid);
            machine.Configuration = GetConfiguration(x => x.Guid == machine.ConfigurationGuid);
            machine.Configuration.HardwareVersion = GetHardwareVersion(x => x.Guid == machine.Configuration.HardwareVersionGuid);

            return machine;
        }

        /// <summary>
        /// Disposes the underlying <see cref="ObservablesContext"/>.
        /// </summary>
        public void Dispose()
        {
            _db.Dispose();
        }
    }
}