aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/DataStore/Tango.DataStore.EF/EFDataStoreCollection.cs
blob: 6ccc6d78ab1558afcdf9a4c65c024ec189c3fd2a (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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.BL.Entities;
using Tango.Core.ExtensionMethods;

namespace Tango.DataStore.EF
{
    public class EFDataStoreCollection : IDataStoreCollection
    {
        public string Name { get; }

        public EFDataStoreCollection(String name)
        {
            Name = name;
        }

        public void Put<T>(string key, T value)
        {
            Put(key, (object)value);
        }

        public void Put(string key, object value)
        {
            Put(key, DataStoreHelper.GetDataType(value), value);
        }

        public void Put(string key, DataType type, object value)
        {
            using (var db = ObservablesContext.CreateDefault())
            {
                var globalItem = db.GlobalDataStoreItems.FirstOrDefault(x => x.CollectionName == Name && x.Key == key);

                if (globalItem != null)
                {
                    if (globalItem.DataType != (int)type)
                    {
                        throw new InvalidOperationException("A global data store item exists with the same key, but different data type.");
                    }
                }

                DataStoreItem item = db.DataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key);

                if (item == null)
                {
                    item = new DataStoreItem();
                    db.DataStoreItems.Add(item);
                }

                item.CollectionName = Name;
                item.Key = key;
                item.DataType = (int)type;
                item.Value = EFDataStoreHelper.CreateBytes(type, value);
                item.IsSynchronized = false;
                item.LastUpdated = DateTime.UtcNow;
                item.IsDeleted = false;

                db.SaveChanges();
            }
        }

        public T Get<T>(string key)
        {
            return (T)Get(key, null);
        }

        public T Get<T>(string key, T defaultValue)
        {
            return (T)Get(key, (object)defaultValue);
        }

        public object Get(string key)
        {
            return Get(key, null);
        }

        public object Get(string key, object defaultValue)
        {
            return GetItem(key, defaultValue).Value;
        }

        public IDataStoreItem GetItem(string key)
        {
            return GetItem(key, null);
        }

        public IDataStoreItem GetItem(string key, object defaultValue)
        {
            using (var db = ObservablesContext.CreateDefault())
            {
                var item = db.DataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key && !x.IsDeleted);

                if (item == null)
                {
                    var globalItem = db.GlobalDataStoreItems.SingleOrDefault(x => x.CollectionName == Name && x.Key == key);

                    if (globalItem == null)
                    {
                        if (defaultValue == null)
                        {
                            throw new KeyNotFoundException("The specified data store key was not found.");
                        }
                        else
                        {
                            Put(key, defaultValue);
                            return GetItem(key);
                        }
                    }
                    else
                    {
                        return globalItem.ToDataStoreItem();
                    }
                }

                return item.ToDataStoreItem();
            }
        }

        public List<IDataStoreItem> GetAll()
        {
            using (var db = ObservablesContext.CreateDefault())
            {
                var localItems = db.DataStoreItems.Where(x => x.CollectionName == Name && !x.IsDeleted).ToList();
                var globalItems = db.GlobalDataStoreItems.Where(x => x.CollectionName == Name).ToList();

                foreach (var globalItem in globalItems.ToList())
                {
                    var localItem = localItems.FirstOrDefault(x => x.CollectionName == globalItem.CollectionName && x.Key == globalItem.Key);

                    if (localItem != null)
                    {
                        globalItems.Remove(globalItem);
                    }
                }

                return localItems.Select(x => x.ToDataStoreItem()).Concat(localItems.Select(x => x.ToDataStoreItem())).ToList();
            }
        }

        public List<IDataStoreItem> GetUnsynchronized()
        {
            using (var db = ObservablesContext.CreateDefault())
            {
                return db.DataStoreItems.Where(x => x.CollectionName == Name && !x.IsSynchronized).ToList().Select(x => x.ToDataStoreItem()).ToList();
            }
        }

        public void Delete(string key)
        {
            using (var db = ObservablesContext.CreateDefault())
            {
                db.Database.ExecuteSqlCommand($"DELETE FROM DATA_STORE_ITEMS WHERE COLLECTION_NAME = '{Name}' AND [KEY] = '{key}'");
            }
        }

        public void DeleteAll()
        {
            using (var db = ObservablesContext.CreateDefault())
            {
                db.Database.ExecuteSqlCommand($"DELETE FROM DATA_STORE_ITEMS WHERE COLLECTION_NAME = '{Name}'");
            }
        }

        public int Count()
        {
            using (var db = ObservablesContext.CreateDefault())
            {
                return db.DataStoreItems.Count(x => x.CollectionName == Name);
            }
        }
    }
}