Imports System.IO Imports DD_LIB_Standards Public Class ClassUpdate Public Shared MyServer_UpdatePath As String Public Shared VERSION_SERVER = "1.0.0.0" Public Shared VERSION_USER = "1.0.0.0" Public Shared FORCE_UPDATE As Boolean = False Public Shared UPDATE_ID As Integer Public Shared ALL_USERS As Boolean = False Public Shared VERSIONS_FOR_FORCE_UPDATE As String = "" Public Shared FOLDER_TEMP As String Public Shared MY_INSTALL_PATH As String Public Shared DT_UPDATE_ITEMS As DataTable Public Shared DT_UPDATE As DataTable Public Shared ERROR_WHILE_UPDATING As Boolean = False Public Shared UPDATE_NECESSARY As Boolean = False Public Shared UPDATE_CANCELLED As Boolean = False Public Shared UPDATE_ERROR As Boolean = False Private Shared RecExe_found As Boolean = False Public Shared Function Init() Try If InitInstallPath() = False Then End If Dim sql = String.Format("SELECT * FROM TBDD_MODULES WHERE UPPER(NAME) = UPPER('Record-Organizer')") Dim KONFIG_DT As DataTable = clsDatabase.Return_Datatable(sql, False) If KONFIG_DT.Rows.Count = 1 Then MyServer_UpdatePath = KONFIG_DT.Rows(0).Item("VERSION_UPATE_PATH") End If sql = String.Format("SELECT * FROM TBDD_VERSION_UPDATE WHERE GUID = (SELECT MAX(GUID) FROM TBDD_VERSION_UPDATE WHERE UPPER(MODULE_NAME) = UPPER('{0}'))", "Record-Organizer") DT_UPDATE = clsDatabase.Return_Datatable(sql, False) If Not IsNothing(DT_UPDATE) Then If DT_UPDATE.Rows.Count = 1 Then UPDATE_ID = DT_UPDATE.Rows(0).Item("GUID") VERSION_SERVER = DT_UPDATE.Rows(0).Item("VERSION_NO") FORCE_UPDATE = DT_UPDATE.Rows(0).Item("FORCE_UPD") VERSIONS_FOR_FORCE_UPDATE = DT_UPDATE.Rows(0).Item("VERSION_FORCE_UPD") ALL_USERS = DT_UPDATE.Rows(0).Item("ALL_USERS") sql = String.Format("SELECT * FROM TBDD_VERSION_ITEMS WHERE UPDATE_ID = {0} AND [INFO1] IS NOT NULL ORDER BY GUID", UPDATE_ID) DT_UPDATE_ITEMS = clsDatabase.Return_Datatable(sql) Else ClassLogger.Add(">> No Updates Configured!", False) End If End If Return True Catch ex As Exception ClassLogger.Add("Unexpected Error in ClassUpdate - Init: " & ex.Message, True) Return False End Try End Function Private Shared Function InitInstallPath() Try Dim readValue = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\Digital Data\Record Organizer", "Path", Nothing) If Not IsNothing(readValue) Then MY_INSTALL_PATH = readValue Return True Else MY_INSTALL_PATH = "" For Each s In Directory.GetLogicalDrives() If RecExe_found = False Then DirSearch(s) End If Next If RecExe_found = False Then MsgBox("Could not find the path for RecordOrganizer - Please check your installation!", MsgBoxStyle.Critical) Return False End If End If Catch ex As Exception ClassLogger.Add("Unexpected Error in InitInstallPath: " & ex.Message, True) Return False End Try End Function Private Shared Sub DirSearch(ByVal sDir As String) Dim d As String Dim f As String Try For Each d In Directory.GetDirectories(sDir) If d.Contains("$Recycle") = False Or d.Contains("System Volume") = False Then If RecExe_found = False Then For Each f In Directory.GetFiles(d, "DD-Record-Organizer.exe") RecExe_found = True MY_INSTALL_PATH = Path.GetDirectoryName(f) Exit Sub Next End If If RecExe_found = False Then DirSearch(d) End If End If Next Catch excpt As System.Exception ClassLogger.Add("Unexpected Error in DirSearch: " & excpt.Message, True) MY_INSTALL_PATH = "" End Try End Sub Public Shared Function InitUser(Optional _User As String = "") Try Dim sql = "" USER_USERNAME = Environment.UserName sql = String.Format("SELECT * FROM TBDD_USER WHERE (LOWER(USERNAME) = LOWER('{0}'))", USER_USERNAME) Dim USER_DT As DataTable = clsDatabase.Return_Datatable(sql, True) If USER_DT.Rows.Count = 0 Then ClassLogger.Add(" - User '" & USER_USERNAME & "' not listed in Useradministration!", False) 'MsgBox("Achtung: Sie sind nicht in der Userverwaltung hinterlegt." & vbNewLine & "Bitte setzen Sie sich mit dem Systembetreuer in Verbindung!", MsgBoxStyle.Critical, "Achtung:") 'Me.Close() Dim msg = String.Format("You are not listed in the Useradministration." & vbNewLine & "Please contact the admin.") MsgBox(msg, MsgBoxStyle.Exclamation) Return False Else USER_GUID = USER_DT.Rows(0).Item("GUID") USER_LANGUAGE = USER_DT.Rows(0).Item("LANGUAGE") sql = String.Format("SELECT UPDATE_PATH FROM TBDD_VERSION_USER_UPDATE_PATH WHERE USER_ID = {0} AND UPPER(MODULE_NAME) = UPPER('{1}')", USER_GUID, "Record-Organizer") Dim USER_UPDATE_PATH = clsDatabase.Execute_Scalar(sql) If Not IsNothing(USER_UPDATE_PATH) Then If USER_UPDATE_PATH <> String.Empty Then MyServer_UpdatePath = USER_UPDATE_PATH End If Else MyServer_UpdatePath = String.Empty End If sql = String.Format("SELECT CASE VERSION_CLIENT WHEN '' THEN '{3}' ELSE VERSION_CLIENT END AS VERSION_CLIENT FROM VWDD_LOGIN_USER_HISTORY WHERE GUID = (select MAX(GUID) from VWDD_LOGIN_USER_HISTORY where USER_ID = {0} AND VERSION_CLIENT <> '' AND UPPER(MACHINE_NAME) = UPPER('{1}') AND UPPER(MODULE) = UPPER('{2}'))", USER_GUID, Environment.MachineName, "Record-Organizer", VERSION_SERVER) VERSION_USER = clsDatabase.Execute_Scalar(sql) If IsNothing(VERSION_USER) Then sql = String.Format("SELECT CASE VERSION_CLIENT WHEN '' THEN '{2}' ELSE VERSION_CLIENT END AS VERSION_CLIENT FROM VWDD_LOGIN_USER_HISTORY WHERE GUID = (select MAX(GUID) from VWDD_LOGIN_USER_HISTORY where USER_ID = {0} AND VERSION_CLIENT <> '' AND UPPER(MODULE) = UPPER('{1}'))", USER_GUID, "Record-Organizer", VERSION_SERVER) VERSION_USER = clsDatabase.Execute_Scalar(sql) If IsNothing(VERSION_USER) Then VERSION_USER = "1.0.0.0" End If End If VERSION_USER = CInt(VERSION_USER.ToString.Replace(".", "")) VERSION_SERVER = CInt(VERSION_SERVER.ToString.Replace(".", "")) Return True End If Catch ex As Exception ClassLogger.Add("Unexpected Error in InitUser_Update_Spec: " & ex.Message, True) Return False End Try End Function Public Shared Sub RUN_UPDATE() If (VERSION_USER = VERSION_SERVER) Or VERSION_SERVER = 1000 Or VERSION_USER = 1000 Then Exit Sub End If UPDATE_NECESSARY = True If FORCE_UPDATE = False Then If VERSIONS_FOR_FORCE_UPDATE.Contains(VERSION_USER) Then ClassLogger.Add(String.Format("VersionChecker: ForceUpdate for User as Version '{0}' is used!", VERSION_USER), False) ClassHelper.InsertEssential_Log(USER_GUID, "USER-ID", String.Format("VersionChecker: ForceUpdate for User as Version '{0}' is used!", VERSION_USER)) FORCE_UPDATE = True End If End If If FORCE_UPDATE = False Then If ALL_USERS = False Then Dim sql = String.Format("SELECT COUNT(GUID) FROM TBDD_VERSION_USER_UPDATE WHERE USER_ID = {0} AND UPDATE_ID = {1}", USER_GUID, UPDATE_ID) If clsDatabase.Execute_Scalar(sql) = 0 Then ClassLogger.Add(String.Format("VersionChecker: User is not part of update-group - UPDATE-ID: {0}", UPDATE_ID), False) ClassHelper.InsertEssential_Log(USER_GUID, "USER-ID", String.Format("VersionChecker: User is not part of update-group - UPDATE-ID: {0}", UPDATE_ID)) Exit Sub End If End If Dim msg = "Auf dem Server liegt ein Update für ein ADDI-AddOn bereit!" & vbNewLine & "Wollen Sie das Update nun durchführen? Die Dauer ist abhängig von Ihrer Netzwerkverbindung!" If USER_LANGUAGE <> "de-DE" Then msg = "There is a new release available for an ADDI-AddOn!" & vbNewLine & "Would You like to install the update now? This might take some time (minute(s))!" End If Dim result As MsgBoxResult result = MessageBox.Show(msg, "Confirmation:", MessageBoxButtons.YesNo, MessageBoxIcon.Question) If result = MsgBoxResult.No Then UPDATE_CANCELLED = True Exit Sub End If End If If MyServer_UpdatePath <> String.Empty Then If Directory.Exists(MyServer_UpdatePath) Then GetTempFolderGuid() If FOLDER_TEMP <> String.Empty Then ' Make a reference to a directory. Dim di As New DirectoryInfo(MyServer_UpdatePath) ' Get a reference to each file in that directory. Dim fiArr As FileInfo() = di.GetFiles() ' Display the names of the files. 'Dim fri As FileInfo Dim error_while_copying = False For Each Upd_item As DataRow In DT_UPDATE_ITEMS.Rows If error_while_copying = True Then Exit For End If Dim updatefile2copy = Path.Combine(MyServer_UpdatePath, Upd_item.Item("ITEM_INFO")) Dim tempfilename = Path.Combine(FOLDER_TEMP, Upd_item.Item("ITEM_INFO")) If File.Exists(updatefile2copy) Then Try System.IO.File.Copy(updatefile2copy, tempfilename) Catch ex As Exception ClassLogger.Add(String.Format("Error while copying file {0} to {1}: " & ex.Message, updatefile2copy, tempfilename)) error_while_copying = True UPDATE_ERROR = True End Try Else ClassLogger.Add(String.Format("UpdateFile {0} is not existing or accessible", updatefile2copy)) End If Next If error_while_copying = False Then Replace_Files() End If Try 'Delete the tempfolder and all data System.IO.Directory.Delete(FOLDER_TEMP, True) Catch ex As Exception End Try Else ClassLogger.Add(String.Format("The temporaryFolder could not be created!")) End If Else ClassLogger.Add(String.Format("The Updatepath '{0}'is not accessible or does not exist", MyServer_UpdatePath)) End If Else ClassLogger.Add(String.Format("NO ACTION: the Updatepath is empty")) End If End Sub Private Shared Function GetTempFolderGuid() As String Try Dim folder As String = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString) If Not Directory.Exists(folder) Then Directory.CreateDirectory(folder) End If FOLDER_TEMP = folder Catch ex As Exception ClassLogger.Add(String.Format("Error while Creating tempfolder: " & ex.Message)) End Try End Function Private Shared Sub Replace_Files() Try 'Jede Datei im tempfolder wird replaced Dim di As New DirectoryInfo(FOLDER_TEMP) ' Get a reference to each file in that directory. Dim fiArr As FileInfo() = di.GetFiles() ' Display the names of the files. ' Dim fri As FileInfo Dim error_while_replacing = False For Each Upd_item As DataRow In DT_UPDATE_ITEMS.Rows Dim BackUpOfFileToReplace As String = MY_INSTALL_PATH & "\" & Upd_item.Item("INFO1") & Upd_item.Item("ITEM_INFO") & ".bac" Dim sourcefile = Path.Combine(FOLDER_TEMP, Upd_item.Item("ITEM_INFO")) Dim targetfile If IsDBNull(Upd_item.Item("INFO1")) Then targetfile = Path.Combine(MY_INSTALL_PATH, Upd_item.Item("ITEM_INFO")) Else If Upd_item.Item("INFO1") <> String.Empty Then targetfile = Path.Combine(MY_INSTALL_PATH, Upd_item.Item("INFO1"), Upd_item.Item("ITEM_INFO")) Else targetfile = Path.Combine(MY_INSTALL_PATH, Upd_item.Item("ITEM_INFO")) End If End If Try ' Replace the file. If File.Exists(targetfile) Then If File_Rename(targetfile, BackUpOfFileToReplace) = True Then If MoveFile(sourcefile, targetfile) = True Then File_Delete(BackUpOfFileToReplace) Else 'Verschieben hat nicht geklappt also die Backupdatei wieder umbenennen! File_Rename(BackUpOfFileToReplace, targetfile) End If End If Else MoveFile(sourcefile, targetfile) End If Catch ex As Exception ClassLogger.Add(String.Format("Error while copying file {0} to {1}: " & ex.Message, sourcefile, targetfile)) error_while_replacing = True UPDATE_ERROR = True End Try Next Catch ex As Exception ClassLogger.Add(String.Format("Unexpected Error in Replace_Files: {0}", ex.Message)) UPDATE_ERROR = True End Try End Sub Private Shared Function File_Rename(targetfile As String, Renamefilestring As String) Try Renamefilestring = Renamefilestring.Replace("\\", "\") If File.Exists(Renamefilestring) Then If File_Delete(Renamefilestring) = False Then Return False End If End If Rename(targetfile, Renamefilestring) Return True Catch ex As Exception ClassLogger.Add(String.Format("Unexpected Error in File_Rename: " & "targetfile '{0}'" & "Renamefilestring '{1}'" & "ERROR: {2}", targetfile, Renamefilestring, ex.Message)) UPDATE_ERROR = True Return False End Try End Function Private Shared Function File_Delete(deletefile As String) Try File.Delete(deletefile) Return True Catch ex As Exception ClassLogger.Add(String.Format("Unexpected Error in File_Delete: " & vbNewLine & "deletefile '{0}'" & vbNewLine & "ERROR: {2}", deletefile, ex.Message)) UPDATE_ERROR = True Return False End Try End Function Private Shared Function MoveFile(sourcefile As String, targetfile As String) Try File.Move(sourcefile, targetfile) Return True Catch ex As Exception ClassLogger.Add(String.Format("Unexpected Error in MoveFile: sourcefile '{0}', targetfile '{1}' - ERROR: {2}", sourcefile, targetfile, ex.Message)) UPDATE_ERROR = True Return False End Try End Function End Class