/[AnywhereTS-MSSQL]/trunk/TSAdminTool/ATSAmdin.Installer.cs
ViewVC logotype

Diff of /trunk/TSAdminTool/ATSAmdin.Installer.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/TSAdminTool/ATSAmdin.Installer.cs	2012/07/15 13:01:23	152
+++ trunk/TSAdminTool/ATSAmdin.Installer.cs	2012/07/15 15:18:55	157
@@ -8,6 +8,8 @@
 using System.Threading;
 using System.Windows.Forms;
 using System.Drawing;
+using AnywhereTS.DBSupport;
+using System.Data.SqlClient;
 
 
 namespace AnywhereTS
@@ -21,13 +23,51 @@
         {
             InitializeComponent();
         }
-        Exception install_ex = null;
+        #region Install Members
         DatabaseInstallerWaitDialog dlg;
         public void InstallDatabaseWaitMessage()
         {
             dlg = new DatabaseInstallerWaitDialog("", string.Format(@"Please Wait... Installing database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName));
             dlg.ForeColor = Color.Black;
             dlg.ShowDialog();            
+        }        
+        private void InstallDatabase()
+        {
+            // install the database 
+            Thread db_installer_thread = new Thread(new ThreadStart(InstallDatabaseWaitMessage));
+            db_installer_thread.IsBackground = true;
+            db_installer_thread.Start();
+            Logging.ATSAdminInstallerLog.DebugFormat(@"Installing database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
+            AnywhereTS.DatabaseSupport dbsup = new DatabaseSupport();
+            dbsup.SetupDatabase();
+            dlg.Message = string.Format(@"Successfully installed the database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
+            dlg.ForeColor = Color.Green;
+            Thread.Sleep(new TimeSpan(0, 0, 15));
+            dlg.Close();
+            Logging.ATSAdminInstallerLog.DebugFormat(@"Successfully Installed database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
+        }
+
+        private void ConfigureATS()
+        {
+            try
+            {
+                Logging.ATSAdminInstallerLog.Debug("Configuring AnywhereTS");
+                frmAdmin atsadmin = new frmAdmin();
+                if (!atsadmin.ConfigureATS())
+                {
+                    throw new Exception("AnywhereTS cannot be installed without configuring it first.");
+                }
+                Logging.ATSAdminInstallerLog.Debug("Successfully Configured AnywhereTS");
+            }
+            catch (Exception ex)
+            {
+                // database install failed
+                using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
+                {
+                    Logging.ATSAdminInstallerLog.Error("ConfigureATS() failed.");
+                }
+                throw ex;
+            }
         }
 
         public override void Install(IDictionary stateSaver)
@@ -50,18 +90,9 @@
                     ATSGlobals.SetATSRegValue(ProSupport.strRegDatabaseInstance, DBInstance);
                     ProSupport.strDatabaseServer = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer);
                     ProSupport.strDatabaseInstance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance);
-                    // install the database 
-                    Thread db_installer_thread = new Thread(new ThreadStart(InstallDatabaseWaitMessage));
-                    db_installer_thread.IsBackground = true;
-                    db_installer_thread.Start();
-                    Logging.ATSAdminInstallerLog.DebugFormat(@"Installing database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
-                    AnywhereTS.DatabaseSupport dbsup = new DatabaseSupport();
-                    dbsup.SetupDatabase();
-                    dlg.Message = string.Format(@"Successfully installed the database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
-                    dlg.ForeColor = Color.Green;
-                    Thread.Sleep(new TimeSpan(0,0,15));
-                    dlg.Close();
-                    Logging.ATSAdminInstallerLog.DebugFormat(@"Successfully Installed database: {2} to {0}\{1}", DBServer, DBInstance, ATSGlobals.strDatabaseName);
+
+                    InstallDatabase();
+                    ConfigureATS();
                 }
             }
             catch (Exception ex)
@@ -114,5 +145,112 @@
                 }
             }
         }
+        #endregion
+
+        public override void Uninstall(IDictionary savedState)
+        {
+            using (log4net.NDC.Push("Logged from ATSAdmin.Installer"))
+            {
+                try
+                {
+                    string path = this.Context.Parameters["targetdir"];
+                    Logging.ATSAdminInstallerLog.DebugFormat("Install Dir: {0}", path);
+                    Logging.UpdateLogPath(string.Format(@"{0}\logs", path));
+                    Logging.ATSAdminInstallerLog.DebugFormat("Showing uninstall dialog");
+                    UninstallDialog udlg = new UninstallDialog();
+                    DialogResult result = udlg.ShowDialog();
+                    if (result == DialogResult.Cancel)
+                    {
+                        Logging.ATSAdminInstallerLog.DebugFormat("user aborted uninstall");
+                        MessageBox.Show("Uninstall canceled ... nothing was uninstalled", "Uinstall was canceled by user", MessageBoxButtons.OK, MessageBoxIcon.Warning);
+                        return;
+                    }
+                    base.Uninstall(savedState);
+                    using (log4net.NDC.Push(string.Format("KeepDatabase={0} KeepApplicationSettings={1} KeepClientImages={2}", udlg.KeepDatabase, udlg.KeepApplicationSettings, udlg.KeepClientImages)))
+                    {
+                        Logging.ATSAdminInstallerLog.DebugFormat("Unistall options");
+                    }
+                    string server = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseServer);
+                    string instance = ATSGlobals.GetATSRegValueString(ProSupport.strRegDatabaseInstance);
+                    string database = ATSGlobals.strDatabaseName;
+                    if (!udlg.KeepDatabase)
+                    {
+                        Exception ErrorInfo = null;
+                        // delete database
+                        using (MsSqlConnector conn = new MsSqlConnector(server, instance, "master"))
+                        {
+                            try
+                            {
+                                conn.CreateConnection(out ErrorInfo);
+                                conn.OpenConnection(out ErrorInfo);
+                                SqlConnection sqlCon;
+                                conn.GetConnectionClone(out sqlCon, out ErrorInfo);
+                                SqlCommand sqlCmd = new SqlCommand();
+                                sqlCmd.Connection = sqlCon;
+                                Logging.ATSAdminInstallerLog.DebugFormat("Removing database: {0}", database);
+                                sqlCmd.CommandText = string.Format("DROP DATABASE {0};",database);
+                                sqlCmd.ExecuteNonQuery();
+                                Logging.ATSAdminInstallerLog.DebugFormat("Successfully removed database: {0}", database);
+                            }
+                            catch (Exception ex)
+                            {
+                                Logging.ATSAdminInstallerLog.DebugFormat("Failed to remove database: {0}", database);
+                                ErrorInfo = ex;
+                            }
+                        }
+                        if (ErrorInfo != null)
+                            throw ErrorInfo;
+                    }
+                    if (!udlg.KeepApplicationSettings)
+                    {
+                        // delete registry settings
+                        try
+                        {
+                            Logging.ATSAdminInstallerLog.DebugFormat("Removing AnywhereTS Registry settings");
+                            Microsoft.Win32.RegistryKey key = null;
+                            if (IntPtr.Size == 8)
+                            {   // 64 bit OS
+                                //string t = @"SOFTWARE\Wow6432Node\" + ATSGlobals.ApplicationName + @"\ts-config";
+                                key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node\", true);
+                                key.DeleteSubKey("TFTPD32", false);
+                                key.DeleteSubKey(ATSGlobals.ApplicationName, false);
+                            }
+                            else
+                            {   // 32 bit OS
+                                //strATSregRoot = @"SOFTWARE\" + ATSGlobals.ApplicationName + @"\ts-config";
+                                key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software", true);
+                                key.DeleteSubKey("TFTPD32", false);
+                                key.DeleteSubKey(ATSGlobals.ApplicationName, false);
+                            }
+                            Logging.ATSAdminInstallerLog.DebugFormat("Successfully removed AnywhereTS Registry settings");
+                        }
+                        catch (Exception ex)
+                        {
+                            Logging.ATSAdminInstallerLog.DebugFormat("Failed to remove AnywhereTS Registry settings");
+                            throw ex;
+                        }
+                    }
+                    if (!udlg.KeepClientImages)
+                    {
+                        // delete client images
+                        try
+                        {
+                        }
+                        catch (Exception ex)
+                        {
+                        }
+                    }
+                }
+                catch (Exception ex)
+                {
+                    // database install failed
+                    using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
+                    {
+                        Logging.ATSAdminInstallerLog.Error("Install() failed.");
+                    }
+                    throw ex;
+                }
+            }
+        }
     }
 }

 

  ViewVC Help
Powered by ViewVC 1.1.22