using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; using System.Threading; using De.Lambertz.Essentials; using System.Data; using System.Windows.Forms; using System.Management; using Microsoft.Data.SqlClient; namespace De.Lambertz.ODAL { public class IDFactory : ISingelton { private static IDFactory me = null; public const String KEY_ANWENDER = "ANWENDER"; public const String KEY_BEREICH = "BEREICH"; public const String KEY_FACHBEREICH = "FBEREICH"; public const String KEY_BESTELLGRUND = "BESTELLGRUND"; public const String KEY_BESTELLUNG = "BESTELLUNG"; public const String KEY_LIEFERADRESSE = "LIEFERADRESSE"; public const String KEY_LIEFERART = "LIEFERART"; public const String KEY_MENGENEINHEIT = "MENGENEINHEIT"; public const String KEY_PARAMETER = "PARAMETER"; public const String KEY_PREISLISTE = "PREISLISTE"; public const String KEY_TEXTBAUSTEIN = "TEXTBAUSTEINE"; public const String KEY_WAEM = "WAEM"; public const String KEY_KONTO = "KONTO"; public const String KEY_KORREKTUR_BUCHUNG = "KORBU"; public const String KEY_ANSPRECHPARTNER = "ANSPRECHPARTNER"; public const String KEY_TERMINSERIEN = "TERMINSERIEN"; public const String KEY_LIEFERANTENZUORNDUNG = "LIEFERANTENZUORNDUNG"; private string secureKeyPart = null; static readonly object padlock0 = new object(); //Threadsafe!! static readonly object padlock1 = new object(); //Threadsafe!! private IDFactory() { } public static IDFactory GetInstance() { lock (padlock0) { if (me == null) { me = new IDFactory(); SingeltonManager.GetInstance().RegisterSingelton(me); } return me; } } private string getSecuryKeyPart() { if (secureKeyPart == null) { try { ManagementClass oMClass = new ManagementClass("Win32_Processor"); ManagementObjectCollection colMObj = oMClass.GetInstances(); foreach (ManagementObject objMO in colMObj) { secureKeyPart = objMO.Properties["ProcessorId"].Value.ToString(); } } catch (Exception) { secureKeyPart = DateTime.Now.Ticks.ToString(); } if (secureKeyPart.Length > 40) { secureKeyPart = secureKeyPart.Substring(0, 40); } } return secureKeyPart; } /// /// Holt die Verbindung zum MSSQL Server /// /// private SqlConnection getConnection() { SqlConnection result = null; try { result = new SqlConnection(); result.ConnectionString = DBTools.ConnectionStringODAL(); result.Open(); } catch (Exception) { result = null; } return result; } /// /// Schliesst die Verbindung zum MSSQL Server /// /// private void returnConnection(SqlConnection con) { if (con != null && con.State == ConnectionState.Open) { con.Close(); } } public int GetID(string key) { lock (padlock1) { int result = 0; string secureKey = key.Trim() + getSecuryKeyPart(); SqlConnection con = getConnection(); SqlCommand command; SqlDataReader reader; StringBuilder sql = new StringBuilder(); bool ok = false; while (!ok) { sql = new StringBuilder(); sql.Append("update ODAL.IDS set [KEY] = "); sql.Append(DBTools.Txt(secureKey)); sql.Append(" where [KEY] = "); sql.Append(DBTools.Txt(key)); command = new SqlCommand(sql.ToString(), con); command.ExecuteNonQuery(); sql = new StringBuilder(); sql.Append("select ID "); sql.Append("from ODAL.IDS "); sql.Append("where [KEY] = "); sql.Append(DBTools.Txt(secureKey)); command = new SqlCommand(sql.ToString(), con); reader = command.ExecuteReader(); if (reader.Read()) { ok = true; result = DBTools.DBGetInt(reader, 0); } else { Thread.Sleep(50); } reader.Close(); } result++; sql = new StringBuilder(); sql.Append("update ODAL.IDS set [KEY] = "); sql.Append(DBTools.Txt(key)); sql.Append(", ID = "); sql.Append(result.ToString()); sql.Append(" where [KEY] = "); sql.Append(DBTools.Txt(secureKey)); command = new SqlCommand(sql.ToString(), con); command.ExecuteNonQuery(); returnConnection(con); return result; } } public void ManagedKill() { lock (padlock0) { me = null; } } public void ManagedReset(string userName) { lock (padlock0) { me = new IDFactory(); } } } }