1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Text;
|
---|
4 | using System.Xml.Serialization;
|
---|
5 | using System.IO;
|
---|
6 |
|
---|
7 | namespace IndianHealthService.BMXNet.WinForm.Configuration
|
---|
8 | {
|
---|
9 | /// <summary>
|
---|
10 | /// Provides settings interface and implementation for settings from command-line,
|
---|
11 | /// stored in PersistentStore, or hardcoded in application.
|
---|
12 | /// </summary>
|
---|
13 | public class Settings
|
---|
14 | {
|
---|
15 | [XmlIgnore()]
|
---|
16 | private bool _willPersistFutureSets = false;
|
---|
17 |
|
---|
18 | /// <summary>
|
---|
19 | /// After set to true, every setting update will be flushed the PersistentStore if
|
---|
20 | /// the Settings is configured with one
|
---|
21 | /// </summary>
|
---|
22 | public bool WillPersistFutureSets
|
---|
23 | {
|
---|
24 | get { return _willPersistFutureSets; }
|
---|
25 | set { _willPersistFutureSets = value; }
|
---|
26 | }
|
---|
27 |
|
---|
28 |
|
---|
29 | /// <summary>
|
---|
30 | /// Answer true if the current Settings IsPersistent. It is possible that the Backup
|
---|
31 | /// Settings IsPersistennt
|
---|
32 | /// </summary>
|
---|
33 | [XmlIgnore()]
|
---|
34 | public bool IsPersistent
|
---|
35 | {
|
---|
36 | get { return this.Store != null; }
|
---|
37 | }
|
---|
38 |
|
---|
39 |
|
---|
40 | private List<Setting> _settingList = new List<Setting>();
|
---|
41 |
|
---|
42 | internal List<Setting> SettingList
|
---|
43 | {
|
---|
44 | get { return _settingList; }
|
---|
45 | set { _settingList = value; }
|
---|
46 | }
|
---|
47 |
|
---|
48 | private Dictionary<String, Setting> _settingLookup = null;
|
---|
49 |
|
---|
50 | [XmlIgnore()]
|
---|
51 | internal Dictionary<String, Setting> SettingLookup
|
---|
52 | {
|
---|
53 | get {
|
---|
54 | if (_settingLookup == null)
|
---|
55 | {
|
---|
56 | _settingLookup = this.BuildLookup(this.SettingList);
|
---|
57 | }
|
---|
58 | return _settingLookup; }
|
---|
59 | set { _settingLookup = value; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /// <summary>
|
---|
63 | /// Update the Setting value aName to aBool
|
---|
64 | /// </summary>
|
---|
65 | /// <param name="aName">Name of the Setting</param>
|
---|
66 | /// <param name="aBool">Boolean value to set</param>
|
---|
67 | /// <returns>True if the setting was changed, False if the setting was the same value</returns>
|
---|
68 | public bool Set(String aName, bool aBool)
|
---|
69 | {
|
---|
70 | return this.Set(aName, aBool ? "True" : "False");
|
---|
71 | }
|
---|
72 |
|
---|
73 | /// <summary>
|
---|
74 | /// Update the Setting value aName to anInt
|
---|
75 | /// </summary>
|
---|
76 | /// <param name="aName">Name of the Setting</param>
|
---|
77 | /// <param name="aBool">Integer value to set</param>
|
---|
78 | /// <returns>True if the setting was changed, False if the setting was the same value</returns>
|
---|
79 | public bool Set(String aName, int anInt)
|
---|
80 | {
|
---|
81 | return this.Set(aName, anInt.ToString());
|
---|
82 | }
|
---|
83 |
|
---|
84 | /// <summary>
|
---|
85 | /// Update the Setting value aName to a string
|
---|
86 | /// </summary>
|
---|
87 | /// <param name="aName">Name of the Setting</param>
|
---|
88 | /// <param name="aBool">String value to set</param>
|
---|
89 | /// <returns>True if the setting was changed, False if the setting was the same value</returns>
|
---|
90 | public bool Set(String aName, String aValue)
|
---|
91 | {
|
---|
92 | Setting current = this.GetLocalSetting(aName);
|
---|
93 |
|
---|
94 | if (current == null)
|
---|
95 | {
|
---|
96 | if (this.WillPersistFutureSets)
|
---|
97 | {
|
---|
98 | current = new Setting() { Name = aName, Value = aValue };
|
---|
99 | this.AddSetting(current);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | else
|
---|
103 | {
|
---|
104 | if (current.Value == aValue)
|
---|
105 | return false;
|
---|
106 | current.Value = aValue;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | if (this.IsPersistent)
|
---|
111 | {
|
---|
112 | this.Save();
|
---|
113 | }
|
---|
114 | else if (this.BackupSettings != null)
|
---|
115 | {
|
---|
116 | this.BackupSettings.Set(aName, aValue);
|
---|
117 | }
|
---|
118 | return true;
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | public Settings BackupSettings { get; set; }
|
---|
123 |
|
---|
124 | internal void Clear()
|
---|
125 | {
|
---|
126 | this.SettingLookup.Clear();
|
---|
127 | }
|
---|
128 |
|
---|
129 | private string[] _storageKeys = null;
|
---|
130 |
|
---|
131 | internal string[] StorageKeys
|
---|
132 | {
|
---|
133 | get { return _storageKeys; }
|
---|
134 | set { _storageKeys = value; }
|
---|
135 | }
|
---|
136 |
|
---|
137 | private PersistentStore _store = null;
|
---|
138 |
|
---|
139 | internal PersistentStore Store
|
---|
140 | {
|
---|
141 | get { return _store; }
|
---|
142 | set { _store = value; }
|
---|
143 | }
|
---|
144 |
|
---|
145 |
|
---|
146 | private XmlSerializer _serializer = null;
|
---|
147 |
|
---|
148 | internal XmlSerializer Serializer
|
---|
149 | {
|
---|
150 | get
|
---|
151 | {
|
---|
152 | if (this._serializer == null)
|
---|
153 | {
|
---|
154 | this._serializer = new XmlSerializer(typeof(Settings));
|
---|
155 | }
|
---|
156 | return _serializer;
|
---|
157 | }
|
---|
158 | }
|
---|
159 |
|
---|
160 | /// <summary>
|
---|
161 | /// Get the string Setting named aString. Answer the defaultValue if the Setting named
|
---|
162 | /// aString does not exist in the receiver or it's Backup chain.
|
---|
163 | /// </summary>
|
---|
164 | /// <param name="aString">Name of the setting</param>
|
---|
165 | /// <param name="defaultValue">Default value</param>
|
---|
166 | /// <returns></returns>
|
---|
167 | public String Get(String aString, String defaultValue)
|
---|
168 | {
|
---|
169 | Setting hit = this.GetSetting(aString);
|
---|
170 |
|
---|
171 | return (hit == null) ? defaultValue : hit.Value;
|
---|
172 | }
|
---|
173 |
|
---|
174 | /// <summary>
|
---|
175 | /// Get the boolean Setting named aString. Answer the defaultValue if the Setting named
|
---|
176 | /// aString does not exist in the receiver or it's Backup chain.
|
---|
177 | /// </summary>
|
---|
178 | /// <param name="aString">Name of the setting</param>
|
---|
179 | /// <param name="defaultValue">Default value</param>
|
---|
180 | /// <returns></returns>
|
---|
181 | public bool Get(String aString, bool defaultValue)
|
---|
182 | {
|
---|
183 | Setting hit = this.GetSetting(aString);
|
---|
184 |
|
---|
185 | return (hit == null) ? defaultValue : "True".Equals(hit.Value,StringComparison.InvariantCultureIgnoreCase);
|
---|
186 | }
|
---|
187 |
|
---|
188 | /// <summary>
|
---|
189 | /// Get the Integer Setting named aString. Answer the defaultValue if the Setting named
|
---|
190 | /// aString does not exist in the receiver or it's Backup chain.
|
---|
191 | /// </summary>
|
---|
192 | /// <param name="aString">Name of the setting</param>
|
---|
193 | /// <param name="defaultValue">Default value</param>
|
---|
194 | /// <returns></returns>
|
---|
195 | public int Get(String aString, int defaultValue)
|
---|
196 | {
|
---|
197 | Setting hit = this.GetSetting(aString);
|
---|
198 |
|
---|
199 | int value = defaultValue;
|
---|
200 |
|
---|
201 | return (hit == null) ? defaultValue : (int.TryParse(hit.Value, out value) ? value : defaultValue);
|
---|
202 | }
|
---|
203 |
|
---|
204 |
|
---|
205 | internal Setting GetLocalSetting(String aString)
|
---|
206 | {
|
---|
207 | Setting result = null;
|
---|
208 |
|
---|
209 | if (this.SettingLookup.TryGetValue(aString.ToLower(), out result))
|
---|
210 | {
|
---|
211 | return result;
|
---|
212 | }
|
---|
213 | else
|
---|
214 | {
|
---|
215 | return null;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | internal Setting GetSetting(String aString)
|
---|
221 | {
|
---|
222 | Setting result = this.GetLocalSetting(aString);
|
---|
223 |
|
---|
224 | if (result !=null)
|
---|
225 | {
|
---|
226 | return result;
|
---|
227 | }
|
---|
228 | else if (this.BackupSettings != null)
|
---|
229 | {
|
---|
230 | return this.BackupSettings.GetSetting(aString);
|
---|
231 | }
|
---|
232 | else
|
---|
233 | {
|
---|
234 | return null;
|
---|
235 | }
|
---|
236 | }
|
---|
237 |
|
---|
238 | /// <summary>
|
---|
239 | /// Merge the settings from someSettings into the receiver
|
---|
240 | /// </summary>
|
---|
241 | /// <param name="someSettings">Settings to copy</param>
|
---|
242 | public void MergeSettings(Settings someSettings)
|
---|
243 | {
|
---|
244 | someSettings.SettingList.ForEach(each => this.AddSetting(each));
|
---|
245 | }
|
---|
246 |
|
---|
247 | internal Dictionary<String, Setting> BuildLookup(List<Setting> someSettings)
|
---|
248 | {
|
---|
249 | Dictionary<String, Setting> answer = new Dictionary<string, Setting>();
|
---|
250 | foreach (Setting each in someSettings)
|
---|
251 | {
|
---|
252 | answer[each.Name.ToLower()] = each;
|
---|
253 | }
|
---|
254 | return answer;
|
---|
255 | }
|
---|
256 |
|
---|
257 | internal void AddSetting(Setting aSetting)
|
---|
258 | {
|
---|
259 | Setting hit = this.GetSetting(aSetting.Name.ToLower());
|
---|
260 | if (hit != null)
|
---|
261 | {
|
---|
262 | this.SettingList.Remove(hit);
|
---|
263 | }
|
---|
264 |
|
---|
265 | this.SettingList.Add(aSetting);
|
---|
266 | this._settingLookup = null;
|
---|
267 | }
|
---|
268 |
|
---|
269 |
|
---|
270 | /// <summary>
|
---|
271 | /// Load the receiver's settings from the PersistentStore
|
---|
272 | /// </summary>
|
---|
273 | public void Load()
|
---|
274 | {
|
---|
275 | this.SettingList = this.Store.Read<List<Setting>>(this.StorageKeys);
|
---|
276 | if (this.SettingList == null)
|
---|
277 | this.SettingList = new List<Setting>();
|
---|
278 |
|
---|
279 | }
|
---|
280 |
|
---|
281 | /// <summary>
|
---|
282 | /// Save the receiver's settings to the PersistentStore
|
---|
283 | /// </summary>
|
---|
284 | public void Save()
|
---|
285 | {
|
---|
286 | this.Store.Write<List<Setting>>(this.SettingList, this.StorageKeys);
|
---|
287 | }
|
---|
288 |
|
---|
289 | }
|
---|
290 | }
|
---|