Beispiel #1
0
        public Attempt <IValueSet> Add(IValueSet valueSet)
        {
            if (!EnsureIsNew(valueSet))
            {
                var invalid = new InvalidOperationException("Cannot save an existing value set as a new value set.");
                return(Attempt <IValueSet> .Failed(invalid));
            }

            var valueSetGuid = valueSet.SetIdsForCustomInsert();

            var descCountOps =
                CreateOperation(new ValueSetDescriptionBaseDto(valueSet), OperationType.Create)
                .AppendOperationBatch(
                    valueSet.CodeCounts.Select(count => new ValueSetCodeCountDto(count)),
                    OperationType.Create);

            // Insert ValueSetDescriptionBASE and ValueSetCodeCountBASE
            var uowDescCount = this.uowManager.CreateUnitOfWork(descCountOps);

            uowDescCount.Commit();

            // Bulk Insert ValueSetCodeBASE
            var codesDtos = valueSet.ValueSetCodes.Select(code => new ValueSetCodeDto(code)).ToList();
            var uowCounts = this.uowManager.CreateBulkCopyUnitOfWork(codesDtos);

            uowCounts.Commit();

            // Get the updated ValueSet
            var added = this.GetValueSet(valueSetGuid);

            return(added.Select(Attempt <IValueSet> .Successful)
                   .Else(
                       () => Attempt <IValueSet> .Failed(
                           new ValueSetNotFoundException($"Could not retrieve newly saved ValueSet {valueSetGuid}"))));
        }
Beispiel #2
0
        public Attempt <IValueSet> Add(IValueSet valueSet)
        {
            if (!valueSet.IsCustom)
            {
                return(Attempt <IValueSet> .Failed(new ValueSetOperationException("Only custom Value Sets may be created or updated.")));
            }

            valueSet.SetIdsForCustomInsert();

            var valueSetDto = valueSet.AsDto();
            var codeDtos    = valueSet.ValueSetCodes.Select(code => code.AsDto());

            this.ClientTermContext.ChangeTracker.AutoDetectChangesEnabled = false;
            using (var transaction = this.ClientTermContext.Database.BeginTransaction())
            {
                try
                {
                    this.ClientTermContext.ValueSetDescriptions.Add(valueSetDto);
                    this.ClientTermContext.ValueSetCodes.AddRange(codeDtos);
                    this.ClientTermContext.SaveChanges();

                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    this.Logger.Error(ex, "Failed to save a custom ValueSet");
                    this.ClientTermContext.ChangeTracker.AutoDetectChangesEnabled = true;
                    return(Attempt <IValueSet> .Failed(
                               new ValueSetOperationException("Failed to save a custom ValueSet", ex),
                               valueSet));
                }
                finally
                {
                    this.ClientTermContext.ChangeTracker.AutoDetectChangesEnabled = true;
                }
            }

            // Get the updated ValueSet
            // TODO remove IsTest parameter
            var added = !this.IsTest ?
                        this.GetValueSet(valueSetDto.ValueSetUniqueID, Enumerable.Empty <string>()) :
                        this.GetCustomValueSet(valueSetDto.ValueSetUniqueID);

            return(!added.HasValue ?
                   Attempt <IValueSet> .Failed(new ValueSetNotFoundException("Could not retrieved newly saved ValueSet")) :
                   Attempt <IValueSet> .Successful(added.Single()));
        }