aboutsummaryrefslogtreecommitdiff
path: root/src/WixToolset.Data.WindowsInstaller/Rows/WixComplexReferenceRow.cs
blob: 40ca45921e719c4b2e5693ada7d0d130ed258b4f (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

namespace WixToolset.Data.Rows
{
    using System;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Xml;

    /// <summary>
    /// Specialization of a row for the WixComplexReference table.
    /// </summary>
    public sealed class WixComplexReferenceRow : Row, IComparable
    {
        /// <summary>
        /// Creates a WixComplexReferenceRow row that belongs to a table.
        /// </summary>
        /// <param name="sourceLineNumbers">Original source lines for this row.</param>
        /// <param name="table">Table this row belongs to and should get its column definitions from.</param>
        public WixComplexReferenceRow(SourceLineNumber sourceLineNumbers, Table table)
            : base(sourceLineNumbers, table)
        {
        }

        /// <summary>
        /// Gets the parent type of the complex reference.
        /// </summary>
        /// <value>Parent type of the complex reference.</value>
        public ComplexReferenceParentType ParentType
        {
            get { return (ComplexReferenceParentType)Enum.ToObject(typeof(ComplexReferenceParentType), (int)this.Fields[1].Data); }
            set { this.Fields[1].Data = (int)value; }
        }

        /// <summary>
        /// Gets or sets the parent identifier of the complex reference.
        /// </summary>
        /// <value>Parent identifier of the complex reference.</value>
        public string ParentId
        {
            get { return (string)this.Fields[0].Data; }
            set { this.Fields[0].Data = value; }
        }

        /// <summary>
        /// Gets the parent language of the complex reference.
        /// </summary>
        /// <value>Parent language of the complex reference.</value>
        public string ParentLanguage
        {
            get { return (string)this.Fields[2].Data; }
            set { this.Fields[2].Data = value; }
        }

        /// <summary>
        /// Gets the child type of the complex reference.
        /// </summary>
        /// <value>Child type of the complex reference.</value>
        public ComplexReferenceChildType ChildType
        {
            get { return (ComplexReferenceChildType)Enum.ToObject(typeof(ComplexReferenceChildType), (int)this.Fields[4].Data); }
            set { this.Fields[4].Data = (int)value; }
        }

        /// <summary>
        /// Gets the child identifier of the complex reference.
        /// </summary>
        /// <value>Child identifier of the complex reference.</value>
        public string ChildId
        {
            get { return (string)this.Fields[3].Data; }
            set { this.Fields[3].Data = value; }
        }

        /// <summary>
        /// Gets if this is the primary complex reference.
        /// </summary>
        /// <value>true if primary complex reference.</value>
        public bool IsPrimary
        {
            get
            {
                return (0x1 == ((int)this.Fields[5].Data & 0x1));
            }

            set
            {
                if (null == this.Fields[5].Data)
                {
                    this.Fields[5].Data = 0;
                }

                if (value)
                {
                    this.Fields[5].Data = (int)this.Fields[5].Data | 0x1;
                }
                else
                {
                    this.Fields[5].Data = (int)this.Fields[5].Data & ~0x1;
                }
            }
        }

        /// <summary>
        /// Determines if two complex references are equivalent.
        /// </summary>
        /// <param name="obj">Complex reference to compare.</param>
        /// <returns>True if complex references are equivalent.</returns>
        public override bool Equals(object obj)
        {
            return 0 == this.CompareTo(obj);
        }

        /// <summary>
        /// Gets the hash code for the complex reference.
        /// </summary>
        /// <returns>Hash code for the complex reference.</returns>
        public override int GetHashCode()
        {
            return this.ChildType.GetHashCode() ^ this.ChildId.GetHashCode() ^ this.ParentType.GetHashCode() ^ this.ParentLanguage.GetHashCode() ^ this.ParentId.GetHashCode() ^ this.IsPrimary.GetHashCode();
        }

        /// <summary>
        /// Compares two complex references.
        /// </summary>
        /// <param name="obj">Complex reference to compare to.</param>
        /// <returns>Zero if the objects are equivalent, negative number if the provided object is less, positive if greater.</returns>
        public int CompareTo(object obj)
        {
            int comparison = this.CompareToWithoutConsideringPrimary(obj);
            if (0 == comparison)
            {
                comparison = ((WixComplexReferenceRow)obj).IsPrimary.CompareTo(this.IsPrimary); // Note: the order of these is purposely switched to ensure that "Yes" is lower than "No" and "NotSet"
            }
            return comparison;
        }

        /// <summary>
        /// Compares two complex references without considering the primary bit.
        /// </summary>
        /// <param name="obj">Complex reference to compare to.</param>
        /// <returns>Zero if the objects are equivalent, negative number if the provided object is less, positive if greater.</returns>
        [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.ArgumentException.#ctor(System.String,System.String)")]
        public int CompareToWithoutConsideringPrimary(object obj)
        {
            var other = obj as WixComplexReferenceRow ?? throw new ArgumentNullException(nameof(obj));

            int comparison = this.ChildType - other.ChildType;
            if (0 == comparison)
            {
                comparison = String.Compare(this.ChildId, other.ChildId, StringComparison.Ordinal);
                if (0 == comparison)
                {
                    comparison = this.ParentType - other.ParentType;
                    if (0 == comparison)
                    {
                        string thisParentLanguage = null == this.ParentLanguage ? String.Empty : this.ParentLanguage;
                        string otherParentLanguage = null == other.ParentLanguage ? String.Empty : other.ParentLanguage;
                        comparison = String.Compare(thisParentLanguage, otherParentLanguage, StringComparison.Ordinal);
                        if (0 == comparison)
                        {
                            comparison = String.Compare(this.ParentId, other.ParentId, StringComparison.Ordinal);
                        }
                    }
                }
            }

            return comparison;
        }

        /// <summary>
        /// Creates a shallow copy of the ComplexReference.
        /// </summary>
        /// <returns>A shallow copy of the ComplexReference.</returns>
        public WixComplexReferenceRow Clone()
        {
            WixComplexReferenceRow wixComplexReferenceRow = new WixComplexReferenceRow(this.SourceLineNumbers, this.Table);
            wixComplexReferenceRow.ParentType = this.ParentType;
            wixComplexReferenceRow.ParentId = this.ParentId;
            wixComplexReferenceRow.ParentLanguage = this.ParentLanguage;
            wixComplexReferenceRow.ChildType = this.ChildType;
            wixComplexReferenceRow.ChildId = this.ChildId;
            wixComplexReferenceRow.IsPrimary = this.IsPrimary;

            return wixComplexReferenceRow;
        }

        /// <summary>
        /// Changes all of the parent references to point to the passed in parent reference.
        /// </summary>
        /// <param name="parent">New parent complex reference.</param>
        public void Reparent(WixComplexReferenceRow parent)
        {
            this.ParentId = parent.ParentId;
            this.ParentLanguage = parent.ParentLanguage;
            this.ParentType = parent.ParentType;

            if (!this.IsPrimary)
            {
                this.IsPrimary = parent.IsPrimary;
            }
        }
    }
}