SECI  1
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events
ConfigurationIO.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4 using System.Text.RegularExpressions;
5 using System.Xml;
6 using System.Xml.Serialization;
7 using Seci.Definitions;
8 
9 namespace Seci.Helpers
10 {
14  internal static class ConfigurationIO
15  {
16  #region Load
17 
26  public static Configuration LoadConfig(String fileName)
27  {
28  try
29  {
30  //Deserialise the required configuration file
31  Configuration temp = DeserialiseConfig(fileName);
32 
33  //If any blocks were set not to save run control settings
34  //Then they need to be set to run control disabled.
35  for (int i = 0; i < temp.Blocks.Count; ++i)
36  {
37 
38  //Fix for back compatability
39  if (temp.Blocks[i].ValueFormatting != null && temp.Blocks[i].ValueFormatting.Contains("#"))
40  {
41  temp.Blocks[i].ValueFormatting = temp.Blocks[i].ValueFormatting.Replace('#', '0');
42  }
43 
44  if (!temp.Blocks[i].SaveSettings)
45  {
46  temp.Blocks[i].UnderRunControl = false;
47  temp.Blocks[i].LowerLimit = Double.NegativeInfinity;
48  temp.Blocks[i].UpperLimit = Double.PositiveInfinity;
49  temp.Blocks[i].RunControlValue = "";
50  }
51  }
52 
53  //Check to see if the configuration contains any components
54  //that need to be loaded
55  List<String> compNames = new List<String>();
56  compNames.AddRange(temp.Components.Components);
57 
58  //Now load and add any components
59  for (int i = 0; i < compNames.Count; ++i)
60  {
61  try
62  {
63  temp.Components[i] = (DeserialiseConfig(Status.ConfigDir + compNames[i]));
64 
65  //Set the owning name of the blocks to that of the component.
66  //Also set run control values based on whether SaveSettings was true
67  for (int j = 0; j < temp.Components[i].Blocks.Count; ++j)
68  {
69  temp.Components[i].Blocks[j].OwningComponent = temp.Components[i].ConfigName;
70  if (!temp.Components[i].Blocks[j].SaveSettings)
71  {
72  temp.Components[i].Blocks[j].UnderRunControl = false;
73  temp.Components[i].Blocks[j].LowerLimit = Double.NegativeInfinity;
74  temp.Components[i].Blocks[j].UpperLimit = Double.PositiveInfinity;
75  temp.Components[i].Blocks[j].RunControlValue = "";
76  }
77  }
78 
79  //then add to the block dictionary
80  temp.Blocks.AddDictionary(temp.Components[i].Blocks);
81  //temp.Components[i].Blocks = null;
82 
83  //Add any alerts
84  Seci.Managers.AlertsMgr.AddComponentBox(temp.Alerts, temp.Components[i].Alerts, compNames[i]);
85 
86  //Set the the owning name of the graphs in the component
87  for (int j = 0; j < temp.Components[i].GraphDefinitions.Count; ++j)
88  {
89  temp.Components[i].GraphDefinitions[j].OwningComponent = temp.Components[i].ConfigName;
90  }
91  //then add to the configuration
92  temp.GraphDefinitions.AddRange(temp.Components[i].GraphDefinitions);
93 
94  }
95  catch
96  {
97  throw new Exception("Could not load component: " + temp.Components[i].ConfigName);
98  }
99  }
100 
101  return temp;
102  }
103  catch (Exception e)
104  {
105  //throw error up to next level
106  throw new ArgumentException(e.Message);
107  }
108  }
109 
116  public static Configuration DeserialiseConfig(String fileName)
117  {
118  //deserialises a xml config file
119  FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
120  Configuration conf = null;
121 
122  try
123  {
124  XmlSerializer deserialize = new XmlSerializer(typeof(Configuration));
125  conf = (Configuration)deserialize.Deserialize(stream);
126 
127  }
128  catch (Exception e)
129  {
130  //throw error up to next level
131  throw new ArgumentException(e.Message);
132  }
133  finally
134  {
135  stream.Close();
136  }
137  return conf;
138  }
139 
140  #endregion
141 
142  #region Save
143 
151  public static bool OkayToSave(String configName, Boolean manager)
152  {
153  try
154  {
155  //Sort out fileName
156  String fileName = Status.ConfigDir + configName;
157 
158  //Does the config already exist?
159  FileInfo fi = new FileInfo(fileName);
160 
161  if (fi.Exists)
162  {
163  //First read the config
164  XmlDocument conf = createXmlDocFromConfig(fileName);
165 
166  //The check the nodes
167  XmlNodeList nodelist = conf.SelectNodes("/Configuration/SavedAsManager");
168 
169  if (nodelist.Count > 0)
170  {
171  //Was it saved previously by the manager account and is the current user = manager
172  if ((nodelist[0].InnerText == "true") && (manager == false))
173  {
174  //Don't save it
175  return false;
176  }
177  }
178  }
179 
180  //Okay to save
181  return true;
182  }
183  catch (Exception err)
184  {
185  //If this throws log it and return false
186  ErrorLogger.SeciError("OkayToSave", err);
187  return false;
188  }
189  }
190 
198  public static void SaveConfig(Configuration conf, Boolean isAutosave, String fileName)
199  {
200  FileInfo fi = new FileInfo(fileName);
201  if (fi.Exists)
202  {
203  //make sure it is not read only
204  fi.IsReadOnly = false;
205  if (!isAutosave)
206  {
207  createLocalBackup(conf.ConfigName, fi);
208  }
209  }
210 
211  //Create the stream
212  FileStream stream = new FileStream(fileName, FileMode.Create);
213 
214  try
215  {
216  //Serialise the config
217  XmlSerializer serialize = new XmlSerializer(typeof(Configuration));
218  serialize.Serialize(stream, conf);
219 
220  }
221  catch (Exception e)
222  {
223  //throw error up to next level
224  throw new ArgumentException(e.Message);
225  }
226  finally
227  {
228  stream.Close();
229  }
230 
231  //Clear the byte array of any associated files as it is just wasting memory once saved.
232  for (int i = 0; i < conf.AssociatedFiles.Count; ++i)
233  {
234  conf.AssociatedFiles[i].FileData = null;
235  }
236  }
237 
244  private static void createLocalBackup(String configName, FileInfo fi)
245  {
246  //Create a back-up version with name fileName.conf.X
247  //where X is the version number.
248  DirectoryInfo di = new DirectoryInfo(Status.ConfigDir);
249 
250  List<FileInfo> files = new List<FileInfo>();
251  files.AddRange(di.GetFiles());
252 
253  string versionMatch = configName.Replace("+", @"\+") + @"\.\d+$";
254  Regex r = new Regex(versionMatch, RegexOptions.IgnoreCase);
255 
256  int version = 0;
257 
258  foreach (FileInfo f in files)
259  {
260  if (r.Match(f.Name).Success)
261  {
262  int tempVer = Convert.ToInt32(f.Name.Substring(f.Name.LastIndexOf('.') + 1));
263  if (tempVer > version)
264  {
265  version = tempVer;
266  }
267  }
268  }
269 
270  fi.MoveTo(fi.FullName + "." + (++version).ToString());
271  }
272 
279  private static XmlDocument createXmlDocFromConfig(String fileName)
280  {
281  try
282  {
283  XmlDocument conf = new XmlDocument();
284 
285  conf.Load(fileName);
286 
287  return conf;
288  }
289  catch (Exception e)
290  {
291  //throw error up to next level
292  throw new ArgumentException(e.Message);
293  }
294  }
295 
296  #endregion
297  }
298 }
Boolean Contains(String blockName)
Wrapper for standard Dictionary &quot;Contains&quot; All keys are lowercase.
Class for holding the standard run-time settings for SECI. A large percentage of the information held...
Definition: Status.cs:12
This class contains all the information that defines the configuration at the SECI level and contains...
static String ConfigDir
Definition: Status.cs:89