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.
///
///
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();
}
///
/// 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();
if (initializing)
{
//Remove Unlinked Configurations..
List remove_configurations = new List();
foreach (var config in Context.Configurations)
{
if (Context.MachineVersions.FirstOrDefault(x => x.DefaultConfigurationGuid == config.Guid) != null)
{
continue;
}
if (Context.MachinesConfigurations.FirstOrDefault(x => x.ConfigurationGuid == config.Guid) != null)
{
continue;
}
if (Context.Machines.FirstOrDefault(x => x.ConfigurationGuid == config.Guid) != null)
{
continue;
}
remove_configurations.Add(config);
}
foreach (var config in remove_configurations)
{
Context.Configurations.Remove(config);
}
Context.SaveChanges();
//Remove Unlinked Configurations..
}
Organizations = Context.Organizations.ToObservableCollection();
Machines = Context.Machines.ToObservableCollection();
foreach (var machine in Machines)
{
machine.MachinesConfigurations = machine.MachinesConfigurations.OrderByDescending(x => x.Configuration.CreationDate).Take(30).ToObservableCollection();
//machine.Jobs = machine.Jobs.OrderByDescending(x => x.CreationDate).ToObservableCollection(); //No need to load jobs.
}
MachinesConfigurations = Context.MachinesConfigurations.ToObservableCollection();
MachineVersions = Context.MachineVersions.ToList().OrderBy(x => x.Version).ToObservableCollection();
Addresses = Context.Addresses.Where(x => !x.Deleted).ToList().OrderBy(x => x.AddressString).ToObservableCollection();
Contacts = Context.Contacts.Where(x => !x.Deleted).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 user in Users)
//{
// user.UsersRoles = user.UsersRoles.Where(x => !x.Deleted).ToObservableCollection();
//}
foreach (var role in Roles)
{
role.RolesPermissions = role.RolesPermissions.ToObservableCollection();
}
Configurations = Context.Configurations.ToList().OrderBy(x => x.LastUpdated).ToObservableCollection();
foreach (var config in Configurations)
{
config.IdsPacks = config.IdsPacks.ToObservableCollection();
}
ApplicationVersions = Context.ApplicationVersions.ToList().OrderBy(x => x.Version).ToObservableCollection();
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();
EmbeddedSoftwareVersions = Context.EmbeddedSoftwareVersions.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();
ActionTypes = Context.ActionTypes.ToObservableCollection();
EventTypesCategories = Context.EventTypesCategories.ToObservableCollection();
EventTypesGroups = Context.EventTypesGroups.ToObservableCollection();
EventTypesActions = Context.EventTypesActions.ToObservableCollection();
EventTypes = Context.EventTypes.ToObservableCollection();
foreach (var eventType in EventTypes)
{
eventType.EventTypesActions = eventType.EventTypesActions.ToObservableCollection();
}
MediaMaterials = Context.MediaMaterials.ToObservableCollection();
MediaColors = Context.MediaColors.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).ToObservableCollection();
}
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();
HtmlPages = Context.HtmlPages.ToObservableCollection();
_dispatcher = Application.Current.Dispatcher;
_dispatcher.Invoke(() =>
{
InitCollectionSources();
});
//var action = ActionTypes.First();
//action.Name = "Action 1 Yesss";
//SaveChanges();
//BindingOperations.EnableCollectionSynchronization(Machines, _syncLock);
//BindingOperations.EnableCollectionSynchronization(MachinesViewSource, _syncLock);
//BindingOperations.EnableCollectionSynchronization(MachinesConfigurations, _syncLock);
//BindingOperations.EnableCollectionSynchronization(MachinesConfigurationsViewSource, _syncLock);
//BindingOperations.EnableCollectionSynchronization(EventTypesActions, _syncLock);
//BindingOperations.EnableCollectionSynchronization(EventTypesActionsViewSource, _syncLock);
}
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;
}
}
}