aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/DataStore/Tango.DataStore.Editing/DataStoreItemModel.cs
blob: 8f00a0ec67c77a77cd33983e6eb07960aab59336 (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core;

namespace Tango.DataStore.Editing
{
    public class DataStoreItemModel : ExtendedObject, IDataStoreItem
    {
        public IDataStoreItem GlobalItem { get; set; }

        public DataType OriginalType { get; set; }
        public Object OriginalValue { get; set; }

        private Object _value;
        public Object Value
        {
            get { return _value; }
            set { _value = value; RaisePropertyChangedAuto(); RaisePropertyChanged(nameof(HasDifference)); RaisePropertyChanged(nameof(FormattedValue)); }
        }

        private bool _isSelected;
        public bool IsSelected
        {
            get { return _isSelected; }
            set { _isSelected = value; RaisePropertyChangedAuto(); }
        }

        public bool HasDifference
        {
            get
            {
                if (OriginalType != Type)
                {
                    return true;
                }

                if (OriginalType == DataType.Bytes && Type == DataType.Bytes)
                {
                    if (OriginalValue != null && Value != null)
                    {
                        return !Enumerable.SequenceEqual(OriginalValue as byte[], Value as byte[]);
                    }
                }

                if (Value == null && OriginalValue == null) return false;

                return !OriginalValue.ToStringSafe().Equals(Value.ToStringSafe()) || (IsGlobal && GlobalItem.Value != Value);
            }
        }

        public String FormattedValue
        {
            get
            {
                var value = this.ToString();

                if (value != null) return value.ToOneLine();
                return null;
            }
        }

        public string Guid { get; set; }
        public string Key { get; set; }

        private DataType _type;
        public DataType Type
        {
            get { return _type; }
            set { _type = value; RaisePropertyChangedAuto(); }
        }

        private DateTime _date;
        public DateTime Date
        {
            get { return _date; }
            set { _date = value; RaisePropertyChangedAuto(); }
        }

        public bool IsSynchronized { get; set; }
        public bool ExistsOnMachine { get; set; }

        private bool _isGlobal;
        public bool IsGlobal
        {
            get { return _isGlobal; }
            set { _isGlobal = value; RaisePropertyChangedAuto(); }
        }

        private bool _isDeleted;
        public bool IsDeleted
        {
            get { return _isDeleted; }
            set { _isDeleted = value; RaisePropertyChangedAuto(); }
        }

        public static DataStoreItemModel FromLocalDataStoreItem(IDataStoreItem local, IDataStoreItem globalItem)
        {
            DataStoreItemModel model = new DataStoreItemModel();

            model.OriginalValue = local.Value;
            model.Value = local.Value;
            model.Guid = local.Guid;
            model.Key = local.Key;
            model.OriginalType = local.Type;
            model.Type = local.Type;
            model.Date = local.Date;
            model.IsGlobal = false;
            model.IsSynchronized = local.IsSynchronized;

            model.GlobalItem = globalItem;

            return model;
        }

        public static DataStoreItemModel FromGlobalDataStoreItem(IDataStoreItem global)
        {
            DataStoreItemModel model = new DataStoreItemModel();

            model.GlobalItem = global;
            model.Guid = global.Guid;
            model.Key = global.Key;
            model.Type = global.Type;
            model.OriginalType = global.Type;
            model.Date = global.Date;
            model.IsGlobal = true;
            model.IsSynchronized = global.IsSynchronized;

            return model;
        }

        public override string ToString()
        {
            if (this.Value != null)
            {
                return DataStoreHelper.FormatDataStoreItem(this);
            }
            else
            {
                return null;
            }
        }
    }
}