/// <summary> /// Deletes the item. /// </summary> /// <param name="entry">The entry.</param> /// <param name="showDeletePrompt">if set to <c>true</c> [show delete prompt].</param> private void DeleteItem(Dataset entry, bool showDeletePrompt = true) { try { if (TargetLookup == null || !TargetLookup.Any()) { return; } if (TargetLookup[entry.ContentType.Name] == null) { return; } if (!TargetLookup[entry.ContentType.Name].IsCustom && string.IsNullOrEmpty(TargetLookup[entry.ContentType.Name].ClrType)) { return; } if (showDeletePrompt) { var attachedWebsites = Service.GetWebsitesForDataset(entry.Id); if (attachedWebsites.Any()) { string websiteNames = string.Join(",", attachedWebsites); var warningMessage = string.Format("Unable to delete dataset \"{0}\" because it is associated with the following websites:", entry.Name); warningMessage += string.Format("{0}{0}Associated Websites: {1}", Environment.NewLine, websiteNames); MessageBox.Show(warningMessage, "Dataset Deletion Warning", MessageBoxButton.OK); return; } // Show blanket warning against dataset deletion. But only show warning if this Dataset is Finished. if (entry.IsFinished) { var warningMessage = string.Format("Dataset \"{0}\" will be deleted. Are you sure you want to delete this data set?", entry.Name); var result = MessageBox.Show(warningMessage, "Dataset Deletion Warning", MessageBoxButton.YesNo); if (result == MessageBoxResult.No || result == MessageBoxResult.Cancel || result == MessageBoxResult.None) { return; } } } if (DataTypesList == null) { return; } foreach (var dataTypeModel in DataTypesList.Where(dataTypeModel => dataTypeModel.RecordsList.Any(ds => ds.Entry.Id == entry.Id)).ToList()) { dataTypeModel.RecordsList.ToList().RemoveAll(ds => ds.Entry.Id != entry.Id); } if (SelectedDataType != null) { var item = SelectedDataType.RecordsList.FirstOrDefault(model => model.Entry.Id == entry.Id); if (item != null) { SelectedDataType.RecordsList.Remove(item); } } using (var session = SessionProvider.SessionFactory.OpenSession()) { using (var trans = session.BeginTransaction()) { session.Evict(entry); var target = TargetLookup[entry.ContentType.Name]; #region Delete queries string targetDeleteQuery; var tableName = target.IsCustom ? entry.ContentType.DbSchemaName : entry.ContentType.ClrType != null?Type.GetType(entry.ContentType.ClrType).EntityTableName() : string.Empty; if (string.IsNullOrEmpty(tableName)) { return; } //Disable Dataset Constraint //var disableConstraint = string.Format("ALTER TABLE {0} NOCHECK CONSTRAINT FK_TARGETS_{1}_DATASETS", tableName, tableName.Replace("Targets_", "")); var disableConstraint = string.Format("ALTER TABLE {0} NOCHECK CONSTRAINT ALL;", tableName /*, tableName.Replace("Targets_", "")*/); session.CreateSQLQuery(disableConstraint) .SetTimeout(25000) .ExecuteUpdate(); // Delete Transactional record var transactionDelete = string.Format("delete from DatasetTransactionRecord ct where ct.Dataset.Id = {0}", entry.Id); session.CreateQuery(transactionDelete) .SetTimeout(25000) .ExecuteUpdate(); //Delete websites with no website_id but have reference to this dataset being deleted var orphanWesbite = string.Format("delete from Websites_WebsiteDatasets where Website_Id IS NULL and Dataset_Id = {0}", entry.Id); session.CreateSQLQuery(orphanWesbite) .SetTimeout(25000) .ExecuteUpdate(); // finally delete content item record. var importDelete = string.Format("delete from Dataset c where c.Id = {0}", entry.Id); session.CreateQuery(importDelete) .SetTimeout(25000) .ExecuteUpdate(); #endregion if (TargetDeletionVisitors != null) { TargetDeletionVisitors = TargetDeletionVisitors.OrderBy(v => v.Order).ToList(); var options = new VisitorOptions() { DataProvider = this.SessionProvider, Logger = this.Logger }; var factory = new TaskFactory(new MonahrqTaskScheduler(2)); var token = new CancellationTokenSource(); Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => ListExtensions.ForEach(TargetDeletionVisitors, visitor => factory.StartNew(() => entry.Accept(visitor, options), token.Token)))); } trans.Commit(); } } } catch (Exception ex) { Events.GetEvent <ErrorNotificationEvent>().Publish(ex); } }