Beispiel #1
0
        protected override void OnDelete(MonahrqRegion entity)
        {
            if (entity == null)
            {
                return;
            }

            if (MessageBox.Show(string.Format("Delete Region: {0}", entity.Name), "Delete Region?",
                                MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.No)
            {
                return;
            }

            if (entity.HospitalCount > 0)
            {
                MessageBox.Show(
                    string.Format("Please delete associations with hospitals before deleting region \"{0}\". There are currently {1} hospital(s) associated with this region.", entity.Name, entity.HospitalCount),
                    "Delete Region?", MessageBoxButton.OK, MessageBoxImage.Exclamation, MessageBoxResult.No); return;
            }

            using (var session = DataserviceProvider.SessionFactory.OpenSession())
            {
                using (var trans = session.BeginTransaction())
                {
                    var updateQuery = string.Format(@"UPDATE {0} SET {1}_Id = NULL WHERE {1}_Id = {2} AND IsDeleted = 1", typeof(Hospital).EntityTableName(), entity.GetType().Name, entity.Id);
                    session.CreateSQLQuery(updateQuery).ExecuteUpdate();
                    session.Flush();
                    trans.Commit();
                }
            }

            CollectionItems.Remove(entity);
            base.OnDelete(entity);

            CollectionItems.Refresh();
            MappedCustomRegionToPopulationCount = Service.GetCustomRegionToPopulationMappingCount(ConfigurationService.HospitalRegion.DefaultStates.OfType <string>().ToList());
        }
        /// <summary>
        /// Called when [delete].
        /// </summary>
        /// <param name="entity">The entity.</param>
        protected override async void OnDelete(db.Physician entity)
        {
            if (entity == null)
            {
                return;
            }

            var result =
                MessageBox.Show(@"Are you sure want to delete the data for this physician, """ + entity.Name + @"""?", @"Delete Confirmation", MessageBoxButtons.OKCancel);

            if (result != DialogResult.OK)
            {
                return;
            }

            var       errorOccurred   = false;
            Exception errorException  = null;
            var       progressService = new ProgressService();

            progressService.SetProgress("Deleting physican", 0, false, true);

            //await Task.Delay(500);

            string entityName          = entity.Name;
            var    executedSuccessully = await progressService.Execute(() =>
            {
                using (var session = DataserviceProvider.SessionFactory.OpenSession())
                {
                    using (var trans = session.BeginTransaction())
                    {
                        try
                        {
                            var updateQuery = string.Format(@"UPDATE {0} SET IsDeleted = 1 WHERE Id = {1} AND IsDeleted = 0",
                                                            typeof(db.Physician).EntityTableName(),
                                                            entity.Id);
                            session.CreateSQLQuery(updateQuery).ExecuteUpdate();
                            session.Flush();
                            trans.Commit();
                        }
                        catch
                        {
                            trans.Rollback();
                            throw;
                        }
                    }
                }
            },
                                                                       opResult =>
            {
                if (!opResult.Status && opResult.Exception != null)
                {
                    errorOccurred  = true;
                    errorException = opResult.Exception;
                }
                else
                {
                    errorOccurred = true;
                }
            }, new CancellationToken());

            progressService.SetProgress("Completed", 100, true, false);

            if (errorOccurred && errorException != null)
            {
                Logger.Write(errorException, "Error deleting physician \"{0}\"", entity.Name);
                LogEntityError(errorException, typeof(db.Physician), entityName);
                return;
            }

            CollectionItems.Remove(entity);
            CollectionItems.Refresh();

            Notify(string.Format("{0} {1} has been deleted.", entityName,
                                 Inflector.Titleize(typeof(db.Physician).Name)));
        }