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 { /// /// Exposes method for retrieving observable collections of observable entities. /// /// [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; /// /// Gets the singleton instance. /// public static ObservablesEntitiesAdapter Instance { get { if (_instance == null) { _instance = new ObservablesEntitiesAdapter(); } return _instance; } } /// /// Gets the DB context. /// public ObservablesContext Context { get; private set; } /// /// Performs an implicit conversion from to . /// /// The instance. /// /// The result of the conversion. /// public static implicit operator DbContext(ObservablesEntitiesAdapter instance) { return instance.Context; } /// /// Prevents a default instance of the class from being created. /// private ObservablesEntitiesAdapter() { Context = ObservablesContext.CreateDefault(); Context.Configuration.LazyLoadingEnabled = true; } /// /// Initializes this instance. /// public void Initialize() { Invalidate(true); Initialized?.Invoke(); } /// /// Saves the current changes to database. /// public void SaveChanges() { try { Context.SaveChanges(); } catch (Exception ex) { throw ex; } finally { Invalidate(); } } /// /// Invokes the specified action on the adapter synchronization context. /// /// The action. public void Invoke(Action action) { _dispatcher.Invoke(() => { action(); }); } /// /// Invalidates the adapter and reloads the database. /// /// if set to true [initializing]. 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."); } } /// /// Creates a collection view from the specified observable collection. /// /// /// The collection. /// private ICollectionView CreateCollectionView(ObservableCollection collection) { var view = CollectionViewSource.GetDefaultView(collection); return view; } } }