OpenKeyWord  Version: 426, Datum:
OKW_Memorize.cs
1 #region Header
2 /*
3  ==============================================================================
4  Author: Zoltan Hrabovszki <zh@openkeyword.de>
5 
6  Copyright © 2012, 2013, 2014, 2015 IT-Beratung Hrabovszki
7  www.OpenKeyWord.de
8  ==============================================================================
9 
10  This file is part of OpenKeyWord.
11 
12  OpenKeyWord is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  OpenKeyWord is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with OpenKeyWord. If not, see <http://www.gnu.org/licenses/>.
24 
25  Diese Datei ist Teil von OpenKeyWord.
26 
27  OpenKeyWord ist Freie Software: Sie können es unter den Bedingungen
28  der GNU General Public License, wie von der Free Software Foundation,
29  Version 3 der Lizenz oder (nach Ihrer Wahl) jeder späteren
30  veröffentlichten Version, weiterverbreiten und/oder modifizieren.
31 
32  OpenKeyWord wird in der Hoffnung, dass es nützlich sein wird, aber
33  OHNE JEDE GEWÄHRLEISTUNG, bereitgestellt; sogar ohne die implizite
34  Gewährleistung der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
35  Siehe die GNU General Public License für weitere Details.
36 
37  Sie sollten eine Kopie der GNU General Public License zusammen mit
38  OpenKeyWord erhalten haben. Wenn nicht, siehe <http://www.gnu.org/licenses/>.
39 */
40 #endregion Header
41 
42 namespace OKW
43 {
44  using System;
45  using System.IO;
46  using System.Xml.Serialization;
47 
48  using OKW.Log;
49 
72  public class OKW_Memorize
73  {
74  #region Fields
75 
84  public string OKW_Memorize_xml = string.Empty;
85 
98 
108  private static OKW_Memorize _Instance;
109 
119 
120  private LogMessenger LM = null;
121 
131 
132  private Logger Log = Logger.Instance;
133 
134  #endregion Fields
135 
136  #region Constructors
137 
149  private OKW_Memorize()
150  {
151  this.LM = new LogMessenger(this.GetType().Name);
152  }
153 
154  #endregion Constructors
155 
156  #region Properties
157 
173  public static OKW_Memorize Instance
174  {
175  get
176  {
177  if (_Instance == null)
178  {
179  _Instance = new OKW_Memorize();
180  _Instance.Init();
181  }
182 
183  return _Instance;
184  }
185  }
186 
187  #endregion Properties
188 
189  #region Methods
190 
202  public static void Reset()
203  {
204  _Instance = null;
205  }
206 
220  public bool Exists(string fpsKey)
221  {
222  bool lvbReturn = false;
223 
224  this.Log.LogFunctionStartDebug(_Instance.GetType().FullName + "Exists", "string fpsKey", fpsKey);
225 
226  if (this._Value.ContainsKey(fpsKey))
227  {
228  lvbReturn = true;
229  }
230 
231  this.Log.LogFunctionEndDebug(lvbReturn);
232 
233  return lvbReturn;
234  }
235 
250  public string Get(string fpsKey)
251  {
252  string lvsReturn = string.Empty;
253  bool bOK = false;
254 
255  this.Log.LogFunctionStartDebug(_Instance.GetType().FullName + "Get", "String fpsKey", fpsKey);
256 
257  try
258  {
259  if (this._Value.ContainsKey(fpsKey))
260  {
261  lvsReturn = this._Value[fpsKey];
262  }
263  else
264  {
265  string ErrorText = this.LM.GetMessage("Get", "OKWMemorizeKeyNotExistsException", fpsKey);
266  throw new OKWMemorizeKeyNotExistsException(ErrorText);
267  }
268 
269  bOK = true;
270  }
271  finally
272  {
273  if (bOK)
274  {
275  this.Log.LogFunctionEndDebug();
276  }
277  else
278  {
279  this.Log.LogFunctionEndDebug(lvsReturn);
280  }
281  }
282 
283  return lvsReturn;
284  }
285 
317  public void Init()
318  {
319  this.Log.LogFunctionStartDebug(this.GetType().FullName + ".Init");
320 
321  // Klassen Variablen erst löschen...
322  _Instance.OKW_Memorize_xml = string.Empty;
323  _Instance._Value.Clear();
324 
325  // ... und dann alles Initialisieren!
326  // 1. Setze Pfad und
327  this.OKW_Memorize_xml = OKW_Ini.Instance.OKW_Enviroment.File_OKW_Memorize_xml;
328 
329  if (!string.IsNullOrEmpty(this.OKW_Memorize_xml))
330  {
331  this.Log.LogPrintDebug("OKW Memorize Datei = >>" + this.OKW_Memorize_xml + "<<");
332  if (System.IO.File.Exists(this.OKW_Memorize_xml))
333  {
334  // Lesen der Daten...
335  this.Read();
336  }
337  else
338  {
339  // Deafault Werte Lesen.
340  this.Log.LogWarning("Datei: >>" + this.OKW_Memorize_xml + "<< nicht gefunden.");
341  }
342  }
343  else
344  {
345  this.Log.LogWarning("Enviroment variable 'OKWIni' not set!");
346  }
347 
348  this.Log.LogFunctionEndDebug();
349  return;
350  }
351 
362  public void Read()
363  {
364  this.Log.LogFunctionStartDebug(_Instance.GetType().FullName + "Read()");
365 
366  try
367  {
368  XmlSerializer serializer = new XmlSerializer(typeof(OKW_Memorize));
369  StreamReader fs = new StreamReader(this.OKW_Memorize_xml);
370  _Instance = (OKW_Memorize)serializer.Deserialize(fs);
371  fs.Close();
372  }
373  finally
374  {
375  this.Log.LogFunctionEndDebug();
376  }
377 
378  return;
379  }
380 
394  public void Save()
395  {
396  this.Log.LogFunctionStartDebug(_Instance.GetType().FullName + "Save()");
397 
398  try
399  {
400  XmlSerializer serializer = new XmlSerializer(typeof(OKW_Memorize));
401  StreamWriter fs = new StreamWriter(this.OKW_Memorize_xml, false);
402  serializer.Serialize(fs, _Instance);
403  fs.Close();
404  }
405  finally
406  {
407  this.Log.LogFunctionEndDebug();
408  }
409 
410  return;
411  }
412 
420  public void Set(string fpsKey, string fpsValue)
421  {
422  this.Log.LogFunctionStartDebug(_Instance.GetType().FullName + "Set", "String fpsKey", fpsKey, "String fpsValue", fpsValue);
423 
424  try
425  {
426  if (this._Value.ContainsKey(fpsKey))
427  {
428  string lvsOverwriteKey = this.LM.GetMessage("Set", "OverwriteKeyWarning", fpsKey);
429  string lvsOldValue = this.LM.GetMessage("Set", "OldValue", this._Value[fpsKey]);
430  string lvsNewValue = this.LM.GetMessage("Set", "NewValue", fpsValue);
431 
432  this.Log.LogWarning(lvsOverwriteKey);
433  this.Log.LogPrint(lvsOldValue);
434  this.Log.LogPrint(lvsNewValue);
435  _Instance._Value[fpsKey] = fpsValue;
436  }
437  else
438  {
439  _Instance._Value.Add(fpsKey, fpsValue);
440  this.Log.LogPrint("Wert: " + fpsValue + " wird gespeichert");
441  }
442 
443  // persistentes Speichern aller Daten...
444  _Instance.Save();
445  }
446  finally
447  {
448  this.Log.LogFunctionEndDebug();
449  }
450 
451  return;
452  }
453 
454  #endregion Methods
455  }
456 }
string GetMessage(string MethodName, string TextKey)
Holt die Log-Meldung für MethodeNmae/Textkey ohne weitere Parameter.
void LogPrint(string fps_Message)
LogPrint Function: Prints the values of expressions to the results file.
Definition: Logger.cs:302
OKW_Memorize()
Privater Konstruktor dieser Klasse.
bool Exists(string fpsKey)
Prüft ob es eine Eintragzum Schlüssel fpsKey vorhanden ist.
string OKW_Memorize_xml
Variable hält Pfad und Dateinamen der OKW_Memorize.xml.
Definition: OKW_Memorize.cs:84
static OKW_Ini Instance
Singelton-Pattern: Instanz gibt die aktuelle, gültige und einzige Innstanz der Klasse zurück...
Definition: OKW_Ini.cs:306
OKW_Enviroment OKW_Enviroment
Dieses Feld hält den Abschnitt OKW_Enviroment der OKW_Ini.xml vor.
Definition: OKW_Ini.cs:219
Logger Log
Referenz auf die einzige Instanz des Klasse OKW.Logger.
static OKW_Memorize Instance
Diese Methode gibt die einzige Instanz dieser Klasse zurück.
string Get(string fpsKey)
Holt den Aktuellen Wert eines Schlüssels.
void Init()
Initialsiert die Klasse OKW.OKW_Memorize.
LogMessenger LM
Eine lokale Instanz des OKW.Log.LogMssenger.
static OKW_Memorize _Instance
Singelton spezifisch: Dieses Feld speichert die einzige Instanz dieser Klasse.
void Save()
Schreibt die Felder (fields) der Klasse OKW_Memorize in eine Datei.
OKW_Memorize ist die Speicher-Klasse hinter den Merke*-Schlüsselwörter.
Definition: OKW_Memorize.cs:72
Die Ausnahme wird ausgelöst, wenn kein Eintrag zu einem Schlüssel vorhanden ist. Diese Ausnahme wird ...
LogMessenger liest Log-Meldungen sprachspezifisch für die im Konstruktor gegeben Klasse aus der Zugeh...
Definition: LogMessenger.cs:90
SerializableDictionary< string, string > _Value
Dictionary speichert die Schlüssel-Wert Paare.
Definition: OKW_Memorize.cs:97
void LogPrintDebug(string fpsMessage)
Loggt eine Nachricht.
Definition: Logger.cs:332
OKW.OKW_Ini ist die Klasse zur Konfigurationsdatei OKW_Ini.xml.
Definition: OKW_Ini.cs:188
void Read()
Liest die Werte der Klasse OKW_Memorize aus einer Datei, gegeben in OKW.OKW_Ini.Xml_Ini_xml, ein. Es wird eine XML Datei gelesen. Hierzu wird die Klasse OKW_Memorize mit System.Xml.XmlSerializer deserialisiert.
void LogWarning(string fps_Message)
LogWarning Function: Logs a warning to the results file.
Definition: Logger.cs:377
static void Reset()
Methode setzt diese Klasse zurück.
void Set(string fpsKey, string fpsValue)
Definition: Core.cs:40
string File_OKW_Memorize_xml
Property get/set von __File_OKW_Memorize_xml.