public void Delete <T>(IEnumerable <T> dataset, bool suppressEventing, CascadeDeleteType cascadeDeleteType, bool referencesFromAllScopes) where T : class, IData { Delete(dataset, suppressEventing, cascadeDeleteType, referencesFromAllScopes, new HashSet <DataSourceId>()); }
private void Delete <T>(IEnumerable <T> dataset, bool suppressEventing, CascadeDeleteType cascadeDeleteType, bool referencesFromAllScopes, HashSet <DataSourceId> dataPendingDeletion) where T : class, IData { Verify.ArgumentNotNull(dataset, nameof(dataset)); dataset = dataset.Evaluate(); foreach (var data in dataset) { var dataSourceId = data.DataSourceId; if (!dataPendingDeletion.Contains(dataSourceId)) { dataPendingDeletion.Add(dataSourceId); } } if (cascadeDeleteType != CascadeDeleteType.Disable) { foreach (IData data in dataset) { Verify.ArgumentCondition(data != null, nameof(dataset), "dataset may not contain nulls"); Type interfaceType = data.DataSourceId.InterfaceType; // Not deleting references if the data is versioned and not all of the // versions of the element are to be deleted if (data is IVersioned && interfaceType.GetKeyProperties().Count == 1) { var key = data.GetUniqueKey(); var versions = DataFacade.TryGetDataVersionsByUniqueKey(interfaceType, key).ToList(); if (versions.Count > 1 && (dataset.Count() < versions.Count || !versions.All(v => dataPendingDeletion.Contains(v.DataSourceId)))) { continue; } } using (new DataScope(data.DataSourceId.DataScopeIdentifier)) { var allReferences = DataReferenceFacade.GetRefereesInt(data, referencesFromAllScopes, (a, b) => true); if (allReferences.Count == 0) { continue; } Verify.IsTrue(cascadeDeleteType != CascadeDeleteType.Disallow, "One of the given datas is referenced by one or more datas"); var optionalReferences = allReferences.Where(kvp => kvp.Item2.IsOptionalReference); var notOptionalReferences = allReferences.Where(kvp => !kvp.Item2.IsOptionalReference && !dataPendingDeletion.Contains(kvp.Item1.DataSourceId)).Evaluate(); foreach (var reference in optionalReferences) { var referee = reference.Item1; reference.Item2.SourcePropertyInfo.SetValue(referee, null, null); DataFacade.Update(referee, false, true, false); } foreach (var refereeInfo in notOptionalReferences) { if (!refereeInfo.Item2.AllowCascadeDeletes) { throw new InvalidOperationException("One of the given data items is referenced by one or more data items that do not allow cascade delete."); } } var toDelete = notOptionalReferences.Select(_ => _.Item1); Delete <IData>(toDelete, suppressEventing, cascadeDeleteType, referencesFromAllScopes); } } } Dictionary <string, Dictionary <Type, List <IData> > > sortedDatas = dataset.ToDataProviderAndInterfaceTypeSortedDictionary(); foreach (KeyValuePair <string, Dictionary <Type, List <IData> > > providerPair in sortedDatas) { foreach (KeyValuePair <Type, List <IData> > interfaceTypePair in providerPair.Value) { DataProviderPluginFacade.Delete(providerPair.Key, interfaceTypePair.Value.Select(d => d.DataSourceId)); if (DataCachingFacade.IsTypeCacheable(interfaceTypePair.Key)) { DataCachingFacade.RemoveFromCache(interfaceTypePair.Value); } } } if (!suppressEventing) { foreach (IData element in dataset) { DataEventSystemFacade.FireDataDeletedEvent(element.DataSourceId.InterfaceType, element); } } }
private void Delete <T>(IEnumerable <T> dataset, bool suppressEventing, CascadeDeleteType cascadeDeleteType, bool referencesFromAllScopes, HashSet <DataSourceId> dataPendingDeletion) where T : class, IData { Verify.ArgumentNotNull(dataset, nameof(dataset)); dataset = dataset.Evaluate(); foreach (var data in dataset) { var dataSourceId = data.DataSourceId; if (!dataPendingDeletion.Contains(dataSourceId)) { dataPendingDeletion.Add(dataSourceId); } } if (cascadeDeleteType != CascadeDeleteType.Disable) { foreach (IData element in dataset) { Verify.ArgumentCondition(element != null, nameof(dataset), "dataset may not contain nulls"); if (!element.IsDataReferred()) { continue; } Type interfaceType = element.DataSourceId.InterfaceType; // Not deleting references if the data is versioned and not all of the // versions of the element are to be deleted if (element is IVersioned) { var key = element.GetUniqueKey(); var versions = DataFacade.TryGetDataVersionsByUniqueKey(interfaceType, key).ToList(); if (versions.Count > 1 && (dataset.Count() < versions.Count || !versions.All(v => dataPendingDeletion.Contains(v.DataSourceId)))) { continue; } } Verify.IsTrue(cascadeDeleteType != CascadeDeleteType.Disallow, "One of the given datas is referenced by one or more datas"); element.RemoveOptionalReferences(); IEnumerable <IData> referees; using (new DataScope(element.DataSourceId.DataScopeIdentifier)) { // For some weird reason, this line does not work.... /MRJ // IEnumerable<IData> referees = dataset.GetRefereesRecursively(); referees = element.GetReferees(referencesFromAllScopes) .Where(reference => !dataPendingDeletion.Contains(reference.DataSourceId)) .Evaluate(); } foreach (IData referee in referees) { if (!referee.CascadeDeleteAllowed(interfaceType)) { throw new InvalidOperationException("One of the given datas is referenced by one or more datas that does not allow cascade delete"); } } Delete <IData>(referees, suppressEventing, cascadeDeleteType, referencesFromAllScopes); } } Dictionary <string, Dictionary <Type, List <IData> > > sortedDatas = dataset.ToDataProviderAndInterfaceTypeSortedDictionary(); foreach (KeyValuePair <string, Dictionary <Type, List <IData> > > providerPair in sortedDatas) { foreach (KeyValuePair <Type, List <IData> > interfaceTypePair in providerPair.Value) { DataProviderPluginFacade.Delete(providerPair.Key, interfaceTypePair.Value.Select(d => d.DataSourceId)); if (DataCachingFacade.IsTypeCacheable(interfaceTypePair.Key)) { DataCachingFacade.ClearCache(interfaceTypePair.Key, interfaceTypePair.Value.First().DataSourceId.DataScopeIdentifier); } } } if (!suppressEventing) { foreach (IData element in dataset) { DataEventSystemFacade.FireDataDeletedEvent(element.DataSourceId.InterfaceType, element); } } }