private static DataBaseBackupFileInfo GetSelectRecordFileName(Window pSourceWindow) { DataBaseBackupFileInfo resultFileInfo = new DataBaseBackupFileInfo(); try { CriteriaOperator criteriaOperator = CriteriaOperator.Parse(string.Format("DataBaseType = '{0}' && FileName IS NOT NULL", GlobalFramework.DatabaseType)); PosSelectRecordDialog <XPCollection, XPGuidObject, TreeViewSystemBackup> dialogSystemBackup = new PosSelectRecordDialog <XPCollection, XPGuidObject, TreeViewSystemBackup>( pSourceWindow, Gtk.DialogFlags.DestroyWithParent, resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "window_title_select_backup_filename"), new Size(780, 580), null, //XpoDefaultValue criteriaOperator, GenericTreeViewMode.Default, null //ActionAreaButtons ); ResponseType response = (ResponseType)dialogSystemBackup.Run(); if (response == ResponseType.Ok) { //Assign Result resultFileInfo.Response = response; sys_systembackup systemBackup = (sys_systembackup)dialogSystemBackup.GenericTreeView.DataSourceRow; if (systemBackup != null) { if (GlobalFramework.DatabaseType == DatabaseType.MSSqlServer) { resultFileInfo.FileName = systemBackup.FileName; resultFileInfo.FileHashValid = true; } else { resultFileInfo.FileName = FrameworkUtils.OSSlash(string.Format(@"{0}{1}", _pathBackups, systemBackup.FileName)); resultFileInfo.FileNamePacked = FrameworkUtils.OSSlash(string.Format(@"{0}{1}", _pathBackups, systemBackup.FileNamePacked)); resultFileInfo.FileHashDB = systemBackup.FileHash; resultFileInfo.FileHashFile = FrameworkUtils.MD5HashFile(resultFileInfo.FileNamePacked); } } } dialogSystemBackup.Destroy(); } catch (Exception ex) { _log.Error(ex.Message, ex); } return(resultFileInfo); }
public static bool Restore(Window pSourceWindow, DataBaseRestoreFrom pRestoreFrom) { try { //FrameworkUtils.ShowWaitingCursor(); Init(); bool restoreResult = false; string sql = string.Empty; string fileName = string.Empty; string fileNamePacked = string.Empty; //default pathBackups from Settings, can be Overrided in ChooseFromFilePickerDialog Mode string pathBackups = _pathBackups; DataBaseBackupFileInfo fileInfo = null; Guid systemBackupGuid = Guid.Empty; //Required to assign current FileName and FileNamePacked after restore, else name will be the TempName ex acegvpls.soj & n2sjiamk.32o sys_systembackup systemBackup = null; string currentFileName = string.Empty, currentFileNamePacked = string.Empty, currentFilePath = string.Empty, currentFileHash = string.Empty; sys_userdetail currentUserDetail = null; switch (pRestoreFrom) { case DataBaseRestoreFrom.SystemBackup: fileInfo = GetSelectRecordFileName(pSourceWindow); //Equal to Filename not FileNamePacked fileNamePacked = fileInfo.FileNamePacked; if (_debug) { _log.Debug(string.Format("RestoreBackup: FileNamePacked:[{0}], FileHashDB:[{1}], FileHashFile:[{2}] FileHashValid:[{3}]", fileInfo.FileNamePacked, fileInfo.FileHashDB, fileInfo.FileHashFile, fileInfo.FileHashValid)); } if (fileInfo.Response != ResponseType.Cancel && !fileInfo.FileHashValid) { //#EQUAL#1 string message = string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "dialog_message_database_restore_error_invalid_backup_file"), fileNamePacked); Utils.ShowMessageTouch(pSourceWindow, DialogFlags.Modal, new Size(600, 300), MessageType.Error, ButtonsType.Ok, resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "global_error"), message); return(false); } break; case DataBaseRestoreFrom.ChooseFromFilePickerDialog: FileFilter fileFilterBackups = Utils.GetFileFilterBackups(); PosFilePickerDialog dialog = new PosFilePickerDialog(pSourceWindow, DialogFlags.DestroyWithParent, fileFilterBackups, FileChooserAction.Open); ResponseType response = (ResponseType)dialog.Run(); if (response == ResponseType.Ok) { fileNamePacked = dialog.FilePicker.Filename; //Override Default pathBackups pathBackups = string.Format("{0}/", Path.GetDirectoryName(fileNamePacked)); dialog.Destroy(); } else { /* IN009164 */ dialog.Destroy(); return(false); } break; default: break; } if (GlobalFramework.DatabaseType != DatabaseType.MSSqlServer) { fileName = Path.ChangeExtension(fileNamePacked, _fileExtension); } else { //Require to assign filename and packed filename from fileInfo fileName = fileInfo.FileName; fileNamePacked = fileInfo.FileName; } if (fileName != string.Empty) { if (_debug) { _log.Debug(string.Format("Restore Filename:[{0}] to pathBackups[{1}]", fileNamePacked, pathBackups)); } if (GlobalFramework.DatabaseType != DatabaseType.MSSqlServer) { // Old Method before PluginSoftwareVendor Implementation //restoreResult = Utils.ZipUnPack(fileNamePacked, pathBackups, true); restoreResult = GlobalFramework.PluginSoftwareVendor.RestoreBackup(SettingsApp.SecretKey, fileNamePacked, pathBackups, true); if (_debug) { _log.Debug(string.Format("RestoreBackup: unpackResult:[{0}]", restoreResult)); } } if (restoreResult || GlobalFramework.DatabaseType == DatabaseType.MSSqlServer) { //Get properties from SystemBackup Object before Restore, to Assign Properties after Restore (FilePath,FileHash,User,Terminal) sql = string.Format("SELECT Oid FROM sys_systembackup WHERE fileNamePacked = '{0}';", Path.GetFileName(fileNamePacked)); systemBackupGuid = FrameworkUtils.GetGuidFromQuery(sql); if (systemBackupGuid != Guid.Empty) { systemBackup = (sys_systembackup)FrameworkUtils.GetXPGuidObject(GlobalFramework.SessionXpo, typeof(sys_systembackup), systemBackupGuid); currentFileName = systemBackup.FileName; currentFileNamePacked = systemBackup.FileNamePacked; currentFilePath = systemBackup.FilePath; currentFileHash = systemBackup.FileHash; currentUserDetail = systemBackup.User; } //Send fileNamePacked only to show its name in success dialog if (Restore(pSourceWindow, fileName, fileNamePacked, systemBackup)) { //Audit DATABASE_RESTORE FrameworkUtils.Audit("DATABASE_RESTORE", string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "audit_message_database_restore"), fileNamePacked)); //Required to DropIdentity before get currentDocumentFinanceYear Object, else it exists in old non restored Session GlobalFramework.SessionXpo.DropIdentityMap(); //Get Current Active FinanceYear fin_documentfinanceyears currentDocumentFinanceYear = ProcessFinanceDocumentSeries.GetCurrentDocumentFinanceYear(); //Disable all Active FinanceYear Series and SeriesTerminal if Exists if (currentDocumentFinanceYear != null) { ProcessFinanceDocumentSeries.DisableActiveYearSeriesAndTerminalSeries(currentDocumentFinanceYear); } //Restore SystemBackup properties else it keeps temp names after Restore acegvpls.soj & n2sjiamk.32o, empty hash etc if (systemBackupGuid != Guid.Empty) { //ReFresh UserDetail from Repository currentUserDetail = (currentUserDetail != null) ? (sys_userdetail)FrameworkUtils.GetXPGuidObject(GlobalFramework.SessionXpo, typeof(sys_userdetail), currentUserDetail.Oid) : null; //Get Current Restored systemBackup Object systemBackup = (sys_systembackup)FrameworkUtils.GetXPGuidObject(GlobalFramework.SessionXpo, typeof(sys_systembackup), systemBackupGuid); systemBackup.FileName = currentFileName; systemBackup.FileNamePacked = currentFileNamePacked; systemBackup.FilePath = currentFilePath; systemBackup.FileHash = currentFileHash; systemBackup.User = currentUserDetail; systemBackup.Save(); } //If Cant get Record, because works on ChooseFromFilePickerDialog, we must recreate Record from file, only in the case of record with miss fileNamePacked else { sql = "DELETE FROM sys_systembackup WHERE FilePath IS NULL AND FileHash IS NULL AND User IS NULL;"; GlobalFramework.SessionXpo.ExecuteNonQuery(sql); } //Audit FrameworkUtils.Audit("APP_CLOSE"); //Call QuitWithoutConfirmation without Audit LogicPos.QuitWithoutConfirmation(false); return(true); } else { return(false); } } else { //#EQUAL#1 string message = string.Format(resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "dialog_message_database_restore_error_invalid_backup_file"), fileNamePacked); Utils.ShowMessageTouch(pSourceWindow, DialogFlags.Modal, new Size(600, 300), MessageType.Error, ButtonsType.Ok, resources.CustomResources.GetCustomResources(GlobalFramework.Settings["customCultureResourceDefinition"], "global_error"), message); return(false); } } else { return(false); } } catch (Exception ex) { _log.Error(ex.Message, ex); return(false); } finally { //FrameworkUtils.HideWaitingCursor(); } }