aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs
blob: d265fbe01b48ffdcaf238e8427f3c3b9d4703279 (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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.Core;
using Tango.Settings;

namespace Tango.BL
{
    public partial class ObservablesContext
    {
        private List<ObservableModifiedEventArgs> _pending_notifications = new List<ObservableModifiedEventArgs>();

        public ObservablesContext()
        {

        }

        /// <summary>
        /// Initializes a new instance of the <see cref="ObservablesContext" /> class.
        /// </summary>
        /// <param name="path">The server file path.</param>
        /// <param name="isFile">if set to <c>true</c> will try to connect to an .mdf file.</param>
        public ObservablesContext(DataSource dataSource) : base(dataSource.ToConnection(), true)
        {
            Database.SetInitializer<ObservablesContext>(null);
        }

        /// <summary>
        /// Creates a default remote database context by the address specified in <see cref="SettingsManager.Default.DataBase.SQLServerAddress" />.
        /// </summary>
        /// <returns></returns>
        public static ObservablesContext CreateDefault()
        {
            return new ObservablesContext(SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource);
        }

        /// <summary>
        /// Creates a default remote database context.
        /// </summary>
        /// <returns></returns>
        public static ObservablesContext CreateDefault(DataSource dataSource)
        {
            return new ObservablesContext(dataSource);
        }

        /// <summary>
        /// Creates a default remote database context.
        /// </summary>
        /// <returns></returns>
        public static ObservablesContext CreateDefault(String address, String catalog, DataSourceType type)
        {
            return new ObservablesContext(new DataSource()
            {
                Address = address,
                Catalog = catalog,
                IntegratedSecurity = true,
                Type = type
            });
        }

        /// <summary>
        /// Creates a default remote database context.
        /// </summary>
        /// <returns></returns>
        public static ObservablesContext CreateDefault(String address, DataSourceType type)
        {
            return CreateDefault(address, "Tango", type);
        }

        /// <summary>
        /// Creates a default remote database context.
        /// </summary>
        /// <returns></returns>
        public static ObservablesContext CreateDefault(String address)
        {
            return CreateDefault(address, "Tango", DataSourceType.SQLServer);
        }

        /// <summary>
        /// Saves all changes made in this context to the underlying database.
        /// </summary>
        /// <returns>
        /// The number of objects written to the underlying database.
        /// </returns>
        public override int SaveChanges()
        {
            var result = base.SaveChanges();
            RaisePendingNotifications();
            return result;
        }

        /// <summary>
        /// Asynchronously saves all changes made in this context to the underlying database.
        /// </summary>
        /// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous save operation.
        /// The task result contains the number of objects written to the underlying database.
        /// </returns>
        /// <remarks>
        /// Multiple active operations on the same context instance are not supported.  Use 'await' to ensure
        /// that any asynchronous operations have completed before calling another method on this context.
        /// </remarks>
        public override Task<int> SaveChangesAsync(CancellationToken cancellationToken)
        {
            var result = base.SaveChangesAsync(cancellationToken);
            RaisePendingNotifications();
            return result;
        }

        /// <summary>
        /// Raises the pending notifications.
        /// </summary>
        private void RaisePendingNotifications()
        {
            Task.Factory.StartNew(() =>
            {
                foreach (var e in _pending_notifications.DistinctBy(x => x.NotifiedEntity))
                {
                    try
                    {
                        e.NotifiedEntity.RaiseModified(e.Context, e.ModifiedEntity, e.NotifiedEntity);
                    }
                    catch { }
                }

                _pending_notifications.Clear();
            });
        }

        /// <summary>
        /// Extension point allowing the user to override the default behavior of validating only
        /// added and modified entities.
        /// </summary>
        /// <param name="entityEntry">DbEntityEntry instance that is supposed to be validated.</param>
        /// <returns>
        /// true to proceed with validation; false otherwise.
        /// </returns>
        protected override bool ShouldValidateEntity(DbEntityEntry entityEntry)
        {
            if (entityEntry.State == EntityState.Modified && entityEntry.Entity is IObservableEntity)
            {
                IObservableEntity modified = entityEntry.Entity as IObservableEntity;

                foreach (var toNotify in ObservableEntitiesContainer.RegisteredEntities.Where(x => x.Guid == modified.Guid))
                {
                    _pending_notifications.Add(new ObservableModifiedEventArgs(this, modified, toNotify));
                }
            }

            return base.ShouldValidateEntity(entityEntry);
        }

        #region Public Methods

        /// <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 = Configurations.SingleOrDefault(condition);

            config.IdsPacks = IdsPacks.Where(x => x.ConfigurationGuid == config.Guid)
                .Include(x => x.LiquidType)
                .Include(x => x.DispenserType)
                .Include(x => x.CartridgeType)
                .Include(x => x.DispenserType)
                .Include(x => x.IdsPackFormula).OrderBy(x => x.PackIndex).ToObservableCollection();

            return config;
        }

        public HardwareVersion GetHardwareVersion(Expression<Func<HardwareVersion, bool>> condition)
        {
            HardwareVersion version = HardwareVersions.SingleOrDefault(condition);

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

            return version;
        }

        public HardwareVersion GetHardwareVersionByMachine(String machineGuid)
        {
            var machine = Machines.Where(x => x.Guid == machineGuid).Include(x => x.Configuration).FirstOrDefault();
            return GetHardwareVersion(x => x.Guid == machine.Configuration.HardwareVersionGuid);
        }

        public ProcessParametersTablesGroup GetActiveProcessParametersTablesGroup(String rmlGuid)
        {
            return ProcessParametersTablesGroups.Where(x => x.RmlGuid == rmlGuid && x.Active).Include(x => x.ProcessParametersTables).FirstOrDefault();
        }

        public ObservableCollection<Cct> GetRmlCCTs(String rmlGuid)
        {
            return Ccts.Where(x => x.RmlGuid == rmlGuid).ToObservableCollection();
        }

        public ObservableCollection<Cat> GetRmlCATs(String rmlGuid, String machineGuid)
        {
            return Cats.Where(x => x.MachineGuid == machineGuid && x.RmlGuid == rmlGuid).ToObservableCollection();
        }

        public ObservableCollection<LiquidTypesRml> GetRmlLiquidTypes(String rmlGuid)
        {
            return LiquidTypesRmls.Where(x => x.RmlGuid == rmlGuid).ToObservableCollection();
        }

        public Organization GetOrganizationAndUsers(String organizationGuid)
        {
            var org = Organizations.SingleOrDefault(x => x.Guid == organizationGuid);

            org.Address = Addresses.SingleOrDefault(x => x.Guid == org.AddressGuid);
            org.Contact = Contacts.SingleOrDefault(x => x.Guid == org.ContactGuid);

            Roles.Load();
            Permissions.Load();
            RolesPermissions.Load();


            org.Users = Users.Where(x => x.OrganizationGuid == organizationGuid)
                .Include(x => x.Address)
                .Include(x => x.Contact)
                .Include(x => x.UsersRoles).ToObservableCollection();

            return org;
        }

        public User GetUser(String userGuid)
        {
            Roles.Load();
            Permissions.Load();
            RolesPermissions.Load();

            return Users.Where(x => x.Guid == userGuid)
                .Include(x => x.Address)
                .Include(x => x.Contact)
                .Include(x => x.UsersRoles)
                .FirstOrDefault();
        }

        #endregion
    }
}