CEL_Payroll/Payroll.BO/Query Analyzer/DABase.cs
2024-09-17 14:30:13 +06:00

317 lines
7.9 KiB
C#

using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb ;
using System.Data.OracleClient ;
using System.Runtime.Remoting;
using System.Collections.Specialized;
namespace Payroll.BO
{
public class DABase
{
protected static string ApplicationType;
protected static string _DBUser;
protected string ConnectionString;
protected string ConnectionStringORCL;
protected string OracleConnectionString;
protected static string OracleConnectionStringStatic;
protected string WarehouseConnectionString;
protected string DSTSConnectionString;
protected string WarehouseHostService;
protected static OleDbConnection OleDbConnection=null;
protected static OleDbTransaction OleDbTransaction=null;
protected static OracleConnection OracleConnection=null;
protected static OracleTransaction OracleTransaction=null;
private const string CONFIG_CONNECTION_STRING = "ConnectionString";
private const string CONFIG_ORACLE_CONNECTION_STRING = "OracleConnectionString";
private const string CONFIG_CONNECTIONORCL_STRING = "ConnectionStringORCL";
private const string CONFIG_CONNECTION_DSTS_STRING = "DSTSConnectionString";
private const string CONFIG_CONNECTION_WAREHOUSE_STRING = "WarehouseConnectionString";
private const string CONFIG_WAREHOUSE_HOSTSERVICE = "WarehouseHostService";
private static bool _bIsChangedConnection=false;
public DABase()
{
// retrieve the key value pairs from the appParams section of the configuration file
NameValueCollection values = (NameValueCollection)ConfigurationSettings.GetConfig("appParams");
if (values == null)
{
throw new ConfigurationException("Server connection is not specified");
}
// read the application type string from the configuration information and store it in the application type property
ApplicationType = values["ApplicationType"];
// read the database connection string from the configuration information and store it in the connection string property
if(ConnectionString==null)
{
ConnectionString = values[CONFIG_CONNECTION_STRING];
if (ConnectionString == null)
{
throw new ConfigurationException("Server connection is not specified");
}
if(ConnectionString!=null)
{
if(ApplicationType=="CSTS")
{
ConnectionString=this.BuildConnectionString(ConnectionString,"CSTS");
}
}
}
// Connect to Oracle DB of Head Office
if(ApplicationType=="DSTS" || ApplicationType=="Finance" || ApplicationType=="CSTS" || ApplicationType=="DeliveryVan")
{
if(values[CONFIG_CONNECTIONORCL_STRING] !=null)
{
if(ConnectionStringORCL ==null)
{
ConnectionStringORCL = values[CONFIG_CONNECTIONORCL_STRING];
}
if(ConnectionStringORCL!=null)
{
if(ApplicationType=="CSTS")
{
ConnectionStringORCL=this.BuildConnectionString(ConnectionStringORCL,"CSTS");
}
}
}
if(values[CONFIG_ORACLE_CONNECTION_STRING] !=null)
{
if(OracleConnectionString ==null)
{
OracleConnectionString = values[CONFIG_ORACLE_CONNECTION_STRING];
if(OracleConnectionString!=null)
{
if(ApplicationType=="CSTS")
{
OracleConnectionString=this.BuildConnectionString(OracleConnectionString,"CSTS");
}
}
OracleConnectionStringStatic=OracleConnectionString;
}
}
// COnnect to Data Warehouse
if(values[CONFIG_CONNECTION_WAREHOUSE_STRING] !=null)
{
if(WarehouseConnectionString ==null)
{
WarehouseConnectionString = values[CONFIG_CONNECTION_WAREHOUSE_STRING];
}
}
if(values[CONFIG_WAREHOUSE_HOSTSERVICE] !=null)
{
if(WarehouseHostService ==null)
{
WarehouseHostService = values[CONFIG_WAREHOUSE_HOSTSERVICE];
_DBUser=WarehouseHostService;
}
}
}
if(ApplicationType=="DeliveryVan")
{
if(values[CONFIG_CONNECTION_DSTS_STRING] !=null)
{
if(DSTSConnectionString ==null)
{
DSTSConnectionString = values[CONFIG_CONNECTION_DSTS_STRING];
}
}
}
//
string sSQLSyntax = values["SQLSyntax"];
if (sSQLSyntax == null)
{
SQL.SQLSyntax=SQLSyntax.Oracle;
}
else
{
switch (sSQLSyntax)
{
case "Access":
SQL.SQLSyntax=SQLSyntax.Access;
break;
case "SQL":
SQL.SQLSyntax=SQLSyntax.SQL;
break;
case "Oracle":
SQL.SQLSyntax=SQLSyntax.Oracle;
break;
}
}
}
public bool IsChangedConnection
{
get{return _bIsChangedConnection;}
set{_bIsChangedConnection=value;}
}
public static string ApplicatyionType
{
get{return ApplicationType;}
}
public static string OracleStaticConnectionString
{
get{return OracleConnectionStringStatic;}
}
public static string DBUser
{
get{return _DBUser;}
}
#region Password related function
public string Encrypt(string sText)
{
int i = 0;
string sEncrypt = "", sKey = "cel.abracadabra"; //sKey="Cel.Admin";//;
char cTextChar, cKeyChar;
char[] cTextData, cKey;
//Save Length of Pass
sText = (char)(sText.Length) + sText;
//Pad Password with space upto 10 Characters
if (sText.Length < 10)
{
sText = sText.PadRight(10, ' ');
}
cTextData = sText.ToCharArray();
//Make the key big enough
while (sKey.Length < sText.Length)
{
sKey = sKey + sKey;
}
sKey = sKey.Substring(0, sText.Length);
cKey = sKey.ToCharArray();
//Encrypting Data
for (i = 0; i < sText.Length; i++)
{
cTextChar = (char)cTextData.GetValue(i);
cKeyChar = (char)cKey.GetValue(i);
sEncrypt = sEncrypt + IntToHex((int)(cTextChar) ^ (int)(cKeyChar));
}
return sEncrypt;
}
public string Decrypt(string sText)
{
string sKey = "cel.abracadabra"; //sKey="Cel.Admin"; //
return Decrypt(sText, sKey);
}
public string Decrypt(string sText, string sKey)
{
int j = 0, i = 0, nLen = 0;
string sTextByte = "", sDecrypt = ""; //sKey="Cel.Admin"; //
char[] cTextData, cKey;
char cTextChar, cKeyChar;
//Taking Lenght, half of Encrypting data
nLen = sText.Length / 2;
//Making key is big Enough
while (sKey.Length < nLen)
{
sKey = sKey + sKey;
}
sKey = sKey.Substring(0, nLen);
cKey = sKey.ToCharArray();
cTextData = sText.ToCharArray();
//Decripting data
for (i = 0; i < nLen; i++)
{
sTextByte = "";
for (j = i * 2; j < (i * 2 + 2); j++)
{
sTextByte = sTextByte + cTextData.GetValue(j).ToString();
}
cTextChar = (char)HexToInt(sTextByte);
cKeyChar = (char)cKey.GetValue(i);
sDecrypt = sDecrypt + (char)((int)(cKeyChar) ^ (int)(cTextChar));
}
nLen = (int)sDecrypt.ToCharArray()[0];
sDecrypt = sDecrypt.Substring(1, nLen);
//Taking real password
//cTextData=sDecrypt.ToCharArray();
//sDecrypt="";
//i=(int)(char)cTextData.GetValue(0);
//for(j=1; j<=i; j++)
//{
// sDecrypt=sDecrypt + cTextData.GetValue(j).ToString();
//}
return sDecrypt;
}
private string IntToHex(int nIntData)
{
return Convert.ToString(nIntData, 16).PadLeft(2, '0').ToUpper();
}
private int HexToInt(string sHexData)
{
return Convert.ToInt32(sHexData, 16);
}
#endregion
private string BuildConnectionString(string sConfigString, string sApplicationType)
{
const string passwordKey="Password";
string password="", keyValue="";
if(sApplicationType=="CSTS")
{
keyValue=sConfigString;
string[] tokens=keyValue.Split(';'); keyValue="";
foreach(string token in tokens)
{
string[] tokenValues=token.Split('=');
if(tokenValues.Length>1)
{
if(tokenValues[0]==passwordKey)
{
password=this.Decrypt(tokenValues[1]);
keyValue= keyValue + ";" + passwordKey + "=" + password;
}
else
{
if(keyValue.Length>0)
{
keyValue = keyValue + ";";
}
keyValue = keyValue + token;
}
}
}
}
return keyValue;
}
}
}