/// ------------------------------------------------------------------------------------ /// <summary> /// Checks to see whether this instance is connected to the requested project. If so, /// attempts to do a restore using the specified restore settings. /// </summary> /// <param name="restoreSettings">The restore settings.</param> /// <returns>True if the project belonged to this instance and the restore was /// successful, false otherwise.</returns> /// ------------------------------------------------------------------------------------ public bool HandleRestoreProjectRequest(FwRestoreProjectSettings restoreSettings) { ProjectMatch isMyProject = FieldWorks.GetProjectMatchStatus( new ProjectId(restoreSettings.Settings.FullProjectPath, null)); if (isMyProject != ProjectMatch.ItsMyProject) return false; FieldWorks.HandleRestoreRequest(restoreSettings); return true; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Checks to see whether this instance is connected to the requested project. If so, /// attempts to do a restore using the specified restore settings. /// </summary> /// <param name="restoreSettings">The restore settings.</param> /// <returns>True if the project belonged to this instance and the restore was /// successful, false otherwise.</returns> /// ------------------------------------------------------------------------------------ public bool HandleRestoreProjectRequest(FwRestoreProjectSettings restoreSettings) { ProjectMatch isMyProject = FieldWorks.GetProjectMatchStatus( new ProjectId(restoreSettings.Settings.FullProjectPath)); if (isMyProject != ProjectMatch.ItsMyProject) { return(false); } FieldWorks.HandleRestoreRequest(restoreSettings); return(true); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Attempts to find another FieldWorks process that can handle a restore for the /// specified restore project settings. /// </summary> /// <param name="settings">The restore project settings.</param> /// <returns>True if another process was found and that process handled the restore, /// false otherwise</returns> /// ------------------------------------------------------------------------------------ private static bool TryFindRestoreHandler(FwRestoreProjectSettings settings) { return RunOnRemoteClients(kFwRemoteRequest, requestor => { // ENHANCE (TimS): We might want to do similar logic to TryFindExistingProcess to // wait for projects that are starting up. return requestor.HandleRestoreProjectRequest(settings); }); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Backs up the project that is about to be restored. /// </summary> /// <param name="restoreSettings">The restore settings.</param> /// <param name="existingCache">The existing cache for the project to backup, or null /// to create a new cache for the project defined in the restore settings.</param> /// <param name="dialogOwner">A form that can be used as an owner for progress dialog/ /// message box.</param> /// <returns><c>true</c> to indicate that it is okay to proceed with the restore; /// <c>false</c> to indicate that the backup failed and the user wasn't comfortable /// with just blindly going ahead with a restore that could potentially leave him in /// tears.</returns> /// ------------------------------------------------------------------------------------ private static bool BackupProjectForRestore(FwRestoreProjectSettings restoreSettings, FdoCache existingCache, Form dialogOwner) { using (var progressDlg = new ProgressDialogWithTask(dialogOwner)) { FdoCache cache = existingCache ?? FdoCache.CreateCacheFromExistingData( new ProjectId(restoreSettings.Settings.FullProjectPath, null), s_sWsUser, s_ui, FwDirectoryFinder.FdoDirectories, CreateFdoSettings(), progressDlg); try { var backupSettings = new BackupProjectSettings(cache, restoreSettings.Settings, FwDirectoryFinder.DefaultBackupDirectory); backupSettings.DestinationFolder = FwDirectoryFinder.DefaultBackupDirectory; backupSettings.AppAbbrev = restoreSettings.FwAppCommandLineAbbrev; var backupService = new ProjectBackupService(cache, backupSettings); string backupFile; if (!backupService.BackupProject(progressDlg, out backupFile)) { string msg = string.Format(FwCoreDlgs.FwCoreDlgs.ksCouldNotBackupSomeFiles, backupService.FailedFiles.ToString(", ", Path.GetFileName)); if (MessageBox.Show(msg, FwCoreDlgs.FwCoreDlgs.ksWarning, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) File.Delete(backupFile); } } catch (FwBackupException e) { if (MessageBox.Show(dialogOwner, string.Format(FwCoreDlgs.FwCoreDlgs.ksBackupErrorCreatingZipfile, e.ProjectName, e.Message) + Environment.NewLine + Environment.NewLine + Properties.Resources.ksBackupErrorDuringRestore, FwCoreDlgs.FwCoreDlgs.ksBackupErrorCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No) { return false; } } finally { if (existingCache == null) // We created a new cache so we need to dispose of it cache.Dispose(); } } return true; }
/// ------------------------------------------------------------------------------------ /// <summary> /// Restores from the backup specified in restoreSettings, replacing the current version /// of this project (requires restarting apps and the cache). /// </summary> /// <param name="restoreSettings">The restore settings.</param> /// <param name="dialogOwner">A form that can be used as an owner for progress dialog/ /// message box.</param> /// ------------------------------------------------------------------------------------ private static void RestoreCurrentProject(FwRestoreProjectSettings restoreSettings, Form dialogOwner) { // When we get here we can safely do the backup of the project because we either // have no cache (and no other process has this project open), or we are the // process that has this project open. (FWR-3344) if (restoreSettings.Settings.BackupOfExistingProjectRequested && !BackupProjectForRestore(restoreSettings, Cache, dialogOwner)) { return; } ExecuteWithAppsShutDown(restoreSettings.FwAppCommandLineAbbrev, () => { bool retry; do { retry = false; try { var restoreService = new ProjectRestoreService(restoreSettings.Settings, s_ui, FwDirectoryFinder.ConverterConsoleExe, FwDirectoryFinder.DbExe); Logger.WriteEvent("Restoring from " + restoreSettings.Settings.Backup.File); if (RestoreProjectDlg.HandleRestoreFileErrors(null, ResourceHelper.GetResourceString("ksRestoreFailed"), restoreSettings.Settings.Backup.File, () => DoRestore(restoreService))) { s_LinkDirChangedTo = restoreService.LinkDirChangedTo; return s_projectId ?? new ProjectId(ClientServerServices.Current.Local.IdForLocalProject(restoreSettings.Settings.ProjectName), null); } } catch (CannotConvertException e) { MessageBoxUtils.Show(e.Message, ResourceHelper.GetResourceString("ksRestoreFailed")); } catch (MissingOldFwException e) { using (var dlg = new MissingOldFieldWorksDlg(restoreSettings.Settings, e.HaveOldFieldWorks, e.HaveFwSqlServer)) { retry = (dlg.ShowDialog() == DialogResult.Retry); } } catch (FailedFwRestoreException e) { MessageBoxUtils.Show(Properties.Resources.ksRestoringOldFwBackupFailed, Properties.Resources.ksFailed); } } while (retry); return null; }); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Handles a request to restore a project. This method is thread safe. /// </summary> /// <param name="dialogOwner">A form that can be used as an owner for progress dialog/ /// message box.</param> /// <param name="restoreSettings">The restore arguments.</param> /// ------------------------------------------------------------------------------------ internal static void HandleRestoreRequest(Form dialogOwner, FwRestoreProjectSettings restoreSettings) { s_threadHelper.Invoke(() => { // Determine if we need to start a new process for the restore if (s_projectId != null && s_projectId.IsSameLocalProject(new ProjectId(restoreSettings.Settings.FullProjectPath, null))) { // We need to invoke so that the mediator that processed the restore menu item // can be safely disposed of (and everything that it holds on to can be released). // If we don't do this, the memory held in the IdentityMap will stay around because // of remaining references. s_threadHelper.InvokeAsync(RestoreCurrentProject, restoreSettings, dialogOwner); } else if (!TryFindRestoreHandler(restoreSettings)) { if (s_projectId == null) { // No other FieldWorks process was running that could handle the request. // However, we don't know what project we are opening yet, so just use the // restored project as our project. (this happens if restoring from the // command-line) RestoreCurrentProject(restoreSettings, dialogOwner); } else { // No other FieldWorks process was running that could handle the request, so // start a brand new process for the project. // Since we know that no other process are running on this project, we can // safely do a backup using a new cache. (FWR-3344) if (restoreSettings.Settings.BackupOfExistingProjectRequested && !BackupProjectForRestore(restoreSettings, null, dialogOwner)) { return; } RestoreProjectSettings settings = restoreSettings.Settings; // REVIEW: it might look strange to dispose the return value of OpenProjectWithNewProcess. // However, that is a Process that gets started, and it is ok to dispose that // right away if we don't work with the process object. It might be better // though to change the signature of OpenProjectWithNewProcess to return // a boolean. using (OpenProjectWithNewProcess((string)null, settings.ProjectName, null, restoreSettings.FwAppCommandLineAbbrev, "-" + FwAppArgs.kRestoreFile, settings.Backup.File, "-" + FwAppArgs.kRestoreOptions, settings.CommandLineOptions)) { } } } }); }
/// ------------------------------------------------------------------------------------ /// <summary> /// Handles a request to restore a project. This method is thread safe. /// </summary> /// <param name="restoreSettings">The restore arguments.</param> /// ------------------------------------------------------------------------------------ internal static void HandleRestoreRequest(FwRestoreProjectSettings restoreSettings) { HandleRestoreRequest(s_activeMainWnd as Form, restoreSettings); }