314 lines
10 KiB
C#
314 lines
10 KiB
C#
using System;
|
|
using System.Configuration;
|
|
using System.Text;
|
|
using System.Xml;
|
|
using System.IO;
|
|
|
|
namespace GZTW.Profile
|
|
{
|
|
/// <summary>
|
|
/// Contain the connection information for
|
|
/// AyaNova to connect to the database or DataPortal
|
|
/// to be used for the session.
|
|
///
|
|
/// Note that this can be set via code by a caller
|
|
/// and used in the AyaBizUtils.Login method or
|
|
/// it will automatically retrieve and store the connection settings
|
|
/// specified in the config.txt file
|
|
/// </summary>
|
|
[Serializable]
|
|
public class AyaNovaConnectionSettings
|
|
{
|
|
#region ConnectionEnums
|
|
public enum ConnectionType
|
|
{
|
|
UnknownConnectionType,
|
|
DataPortal,
|
|
DataBase
|
|
}
|
|
|
|
public enum DataBaseType
|
|
{
|
|
UnknownDBType,
|
|
FireBird,
|
|
MSSQL,
|
|
Oracle
|
|
}
|
|
#endregion
|
|
|
|
private DataBaseType _dbType= DataBaseType.UnknownDBType;
|
|
private ConnectionType _cnType= ConnectionType.UnknownConnectionType;
|
|
private string _DataBaseConnectionString="";
|
|
private string _DataPortalConnection="";
|
|
private bool? _IsSingleUser = null;
|
|
public bool IsRemote
|
|
{
|
|
get
|
|
{
|
|
return _cnType == ConnectionType.DataPortal;
|
|
}
|
|
}
|
|
public AyaNovaConnectionSettings ()
|
|
{ }
|
|
|
|
public override string ToString()
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.Append("\r\n\tAyaNova Connection:");
|
|
sb.Append("\r\n\t");
|
|
|
|
sb.Append(_cnType.ToString());
|
|
sb.Append(" - ");
|
|
|
|
if (_cnType == ConnectionType.DataBase)
|
|
{
|
|
sb.Append(_DataBaseConnectionString);
|
|
if (_DataBaseConnectionString != DataBaseConnectionStringUnTokenized)
|
|
{
|
|
sb.Append(" ("+DataBaseConnectionStringUnTokenized+")");
|
|
}
|
|
}
|
|
else
|
|
sb.Append(_DataPortalConnection);
|
|
|
|
|
|
sb.Append("\r\n");
|
|
return sb.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public DataBaseType DBType
|
|
{
|
|
get
|
|
{
|
|
return _dbType;
|
|
}
|
|
set
|
|
{
|
|
_dbType=value;
|
|
}
|
|
}
|
|
|
|
public ConnectionType CNType
|
|
{
|
|
get
|
|
{
|
|
return _cnType;
|
|
}
|
|
set
|
|
{
|
|
_cnType=value;
|
|
}
|
|
}
|
|
|
|
public bool UsingDataPortal
|
|
{
|
|
get
|
|
{
|
|
return _cnType == ConnectionType.DataPortal;
|
|
}
|
|
}
|
|
|
|
public string DataBaseConnectionString
|
|
{
|
|
get
|
|
{
|
|
return _DataBaseConnectionString;
|
|
}
|
|
set
|
|
{
|
|
_DataBaseConnectionString=value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// If a special folder token is contained in the connection string
|
|
/// this property returns the actual path
|
|
/// </summary>
|
|
public string DataBaseConnectionStringUnTokenized
|
|
{
|
|
get
|
|
{
|
|
string token = "%AllUsersProfile%";
|
|
int x=_DataBaseConnectionString.LastIndexOf(token,StringComparison.InvariantCultureIgnoreCase);
|
|
if (x!=-1)
|
|
{
|
|
string s= _DataBaseConnectionString.Substring(0, x) +
|
|
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
|
|
_DataBaseConnectionString.Substring(x + 17);
|
|
return s;
|
|
}
|
|
else
|
|
return _DataBaseConnectionString;
|
|
}
|
|
|
|
}
|
|
|
|
public string DataPortalConnection
|
|
{
|
|
get
|
|
{
|
|
return _DataPortalConnection;
|
|
}
|
|
set
|
|
{
|
|
_DataPortalConnection=value;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// If stand alone firebird returns true
|
|
/// </summary>
|
|
public bool SingleUserConnection
|
|
{
|
|
get
|
|
{
|
|
|
|
if (_IsSingleUser == null)//Nullable bool
|
|
{
|
|
//Only fb will contain the servertype parameter, if it doesn't then it can't be standalone
|
|
string s = _DataBaseConnectionString.Replace(" ", "").ToLowerInvariant();
|
|
if (s.Contains("servertype=1"))
|
|
_IsSingleUser= true;
|
|
else//this was missing duh! Case 659
|
|
_IsSingleUser= false;
|
|
}
|
|
return (bool)_IsSingleUser;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Populate the properties of this ConnectionSetting object
|
|
/// from a config.txt file in the same file system location as
|
|
/// this assembly.
|
|
/// </summary>
|
|
public void GetConnectionData()
|
|
{
|
|
|
|
XmlDocument d = new XmlDocument();
|
|
XmlNode n;
|
|
|
|
string configfile = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + "config.txt";
|
|
if (!File.Exists(configfile))
|
|
{
|
|
//Added for Case 657
|
|
//Could be shadow copied (asp.net etc), try the escapedcodebase value instead
|
|
configfile = Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath) + Path.DirectorySeparatorChar + "config.txt";
|
|
if (!File.Exists(configfile))
|
|
throw new ApplicationException("Error config.txt file is missing. \r\nIt should be here:\r\n" + configfile);
|
|
}
|
|
|
|
|
|
d.Load(configfile);
|
|
|
|
//for error message purposes:
|
|
configfile = "(" + configfile + ")";
|
|
|
|
n = d.SelectSingleNode("/configuration/ConnectionType");
|
|
if (string.IsNullOrEmpty(n.InnerText))
|
|
throw new ApplicationException("Required setting ConnectionType is not set in configuration file\r\n" + configfile);
|
|
|
|
|
|
|
|
switch (n.InnerText)
|
|
{
|
|
case "DataBase":
|
|
_cnType = ConnectionType.DataBase;
|
|
break;
|
|
case "DataPortal":
|
|
_cnType = ConnectionType.DataPortal;
|
|
break;
|
|
default:
|
|
throw new ApplicationException("Connection type: \"" + n.InnerText + "\" not valid in configuration file\r\nValid values are: DataBase or DataPortal\r\n" + configfile);
|
|
|
|
}
|
|
|
|
if (_cnType == ConnectionType.DataBase)
|
|
{
|
|
n = d.SelectSingleNode("/configuration/DataBaseType");
|
|
if (string.IsNullOrEmpty(n.InnerText))
|
|
throw new ApplicationException("Required setting DataBaseType is not set in configuration file\r\n" + configfile);
|
|
|
|
switch (n.InnerText)
|
|
{
|
|
case "MSSQL":
|
|
_dbType = DataBaseType.MSSQL;
|
|
break;
|
|
case "FireBird":
|
|
_dbType = DataBaseType.FireBird;
|
|
break;
|
|
default:
|
|
throw new ApplicationException("Database type: \"" + n.InnerText + "\" not valid in application configuration file\r\nValid values are: MSSQL or FireBird\r\n" + configfile);
|
|
|
|
}
|
|
|
|
n = d.SelectSingleNode("/configuration/DataBaseConnectionString");
|
|
if (string.IsNullOrEmpty(n.InnerText))
|
|
throw new ApplicationException("Required setting DataBaseConnectionString is not set in configuration file\r\n" + configfile);
|
|
|
|
_DataBaseConnectionString = n.InnerText;
|
|
}
|
|
else
|
|
{
|
|
//it's a dataportal
|
|
n = d.SelectSingleNode("/configuration/PortalServer");
|
|
if (string.IsNullOrEmpty(n.InnerText))
|
|
throw new ApplicationException("Required setting PortalServer is not set in configuration file\r\n" + configfile);
|
|
|
|
_DataPortalConnection = n.InnerText;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Flip config.txt from / to lite / full
|
|
/// </summary>
|
|
public void TrialLiteFlip()
|
|
{
|
|
string configfile = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar + "config.txt";
|
|
if (!File.Exists(configfile))
|
|
{
|
|
//Added for Case 657
|
|
//Could be shadow copied (asp.net etc), try the escapedcodebase value instead
|
|
configfile = Path.GetDirectoryName(new System.Uri(System.Reflection.Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath) + Path.DirectorySeparatorChar + "config.txt";
|
|
if (!File.Exists(configfile))
|
|
throw new ApplicationException("Error config.txt file is missing. \r\nIt should be here:\r\n" + configfile);
|
|
}
|
|
|
|
|
|
|
|
StreamReader reader = new StreamReader(configfile);
|
|
string content = reader.ReadToEnd();
|
|
reader.Close();
|
|
|
|
if (0 <= content.IndexOf("ayanova.fdb", StringComparison.InvariantCultureIgnoreCase))
|
|
content = System.Text.RegularExpressions.Regex.Replace(content, "ayanova.fdb", "ayanovalite.fdb", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
|
|
else
|
|
{
|
|
if (0 <= content.IndexOf("ayanovalite.fdb", StringComparison.InvariantCultureIgnoreCase))
|
|
content = System.Text.RegularExpressions.Regex.Replace(content, "ayanovalite.fdb", "ayanova.fdb", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
|
|
else
|
|
throw new ApplicationException("Error config.txt file does not contain a reference to AyaNova.fdb or AyaNovaLite.fdb");
|
|
}
|
|
|
|
StreamWriter writer = new StreamWriter(configfile);
|
|
writer.Write(content);
|
|
writer.Close();
|
|
}
|
|
|
|
|
|
|
|
//end of class
|
|
}
|
|
}
|