using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using Tango.BL.Entities; using System.Data.Entity; using System.Collections.ObjectModel; namespace Tango.BL { /// /// Represents an adapter for retrieving aggregated entities. /// public class ObservablesContextAdapter : IDisposable { private ObservablesContext _db; /// /// Gets the underlying . /// public ObservablesContext Context { get { return _db; } } /// /// Initializes a new instance of the class. /// /// The context. public ObservablesContextAdapter(ObservablesContext context) { _db = context; } /// /// Creates the a new instance of wrapping a default . /// /// public static ObservablesContextAdapter CreateDefault() { var context = ObservablesContext.CreateDefault(); return new ObservablesContextAdapter(context); } /// /// Loads the configuration with it's Ids packs. /// /// The condition. /// public Configuration GetConfiguration(Expression> condition) { var config = _db.Configurations.SingleOrDefault(condition); var l = _db.IdsPacks.Where(x => x.ConfigurationGuid == config.Guid) .Include(x => x.LiquidType) .Include(x => x.MidTankType) .Include(x => x.CartridgeType) .Include(x => x.Dispenser) .Include(x => x.Dispenser.DispenserType) .Include(x => x.IdsPackFormula).OrderBy(x => x.PackIndex).ToList(); return config; } /// /// Gets the hardware version by the specified condition. /// /// The condition. /// public HardwareVersion GetHardwareVersion(Expression> condition) { HardwareVersion version = _db.HardwareVersions.SingleOrDefault(condition); version.HardwareBlowers = _db.HardwareBlowers.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareBlowerType).ToList().OrderBy(x => x.HardwareBlowerType.Code).ToSynchronizedObservableCollection(); version.HardwareBreakSensors = _db.HardwareBreakSensors.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareBreakSensorType).ToList().OrderBy(x => x.HardwareBreakSensorType.Code).ToSynchronizedObservableCollection(); version.HardwareDancers = _db.HardwareDancers.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareDancerType).ToList().OrderBy(x => x.HardwareDancerType.Code).ToSynchronizedObservableCollection(); version.HardwareMotors = _db.HardwareMotors.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareMotorType).ToList().OrderBy(x => x.HardwareMotorType.Code).ToSynchronizedObservableCollection(); version.HardwarePidControls = _db.HardwarePidControls.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwarePidControlType).ToList().OrderBy(x => x.HardwarePidControlType.Code).ToSynchronizedObservableCollection(); version.HardwareSpeedSensors = _db.HardwareSpeedSensors.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareSpeedSensorType).ToList().OrderBy(x => x.HardwareSpeedSensorType.Code).ToSynchronizedObservableCollection(); version.HardwareWinders = _db.HardwareWinders.Where(x => x.HardwareVersionGuid == version.Guid).Include(x => x.HardwareWinderType).ToList().OrderBy(x => x.HardwareWinderType.Code).ToSynchronizedObservableCollection(); return version; } /// /// Gets the hardware version by machine. /// /// The machine unique identifier. /// public HardwareVersion GetHardwareVersionByMachine(String machineGuid) { var machine = _db.Machines.Where(x => x.Guid == machineGuid).Include(x => x.Configuration).FirstOrDefault(); return GetHardwareVersion(x => x.Guid == machine.Configuration.HardwareVersionGuid); } /// /// Gets the active process parameters tables group. /// /// The RML unique identifier. /// public ProcessParametersTablesGroup GetRmlActiveProcessParametersTablesGroup(String rmlGuid) { return _db.ProcessParametersTablesGroups.Where(x => x.RmlGuid == rmlGuid && x.Active).Include(x => x.ProcessParametersTables).FirstOrDefault(); } /// /// Gets the RML process parameters tables groups. /// /// The RML unique identifier. /// public List GetRmlProcessParametersTablesGroups(String rmlGuid) { var groups = _db.ProcessParametersTablesGroups.Where(x => x.RmlGuid == rmlGuid).ToList(); foreach (var group in groups) { group.ProcessParametersTables = _db.ProcessParametersTables.Where(x => x.ProcessParametersTablesGroupGuid == group.Guid).OrderBy(x => x.TableIndex).ToSynchronizedObservableCollection(); } return groups; } /// /// Gets the RML CATS. /// /// The RML unique identifier. /// The machine unique identifier. /// public ObservableCollection GetRmlCATs(String rmlGuid, String machineGuid) { return _db.Cats.Where(x => x.MachineGuid == machineGuid && x.RmlGuid == rmlGuid).ToObservableCollection(); } /// /// Gets the RML liquid types factors. /// /// The RML unique identifier. /// public ObservableCollection GetRmlLiquidTypes(String rmlGuid) { return _db.LiquidTypesRmls.Where(x => x.RmlGuid == rmlGuid).ToObservableCollection(); } /// /// Gets the job with all its segments and brush stops. /// /// The job unique identifier. /// public Job GetJob(String jobGuid) { Job job = _db.Jobs.Where(x => x.Guid == jobGuid) .Include(x => x.Machine) .Include(x => x.Rml) .Include(x => x.ColorSpace) .Include(x => x.WindingMethod) .Include(x => x.SpoolType).FirstOrDefault(); job.Segments = _db.Segments.Where(x => x.JobGuid == jobGuid).OrderBy(x => x.SegmentIndex).ToSynchronizedObservableCollection(); foreach (var segment in job.Segments) { segment.BrushStops = _db.BrushStops.Where(x => x.SegmentGuid == segment.Guid).OrderBy(x => x.StopIndex).ToSynchronizedObservableCollection(); } GetMachine(job.Machine.SerialNumber); GetRmlActiveProcessParametersTablesGroup(job.RmlGuid); return job; } /// /// Gets the machine with its configuration and organization. /// /// The serial number. /// public Machine GetMachine(String serialNumber) { var machine = _db.Machines.SingleOrDefault(x => x.SerialNumber == serialNumber); machine.Organization = _db.Organizations.SingleOrDefault(x => x.Guid == machine.OrganizationGuid); machine.Configuration = GetConfiguration(x => x.Guid == machine.ConfigurationGuid); machine.Configuration.HardwareVersion = GetHardwareVersion(x => x.Guid == machine.Configuration.HardwareVersionGuid); return machine; } /// /// Disposes the underlying . /// public void Dispose() { _db.Dispose(); } } }