aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.BL/Entities/Configuration.cs
blob: ae48b52c97369331bdecece0e1bdf085c222dd34 (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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using Tango.BL.Serialization;
using Tango.BL.ValueObjects;

namespace Tango.BL.Entities
{
    public partial class Configuration : ConfigurationBase
    {
        /// <summary>
        /// Gets a collection of this configuration IDS packs where <see cref="IdsPackBase.IsEmpty"/> is 'true'.
        /// </summary>
        [NotMapped]
        [XmlIgnore]
        [JsonIgnore]
        public IEnumerable<IdsPack> NoneEmptyIdsPacks
        {
            get { return IdsPacks.Where(x => !x.IsEmpty); }
        }

        [NotMapped]
        [XmlIgnore]
        [JsonIgnore]
        public List<IdsPack> IdsPacksOrderedByPackIndex
        {
            get { return IdsPacks.OrderBy(x => x.PackIndex).ToList(); }
        }

        public IdsPack GetIdsPackByColorMatch(String name)
        {
            return NoneEmptyIdsPacks.Where(x => x.LiquidType.AvailableForStandardUser).FirstOrDefault(x => x.LiquidType.Type.ToString().ToLower().Contains(name.ToLower()));
        }

        /// <summary>
        /// Gets a collection of this configuration IDS packs where packs are not 'empty' and are supported by the specified <see cref="Rml"/>.
        /// </summary>
        /// <param name="rml">The RML.</param>
        /// <returns></returns>
        /// <exception cref="NullReferenceException">The specified RML cannot be null.</exception>
        public IEnumerable<IdsPack> GetSupportedIdsPacks(Rml rml)
        {
            if (rml == null)
            {
                throw new NullReferenceException("The specified RML cannot be null.");
            }

            //return NoneEmptyIdsPacks.Where(x => rml.LiquidTypesRmls.ToList().Exists(y => x.LiquidType != null && y.LiquidType.Guid == x.LiquidType.Guid)).OrderBy(x => x.PackIndex).ToList();
            return NoneEmptyIdsPacks.OrderBy(x => x.PackIndex).ToList();
        }

        /// <summary>
        /// Clones configuration and its IDS packs (excluding the dispenser).
        /// </summary>
        /// <returns></returns>
        public override Configuration Clone()
        {
            Configuration cloned = base.Clone();

            foreach (var idsPack in this.IdsPacks)
            {
                IdsPack clonedPack = idsPack.Clone();
                clonedPack.Configuration = cloned;
                clonedPack.CartridgeType = idsPack.CartridgeType;
                clonedPack.MidTankType = idsPack.MidTankType;
                clonedPack.IdsPackFormula = idsPack.IdsPackFormula;
                clonedPack.IsEmpty = idsPack.IsEmpty;
                clonedPack.Dispenser = null;
                clonedPack.DispenserGuid = null;
                cloned.IdsPacks.Add(clonedPack);
            }

            cloned.HardwareConfigurationString = HardwareConfigurationString;

            return cloned;
        }

        /// <summary>
        /// Removes this entity and all dependent entities from the specified db context.
        /// </summary>
        /// <param name="context">The context.</param>
        public override void Delete(ObservablesContext context)
        {
            base.Delete(context);

            context.IdsPacks.RemoveRange(IdsPacks);
            context.Configurations.Remove(this);
        }

        public HardwareConfiguration GetHardwareConfiguration()
        {
            return HardwareConfiguration.FromJson(HardwareConfigurationString);
        }

        public void SetHardwareConfiguration(HardwareConfiguration hwConfig)
        {
            HardwareConfigurationString = hwConfig.ToJson();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Configuration" /> class.
        /// </summary>
        public Configuration() : base()
        {

        }
    }
}