public bool PrepareAndSave()
        {
            var result = false;

            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            _configurationViewModel.CancellationSource = tokenSource;
            var configurationNavigationItem = new NavigationItem(NavigationNames.ConfigurationName, _configurationViewModel);

            // UnRegister previous, if any
            OnUIThread(() => _navigationManager.UnRegisterNavigationItem(configurationNavigationItem));
            _navigationManager.Navigate(configurationNavigationItem);

            // start configuring in tasks
            var taskLocation = Task.Factory.StartNew(() => _projectLocationStepViewModel.Configure(token), token);

            taskLocation.ContinueWith(x => tokenSource.Cancel(), TaskContinuationOptions.OnlyOnFaulted);

            var taskDbAndSearch = Task.Factory.StartNew(() =>
            {
                _databaseSettingsStepViewModel.Configure(token);
                _searchSettingsStepViewModel.Configure(token);
            }, token);

            taskLocation.ContinueWith(x => tokenSource.Cancel(), TaskContinuationOptions.OnlyOnFaulted);

            try
            {
                Task.WaitAll(new[] { taskLocation, taskDbAndSearch });
            }
            catch (AggregateException e)
            {
                // Display information about each exception.
#if (DEBUG)
                foreach (var v in e.InnerExceptions)
                {
                    if (v is TaskCanceledException)
                    {
                        Console.WriteLine("   TaskCanceledException: Task {0}", ((TaskCanceledException)v).Task.Id);
                    }
                    else
                    {
                        Console.WriteLine("   Exception: {0}", v.GetType().Name);
                    }
                }
#endif
            }

            if (tokenSource.IsCancellationRequested)
            {
                var cancelTasks = new[]
                {
                    Task.Factory.StartNew(() => _projectLocationStepViewModel.Cancel()),
                    Task.Factory.StartNew(() => _databaseSettingsStepViewModel.Cancel()),
                    Task.Factory.StartNew(() => _searchSettingsStepViewModel.Cancel())
                };

                try
                {
                    Task.WaitAll(cancelTasks);
                }
                catch (AggregateException)
                {
                }
            }

            if (_projectLocationStepViewModel.Result == OperationResult.Failed ||
                _databaseSettingsStepViewModel.Result == OperationResult.Failed ||
                _searchSettingsStepViewModel.Result == OperationResult.Failed)
            {
                _configurationViewModel.Result = OperationResult.Failed;
            }
            else if (_projectLocationStepViewModel.Result == OperationResult.Cancelled ||
                     _databaseSettingsStepViewModel.Result == OperationResult.Cancelled ||
                     _searchSettingsStepViewModel.Result == OperationResult.Cancelled)
            {
                _configurationViewModel.Result = OperationResult.Cancelled;
            }

            if (_configurationViewModel.Result == OperationResult.Cancelled || _configurationViewModel.Result == OperationResult.Failed)
            {
                _configurationViewModel.Message = _projectLocationStepViewModel.Message + Environment.NewLine +
                                                  _databaseSettingsStepViewModel.Message + Environment.NewLine +
                                                  _searchSettingsStepViewModel.Message;
            }
            else
            {
                // create item
                _item.BrowseUrl          = "Click browse to resolve";
                _item.Name               = _projectLocationStepViewModel.ProjectName;
                _item.Location.Type      = LocationType.FileSystem;
                _item.Location.Url       = _projectLocationStepViewModel.ProjectLocation;
                _item.Location.LocalPath = _projectLocationStepViewModel.ProjectLocation;

                var repo = _projectRepositoryFactory.GetRepositoryInstance();
                repo.Add(_item);
                repo.UnitOfWork.Commit();

                _configurationViewModel.Result = OperationResult.Successful;
                result = true;
            }

            return(result);
        }
Exemple #2
0
        public bool PrepareAndSave()
        {
            var result    = false;
            var allErrors = string.Empty;

            var tokenSource = new CancellationTokenSource();
            var token       = tokenSource.Token;

            _configurationViewModel.CancellationSource = tokenSource;
            var configurationNavigationItem = new NavigationItem(NavigationNames.ConfigurationName, _configurationViewModel);

            // UnRegister previous, if any
            OnUIThread(() => _navigationManager.UnRegisterNavigationItem(configurationNavigationItem));
            _navigationManager.Navigate(configurationNavigationItem);

            // start configuring in tasks
            var taskLocation = Task.Factory.StartNew(() => _projectLocationStepViewModel.Configure(token), token);

            taskLocation.ContinueWith(x => tokenSource.Cancel(), TaskContinuationOptions.OnlyOnFaulted);

            var taskDbAndSearch = Task.Factory.StartNew(() =>
            {
                _databaseSettingsStepViewModel.Configure(token);
                _searchSettingsStepViewModel.Configure(token);
            }, token);

            taskDbAndSearch.ContinueWith(x => tokenSource.Cancel(), TaskContinuationOptions.OnlyOnFaulted);

            try
            {
                Task.WaitAll(new[] { taskLocation, taskDbAndSearch });
            }
            catch (AggregateException e)
            {
                allErrors += GetMessages(e);
            }

            // cancel all steps
            if (tokenSource.IsCancellationRequested)
            {
                var cancelTasks = new[]
                {
                    Task.Factory.StartNew(() => _projectLocationStepViewModel.Cancel()),
                    Task.Factory.StartNew(() => _databaseSettingsStepViewModel.Cancel()),
                    Task.Factory.StartNew(() => _searchSettingsStepViewModel.Cancel())
                };

                try
                {
                    Task.WaitAll(cancelTasks);
                }
                catch (AggregateException e)
                {
                    allErrors += GetMessages(e);
                }
            }

            if (_projectLocationStepViewModel.Result == OperationResult.Failed ||
                _databaseSettingsStepViewModel.Result == OperationResult.Failed ||
                _searchSettingsStepViewModel.Result == OperationResult.Failed)
            {
                _configurationViewModel.Result = OperationResult.Failed;
            }
            else if (_projectLocationStepViewModel.Result == OperationResult.Cancelled ||
                     _databaseSettingsStepViewModel.Result == OperationResult.Cancelled ||
                     _searchSettingsStepViewModel.Result == OperationResult.Cancelled)
            {
                _configurationViewModel.Result = OperationResult.Cancelled;
            }

            if (_configurationViewModel.Result == OperationResult.Cancelled || _configurationViewModel.Result == OperationResult.Failed)
            {
                allErrors += _projectLocationStepViewModel.Message + Environment.NewLine +
                             _databaseSettingsStepViewModel.Message + Environment.NewLine +
                             _searchSettingsStepViewModel.Message;
            }

            if (!string.IsNullOrEmpty(allErrors))
            {
                _configurationViewModel.Message = allErrors;
            }
            else
            {
                // create item
                _item.BrowseUrl          = "Click browse to resolve";
                _item.Name               = _projectLocationStepViewModel.ProjectName;
                _item.Location.Type      = LocationType.FileSystem;
                _item.Location.Url       = _projectLocationStepViewModel.ProjectLocation;
                _item.Location.LocalPath = _projectLocationStepViewModel.ProjectLocation;

                var repo = _projectRepositoryFactory.GetRepositoryInstance();
                repo.Add(_item);
                repo.UnitOfWork.Commit();

                _configurationViewModel.Result = OperationResult.Successful;
                result = true;
            }

            return(result);
        }