source: BMXNET_RPMS_dotNET_UTILITIES-BMX/branch/IHS BMX Framework/IndianHealthService.BMXNet.WinForm/Configuration/LocalPersistentStore.cs@ 1146

Last change on this file since 1146 was 1146, checked in by Sam Habiel, 13 years ago

Initial Import of BMX4

File size: 8.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.IO;
5using System.IO.IsolatedStorage;
6using System.Xml.Serialization;
7using System.Security.Permissions;
8
9namespace IndianHealthService.BMXNet.WinForm.Configuration
10{
11
12 /// <summary>
13 /// LocalPersistentStore is the core BMX implementation for the most common
14 /// local setting store patterns:
15 ///
16 /// 1) In a special folder
17 /// 2) In any specified path
18 /// 3) In IsolatedStorage
19 ///
20 /// Use the object factory static class methods to create the different storage
21 /// mechanisms.
22 /// </summary>
23 public class LocalPersistentStore : PersistentStore
24 {
25 private bool _attemptAncestorKeysOnRead = false;
26
27 public bool AttemptAncestorKeysOnRead
28 {
29 get { return _attemptAncestorKeysOnRead; }
30 set { _attemptAncestorKeysOnRead = value; }
31 }
32
33 public static LocalPersistentStore CreateIn(Environment.SpecialFolder aSpecialFolder, String subDirectoryPath,bool tryParentKeysOnRead)
34 {
35 LocalPersistentStore answer = new LocalPersistentStore();
36 answer.SpecialFolder = aSpecialFolder;
37 answer.DirectoryPath = subDirectoryPath;
38 answer.AttemptAncestorKeysOnRead = tryParentKeysOnRead;
39 return answer;
40
41 }
42
43 public static LocalPersistentStore CreateIn(String aDirectoryPath, bool tryParentKeysOnRead)
44 {
45 LocalPersistentStore answer = new LocalPersistentStore();
46 answer.DirectoryPath = aDirectoryPath;
47 answer.AttemptAncestorKeysOnRead = tryParentKeysOnRead;
48 return answer;
49 }
50
51 protected static LocalPersistentStore CreateIn(IsolatedStorageScope aScope, bool tryParentKeysOnRead)
52 {
53 LocalPersistentStore answer = new LocalPersistentStore();
54 answer.IsolatedStorageScope = aScope;
55 answer.AttemptAncestorKeysOnRead = tryParentKeysOnRead;
56 return answer;
57 }
58
59 public static LocalPersistentStore CreateDefaultStorage(bool tryParentKeysOnRead)
60 {
61 return CreateIn(System.IO.IsolatedStorage.IsolatedStorageScope.User | System.IO.IsolatedStorage.IsolatedStorageScope.Assembly, tryParentKeysOnRead);
62 }
63
64 private Environment.SpecialFolder? _specialFolder = null;
65
66 public Environment.SpecialFolder? SpecialFolder
67 {
68 get { return _specialFolder; }
69 set { _specialFolder = value; }
70 }
71
72 private String _directoryPath = null;
73
74 public String DirectoryPath
75 {
76 get { return _directoryPath; }
77 set { _directoryPath = value; }
78 }
79
80
81 private IsolatedStorageScope? _isolatedStorageScope = null;
82
83 public IsolatedStorageScope? IsolatedStorageScope
84 {
85 get { return _isolatedStorageScope; }
86 set { _isolatedStorageScope = value; }
87 }
88
89
90 private T ReadFromFileSystem<T>(String aDirectory, params string[] keys)
91 {
92 String path = Path.Combine(aDirectory, this.TranslateToValidFileName(keys));
93
94 T result = default(T);
95
96 using (Stream stream = File.OpenRead(path))
97 {
98 XmlSerializer serializer = new XmlSerializer(typeof(T));
99 result=(T)serializer.Deserialize(stream);
100 }
101 return result;
102 }
103
104
105 private void WriteToFileSystem<T>(String aDirectory, T anObject, params string[] keys)
106 {
107 String path = Path.Combine(aDirectory, this.TranslateToValidFileName(keys));
108
109 if (!Directory.Exists(aDirectory))
110 {
111 Directory.CreateDirectory(aDirectory);
112 }
113
114 using (Stream stream = File.Create(path))
115 {
116 XmlSerializer serializer = new XmlSerializer(typeof(T));
117 serializer.Serialize(stream, anObject);
118 }
119 }
120
121 /// <summary>
122 /// Remove on non-letters, digits, to _ and add Bmx_ to the front.
123 /// </summary>
124 /// <param name="aKey"></param>
125 /// <returns></returns>
126 private string TranslateToValidFileName(params string[] keys)
127 {
128 StringBuilder builder = new StringBuilder();
129
130 builder.Append("bmx_");
131
132 foreach (String key in keys)
133 {
134 foreach (char each in key)
135 {
136 builder.Append((Char.IsLetterOrDigit(each)) ? each : '_');
137 }
138 builder.Append("_");
139 }
140
141 builder.Append("config.xml");
142
143 return builder.ToString();
144 }
145
146
147
148 /// <summary>
149 /// Isolated storage is scoped on user/application isolated storage
150 /// </summary>
151 /// <param name="aKey"></param>
152 /// <returns></returns>
153 private T ReadFromIsolatedStorage<T>(params string[] keys)
154 {
155 T result = default(T);
156
157 String validFileName = this.TranslateToValidFileName(keys) + ".is";
158
159 using (IsolatedStorageFile storage = IsolatedStorageFile.GetStore(this.IsolatedStorageScope.Value,null, null))
160 {
161 using (Stream stream = new IsolatedStorageFileStream(validFileName, FileMode.Open, FileAccess.Read, storage))
162 {
163 XmlSerializer serializer = new XmlSerializer(typeof(T));
164 result = (T)serializer.Deserialize(stream);
165 }
166 }
167 return result;
168 }
169
170
171
172 /// <summary>
173 /// Isolated storage is scoped on user/application isolated storage
174 /// </summary>
175 /// <param name="aKey"></param>
176 /// <param name="someContent"></param>
177 private void WriteToIsolatedStorage<T>(T anObject, string[] keys)
178 {
179 String validFileName = this.TranslateToValidFileName(keys) + ".is";
180
181 using (IsolatedStorageFile storage = IsolatedStorageFile.GetStore(this.IsolatedStorageScope.Value, null,null))
182 {
183 using (Stream stream = new IsolatedStorageFileStream(validFileName, FileMode.Create, FileAccess.Write, storage))
184 {
185 XmlSerializer serializer = new XmlSerializer(typeof(T));
186 serializer.Serialize(stream,anObject);
187 }
188 }
189 }
190
191
192 public T Read<T>(params string[] keys)
193 {
194 String[] keysToUse = keys;
195
196 try
197 {
198 if (this.IsolatedStorageScope.HasValue)
199 {
200 return this.ReadFromIsolatedStorage<T>(keysToUse);
201 }
202 else if (this.SpecialFolder.HasValue)
203 {
204 return this.ReadFromFileSystem<T>(Path.Combine(Environment.GetFolderPath(this.SpecialFolder.Value),this.DirectoryPath), keysToUse);
205 }
206 else if (this.DirectoryPath != null)
207 {
208 return this.ReadFromFileSystem<T>(this.DirectoryPath, keysToUse);
209 }
210 else
211 {
212 return default(T);
213 }
214 }
215 catch
216 {
217 if (this.AttemptAncestorKeysOnRead && keysToUse.Length>1) {
218 string[] newKeysToUse=new string[keysToUse.Length-1];
219 Array.Copy(keysToUse, newKeysToUse, keysToUse.Length - 1);
220 return this.Read<T>(newKeysToUse);
221 }
222
223
224 return default(T);
225 }
226 }
227
228
229 public void Write<T>(T anObject, params string[] keys)
230 {
231 if (this.IsolatedStorageScope.HasValue)
232 {
233 this.WriteToIsolatedStorage<T>(anObject, keys);
234 }
235 else if (this.SpecialFolder.HasValue)
236 {
237 this.WriteToFileSystem<T>(Path.Combine(Environment.GetFolderPath(this.SpecialFolder.Value),this.DirectoryPath), anObject, keys);
238 }
239 else if (this.DirectoryPath != null)
240 {
241 this.WriteToFileSystem<T>(this.DirectoryPath, anObject, keys);
242 }
243 else
244 {
245 throw new Exception("Local persistent store not configured.");
246 }
247 }
248
249 }
250}
Note: See TracBrowser for help on using the repository browser.