Example #1
0
        private bool SlotDoorFits(string slotReference, ConfiguredProductDto childDto, ConfiguredProduct parent)
        {
            var sum = 0;

            foreach (var part in parent.Parts)
            {
                if (part.ChosenSlotReference == slotReference)
                {
                    ConfiguredProduct configuredProductPart =
                        _configuredProductRepository.GetByReference(part.ConfiguredChildReference);
                    Product  productPart = _productRepository.GetByReference(configuredProductPart.ProductReference);
                    Category category    = _categoryRepository.GetByReference(productPart.CategoryReference);
                    if (category.IsExternal)
                    {
                        sum += configuredProductPart.ConfiguredDimension.Width;
                    }
                }
            }

            foreach (var slot in parent.ConfiguredSlots)
            {
                if (slot.Reference == slotReference)
                {
                    return(slot.Size - sum >= childDto.ConfiguredDimension.Width);
                }
            }

            return(parent.ConfiguredDimension.Width - sum >= childDto.ConfiguredDimension.Width);
        }
Example #2
0
        public async Task <IActionResult> Create([FromHeader(Name = "Authorization")] string authorization, ChildConfiguredProductDto receivedDto)
        {
            if (!_userValidationService.CheckAuthorizationToken(authorization))
            {
                return(Unauthorized());
            }
            var userRef = await _userValidationService.GetUserRef(authorization.Split(" ")[1]);

            _logger.logInformation(userRef, LoggingEvents.PostItem, "Creating By Dto: {0}", receivedDto.Reference);
            ValidationOutput validationOutput = _configuredProductService.Register(receivedDto);

            if (validationOutput.HasErrors())
            {
                if (validationOutput is ValidationOutputBadRequest)
                {
                    _logger.logCritical(userRef, LoggingEvents.PostBadRequest, "Creating Configured Product Failed: {0}", ((ValidationOutputBadRequest)validationOutput).ToString());
                    return(BadRequest(validationOutput.FoundErrors));
                }

                if (validationOutput is ValidationOutputNotFound)
                {
                    _logger.logCritical(userRef, LoggingEvents.PostNotFound, "Creating Configured Product Failed: {0}", ((ValidationOutputNotFound)validationOutput).ToString());
                    return(NotFound(validationOutput.FoundErrors));
                }

                _logger.logCritical(userRef, LoggingEvents.PostInternalError, "Type of validation output not recognized. Please contact your software provider.");
                return(BadRequest("Type of validation output not recognized. Please contact your software provider."));
            }
            else
            {
                ConfiguredProductDto dto = (ConfiguredProductDto)validationOutput.DesiredReturn;
                _logger.logInformation(userRef, LoggingEvents.PostOk, "Creating Configured Product Succeeded: {0}", dto.ToString());
                return(CreatedAtRoute("GetConfiguredProduct", new { reference = receivedDto.Reference }, dto));
            }
        }
Example #3
0
        private bool CheckPartAlgorithms(Product baseProduct, ConfiguredProductDto configuredProduct)
        {
            ConfiguredProduct parentConfigured = _configuredProductRepository.GetByReference(((ChildConfiguredProductDto)configuredProduct).ParentReference);
            Product           parentProduct    = _productRepository.GetByReference(parentConfigured.ProductReference);
            Part part = parentProduct.IsProductPart(baseProduct);

            if (part == null)
            {
                return(false);
            }
            foreach (var algorithm in part.Algorithms)
            {
                Dictionary <string, Object> parameters = new Dictionary <string, Object>();
                parameters.Add("ParentConfiguredProduct", parentConfigured);
                parameters.Add("ChildConfiguredProduct", configuredProduct);
                if (!algorithm.validate(parameters))
                {
                    return(false);
                }
            }
            return(true);
            // foreach (var configuredPart in configuredProduct.Parts)
            // {
            //     ConfiguredProduct childConfiguredProduct = _configuredProductRepository.GetByReference(configuredPart.ConfiguredChildReference);
            //     Product baseProductOfChildConfiguredProduct = _productRepository.GetByReference(childConfiguredProduct.ProductReference);

            //     foreach (var part in baseProduct.Parts)
            //     {
            //         if (part.ProductReference == baseProductOfChildConfiguredProduct.Reference)
            //         {
            //             foreach (var restriction in part.Algorithms)
            //             {
            //                 Dictionary<string, Object> parameters = new Dictionary<string, Object>();
            //                 parameters.Add("FatherConfiguredProduct", configuredProduct);
            //                 parameters.Add("ChildConfiguredProduct", childConfiguredProduct);
            //                 parameters.Add("FatherBaseProduct", baseProduct);
            //                 parameters.Add("ChildBaseProduct", baseProductOfChildConfiguredProduct);

            //                 if (!restriction.validate(parameters))
            //                 {
            //                     return false;
            //                 }
            //             }
            //             break;
            //         }
            //     }
            // }
        }
Example #4
0
        private void ChildrenFits(string slotReference, ConfiguredProduct parent, ConfiguredProductDto childDto, ValidationOutput validationOutput)
        {
            Product  product  = _productRepository.GetByReference(childDto.ProductReference);
            Category category = _categoryRepository.GetByReference(product.CategoryReference);

            if (!category.IsExternal)
            {
                if (!ChildrenHeightFits(slotReference, parent, childDto))
                {
                    validationOutput.AddError("Configured Product's height", "Parent Configured Product's height is insufficient!");
                }
                if (!ChildrenWidthFits(slotReference, parent, childDto))
                {
                    validationOutput.AddError("Configured Product's width", "Parent Configured Product's width is insufficient!");
                }
            }
            else if (category.IsExternal)
            {
                if (!(parent.ConfiguredDimension.Height >= childDto.ConfiguredDimension.Height))
                {
                    validationOutput.AddError("Configured Product's height", "Parent Configured Product's height is insufficient!");
                }

                if (slotReference != null)
                {
                    if (!(SlotDoorFits(slotReference, childDto, parent)))
                    {
                        validationOutput.AddError("Configured Product's width",
                                                  "Parent Configured Product's width is insufficient!");
                    }
                }
                else
                {
                    if (!(SlideDoorFits(childDto, parent)))
                    {
                        validationOutput.AddError("Configured Product's width", "Parent Configured Product's width is insufficient!");
                    }
                }
            }
            if (!ChildrenDepthFits(slotReference, parent, childDto))
            {
                validationOutput.AddError("Configured Product's depth", "Parent Configured Product's depth is insufficient!");
            }
        }
Example #5
0
        private bool ObeysRestrictions(ConfiguredProductDto configuredProduct)
        {
            Product baseProduct = _productRepository.GetByReference(configuredProduct.ProductReference);

            if (!CheckDimensionAlgorithms(baseProduct, configuredProduct))
            {
                return(false);
            }
            if (((ChildConfiguredProductDto)configuredProduct).ParentReference != null)
            {
                if (configuredProduct is ChildConfiguredProductDto && !CheckPartAlgorithms(baseProduct, configuredProduct))
                {
                    return(false);
                }
            }

            // foreach (var materialProd in baseProduct.ProductMaterialList)
            // {
            //     if (configuredProduct.ConfiguredMaterial.OriginMaterialReference.Equals(materialProd.MaterialReference))
            //     {
            //         if (materialProd.Algorithms != null)
            //         {
            //             foreach (var restriction in materialProd.Algorithms)
            //             {
            //                 Dictionary<string, Object> parameters = new Dictionary<string, Object>();

            //                 parameters.Add("ConfiguredMaterial", configuredProduct.ConfiguredMaterial);
            //                 if (!restriction.validate(parameters))
            //                 {
            //                     return false;
            //                 }
            //             }
            //         }
            //         break;
            //     }
            // }


            return(true);
        }
Example #6
0
 /* verifica se obdece as restricoes ao primeiro conjunto de dimensoes que couber */
 private bool CheckDimensionAlgorithms(Product baseProduct, ConfiguredProductDto configuredProduct)
 {
     foreach (var dimensionValues in baseProduct.Dimensions)
     {
         if (dimensionValues.CheckIfItBelongs(configuredProduct.ConfiguredDimension.Width, configuredProduct.ConfiguredDimension.Height, configuredProduct.ConfiguredDimension.Depth))
         {
             foreach (var restriction in dimensionValues.Algorithms)
             {
                 Dictionary <string, Object> parameters = new Dictionary <string, Object>();
                 parameters.Add("height", configuredProduct.ConfiguredDimension.Height);
                 parameters.Add("depth", configuredProduct.ConfiguredDimension.Depth);
                 parameters.Add("width", configuredProduct.ConfiguredDimension.Width);
                 if (!restriction.validate(parameters))
                 {
                     return(false);
                 }
             }
             return(true);
         }
     }
     return(false);
 }
Example #7
0
        private bool SlideDoorFits(ConfiguredProductDto childDto, ConfiguredProduct parent)
        {
            var sum = 0;

            foreach (var part in parent.Parts)
            {
                ConfiguredProduct configuredProductPart =
                    _configuredProductRepository.GetByReference(part.ConfiguredChildReference);
                Product  productPart = _productRepository.GetByReference(configuredProductPart.ProductReference);
                Category category    = _categoryRepository.GetByReference(productPart.CategoryReference);
                if (category.IsExternal)
                {
                    sum += configuredProductPart.ConfiguredDimension.Width;
                }
            }

            if (parent.ConfiguredDimension.Width - sum >= childDto.ConfiguredDimension.Width)
            {
                return(true);
            }

            return(false);
        }
Example #8
0
        private float relationCalculation(ConfiguredProductDto child, ConfiguredProduct.ConfiguredProduct parent)
        {
            float firstValue;
            float secondValue;

            switch (SizeType)
            {
            case "height":
                firstValue  = (int)child.ConfiguredDimension.Height;
                secondValue = (int)parent.ConfiguredDimension.Height;
                return(firstValue / secondValue);

            case "width":
                firstValue  = (int)child.ConfiguredDimension.Width;
                secondValue = (int)parent.ConfiguredDimension.Width;
                return(firstValue / secondValue);

            case "depth":
                firstValue  = (int)child.ConfiguredDimension.Depth;
                secondValue = (int)parent.ConfiguredDimension.Depth;
                return(firstValue / secondValue);
            }
            return(-1);
        }
Example #9
0
 //não deve ser atualizado
 public ValidationOutput Update(string reference, ConfiguredProductDto dto)
 {
     return(new ValidationOutputBadRequest());
 }
Example #10
0
        /**
         * Method that will validate and create a new configured product in the database.
         *
         * Validations performed:
         * 1. Validation of the new configured product's reference (business rules);
         * 2. Validation of the new configured product's reference (database);
         * 3. Validation of the received info. (product reference, configured material, configured dimension) (business rules)
         * 4. Validation of the received info. (origin product, origin material, parent product, slot, dimensions, restrictions) (database)
         * 5. Validation of the received info. (everything fits)
         */
        public ValidationOutput Register(ConfiguredProductDto configuredDto)
        {
            ValidationOutput validationOutput = new ValidationOutputBadRequest();

            if (configuredDto == null)
            {
                validationOutput.AddError("ConfiguredProduct", "Null configured product.");
                return(validationOutput);
            }

            ConfiguredProduct configuredProduct = _configuredProductRepository.GetByReference(configuredDto.Reference);

            if (configuredProduct != null)
            {
                validationOutput = new ValidationOutputBadRequest();
                validationOutput.AddError("Configured Product's reference", "There are already configured product with the given reference");
                return(validationOutput);
            }

            //1.
            validationOutput = _configuredProductDTOValidator.DTOReferenceIsValid(configuredDto.Reference);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //2
            if (_configuredProductRepository.GetByReference(configuredDto.Reference) != null)
            {
                validationOutput.AddError("ConfiguredProduct's reference", "A ConfiguredProduct with the reference '" + configuredDto.Reference + "' already exists in the system!");
                return(validationOutput);
            }

            //3
            validationOutput = _configuredProductDTOValidator.DTOIsValidForRegister(configuredDto);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            ChildConfiguredProductDto ccpd = (ChildConfiguredProductDto)configuredDto;

            //4
            validationOutput = DataExists(ccpd);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            //5
            validationOutput = ValidateConfiguredProduct(ccpd);
            if (validationOutput.HasErrors())
            {
                return(validationOutput);
            }

            var configured = _mapper.Map <ConfiguredProduct>(configuredDto);

            if (_productRepository.GetByReference(configured.ProductReference).SlotDefinition == null || !configured.ConfiguredSlots.Any())
            {
                configured.ConfiguredSlots.Clear();
                configured.ConfiguredSlots.Add(new ConfiguredSlot(configured.ConfiguredDimension.Width, "default_slot_reference"));
            }

            configured.Price = new Price(CalculatePriceValue(((Price[])validationOutput.DesiredReturn)[2], ((Price[])validationOutput.DesiredReturn)[1], ((Price[])validationOutput.DesiredReturn)[0], configured.ConfiguredDimension));

            if (ccpd.ParentReference != null)
            {
                var parentConfigured = _configuredProductRepository.GetByReference(ccpd.ParentReference);
                parentConfigured.Parts.Add(new ConfiguredPart(ccpd.Reference, ccpd.SlotReference));
                parentConfigured.Price.Value += configured.Price.Value;
                _configuredProductRepository.Update(parentConfigured);
            }

            validationOutput.DesiredReturn = _mapper.Map <ConfiguredProductDto>(_configuredProductRepository.Add(configured));

            return(validationOutput);
        }
Example #11
0
 private bool ChildrenWidthFits(string slotReference, ConfiguredProduct parentDto, ConfiguredProductDto childDto)
 {
     foreach (var slot in parentDto.ConfiguredSlots)
     {
         if (string.Equals(slot.Reference, slotReference, StringComparison.Ordinal))
         {
             return(slot.Size >= childDto.ConfiguredDimension.Width);
         }
     }
     return(false);
 }
Example #12
0
        private bool ChildrenHeightFits(string slotReference, ConfiguredProduct parentDto, ConfiguredProductDto childDto)
        {
            int sum = 0;

            foreach (var part in parentDto.Parts)
            {
                if (string.Equals(part.ChosenSlotReference, slotReference, StringComparison.Ordinal))
                {
                    sum += _configuredProductRepository.GetByReference(part.ConfiguredChildReference).ConfiguredDimension.Height;
                }
            }
            return(parentDto.ConfiguredDimension.Height - sum >= childDto.ConfiguredDimension.Height);
        }
Example #13
0
 private bool ChildrenDepthFits(string slotReference, ConfiguredProduct parentDto, ConfiguredProductDto childDto)
 {
     return(parentDto.ConfiguredDimension.Depth >= childDto.ConfiguredDimension.Depth);
 }