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 | /// Container for all persistent and available connection spec
|
---|
11 | /// </summary>
|
---|
12 | public class RpmsConnectionSettings
|
---|
13 | {
|
---|
14 | private Settings _userSettings = new Settings();
|
---|
15 |
|
---|
16 | public Settings UserSettings
|
---|
17 | {
|
---|
18 | get { return _userSettings; }
|
---|
19 | set { _userSettings = value; }
|
---|
20 | }
|
---|
21 |
|
---|
22 | private RpmsConnectionSpec _commandLineConnectionSpec = null;
|
---|
23 |
|
---|
24 | public RpmsConnectionSpec CommandLineConnectionSpec
|
---|
25 | {
|
---|
26 | get { return _commandLineConnectionSpec; }
|
---|
27 | set { _commandLineConnectionSpec = value; }
|
---|
28 | }
|
---|
29 |
|
---|
30 | private List<RpmsConnectionSpec> _managedConnectionSpecs = new List<RpmsConnectionSpec>();
|
---|
31 |
|
---|
32 | /// <summary>
|
---|
33 | /// Answer all connection specs in user defined order
|
---|
34 | /// </summary>
|
---|
35 | public List<RpmsConnectionSpec> ManagedConnectionSpecs
|
---|
36 | {
|
---|
37 | get { return _managedConnectionSpecs; }
|
---|
38 | set { _managedConnectionSpecs = value; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | /// <summary>
|
---|
42 | /// Answer all available connection specs, including command-line spec
|
---|
43 | /// </summary>
|
---|
44 | public List<RpmsConnectionSpec> ConnectionSpecs
|
---|
45 | {
|
---|
46 | get {
|
---|
47 | if (this.CommandLineConnectionSpec==null) {
|
---|
48 | return this.ManagedConnectionSpecs; }
|
---|
49 | else {
|
---|
50 | List<RpmsConnectionSpec> allSpecs=new List<RpmsConnectionSpec>(this.ManagedConnectionSpecs);
|
---|
51 | allSpecs.Insert(0,this.CommandLineConnectionSpec);
|
---|
52 | return allSpecs;
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | private RpmsConnectionSpec _defaultConnectionSpec=null;
|
---|
58 | /// <summary>
|
---|
59 | /// Answer the default connection spec. If there is not
|
---|
60 | /// user defined default, answer the first connection spec
|
---|
61 | /// </summary>
|
---|
62 | public RpmsConnectionSpec DefaultConnectionSpec
|
---|
63 | {
|
---|
64 | get
|
---|
65 | {
|
---|
66 | if (_defaultConnectionSpec== null)
|
---|
67 | {
|
---|
68 | if (this.CommandLineConnectionSpec == null)
|
---|
69 | {
|
---|
70 | foreach (RpmsConnectionSpec each in this.ManagedConnectionSpecs)
|
---|
71 | {
|
---|
72 | if (each.IsDefault)
|
---|
73 | {
|
---|
74 | return each;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | }
|
---|
78 | return this.ConnectionSpecs.Count == 0 ? null : this.ConnectionSpecs[0];
|
---|
79 |
|
---|
80 | }
|
---|
81 | else
|
---|
82 | {
|
---|
83 | return _defaultConnectionSpec;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | set { this._defaultConnectionSpec = value; }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /// <summary>
|
---|
90 | /// Add aSpec to the receiver. Not persisted
|
---|
91 | /// </summary>
|
---|
92 | /// <param name="aSpec"></param>
|
---|
93 | public void AddConnectionSpec(RpmsConnectionSpec aSpec)
|
---|
94 | {
|
---|
95 | this.ManagedConnectionSpecs.Add(aSpec);
|
---|
96 | if (aSpec.IsDefault)
|
---|
97 | {
|
---|
98 | this.SetConnectionSpecAsDefault(aSpec);
|
---|
99 | }
|
---|
100 |
|
---|
101 | }
|
---|
102 |
|
---|
103 | /// <summary>
|
---|
104 | /// Remove aSpec from the receiver. Not persisted.
|
---|
105 | /// </summary>
|
---|
106 | /// <param name="aSpec"></param>
|
---|
107 | public void RemoveConnectionSpec(RpmsConnectionSpec aSpec)
|
---|
108 | {
|
---|
109 | this.ManagedConnectionSpecs.Remove(aSpec);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /// <summary>
|
---|
113 | ///
|
---|
114 | /// </summary>
|
---|
115 | /// <param name="aSpec"></param>
|
---|
116 | /// <returns>True if there was any movement</returns>
|
---|
117 | public bool MoveConnectionSpecUp(RpmsConnectionSpec aSpec)
|
---|
118 | {
|
---|
119 | int currentIndex = this.ManagedConnectionSpecs.IndexOf(aSpec);
|
---|
120 |
|
---|
121 | if (currentIndex == 0)
|
---|
122 | {
|
---|
123 | return false;
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | this.RemoveConnectionSpec(aSpec);
|
---|
128 | this.ManagedConnectionSpecs.Insert(currentIndex - 1, aSpec);
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /// <summary>
|
---|
134 | ///
|
---|
135 | /// </summary>
|
---|
136 | /// <param name="aSpec"></param>
|
---|
137 | /// <returns>True if there was any movement</returns>
|
---|
138 | public bool MoveConnectionSpecDown(RpmsConnectionSpec aSpec)
|
---|
139 | {
|
---|
140 | int currentIndex = this.ManagedConnectionSpecs.IndexOf(aSpec);
|
---|
141 |
|
---|
142 | if (currentIndex == this.ManagedConnectionSpecs.Count - 1)
|
---|
143 | {
|
---|
144 | return false;
|
---|
145 | } else {
|
---|
146 | this.RemoveConnectionSpec(aSpec);
|
---|
147 | this.ManagedConnectionSpecs.Insert(currentIndex + 1, aSpec);
|
---|
148 | return true;
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | public void SetConnectionSpecAsDefault(RpmsConnectionSpec aSpec)
|
---|
153 | {
|
---|
154 | foreach (RpmsConnectionSpec each in this.ManagedConnectionSpecs)
|
---|
155 | {
|
---|
156 | each.IsDefault = false;
|
---|
157 | }
|
---|
158 | aSpec.IsDefault = true;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 |
|
---|
165 |
|
---|
166 |
|
---|
167 | /// <summary>
|
---|
168 | /// Refresh the receiver from the current state of the persistent store
|
---|
169 | /// </summary>
|
---|
170 | public void Revert()
|
---|
171 | {
|
---|
172 | this.Load();
|
---|
173 | }
|
---|
174 |
|
---|
175 | public void Load()
|
---|
176 | {
|
---|
177 | this.ManagedConnectionSpecs = this.Store.Read<List<RpmsConnectionSpec>>(this.StorageKeys);
|
---|
178 | if (this.ManagedConnectionSpecs == null)
|
---|
179 | this.ManagedConnectionSpecs = new List<RpmsConnectionSpec>();
|
---|
180 |
|
---|
181 | }
|
---|
182 |
|
---|
183 | public void Save()
|
---|
184 | {
|
---|
185 | this.Store.Write<List<RpmsConnectionSpec>>(this.ManagedConnectionSpecs, this.StorageKeys);
|
---|
186 | }
|
---|
187 |
|
---|
188 | private string[] _storageKeys = null;
|
---|
189 |
|
---|
190 | public string[] StorageKeys
|
---|
191 | {
|
---|
192 | get { return _storageKeys; }
|
---|
193 | set { _storageKeys = value; }
|
---|
194 | }
|
---|
195 |
|
---|
196 | private PersistentStore _store = null;
|
---|
197 |
|
---|
198 | public PersistentStore Store
|
---|
199 | {
|
---|
200 | get { return _store; }
|
---|
201 | set { _store = value; }
|
---|
202 | }
|
---|
203 |
|
---|
204 | }
|
---|
205 | }
|
---|