1 /*
2  *  "GKCommunicator", the chat and bulletin board of the genealogical network.
3  *  Copyright (C) 2018 by Sergey V. Zhdanovskih.
4  *
5  *  This file is part of "GEDKeeper".
6  *
7  *  This program is free software: you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation, either version 3 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 using System;
22 using System.IO;
23 using GKNet.Logging;
24 
25 namespace GKNet.Database
26 {
27     public class DatabaseException : Exception
28     {
DatabaseException(string message)29         public DatabaseException(string message) : base(message)
30         {
31         }
32     }
33 
34     /// <summary>
35     ///
36     /// </summary>
37     public abstract class IDatabase
38     {
39         protected readonly ILogger fLogger;
40 
41         public abstract bool IsConnected { get; }
42 
43         public bool IsExists
44         {
45             get {
46                 string baseName = GetBaseName();
47                 return File.Exists(baseName);
48             }
49         }
50 
IDatabase()51         protected IDatabase()
52         {
53             fLogger = LogManager.GetLogger(ProtocolHelper.LOG_FILE, ProtocolHelper.LOG_LEVEL, "IDatabase");
54         }
55 
GetBaseName()56         protected abstract string GetBaseName();
57 
Connect()58         public abstract void Connect();
Disconnect()59         public abstract void Disconnect();
60 
CreateDatabase()61         public abstract void CreateDatabase();
62 
DeleteDatabase()63         public void DeleteDatabase()
64         {
65             if (IsConnected) Disconnect();
66 
67             string baseName = GetBaseName();
68             try {
69                 if (File.Exists(baseName)) {
70                     File.Delete(baseName);
71                 }
72             } catch (Exception ex) {
73                 fLogger.WriteError("IDatabase.DeleteDatabase()", ex);
74             }
75         }
76 
GetParameterValue(string paramName)77         public abstract string GetParameterValue(string paramName);
SetParameterValue(string paramName, string paramValue)78         public abstract void SetParameterValue(string paramName, string paramValue);
79 
GetParameterBool(string paramName)80         public bool GetParameterBool(string paramName)
81         {
82             string val = GetParameterValue(paramName);
83             if (string.IsNullOrEmpty(val)) {
84                 return false;
85             } else {
86                 return bool.Parse(val);
87             }
88         }
89 
SetParameterBool(string paramName, bool paramValue)90         public void SetParameterBool(string paramName, bool paramValue)
91         {
92             SetParameterValue(paramName, paramValue.ToString());
93         }
94 
LoadProfile(UserProfile profile)95         public void LoadProfile(UserProfile profile)
96         {
97             bool initialized = GetParameterBool("profile_initialized");
98             if (initialized) {
99                 profile.UserName = GetParameterValue("user_name");
100                 profile.Country = GetParameterValue("user_country");
101                 profile.TimeZone = GetParameterValue("user_timezone");
102                 profile.Languages = GetParameterValue("user_languages");
103             } else {
104                 profile.ResetSystem();
105                 SaveProfile(profile);
106             }
107         }
108 
SaveProfile(UserProfile profile)109         public void SaveProfile(UserProfile profile)
110         {
111             SetParameterBool("profile_initialized", true);
112             SetParameterValue("user_name", profile.UserName);
113             SetParameterValue("user_country", profile.Country);
114             SetParameterValue("user_timezone", profile.TimeZone);
115             SetParameterValue("user_languages", profile.Languages);
116         }
117 
CreateDefault()118         public static IDatabase CreateDefault()
119         {
120             return new LtDatabase();
121         }
122     }
123 }
124