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
generated by cgit v1.3.1 (git 2.54.0) at 2026-07-29 07:49:28 +0000
 


ef='#n211'>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;
        }
    }
}