public void Delete(IValueSet valueSet) { if (!this.valueSetUpdateValidationPolicy.CanBeDeleted(valueSet)) { var statusEx = new ValueSetOperationException($"Could not delete ValueSet with ID {valueSet.ValueSetGuid}. ValueSet status must be `{ValueSetStatus.Draft.ToString()}`, found: {valueSet.StatusCode.ToString()}"); this.logger.Error(statusEx, "Failed to delete custom ValueSet."); throw statusEx; } using (var transaction = this.uowManager.Context.Database.BeginTransaction()) { try { this.uowManager.Context.BulkDelete( new[] { typeof(ValueSetDescriptionBaseDto), typeof(ValueSetCodeDto), typeof(ValueSetCodeCountDto) }, new Dictionary <string, object> { { nameof(ValueSetDescriptionBaseDto.ValueSetGUID), valueSet.ValueSetGuid } }); transaction.Commit(); this.cacheManager.Clear(valueSet.ValueSetGuid); } catch (Exception ex) { var operationException = new ValueSetOperationException( $"Failed to delete custom ValueSet with ID {valueSet.ValueSetGuid}", ex); this.logger.Error(operationException, "Failed to delete custom ValueSet"); throw operationException; } } }
private IReadOnlyCollection <IValueSetSummary> BuildValueSetSummaries( IReadOnlyCollection <IValueSetBackingItem> backingItems, IDictionary <Guid, IReadOnlyCollection <IValueSetCodeCount> > countsDictionary) { var valueSets = backingItems.SelectMany( backingItem => countsDictionary.GetMaybe(backingItem.ValueSetGuid) .Select(counts => new ValueSetSummary(backingItem, counts))) .ToList(); if (valueSets.Count == backingItems.Count) { return(valueSets); } var vsex = new ValueSetOperationException( "Failed to find ValueSetCodeCounts collection for every ValueSetBackingItem (ValueSetDescription)"); this.logger.Error(vsex, "Failed to match codes with every valueset."); throw vsex; }
public void Delete(IValueSet valueSet) { if (!valueSet.IsCustom) { throw new ValueSetOperationException("Only custom value sets may be deleted."); } var valueSetDto = this.ClientTermContext.ValueSetDescriptions.Find(valueSet.ValueSetUniqueId); if (valueSetDto == null) { throw new ValueSetNotFoundException($"ValueSet with UniqueID {valueSet.ValueSetUniqueId} was not found."); } var codes = this.ClientTermContext.ValueSetCodes.Where(code => code.ValueSetUniqueID == valueSetDto.ValueSetUniqueID); using (var transaction = this.ClientTermContext.Database.BeginTransaction()) { try { this.ClientTermContext.ValueSetCodes.RemoveRange(codes); this.ClientTermContext.ValueSetDescriptions.Remove(valueSetDto); this.ClientTermContext.SaveChanges(); transaction.Commit(); this.Cache.ClearItem(CacheKeys.ValueSetKey(valueSet.ValueSetId)); // FYI - brittle point here as we may (albeit unlikely) have cached valuesets with codesystem filters applied - thus still stored in cache. // Current caching mechanism does not allow for searching keys for key pattern so we have to rely on // the cache expiration (default 5 mins). } catch (Exception ex) { var operationException = new ValueSetOperationException($"Failed to delete custom ValueSet with ID {valueSet.ValueSetUniqueId}", ex); this.Logger.Error(operationException, "Failed to delete custom ValueSet"); throw operationException; } } }
public void SaveAsNew(IValueSet valueSet) { var attempt = this.clientTermValueSetRepository.Add(valueSet); if (attempt.Success && attempt.Result != null) { Saved?.Invoke(this, attempt.Result); return; } if (attempt.Exception == null) { var vsex = new ValueSetOperationException( "An exception was not returned by the attempt to save a ValueSet but the save failed.", attempt.Exception); this.logger.Error( vsex, "An exception was not returned by the attempt to save a ValueSet but the save failed."); throw vsex; } throw attempt.Exception; }
public void Commit() { var executable = this.operations.Where(op => op.OperationType != OperationType.None).ToList(); var expectedOperations = executable.Count(); this.BatchLoad(new Queue <Operation>(executable)); var ctx = this.manager.Context; ctx.ChangeTracker.AutoDetectChangesEnabled = false; using (var transaction = ctx.Database.BeginTransaction()) { try { var changes = ctx.SaveChanges(); if (changes != expectedOperations) { var ex = new ValueSetOperationException( $"When saving a ValueSet, we expected {expectedOperations} changes, but was told there were {changes} changes"); throw ex; } transaction.Commit(); } catch (Exception ex) { ctx.ChangeTracker.AutoDetectChangesEnabled = true; this.manager.logger.Error(ex, $"Failed to commit unit of work transaction. Rolling back. Expected operations {expectedOperations}"); transaction.Rollback(); throw; } finally { ctx.ChangeTracker.AutoDetectChangesEnabled = true; } } }