aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/ObservablesEntitiesAdapter.cs
blob: eb2d176e85c841eb8e45870f855b2bb2321c3fe4 (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
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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using System.Windows.Threading;
using Tango.BL.Entities;
using Tango.Core;
using Tango.DAL.Remote.DB;
using Tango.Logging;
using Tango.Settings;

namespace Tango.BL
{
    /// <summary>
    /// Exposes method for retrieving observable collections of observable entities.
    /// </summary>
    /// <seealso cref="Tango.Core.ExtendedObject" />
    [Obsolete("ObservablesEntitiesAdapter should not be used. Use ObservablesStaticCollections instead!")]
    public partial class ObservablesEntitiesAdapter : ExtendedObject
    {
        public event Action Initialized;
        private Dispatcher _dispatcher;

        private static object _syncLock;

        private static ObservablesEntitiesAdapter _instance;
        /// <summary>
        /// Gets the singleton instance.
        /// </summary>
        public static ObservablesEntitiesAdapter Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ObservablesEntitiesAdapter();
                }
                return _instance;
            }
        }

        /// <summary>
        /// Gets the DB context.
        /// </summary>
        public ObservablesContext Context { get; private set; }

        /// <summary>
        /// Performs an implicit conversion from <see cref="ObservablesEntitiesAdapter"/> to <see cref="DbContext"/>.
        /// </summary>
        /// <param name="instance">The instance.</param>
        /// <returns>
        /// The result of the conversion.
        /// </returns>
        public static implicit operator DbContext(ObservablesEntitiesAdapter instance)
        {
            return instance.Context;
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="ObservablesEntitiesAdapter"/> class from being created.
        /// </summary>
        private ObservablesEntitiesAdapter()
        {
            Context = ObservablesContext.CreateDefault();
            Context.Configuration.LazyLoadingEnabled = true;
        }

        /// <summary>
        /// Initializes this instance.
        /// </summary>
        public void Initialize()
        {
            Invalidate(true);
            Initialized?.Invoke();
        }

        /// <summary>
        /// Saves the current changes to database.
        /// </summary>
        public void SaveChanges()
        {
            try
            {
                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Invalidate();
            }
        }

        /// <summary>
        /// Invokes the specified action on the adapter synchronization context.
        /// </summary>
        /// <param name="action">The action.</param>
        public void Invoke(Action action)
        {
            _dispatcher.Invoke(() =>
            {
                action();
            });
        }

        /// <summary>
        /// Invalidates the adapter and reloads the database.
        /// </summary>
        /// <param name="initializing">if set to <c>true</c> [initializing].</param>
        public void Invalidate(bool initializing = false)
        {
            try
            {
                _syncLock = new object();

                Organizations = Context.Organizations.ToObservableCollection();

                Machines = Context.Machines.ToObservableCollection();

                MachineVersions = Context.MachineVersions.ToList().OrderBy(x => x.Version).ToObservableCollection();

                Addresses = Context.Addresses.ToList().OrderBy(x => x.AddressString).ToObservableCollection();

                Contacts = Context.Contacts.ToList().OrderBy(x => x.FullName).ToObservableCollection();

                Roles = Context.Roles.ToList().OrderBy(x => x.Name).ToObservableCollection();

                Permissions = Context.Permissions.ToList().OrderBy(x => x.Name).ToObservableCollection();

                UsersRoles = Context.UsersRoles.ToObservableCollection();

                Users = Context.Users.Where(x => !x.Deleted).ToList().OrderBy(x => x.Contact.FullName).ToObservableCollection();

                foreach (var role in Roles)
                {
                    role.RolesPermissions = role.RolesPermissions.ToSynchronizedObservableCollection();
                }

                Configurations = Context.Configurations.ToList().OrderBy(x => x.LastUpdated).ToObservableCollection();

                foreach (var config in Configurations)
                {
                    //config.IdsPacks = config.IdsPacks.ToObservableCollection2();
                }

                ApplicationOsVersions = Context.ApplicationOsVersions.ToList().OrderBy(x => x.Version).ToObservableCollection();

                ApplicationFirmwareVersions = Context.ApplicationFirmwareVersions.ToList().OrderBy(x => x.Version).ToObservableCollection();

                ApplicationDisplayPanelVersions = Context.ApplicationDisplayPanelVersions.ToList().OrderBy(x => x.Version).ToObservableCollection();

                EmbeddedFirmwareVersions = Context.EmbeddedFirmwareVersions.ToList().OrderBy(x => x.Version).ToObservableCollection();

                HardwareVersions = Context.HardwareVersions.ToList().OrderBy(x => x.Version).ToObservableCollection();

                IdsPacks = Context.IdsPacks.ToObservableCollection();

                DispenserTypes = Context.DispenserTypes.ToObservableCollection();

                LiquidTypes = Context.LiquidTypes.ToObservableCollection();

                CartridgeTypes = Context.CartridgeTypes.ToObservableCollection();

                MidTankTypes = Context.MidTankTypes.ToObservableCollection();

                SpoolTypes = Context.SpoolTypes.ToObservableCollection();

                EventTypes = Context.EventTypes.ToObservableCollection();

                MediaMaterials = Context.MediaMaterials.ToObservableCollection();

                MediaPurposes = Context.MediaPurposes.ToObservableCollection();

                MediaConditions = Context.MediaConditions.ToObservableCollection();

                LinearMassDensityUnits = Context.LinearMassDensityUnits.ToObservableCollection();

                FiberShapes = Context.FiberShapes.ToObservableCollection();

                FiberSynths = Context.FiberSynths.ToObservableCollection();

                Rmls = Context.Rmls.ToObservableCollection();

                LiquidTypesRmls = Context.LiquidTypesRmls.ToObservableCollection();

                Ccts = Context.Ccts.ToObservableCollection();

                Cats = Context.Cats.ToObservableCollection();

                ProcessParametersTables = Context.ProcessParametersTables.ToObservableCollection();

                ProcessParametersTablesGroups = Context.ProcessParametersTablesGroups.ToObservableCollection();

                foreach (var group in ProcessParametersTablesGroups)
                {
                    group.ProcessParametersTables = group.ProcessParametersTables.OrderBy(x => x.TableIndex).ToSynchronizedObservableCollection();
                }

                WindingMethods = Context.WindingMethods.ToObservableCollection();

                TechMonitors = Context.TechMonitors.ToObservableCollection();

                TechDispensers = Context.TechDispensers.ToObservableCollection();

                TechValves = Context.TechValves.ToObservableCollection();

                TechIos = Context.TechIos.ToObservableCollection();

                TechControllers = Context.TechControllers.ToObservableCollection();

                IdsPackFormulas = Context.IdsPackFormulas.ToObservableCollection();

                ColorSpaces = Context.ColorSpaces.ToObservableCollection();

                HardwareDancerTypes = Context.HardwareDancerTypes.ToObservableCollection();

                HardwareDancers = Context.HardwareDancers.ToObservableCollection();

                HardwareMotorTypes = Context.HardwareMotorTypes.ToObservableCollection();

                HardwareMotors = Context.HardwareMotors.ToObservableCollection();

                HardwarePidControlTypes = Context.HardwarePidControlTypes.ToObservableCollection();

                HardwarePidControls = Context.HardwarePidControls.ToObservableCollection();

                HardwareWinderTypes = Context.HardwareWinderTypes.ToObservableCollection();

                HardwareWinders = Context.HardwareWinders.ToObservableCollection();

                HardwareSpeedSensorTypes = Context.HardwareSpeedSensorTypes.ToObservableCollection();

                HardwareSpeedSensors = Context.HardwareSpeedSensors.ToObservableCollection();

                HardwareBlowerTypes = Context.HardwareBlowerTypes.ToObservableCollection();

                HardwareBlowers = Context.HardwareBlowers.ToObservableCollection();

                HardwareBreakSensorTypes = Context.HardwareBreakSensorTypes.ToObservableCollection();

                HardwareBreakSensors = Context.HardwareBreakSensors.ToObservableCollection();

                Customers = Context.Customers.ToObservableCollection();

                _dispatcher = Application.Current.Dispatcher;

                _dispatcher.Invoke(() =>
                {
                    InitCollectionSources();
                });
            }
            catch (Exception ex)
            {
                throw LogManager.Default.Log(ex, "Error on observables entities adapter initialization.");
            }
        }

        /// <summary>
        /// Creates a collection view from the specified observable collection.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="collection">The collection.</param>
        /// <returns></returns>
        private ICollectionView CreateCollectionView<T>(ObservableCollection<T> collection)
        {
            var view = CollectionViewSource.GetDefaultView(collection);
            return view;
        }
    }
}