Exemplo n.º 1
0
        /// <inheritdoc/>
        public async Task DeleteAsync(
            SourceFormat sourceFormat,
            CancellationToken cancellationToken = default)
        {
            try
            {
                Check.IsNotNull(sourceFormat);
                await _masterDataValidators.SourceFormatValidator.ValidateAsync(sourceFormat, o =>
                {
                    o.IncludeRuleSets(SourceFormatValidatorRulesets.Delete);
                    o.ThrowOnFailures();
                }, cancellationToken).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    SourceFormat result = await ctx.SourceFormats
                                          .Include(p => p.DimensionStructureNodes)
                                          .Include(pp => pp.SourceFormatDimensionStructureNode)
                                          .FirstOrDefaultAsync(
                        w => w.Id == sourceFormat.Id,
                        cancellationToken
                        )
                                          .ConfigureAwait(false);

                    if (result is null)
                    {
                        string msg = $"There is no {nameof(SourceFormat)} with id: {sourceFormat.Id}";
                        throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                    }

                    if (result.DimensionStructureNodes.Any())
                    {
                        foreach (DimensionStructureNode node in result.DimensionStructureNodes)
                        {
                            ctx.Entry(node).State = EntityState.Deleted;
                        }

                        await ctx.SaveChangesAsync(cancellationToken)
                        .ConfigureAwait(false);
                    }

                    if (result.SourceFormatDimensionStructureNode is not null)
                    {
                        ctx.Entry(result.SourceFormatDimensionStructureNode).State = EntityState.Deleted;
                        await ctx.SaveChangesAsync(cancellationToken)
                        .ConfigureAwait(false);
                    }

                    ctx.Entry(result).State = EntityState.Deleted;
                    await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                             $"{nameof(DeleteAsync)} operation failed! " +
                             "For further information see inner exception!";
                throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
            }
        }
        /// <inheritdoc/>
        public async Task <DimensionStructureNode> CreateDimensionStructureNodeAsync(
            DimensionStructureNode node,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
            {
                try
                {
                    Check.IsNotNull(node);
                    await _masterDataValidators.DimensionStructureNodeValidator.ValidateAsync(node, o =>
                    {
                        o.IncludeRuleSets(SourceFormatValidatorRulesets.CreateDimensionStructureNode);
                        o.ThrowOnFailures();
                    }, cancellationToken)
                    .ConfigureAwait(false);

                    await ctx.DimensionStructureNodes.AddAsync(node, cancellationToken).ConfigureAwait(false);

                    await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                    return(node);
                }
                catch (Exception e)
                {
                    string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                                 $"${nameof(CreateDimensionStructureNodeAsync)} failed. " +
                                 $"For further info see inner exception.";
                    throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                }
            }
        }
Exemplo n.º 3
0
        public async Task <Dimension> AddDimensionAsync(Dimension dimension)
        {
            try
            {
                Check.IsNotNull(dimension);

                await _masterDataValidators.DimensionValidator.ValidateAsync(dimension, o =>
                {
                    o.IncludeRuleSets(ValidatorRulesets.AddNewDimension);
                    o.ThrowOnFailures();
                }).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    await ctx.Dimensions.AddAsync(dimension)
                    .ConfigureAwait(false);

                    await ctx.SaveChangesAsync().ConfigureAwait(false);

                    return(dimension);
                }
            }
            catch (Exception e)
            {
                throw new MasterDataBusinessLogicAddDimensionAsyncOperationException(e.Message, e);
            }
        }
Exemplo n.º 4
0
        /// <inheritdoc/>
        public async Task <SourceFormat> AddAsync(
            SourceFormat sourceFormat,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
            {
                try
                {
                    Check.IsNotNull(sourceFormat);

                    await _masterDataValidators.SourceFormatValidator.ValidateAsync(sourceFormat, o =>
                    {
                        o.IncludeRuleSets(SourceFormatValidatorRulesets.Add);
                        o.ThrowOnFailures();
                    }, cancellationToken).ConfigureAwait(false);

                    await ctx.SourceFormats
                    .AddAsync(sourceFormat, cancellationToken)
                    .ConfigureAwait(false);

                    await ctx.SaveChangesAsync(cancellationToken)
                    .ConfigureAwait(false);

                    return(sourceFormat);
                }
                catch (Exception e)
                {
                    string msg = $"Operation failed: {nameof(AddAsync)}. " +
                                 $"For further details see inner exception.";
                    throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
                }
            }
        }
Exemplo n.º 5
0
        /// <inheritdoc/>
        public async Task DeleteAsync(
            DimensionStructure tobeDeleted,
            CancellationToken cancellationToken = default)
        {
            try
            {
                Check.IsNotNull(tobeDeleted);
                await _masterDataValidators.DimensionStructureValidator.ValidateAsync(tobeDeleted, o =>
                {
                    o.IncludeRuleSets(DimensionStructureValidatorRulesets.Delete);
                    o.ThrowOnFailures();
                }, cancellationToken).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    DimensionStructure res = await ctx.DimensionStructures
                                             .FirstOrDefaultAsync(w => w.Id == tobeDeleted.Id, cancellationToken).ConfigureAwait(false);

                    if (res != null)
                    {
                        ctx.DimensionStructures.Remove(res);
                        await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataDimensionStructureBusinessLogic)}." +
                             $"{nameof(DeleteAsync)} operation failed. " +
                             $"For further details see inner exception!";
                throw new MasterDataBusinessLogicDimensionStructureDatabaseOperationException(msg, e);
            }
        }
        public async Task DeleteDimensionAsync(Dimension dimension)
        {
            try
            {
                Check.IsNotNull(dimension);
                await _masterDataValidators.DimensionValidator.ValidateAsync(dimension, o =>
                {
                    o.IncludeRuleSets(ValidatorRulesets.DeleteDimension);
                    o.ThrowOnFailures();
                }).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    Dimension toBeDeleted = await ctx.Dimensions.FindAsync(dimension.Id)
                                            .ConfigureAwait(false);

                    if (toBeDeleted == null)
                    {
                        string msg = $"There is no {nameof(Dimension)} " +
                                     $"with id: {dimension}.";
                        throw new MasterDataBusinessLogicNoSuchDimensionEntity(msg);
                    }

                    ctx.Dimensions.Remove(toBeDeleted);
                    await ctx.SaveChangesAsync().ConfigureAwait(false);
                }
            }
            catch (Exception e)
            {
                throw new MasterDataBusinessLogicDeleteDimensionAsyncOperationException(e.Message, e);
            }
        }
Exemplo n.º 7
0
        /// <inheritdoc/>
        public async Task <SourceFormat> UpdateAsync(
            SourceFormat sourceFormat,
            CancellationToken cancellationToken = default)
        {
            try
            {
                Check.IsNotNull(sourceFormat);

                await _masterDataValidators.SourceFormatValidator.ValidateAsync(sourceFormat, o =>
                {
                    o.IncludeProperties(SourceFormatValidatorRulesets.Update);
                    o.ThrowOnFailures();
                }, cancellationToken).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    SourceFormat target = await ctx.SourceFormats
                                          .FirstOrDefaultAsync(w => w.Id == sourceFormat.Id, cancellationToken)
                                          .ConfigureAwait(false);

                    if (target != null)
                    {
                        target.Name     = sourceFormat.Name;
                        target.Desc     = sourceFormat.Desc;
                        target.IsActive = sourceFormat.IsActive;

                        ctx.Entry(target).State = EntityState.Modified;
                        await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                        return(await ctx.SourceFormats
                               .AsNoTracking()
                               .FirstOrDefaultAsync(w => w.Id == sourceFormat.Id, cancellationToken)
                               .ConfigureAwait(false));
                    }

                    string msg = $"There is no {nameof(SourceFormat)} entity in the system with " +
                                 $"id: {sourceFormat.Id}.";
                    throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                }
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                             $"{nameof(UpdateAsync)} operation failed! " +
                             "For further information see inner exception!";
                throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
            }
        }
Exemplo n.º 8
0
        /// <inheritdoc/>
        public async Task <DimensionStructure> UpdateAsync(
            DimensionStructure dimensionStructure,
            CancellationToken cancellationToken = default)
        {
            try
            {
                Check.IsNotNull(dimensionStructure);

                await _masterDataValidators.DimensionStructureValidator.ValidateAsync(dimensionStructure, o =>
                {
                    o.IncludeRuleSets(DimensionStructureValidatorRulesets.Update);
                    o.ThrowOnFailures();
                }, cancellationToken).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    DimensionStructure toBeModified = await ctx.DimensionStructures
                                                      .FirstOrDefaultAsync(w => w.Id == dimensionStructure.Id, cancellationToken)
                                                      .ConfigureAwait(false);

                    string msg = $"There is no {typeof(DimensionStructure)} " +
                                 $"entity with id: {dimensionStructure.Id}";
                    Check.IsNotNull(toBeModified, msg);

                    toBeModified.Name     = dimensionStructure.Name;
                    toBeModified.Desc     = dimensionStructure.Desc;
                    toBeModified.IsActive = dimensionStructure.IsActive;

                    ctx.Entry(toBeModified).State = EntityState.Modified;
                    await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                    return(toBeModified);
                }
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataDimensionStructureBusinessLogic)}." +
                             $"{nameof(UpdateAsync)} operation failed. " +
                             $"For further information see inner exception.";
                throw new MasterDataBusinessLogicDimensionStructureDatabaseOperationException(msg, e);
            }
        }
Exemplo n.º 9
0
        /// <inheritdoc/>
        public async Task <DimensionStructureNode> AddAsync(
            DimensionStructureNode dimensionStructureNode,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
            {
                try
                {
                    Check.IsNotNull(dimensionStructureNode);

                    await _masterDataValidators
                    .DimensionStructureNodeValidator.ValidateAsync(dimensionStructureNode, o =>
                    {
                        o.IncludeRuleSets(DimensionStructureNodeValidatorRulesets.Add);
                        o.ThrowOnFailures();
                    }, cancellationToken).ConfigureAwait(false);

                    DimensionStructureNode newNode = new DimensionStructureNode
                    {
                        IsActive = dimensionStructureNode.IsActive,
                    };

                    await ctx.DimensionStructureNodes.AddAsync(
                        newNode,
                        cancellationToken)
                    .ConfigureAwait(false);

                    await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                    return(newNode);
                }
                catch (Exception e)
                {
                    string msg = $"{nameof(MasterDataDimensionStructureNodeBusinessLogic)}." +
                                 $"{nameof(AddAsync)} operation has failed. " +
                                 $"For further info see inner exception.";
                    throw new MasterDataDimensionStructureNodeBusinessLogicException(msg, e);
                }
            }
        }
Exemplo n.º 10
0
        /// <inheritdoc/>
        public async Task InactivateAsync(
            DimensionStructure dimensionStructure,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
            {
                try
                {
                    Check.IsNotNull(dimensionStructure);

                    await _masterDataValidators.DimensionStructureValidator.ValidateAsync(
                        dimensionStructure, options =>
                    {
                        options.IncludeRuleSets(DimensionStructureValidatorRulesets.Inactivate);
                        options.ThrowOnFailures();
                    }, cancellationToken)
                    .ConfigureAwait(false);

                    DimensionStructure toBeDeleted = await ctx.DimensionStructures.FirstOrDefaultAsync(
                        w => w.Id == dimensionStructure.Id,
                        cancellationToken)
                                                     .ConfigureAwait(false);

                    string msg = $"There is no {nameof(DimensionStructure)} entity " +
                                 $"with id: {dimensionStructure.Id}.";
                    Check.IsNotNull(toBeDeleted, msg);

                    toBeDeleted.IsActive         = 0;
                    ctx.Entry(toBeDeleted).State = EntityState.Modified;
                    await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    string msg = $"{nameof(MasterDataDimensionStructureBusinessLogic)}." +
                                 $"{nameof(InactivateAsync)} operation failed! " +
                                 $"For further info see inner exception!";
                    throw new MasterDataBusinessLogicDimensionStructureDatabaseOperationException(msg, e);
                }
            }
        }
        private async Task DeleteChildNodesOfDimensionStructureNodeAsync(
            DimensionStructureNode tree,
            MasterDataContext ctx,
            CancellationToken cancellationToken)
        {
            try
            {
                if (tree.ChildNodes.Any())
                {
                    foreach (DimensionStructureNode treeChildNode in tree.ChildNodes)
                    {
                        await DeleteChildNodesOfDimensionStructureNodeAsync(
                            treeChildNode,
                            ctx,
                            cancellationToken)
                        .ConfigureAwait(false);
                    }
                }

                DimensionStructureNode dimensionStructureNode = await ctx.DimensionStructureNodes
                                                                .FirstAsync(
                    w => w.Id == tree.Id,
                    cancellationToken)
                                                                .ConfigureAwait(false);

                ctx.Entry(dimensionStructureNode).State = EntityState.Deleted;
                await ctx.SaveChangesAsync(cancellationToken)
                .ConfigureAwait(false);
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                             $"{nameof(DeleteChildNodesOfDimensionStructureNodeAsync)} operation has failed. " +
                             $"For further information see inner exception.";
                throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
            }
        }
        public async Task <Dimension> UpdateDimensionAsync(Dimension dimension)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
            {
                try
                {
                    string msg = $"{nameof(dimension)} is null.";
                    Check.IsNotNull(dimension, msg);

                    await _masterDataValidators.DimensionValidator.ValidateAsync(dimension, o =>
                    {
                        o.IncludeRuleSets(ValidatorRulesets.UpdateDimension);
                        o.ThrowOnFailures();
                    }).ConfigureAwait(false);

                    Dimension toBeModified = await ctx.Dimensions.FindAsync(dimension.Id)
                                             .ConfigureAwait(false);

                    string tobeModifiedErrorMessage = $"No Dimension entity with id: {dimension.Id}";
                    Check.IsNotNull(toBeModified, tobeModifiedErrorMessage);

                    toBeModified.Name        = dimension.Name;
                    toBeModified.Description = dimension.Description;
                    toBeModified.IsActive    = dimension.IsActive;

                    ctx.Entry(toBeModified).State = EntityState.Modified;
                    await ctx.SaveChangesAsync().ConfigureAwait(false);

                    return(toBeModified);
                }
                catch (Exception e)
                {
                    throw new MasterDataBusinessLogicUpdateDimensionAsyncOperationException(e.Message, e);
                }
            }
        }
Exemplo n.º 13
0
        /// <inheritdoc/>
        public async Task InactivateAsync(
            SourceFormat sourceFormat,
            CancellationToken cancellationToken = default)
        {
            try
            {
                Check.IsNotNull(sourceFormat);
                await _masterDataValidators.SourceFormatValidator.ValidateAsync(sourceFormat, o =>
                {
                    o.IncludeRuleSets(SourceFormatValidatorRulesets.Inactivate);
                    o.ThrowOnFailures();
                }, cancellationToken).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    SourceFormat result = await ctx.SourceFormats.FirstOrDefaultAsync(
                        w => w.Id == sourceFormat.Id,
                        cancellationToken)
                                          .ConfigureAwait(false);

                    if (result != null)
                    {
                        result.IsActive         = 0;
                        ctx.Entry(result).State = EntityState.Modified;
                        await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                    }
                }
            }
            catch (Exception e)
            {
                string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                             $"{nameof(InactivateAsync)} operation failed! " +
                             $"For further information see inner exception!";
                throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
            }
        }
        /// <inheritdoc/>
        public async Task DeleteRootDimensionStructureNodeAsync(
            long dimensionStructureNodeId,
            long sourceFormatId,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                using (IDbContextTransaction transaction = await ctx.Database
                                                           .BeginTransactionAsync(cancellationToken)
                                                           .ConfigureAwait(false))
                {
                    try
                    {
                        Check.AreNotEqual(dimensionStructureNodeId, 0);
                        Check.AreNotEqual(sourceFormatId, 0);

                        DimensionStructureNode rootNode = await ctx.DimensionStructureNodes
                                                          .AsNoTracking()
                                                          .Include(i => i.SourceFormatDimensionStructureNode)
                                                          .ThenInclude(ii => ii.SourceFormat)
                                                          .Include(iii => iii.ChildNodes)
                                                          .FirstOrDefaultAsync(
                            w => w.Id == dimensionStructureNodeId &&
                            w.SourceFormatDimensionStructureNode.SourceFormatId == sourceFormatId,
                            cancellationToken)
                                                          .ConfigureAwait(false);

                        if (rootNode == null)
                        {
                            string msg = $"There is no {nameof(DimensionStructureNode)} with Id: " +
                                         $"{dimensionStructureNodeId}.";
                            throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                        }

                        SourceFormatDimensionStructureNode sourceFormatDimensionStructureNode = await ctx
                                                                                                .SourceFormatDimensionStructureNodes
                                                                                                .FirstOrDefaultAsync(
                            w => w.Id == rootNode.Id,
                            cancellationToken)
                                                                                                .ConfigureAwait(false);

                        ctx.Entry(sourceFormatDimensionStructureNode).State = EntityState.Deleted;
                        await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                        if (rootNode.ChildNodes.Any())
                        {
                            DimensionStructureNode rootNodeWithoutChildren = new DimensionStructureNode
                            {
                                Id = rootNode.Id,
                            };

                            DimensionStructureNode tree = await GetDimensionStructureNodeTreeAsync(
                                rootNodeWithoutChildren, ctx)
                                                          .ConfigureAwait(false);
                            await DeleteChildNodesOfDimensionStructureNodeAsync(
                                tree,
                                ctx,
                                cancellationToken)
                            .ConfigureAwait(false);
                        }

                        DimensionStructureNode refresh = await ctx.DimensionStructureNodes
                                                         .FirstOrDefaultAsync(
                            w => w.Id == dimensionStructureNodeId,
                            cancellationToken)
                                                         .ConfigureAwait(false);

                        if (refresh != null)
                        {
                            ctx.Entry(refresh).State = EntityState.Deleted;
                            await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
                        }

                        await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        await transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);

                        string msg = $"{nameof(MasterDataSourceFormatBusinessLogic)}." +
                                     $"{nameof(DeleteRootDimensionStructureNodeAsync)} operation has failed. " +
                                     "For further information see inner exception.";
                        throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
                    }
                }
        }
        public async Task <DimensionValue> ModifyDimensionValueAsync(
            long dimensionId,
            DimensionValue oldDimensionValue,
            DimensionValue newDimensionValue)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
            {
                using (IDbContextTransaction transaction = await ctx.Database.BeginTransactionAsync()
                                                           .ConfigureAwait(false))
                {
                    try
                    {
                        string dimensionIdErrorMsg = $"{nameof(dimensionId)} is zero.";
                        Check.AreNotEqual(dimensionId, 0, dimensionIdErrorMsg);

                        string oldDimensionValueErrorMsg = $"{nameof(oldDimensionValue)} is null.";
                        Check.IsNotNull(oldDimensionValue, oldDimensionValueErrorMsg);

                        string newDimensionValueErrorMsg = $"{nameof(newDimensionValue)} is null.";
                        Check.IsNotNull(newDimensionValue, newDimensionValueErrorMsg);

                        await _masterDataValidators.DimensionValueValidator.ValidateAsync(oldDimensionValue, o =>
                        {
                            o.IncludeRuleSets(ValidatorRulesets.ModifyDimensionValue);
                            o.ThrowOnFailures();
                        }).ConfigureAwait(false);

                        DomainModel.Dimension dim = await ctx.Dimensions.FindAsync(dimensionId).ConfigureAwait(false);

                        string noDimErrMsg = $"There is no dimension with id: {dimensionId}";
                        Check.IsNotNull(dim, noDimErrMsg);

                        DimensionValue dimVal = await ctx.DimensionValues.FindAsync(oldDimensionValue.Id)
                                                .ConfigureAwait(false);

                        string dimValErrMsg = $"There is no dimension value with id: {oldDimensionValue.Id}";
                        Check.IsNotNull(dimVal, dimValErrMsg);

                        // count how many dimension - dimension value relation exists
                        List <DimensionDimensionValue> countOfDimensionDimensionValueRelation = await ctx
                                                                                                .DimensionDimensionValues
                                                                                                .Where(p => p.DimensionValueId == oldDimensionValue.Id &&
                                                                                                       p.DimensionId != dimensionId)
                                                                                                .ToListAsync().ConfigureAwait(false);

                        if (countOfDimensionDimensionValueRelation.Any())
                        {
                            // If multiple dimensions references to the given dimension value
                            // then we are going to create a new dimension value and we are going to modify
                            // the dimension - dimension value reference to that
                            DimensionValue modifiedButNewDimensionValue = new DimensionValue
                            {
                                Value = newDimensionValue.Value,
                            };
                            await ctx.DimensionValues.AddAsync(modifiedButNewDimensionValue).ConfigureAwait(false);

                            await ctx.SaveChangesAsync().ConfigureAwait(false);

                            DimensionDimensionValue theOneGoingToBeModified = await ctx.DimensionDimensionValues
                                                                              .FirstOrDefaultAsync(p => p.DimensionId == dimensionId &&
                                                                                                   p.DimensionValueId == oldDimensionValue.Id)
                                                                              .ConfigureAwait(false);

                            if (theOneGoingToBeModified == null)
                            {
                                string msg = $"There is no DimensionDimensionValue entity with " +
                                             $"dimension id: {dimensionId}, and" +
                                             $"dimension value id: {oldDimensionValue.Id}!";
                                throw new MasterDataBusinessLogicNoSuchDimensionDimensionValueEntity(msg);
                            }

                            theOneGoingToBeModified.DimensionValueId = modifiedButNewDimensionValue.Id;
                            ctx.Entry(theOneGoingToBeModified).State = EntityState.Modified;
                            await ctx.SaveChangesAsync().ConfigureAwait(false);

                            await transaction.CommitAsync().ConfigureAwait(false);

                            DimensionValue modifiedResult = await ctx.DimensionValues.FirstOrDefaultAsync(
                                p => p.Id == modifiedButNewDimensionValue.Id).ConfigureAwait(false);

                            return(modifiedResult);
                        }

                        DimensionValue dimValToBeModified = await ctx.DimensionValues.FirstOrDefaultAsync(
                            p => p.Id == oldDimensionValue.Id).ConfigureAwait(false);

                        if (dimValToBeModified == null)
                        {
                            string erMsg = $"There is no dimension value with id: {oldDimensionValue.Id}";
                            throw new MasterDataBusinessLogicNoSuchDimensionValueEntity(erMsg);
                        }

                        dimValToBeModified.Value            = newDimensionValue.Value;
                        ctx.Entry(dimValToBeModified).State = EntityState.Modified;
                        await ctx.SaveChangesAsync().ConfigureAwait(false);

                        await transaction.CommitAsync().ConfigureAwait(false);

                        return(dimValToBeModified);
                    }
                    catch (Exception e)
                    {
                        await transaction.CommitAsync().ConfigureAwait(false);

                        throw new MasterDataBusinessLogicModifyDimensionValueAsyncOperationException(
                                  e.Message, e);
                    }
                }
            }
        }
        public async Task <DimensionValue> AddDimensionValueAsync(
            DimensionValue dimensionValue,
            long dimensionId)
        {
            try
            {
                Check.IsNotNull(dimensionValue);
                Check.AreNotEqual(dimensionId, 0);

                await _masterDataValidators.DimensionValueValidator.ValidateAsync(dimensionValue, o =>
                {
                    o.IncludeRuleSets(ValidatorRulesets.AddNewDimensionValue);
                    o.ThrowOnFailures();
                }).ConfigureAwait(false);

                using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                {
                    using (IDbContextTransaction transaction = await ctx.Database.BeginTransactionAsync()
                                                               .ConfigureAwait(false))
                    {
                        try
                        {
                            // Check whether dimension exist
                            DomainModel.Dimension dimension = await ctx.Dimensions
                                                              .FindAsync(dimensionId)
                                                              .ConfigureAwait(true);

                            string msg = $"Dimension with Id {dimensionId} doesnt exists";
                            Check.IsNotNull(dimension, msg);

                            // check whether value already exist
                            DimensionValue doesDimensionValueExists = await ctx.DimensionValues
                                                                      .FirstOrDefaultAsync(w => w.Value == dimensionValue.Value)
                                                                      .ConfigureAwait(false);

                            if (doesDimensionValueExists != null)
                            {
                                // check whether dimension - dimension value pair exist
                                DimensionDimensionValue doesDimensionDimensionValueRelationExist = await ctx
                                                                                                   .DimensionDimensionValues
                                                                                                   .FirstOrDefaultAsync(p => p.DimensionId == dimension.Id &&
                                                                                                                        p.DimensionValueId == doesDimensionValueExists.Id)
                                                                                                   .ConfigureAwait(false);

                                // if doesnt exists create one
                                if (doesDimensionDimensionValueRelationExist == null)
                                {
                                    DimensionDimensionValue dimensionDimensionValue = new DimensionDimensionValue
                                    {
                                        DimensionId      = dimension.Id,
                                        DimensionValueId = doesDimensionValueExists.Id,
                                    };
                                    await ctx.DimensionDimensionValues.AddAsync(dimensionDimensionValue)
                                    .ConfigureAwait(false);

                                    await ctx.SaveChangesAsync().ConfigureAwait(false);

                                    await transaction.CommitAsync().ConfigureAwait(false);

                                    return(doesDimensionValueExists);
                                }

                                // include all related entities
                                // return doesDimensionValueExists;
                                DimensionValue alreadyExistingDimensionValue =
                                    await GetDimensionValueWithRelatedEntities(
                                        dimensionValue.Value, ctx)
                                    .ConfigureAwait(false);

                                return(alreadyExistingDimensionValue);
                            }

                            // create dimension value entry
                            DimensionValue newDimensionValue = new DimensionValue
                            {
                                Value = dimensionValue.Value,
                            };
                            await ctx.DimensionValues.AddAsync(newDimensionValue)
                            .ConfigureAwait(false);

                            await ctx.SaveChangesAsync().ConfigureAwait(false);

                            // create dimension - dimension value relation
                            DimensionDimensionValue newlyAddedDimensionValue = new DimensionDimensionValue
                            {
                                DimensionId      = dimension.Id,
                                DimensionValueId = newDimensionValue.Id,
                            };
                            await ctx.DimensionDimensionValues.AddAsync(newlyAddedDimensionValue)
                            .ConfigureAwait(false);

                            await ctx.SaveChangesAsync().ConfigureAwait(false);

                            await transaction.CommitAsync().ConfigureAwait(false);

                            DimensionValue createdDimensionValue =
                                await GetDimensionValueWithRelatedEntities(dimensionValue.Value, ctx)
                                .ConfigureAwait(false);

                            return(createdDimensionValue);
                        }
                        catch (Exception e)
                        {
                            await transaction.RollbackAsync().ConfigureAwait(false);

                            throw;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new MasterDataBusinessLogicAddDimensionValueAsyncOperationException(e.Message, e);
            }
        }
Exemplo n.º 17
0
        /// <inheritdoc/>
        public async Task <DimensionStructureNode> CreateRootNodeAsync(
            SourceFormat sourceFormat,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                using (IDbContextTransaction transaction = await ctx.Database
                                                           .BeginTransactionAsync(cancellationToken)
                                                           .ConfigureAwait(false))
                {
                    try
                    {
                        await _masterDataValidators.SourceFormatValidator.ValidateAsync(sourceFormat, o =>
                        {
                            o.IncludeRuleSets(SourceFormatValidatorRulesets.AddRootNode);
                            o.ThrowOnFailures();
                        }, cancellationToken).ConfigureAwait(false);

                        DimensionStructureNode newNode = new DimensionStructureNode();
                        await ctx.DimensionStructureNodes.AddAsync(newNode, cancellationToken).ConfigureAwait(false);

                        await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                        SourceFormat sourceFormatResult = await ctx.SourceFormats
                                                          .FirstOrDefaultAsync(p => p.Id == sourceFormat.Id, cancellationToken)
                                                          .ConfigureAwait(false);

                        if (sourceFormatResult == null)
                        {
                            string msg = $"There is no {nameof(SourceFormat)} entity " +
                                         $"with id: {sourceFormat.Id}";
                            throw new MasterDataDimensionStructureNodeBusinessLogicException(msg);
                        }

                        SourceFormatDimensionStructureNode sourceFormatDimensionStructureNode =
                            new SourceFormatDimensionStructureNode
                        {
                            DimensionStructureNode = newNode,
                            SourceFormat           = sourceFormatResult,
                        };
                        await ctx.SourceFormatDimensionStructureNodes.AddAsync(
                            sourceFormatDimensionStructureNode,
                            cancellationToken)
                        .ConfigureAwait(false);

                        await ctx.SaveChangesAsync(cancellationToken).ConfigureAwait(false);

                        await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);

                        return(newNode);
                    }
                    catch (Exception e)
                    {
                        await transaction.RollbackAsync(cancellationToken).ConfigureAwait(false);

                        string msg = $"{nameof(MasterDataDimensionStructureNodeBusinessLogic)}." +
                                     $"{nameof(CreateRootNodeAsync)} operation failed. " +
                                     $"For further information see inner exception!";
                        throw new MasterDataDimensionStructureNodeBusinessLogicException(msg, e);
                    }
                }
        }
        /// <inheritdoc/>
        public async Task AddRootDimensionStructureNodeAsync(
            long sourceFormatId,
            long dimensionStructureNodeId,
            CancellationToken cancellationToken = default)
        {
            using (MasterDataContext ctx = new MasterDataContext(_dbContextOptions))
                using (IDbContextTransaction transaction = await ctx.Database
                                                           .BeginTransactionAsync(cancellationToken)
                                                           .ConfigureAwait(false))
                {
                    try
                    {
                        Check.AreNotEqual(sourceFormatId, 0);
                        Check.AreNotEqual(dimensionStructureNodeId, 0);

                        SourceFormat sourceFormat = await ctx.SourceFormats
                                                    .Include(root => root.SourceFormatDimensionStructureNode)
                                                    .FirstOrDefaultAsync(k => k.Id == sourceFormatId, cancellationToken)
                                                    .ConfigureAwait(false);

                        DimensionStructureNode dimensionStructureNode = await ctx.DimensionStructureNodes
                                                                        .FirstOrDefaultAsync(k => k.Id == dimensionStructureNodeId, cancellationToken)
                                                                        .ConfigureAwait(false);

                        if (sourceFormat is null)
                        {
                            string msg = $"No {nameof(SourceFormat)} with id: {sourceFormatId}.";
                            throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                        }

                        if (dimensionStructureNode is null)
                        {
                            string msg = $"No {nameof(DimensionStructureNode)} with id: {dimensionStructureNodeId}.";
                            throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                        }

                        if (sourceFormat.SourceFormatDimensionStructureNode is not null)
                        {
                            string msg = $"{nameof(SourceFormat)}(${sourceFormat.Id}) already has " +
                                         $"root ${nameof(DimensionStructureNode)}.";
                            throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg);
                        }

                        dimensionStructureNode.SourceFormat     = sourceFormat;
                        ctx.Entry(dimensionStructureNode).State = EntityState.Modified;

                        SourceFormatDimensionStructureNode sourceFormatDimensionStructureNode =
                            new SourceFormatDimensionStructureNode
                        {
                            DimensionStructureNode = dimensionStructureNode,
                            SourceFormat           = sourceFormat,
                        };

                        await ctx.AddAsync(sourceFormatDimensionStructureNode, cancellationToken)
                        .ConfigureAwait(false);

                        await ctx.SaveChangesAsync(cancellationToken)
                        .ConfigureAwait(false);

                        await transaction.CommitAsync(cancellationToken)
                        .ConfigureAwait(false);
                    }
                    catch (Exception e)
                    {
                        await transaction.RollbackAsync(cancellationToken)
                        .ConfigureAwait(false);

                        string msg = $"{nameof(AddRootDimensionStructureNodeAsync)} operation failed!" +
                                     $" For further information see inner exception.";
                        throw new MasterDataBusinessLogicSourceFormatDatabaseOperationException(msg, e);
                    }
                }
        }