Skip Navigation Links.

Northwind\General\NorthwindGeneralBase.cs

D:\NorthwindForWeb\Northwind\CS\DotNet\ADO.Net\Northwind\General\NorthwindGeneralBase.cs
/*
 * This file was generated by ProCG version 2.0
 *
 * File name:	Northwind\General\NorthwindGeneralBase.cs
 * Language:	C# - ADO.Net 
 * Database:	My Sql
 *
 * Copyright (c) 2002-2019 iGenXSoft.
 * For more information visit http://www.igenxsoft.com
 */

using System;
using System.Globalization;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Web;
using System.Web.SessionState;
using System.Configuration;
using System.Drawing.Imaging;
using System.IO;
using Microsoft.Win32;
using System.Text.RegularExpressions;

namespace Northwind.General
{
	/// <summary>
	/// Summary description for NorthwindGeneralBase.
	/// </summary>
	public class NorthwindGeneralBase
	{
		protected static bool		m_bShowCodeFieldWithComboBox = false;
		protected static string		m_CodesTableName = "Codes";
		protected static string		m_ConnectionString;
		protected static string		m_DateTimeFormat = "dd-MM-yyyy HH:mm:ss";
		protected static string		m_DateFormat = "dd-MM-yyyy";
		protected static IFormatProvider culture;
		protected static string		m_UrlPageWhenSessionIsEnded;
		protected static string		m_LoginPage;
		protected static string		m_ChangePasswordPage;
		protected static int		m_NumPagesToShowFirstAndLast= 5;
		protected static int		m_NumPagesToShowJumpToPage  = 5;
		protected static int		m_NumPagesToShowSetPageSize = 2;
		protected static bool		m_DigIntoSamePlace = true;		
		private static bool m_AllowNavigateToOtherEntity = false;
		protected static WebControlsStyle m_WebControlStyle = WebControlsStyle.MicrosoftStandard;
		protected static bool m_ShowMessageBoxForFormsErrors = true;
		protected static bool m_ShowMessageBoxForFormsInformations = true;
		protected static bool m_ShowMessageBoxForControlsErrors = true;
		protected static bool m_ShowMessageBoxForControlsInformations = true;

		public NorthwindGeneralBase()
		{
		}


		public static bool ShowCodeFieldWithComboBox
		{
			get { return m_bShowCodeFieldWithComboBox;}
			set { m_bShowCodeFieldWithComboBox = value;}
		}

		public static string CodesTableName
		{
			get { return m_CodesTableName;}
			set { m_CodesTableName = value;}
		}

#if !SILVERLIGHT
		public static DbConnection GetDefaultConnection(String connectionString = null)
		{
			if (connectionString == null)
				connectionString = ConnectionString;
			MySql.Data.MySqlClient.MySqlConnection connection = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
			return connection;
		}

		public static string ConnectionString
		{
			get 
			{
				if (m_ConnectionString == "" || m_ConnectionString == null)
				{
					m_ConnectionString = GetConnectionStringFromRegistry(GetShortCompanyName(),"Northwind");
				}
				return (m_ConnectionString);
			}
			set 
			{
				m_ConnectionString = value;
			}
		}

		public static string GetDefaultConnectionString()
		{	
			if (m_ConnectionString == null)
				m_ConnectionString = @"Provider=MSDASQL.1;Persist Security Info=False;User ID=root;Data Source=EspV02;InitialCatalog=test";
			return (m_ConnectionString);
		}
			
		public static string GetConnectionStringFromRegistry(string companyName,string projectName)
		{							
			string connectionString = GetStringFromRegistry(companyName,projectName,"ConnectionString");
			return (connectionString);
		}	

		public static string GetStringFromRegistry(string companyName,string projectName, string paramName)
		{
			RegistryKey subKey=null, topKey=null, rootKey=null;
			rootKey=Registry.LocalMachine.OpenSubKey("SOFTWARE");
			string paramValue = null;

			topKey=rootKey.OpenSubKey(companyName);
			if (topKey != null)
			{
				subKey = topKey.OpenSubKey(projectName);
				if (subKey != null)
				{
					paramValue = (string)subKey.GetValue(paramName,"");
				}
			}
			if (paramValue == null || paramValue == "")
			{
				throw new Exception(@"You need to define registry key: My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\"+companyName+@"\"+projectName+@"\"+paramName);
			}
			if (subKey != null) subKey.Close();
			if (topKey != null) topKey.Close();
			if (rootKey != null) rootKey.Close();

			return (paramValue);
		}
	
	
		public static string GetConnectionStringFromAppConfig(string projectName)
		{
			string connectionString;
			connectionString = ConfigurationManager.ConnectionStrings[projectName + "ConnectionString"].ConnectionString;
			return connectionString;
		}	

		public static string GetStringFromAppConfig(string paramName)
		{
			string paramValue;
			if (ConfigurationManager.AppSettings[paramName] == null)
				throw new Exception(string.Format("Symbol {0} was not defined in App/Web Config file",paramName));
			paramValue = ConfigurationManager.AppSettings[paramName];
			return paramValue;
		}

		public static string GetDBProviderNameFromAppConfig(string projectName)
		{
			string dbProviderName;
			dbProviderName = ConfigurationManager.ConnectionStrings[projectName + "ConnectionString"].ProviderName;
			return dbProviderName;
		}
		
		public static string GetConnectionStringFromIniFile(string fileName)
		{

			try
			{
				IniFile iniFile = new IniFile(fileName);
				Dictionary<string, string> hashTable = iniFile.GetAllFromFile();
				m_ConnectionString = (string)hashTable["ConnectionString"];
			}
			catch (Exception)
			{
				m_ConnectionString = "";
			}

			if (m_ConnectionString == null || m_ConnectionString == "")
			{
				throw new Exception("IniFile " +fileName + " Does not exist or ConnectionString sysmbol deos not exist in IniFile");
			}
			return (m_ConnectionString);
		}
		
		public static void SetConnectionStringToIniFile(string fileName, string connectionString)
		{
			Dictionary<string, string> hashTable;

			IniFile iniFile = new IniFile(fileName);
			try
			{
				hashTable = iniFile.GetAllFromFile();
			}
			catch (Exception)
			{
				hashTable = new Dictionary<string, string>();
			}
			hashTable["ConnectionString"] = connectionString;
			iniFile.SetToFile(hashTable);
		}
		public static string GetWebServerRootURL()  
		{
			string applicationPath;

			if (HttpContext.Current == null)
			{
				// You're in design mode
				string runtimeDirectory = GetRootDirectory()+@"\Northwind\Web\";
				applicationPath = runtimeDirectory;
			}
			else
			{
				applicationPath = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
				if (applicationPath == "/")  //a site
				{
					//applicationPath = "~/";
					applicationPath = "/";
				}
				else if(!applicationPath.EndsWith(@"/")) //a virtual
					applicationPath += @"/";
			}
			//if (applicationPath == @"/")
			//	applicationPath = "";
			return applicationPath;
		}

		public static string GetServerFilePathFromUrl(string url)
		{
			if (url == null || url.Equals(string.Empty))
				return HttpContext.Current.Server.MapPath(GetWebServerRootURL());
			else
				return HttpContext.Current.Server.MapPath(url);
		}
		
		public static string GetWebImagesURL(string theme)  
		{
			if (theme == null || theme.Length == 0)
				return (GetWebServerRootURL() + "images/"); 
			else
				return GetWebServerRootURL() + "App_Themes/" + theme + "/images/";
		}

		public static string GetWebToolbarImagesURL(string theme)
		{
			if (theme == null || theme.Length == 0)
				return (GetWebServerRootURL() + "images/32x32/");
			else
				return GetWebServerRootURL() + "App_Themes/" + theme + "/images/32x32/";
		}

		public static string GetWebGridImagesURL(string theme) 
		{
			if (theme == null || theme.Length == 0)
				return (GetWebServerRootURL() + "images/gridicons/");
			else
				return GetWebServerRootURL() + "App_Themes/" + theme + "/images/gridicons/"; 
		}

		public static string GetRootWebUploadURL()
		{
			return (GetWebServerRootURL() + "resources/");
		}

		public static string GetRootWebUploadImagesURL()
		{
			return (GetRootWebUploadURL() + "images/");
		}
		
		public static string GetRootWebUploadAudioURL()
		{
			return (GetRootWebUploadURL() + "audio/");
		}

		public static string GetRootWebUploadVideoURL()
		{
			return (GetRootWebUploadURL() + "video/");
		}
		
		public static void ThrowInvalidFileTypeException(FileTypeEnum fileType)
		{
			throw new Exception("Unknown file type: " + fileType.ToString());
		}
		
		public static string GetImageURLFromFullPath(string imageFullPath)
		{
			string physicalPath = HttpContext.Current.Request.MapPath("~");
			if (imageFullPath.ToLower().IndexOf(physicalPath.ToLower()) == -1)
				throw new Exception("Image file must be from the Web Site's physical path");
			int startOfImages = imageFullPath.ToLower().IndexOf("images");
			if (startOfImages == -1)
				throw new Exception("Image full path does not has 'images' directory in it");
			startOfImages += "images/".Length;
			string imageUrl =GetWebServerRootURL() + "images/" + imageFullPath.Substring(startOfImages);
			return imageUrl;
		}

		public static string GetFileURLFromFullPath(FileTypeEnum fileType, string fileFullPath)
		{
			string searchDirectory = null;

			switch (fileType)
			{
				case FileTypeEnum.IMAGE:
					searchDirectory = "images";
					break;

				case FileTypeEnum.AUDIO:
					searchDirectory = "audio";
					break;

				case FileTypeEnum.VIDEO:
					searchDirectory = "video";
					break;
				default:
					ThrowInvalidFileTypeException(fileType);
					break;
			}
			int startOfImages = fileFullPath.ToLower().IndexOf(searchDirectory);
			if (startOfImages == -1)
				throw new Exception("File full path does not has "+searchDirectory +" directory in it");
			startOfImages += (searchDirectory +"/").Length;
			string imageUrl = GetWebServerRootURL() + searchDirectory +"/" + fileFullPath.Substring(startOfImages);
			return imageUrl;
		}
		
		public static FileTypeEnum GetFileType(string file)
		{
			string fileExtension;
			int index;

			if ((index = file.LastIndexOf(".")) > 0)
				fileExtension = file.Substring(index + 1);
			else
				fileExtension = file;

			switch (fileExtension.ToLower())
			{
				case "gif":
				case "ico":
				case "png":
				case "jpg":
					return FileTypeEnum.IMAGE;
				case "3gp":
				case "amv":
				case "asf":
				case "avi":
				case "moov":
				case "mov":
				case "mp4":
				case "mpg":
				case "swf":
				case "wmv":
					return FileTypeEnum.VIDEO;
				case "mp3":
				case "wma":
				case "ram":
				case "ra":
				case "rm":
				case "mid":
				case "wav":
					return FileTypeEnum.AUDIO;
				default: throw new Exception(string.Format("Unknown file extension: {0} in NorthwindGeneral.GetFileType({1}) ",fileExtension, file));
			}
		}
		public string ClientUploadFilesRoot
		{
			get
			{
				return ((string)HttpContext.Current.Session["UserClientUploadFilesRoot"]);
			}
			set
			{
				HttpContext.Current.Session["UserClientUploadFilesRoot"] = value;
			}
		}

		public static string WebTextBoxClassName
		{
			get
			{
				return ("TextBoxCss");
			}
		}

		public static string WebTextAreaClassName
		{
			get
			{
				return ("TextAreaCss");
			}
		}

		public static string WebLabelClassName
		{
			get
			{
				return ("formlabel");
			}
		}

		public static string WebDropDownClassName
		{
			get
			{
				return ("DropDownCss");
			}
		}
		public static string WebRadioButtonListClassName
		{
			get
			{
				return ("RadioButtonListCss");
			}
		}

		public static string WebCheckBoxClassName
		{
			get
			{
				return ("CheckBoxCss");
			}
		}
		
		public static string WebCalendarClassName
		{
			get
			{
				return ("CalendarCss");
			}
		}
		

		public static string WebImageClassName
		{
			get
			{
				return ("ImageCss");
			}
		}

		public static string WebImageFileClassName
		{
			get
			{
				return ("ImageFileCss");
			}
		}

		public static string WebFileClassName
		{
			get
			{
				return ("FileCss");
			}
		}

		public static string WebFileBinaryClassName
		{
			get
			{
				return ("FileBinaryCss");
			}
		}
		

		public static string WebButtonClassName
		{
			get
			{
				return ("ButtonCss");
			}
		}

		public static string WebMessageClassName
		{
			get
			{
				return ("MessageCss");
			}
		}

		public static string WebMediaPlayerClassName
		{
			get
			{
				return ("MediaPlayerCss");
			}
		}
#endif // if SILVERLIGHT

		public static bool BooleanValue(string boolValue)
		{
			if (boolValue == Boolean.TrueString || boolValue == "1")
			{
				return (true);
			}
			else
			{
				return (false);
			}
		}
		
		public static string GetWebSiteName() 
		{
			return "Northwind";
		}
		
		public static string GetDateTimeFormat()
		{
			return m_DateTimeFormat;
		}

		public static void SetDateTimeFormat(string dateTimeFormat)
		{
			m_DateTimeFormat = dateTimeFormat;
		}

		public static string GetDateFormat()
		{
			return m_DateFormat;
		}

		public static void SetDateFormat(string dateFormat)
		{
			m_DateFormat = dateFormat;
		}

		public static IFormatProvider IFormatProvider
		{
			get
			{
				if (culture == null)
#if !SILVERLIGHT
					culture = new CultureInfo("he-IL", true); 
#else
					culture = new CultureInfo("he-IL"); 
#endif
				return culture;
			}
		}

#if !SILVERLIGHT
		public static DateTime InitialDateTime
		{
			get
			{
				return DateTime.MinValue;
			}
		}
#else
		public static DateTime InitialDateTime
		{
			get
			{
				return new DateTime(1753,1,1);
			}
		}
#endif
		
		public static string GetDateTimeAsString(DateTime dateTime)
		{
			if (dateTime.Equals(InitialDateTime))
				return "";
			else
				return dateTime.ToString(GetDateTimeFormat());
		}

		public static string GetDateTimeAsString(DateTime? dateTime)
		{
			if (dateTime == null || dateTime.Equals(InitialDateTime))
				return "";
			else
			{
				DateTime dateTime2 = (DateTime)dateTime;
				return dateTime2.ToString(GetDateTimeFormat());
			}
		}

		public static string GetDateAsString(DateTime dateTime)
		{
			if (dateTime.Equals(InitialDateTime))
				return "";
			else
				return dateTime.ToString(GetDateFormat());
		}

		static public DateTime ConvertToDateTime(string fieldValue)
		{
			if (fieldValue == "" || fieldValue == "{null}" || fieldValue == "00/00/0000 00:00:00")
				return InitialDateTime;
			else
				return Convert.ToDateTime(fieldValue);
		}

		static public DateTime ConvertToDateTime(string fieldValue, IFormatProvider formatProvider)
		{
			if (fieldValue == "" || fieldValue == "{null}" || fieldValue == "00/00/0000 00:00:00")
				return InitialDateTime;
			else
				return DateTime.Parse(fieldValue, formatProvider);
		}

		static public DateTime ConvertToDate(string fieldValue)
		{
			if (fieldValue == "" || fieldValue == "{null}" || fieldValue == "00/00/0000")
				return InitialDateTime;
			else
				return Convert.ToDateTime(fieldValue);
		}

		static public DateTime ConvertToDate(string fieldValue, IFormatProvider formatProvider)
		{
			if (fieldValue == "" || fieldValue == "{null}" || fieldValue == "00/00/0000")
				return InitialDateTime;
			else
				return DateTime.Parse(fieldValue, formatProvider);
		}

		static public bool DateTimeAreEqualToleranceSecond(DateTime? dateTime1, DateTime? dateTime2)
		{
			if (dateTime1 == dateTime2)
				return true;
			else if (dateTime1 == null || dateTime2 == null)
				return false;

			if (Math.Abs((dateTime1.Value - dateTime2.Value).Ticks) >= TimeSpan.FromSeconds(1).Ticks || dateTime1.Value.Second != dateTime2.Value.Second)
				return false;
			else
				return true;
		}

		static public bool DateTimeAreEqualToleranceSecond(DateTime dateTime1, DateTime dateTime2)
		{
			if (Math.Abs((dateTime1 - dateTime2).Ticks) >= TimeSpan.FromSeconds(1).Ticks || dateTime1.Second != dateTime2.Second)
				return false;
			else
				return true;
		}

		static public bool DateTimeAreEqualToleranceMinute(DateTime? dateTime1, DateTime? dateTime2)
		{
			if (dateTime1 == dateTime2)
				return true;
			else if (dateTime1 == null || dateTime2 == null)
				return false;

			if (Math.Abs((dateTime1.Value - dateTime2.Value).Ticks) >= TimeSpan.FromMinutes(1).Ticks || dateTime1.Value.Minute != dateTime2.Value.Minute)
				return false;
			else
				return true;
		}

		static public bool DateTimeAreEqualToleranceMinute(DateTime dateTime1, DateTime dateTime2)
		{
			if (Math.Abs((dateTime1 - dateTime2).Ticks) >= TimeSpan.FromMinutes(1).Ticks || dateTime1.Minute != dateTime2.Minute)
				return false;
			else
				return true;
		}

		public  static string GetShortCompanyName()
		{
			return  "Northwind"; 
		}
#if !SILVERLIGHT


		public static string GetUrlPageWhenSessionIsEnded()
		{
			if (m_UrlPageWhenSessionIsEnded == null)
				m_UrlPageWhenSessionIsEnded = GetLoginPage(); 
			return m_UrlPageWhenSessionIsEnded;
		}

		public static void SetUrlPageWhenSessionIsEnded(string urlPageWhenSessionIsEnded)
		{
			m_UrlPageWhenSessionIsEnded = urlPageWhenSessionIsEnded;
		}
		
		public static string GetLoginPage()
		{
			if (m_LoginPage == null)
				m_LoginPage = GetWebServerRootURL() + @"Login.aspx";
			return m_LoginPage;
		}

		public static void SetLoginPage(string loginPage)
		{
			m_LoginPage = loginPage;
		}

		public static string GetChangePasswordPage()
		{
			if (m_ChangePasswordPage == null)
				m_ChangePasswordPage = GetWebServerRootURL() + @"Records\NorthwindUser\ChangePassword.aspx";
			return m_ChangePasswordPage;
		}

#endif

		public static int NumPagesToShowFirstAndLast
		{
			get
			{
				return m_NumPagesToShowFirstAndLast;
			}
			set
			{
				m_NumPagesToShowFirstAndLast = value;
			}
		}

		public static int NumPagesToShowSetPageSize
		{
			get
			{
				return m_NumPagesToShowSetPageSize;
			}
			set
			{
				m_NumPagesToShowSetPageSize = value;
			}
		}

		public static int NumPagesToShowJumpToPage
		{
			get
			{
				return m_NumPagesToShowJumpToPage;
			}
			set
			{
				m_NumPagesToShowJumpToPage = value;
			}
		}

		public static bool DigIntoSamePlace
		{
			get
			{
				return m_DigIntoSamePlace;
			}
			set
			{
				m_DigIntoSamePlace = value;
			}
		}

#if !SILVERLIGHT

		public static string GetRootDirectory()
		{
			return @"D:\Temp3\Northwind\CS\DotNet\ADO.Net";
		}

		public static string GetFCKEditorBasePath()
		{
			string fCKEditorBasePath = NorthwindGeneral.GetWebServerRootURL() + "FCKeditor/";
			return fCKEditorBasePath;
		}

		static public HtmlEditorLangDirectionEnum HtmlEditotLangDirectionEnum 
		{
			get
			{
				return HtmlEditorLangDirectionEnum.LeftToRight;
			}
		}

#endif

		static public string GetShortTextNicely(string text, int textSize)
		{

			if (text.Length <= textSize)
				return (text);
			StringBuilder shortText = new StringBuilder(text.Substring(0,textSize));
			shortText[textSize-1] = '.';
			shortText[textSize-2] = '.';
			shortText[textSize-3] = '.';

			int spacePos = textSize-4;
			int lastPoint = textSize-1;
			while (true && spacePos > 0)
			{
				if (shortText[spacePos] == ' ')
					break;
				shortText[spacePos--] = '.';
				shortText.Remove(lastPoint--,1);
			}

			return (shortText.ToString());
		}
		
		public static string ExpandFromCSName(string MainObjectName)
		{
			StringBuilder sb = new StringBuilder();

			for (int i = 0; i < MainObjectName.Length; i++)
			{
				string currentChar = MainObjectName.Substring(i,1);
				
				if (currentChar.Equals(currentChar.ToUpper()) && i != 0)
				{
					sb.Append(" ");
				}
				sb.Append(currentChar);
			}
			return sb.ToString();
		}

		public static WebControlsStyle WebControlsStyle
		{
			get { return m_WebControlStyle; }
			set { m_WebControlStyle = value; }
		}

		public static bool AllowNavigateToOtherEntity
		{
			get { return m_AllowNavigateToOtherEntity; }
			set { m_AllowNavigateToOtherEntity = value; }
		}		

		public static bool ShowMessageBoxForFormsErrors
		{
			get { return m_ShowMessageBoxForFormsErrors; }
			set { m_ShowMessageBoxForFormsErrors = value; }
		}

		public static bool ShowMessageBoxForFormsInformations
		{
			get { return m_ShowMessageBoxForFormsInformations; }
			set { m_ShowMessageBoxForFormsInformations = value; }
		}

		public static bool ShowMessageBoxForControlsErrors
		{
			get { return m_ShowMessageBoxForControlsErrors; }
			set { m_ShowMessageBoxForControlsErrors = value; }
		}

		public static bool ShowMessageBoxForControlsInformations
		{
			get { return m_ShowMessageBoxForControlsInformations; }
			set { m_ShowMessageBoxForControlsInformations = value; }
		}

		public static string GetFullExceptionMessages(Exception ex)
		{
			StringBuilder stringBuilder = new StringBuilder();
			Exception currentException = ex;
			while (currentException != null)
			{
				stringBuilder.Append(currentException.Message);
				stringBuilder.Append(Environment.NewLine);
				currentException = currentException.InnerException;
			}
			return stringBuilder.ToString();
		}

		public static string GetBackupDirectory()
		{
			string backupDirectory = ConfigurationManager.AppSettings["BackupDirectory"];
			if (backupDirectory == null)
			{
				throw new Exception("BackupDirectory symbol under AppSetting is not defined in config file");
			}
			return backupDirectory;
		}

		public static byte[] StringToByteArray(string str)
		{
			UTF8Encoding encoding = new UTF8Encoding();
			return encoding.GetBytes(str);
		}

		public static string ByteArrayToString(byte[] input)
		{
			UTF8Encoding enc = new UTF8Encoding();
			string str = enc.GetString(input);
			return str;
		}
		// replace symbols ${symbol.Name} to symbols values
		// the symbols list contains name1,value1,name2,value2...
		public static string ReplaceSymbols(List<string> symbols, string text)
		{
			string origin;
			string pattern;
			do
			{
				origin = text;
				for (int i = 0; i < symbols.Count; i = i +2)
				{
					pattern = @"\${" + symbols[i] + @"}";
					text = Regex.Replace(text, pattern, symbols[i + 1]);
				}
			} while (origin != text);
			return text;
		}
	}
}


  //     22612 ProCG uses this line - don't edit it