/// <summary>
        /// Builds an instance of CustomizedProduct based on the given Product with the data in the given instance of AddCustomizedProductModelView.
        /// </summary>
        /// <param name="addCustomizedProductModelView">AddCustomizedProductModelView containing the CustomizedProduct's information.</param>
        /// <param name="product">Instance of Product.</param>
        /// <param name="parentCustomizedProduct">Parent CustomizedProduct.</param>
        /// <param name="insertedInSlot">Slot in which the CustomizedProduct will be inserted.</param>
        /// <exception cref="System.ArgumentException">
        /// Thrown when the Material referenced by the CustomizedMaterial is not found or when no CustomizedDimensions are provided.
        /// </exception>
        /// <returns>A new instance of CustomizedProduct.</returns>
        private static CustomizedProduct buildSubCustomizedProduct(AddCustomizedProductModelView addCustomizedProductModelView, Product product,
                                                                   CustomizedProduct parentCustomizedProduct, Slot insertedInSlot)
        {
            CustomizedProductBuilder customizedProductBuilder = null;

            if (addCustomizedProductModelView.customizedDimensions == null)
            {
                throw new ArgumentException(ERROR_NO_CUSTOMIZED_DIMENSIONS);
            }

            CustomizedDimensions customizedProductDimensions = CustomizedDimensionsModelViewService.fromModelView(addCustomizedProductModelView.customizedDimensions);

            if (addCustomizedProductModelView.userAuthToken == null)
            {
                customizedProductBuilder = CustomizedProductBuilder.createCustomizedProduct(product, customizedProductDimensions, parentCustomizedProduct, insertedInSlot);
            }
            else
            {
                customizedProductBuilder = CustomizedProductBuilder
                                           .createCustomizedProduct(addCustomizedProductModelView.userAuthToken, product, customizedProductDimensions, parentCustomizedProduct, insertedInSlot);
            }

            if (addCustomizedProductModelView.customizedMaterial != null)
            {
                CustomizedMaterial customizedMaterial = CreateCustomizedMaterialService.create(addCustomizedProductModelView.customizedMaterial);

                customizedProductBuilder.withMaterial(customizedMaterial);
            }

            //ignore designation since only the base customized products can set the designation

            return(customizedProductBuilder.build());
        }
Exemple #2
0
        /// <summary>
        /// Converts an instance of CustomizedProduct into an instance of GetCustomizedProductModelView.
        /// </summary>
        /// <param name="customizedProduct">Instance of CustomizedProduct being converted.</param>
        /// <param name="unit">String representing the unit to which the CustomizedProduct's dimensions will be converted to.</param>
        /// <returns>Instance of GetCustomizedProductModelView.</returns>
        /// <exception cref="System.ArgumentException">Thrown when the provided instance of CustomizedProduct is null.</exception>
        public static GetCustomizedProductModelView fromEntity(CustomizedProduct customizedProduct, string unit)
        {
            if (customizedProduct == null)
            {
                throw new ArgumentException(ERROR_NULL_CUSTOMIZED_PRODUCT);
            }

            GetCustomizedProductModelView customizedProductModelView = new GetCustomizedProductModelView();

            customizedProductModelView.customizedProductId = customizedProduct.Id;
            customizedProductModelView.reference           = customizedProduct.reference;
            customizedProductModelView.designation         = customizedProduct.designation;
            customizedProductModelView.status = customizedProduct.status;
            customizedProductModelView.customizedDimensions = CustomizedDimensionsModelViewService.fromEntity(customizedProduct.customizedDimensions, unit);

            if (customizedProduct.customizedMaterial != null)
            {
                customizedProductModelView.customizedMaterial = CustomizedMaterialModelViewService.fromEntity(customizedProduct.customizedMaterial);
            }

            customizedProductModelView.product = ProductModelViewService.fromEntityAsBasic(customizedProduct.product);
            customizedProductModelView.slots   = SlotModelViewService.fromCollection(customizedProduct.slots, unit);


            return(customizedProductModelView);
        }
        public void ensureFromModelViewThrowsExceptionIfModelViewIsNull()
        {
            AddCustomizedDimensionsModelView addCustomizedDimensionsModelView = null;

            Action fromModelView = () => CustomizedDimensionsModelViewService.fromModelView(addCustomizedDimensionsModelView);

            Assert.Throws <ArgumentException>(fromModelView);
        }
        public void ensureFromEntityDoesNotConvertValuesIfNoUnitIsProvided()
        {
            double height = 30;
            double width  = 50;
            double depth  = 15;

            CustomizedDimensions customizedDimensions = CustomizedDimensions.valueOf(height, width, depth);

            GetCustomizedDimensionsModelView getCustomizedDimensionsModelView = CustomizedDimensionsModelViewService.fromEntity(customizedDimensions);

            Assert.Equal(getCustomizedDimensionsModelView.height, height);
            Assert.Equal(getCustomizedDimensionsModelView.width, width);
            Assert.Equal(getCustomizedDimensionsModelView.depth, depth);
        }
        public void ensureFromEntityThrowsExceptionIfCustomizedDimensionsIsNull()
        {
            CustomizedDimensions customizedDimensions = null;

            Action fromEntity = () => CustomizedDimensionsModelViewService.fromEntity(customizedDimensions);

            Assert.Throws <ArgumentException>(fromEntity);


            //Make sure overload has the same behaviour
            fromEntity = () => CustomizedDimensionsModelViewService.fromEntity(customizedDimensions, "m");

            Assert.Throws <ArgumentException>(fromEntity);
        }
        public void ensureFromModelViewDoesNotConvertValuesIfNoUnitIsProvided()
        {
            AddCustomizedDimensionsModelView addCustomizedDimensionsModelView = new AddCustomizedDimensionsModelView();

            addCustomizedDimensionsModelView.height = 30;
            addCustomizedDimensionsModelView.width  = 50;
            addCustomizedDimensionsModelView.depth  = 15;


            CustomizedDimensions customizedDimensions = CustomizedDimensionsModelViewService.fromModelView(addCustomizedDimensionsModelView);

            Assert.Equal(addCustomizedDimensionsModelView.height, customizedDimensions.height);
            Assert.Equal(addCustomizedDimensionsModelView.width, customizedDimensions.width);
            Assert.Equal(addCustomizedDimensionsModelView.depth, customizedDimensions.depth);
        }
        public void ensureFromEntityConvertsValuesToProvidedUnit()
        {
            double height = 30;
            double width  = 50;
            double depth  = 15;

            CustomizedDimensions customizedDimensions = CustomizedDimensions.valueOf(height, width, depth);

            string unit = "m";

            GetCustomizedDimensionsModelView getCustomizedDimensionsModelView = CustomizedDimensionsModelViewService.fromEntity(customizedDimensions, unit);

            Assert.Equal(getCustomizedDimensionsModelView.height, MeasurementUnitService.convertToUnit(height, unit));
            Assert.Equal(getCustomizedDimensionsModelView.width, MeasurementUnitService.convertToUnit(width, unit));
            Assert.Equal(getCustomizedDimensionsModelView.depth, MeasurementUnitService.convertToUnit(depth, unit));
        }
        /// <summary>
        /// Gets min slots from a certain customized product
        /// </summary>
        /// <param name="findCustomizedProductModelView">Instance of FindCustomizedProductModelView.</param>
        /// <exception cref="ResourceNotFoundException">Thrown when no CustomizedProduct could be found with the given identifier.</exception>
        /// <returns>Instance of GetAllCustomizedDimensionsModelView representing the minimum Slots.</returns>
        public GetAllCustomizedDimensionsModelView getMinSlots(FindCustomizedProductModelView findCustomizedProductModelView)
        {
            CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories().createCustomizedProductRepository();
            CustomizedProduct           customizedProduct           = customizedProductRepository.find(findCustomizedProductModelView.customizedProductId);

            if (customizedProduct == null)
            {
                throw new ResourceNotFoundException(
                          string.Format(ERROR_UNABLE_TO_FIND_CUSTOMIZED_PRODUCT_BY_ID, findCustomizedProductModelView.customizedProductId)
                          );
            }

            List <CustomizedDimensions> customizedDimensions = customizedProduct.minSlots();

            return(CustomizedDimensionsModelViewService.fromCollection(customizedDimensions, findCustomizedProductModelView.options.unit));
        }
        public void ensureFromModelViewConvertsValuesToProvidedUnit()
        {
            AddCustomizedDimensionsModelView addCustomizedDimensionsModelView = new AddCustomizedDimensionsModelView();

            string unit = "cm";

            addCustomizedDimensionsModelView.height = 30;
            addCustomizedDimensionsModelView.width  = 50;
            addCustomizedDimensionsModelView.depth  = 15;
            addCustomizedDimensionsModelView.unit   = unit;

            //the customized dimensions' values should be in the minimum unit
            CustomizedDimensions customizedDimensions = CustomizedDimensionsModelViewService.fromModelView(addCustomizedDimensionsModelView);

            Assert.Equal(addCustomizedDimensionsModelView.height, MeasurementUnitService.convertToUnit(customizedDimensions.height, unit));
            Assert.Equal(addCustomizedDimensionsModelView.width, MeasurementUnitService.convertToUnit(customizedDimensions.width, unit));
            Assert.Equal(addCustomizedDimensionsModelView.depth, MeasurementUnitService.convertToUnit(customizedDimensions.depth, unit));
        }
        /// <summary>
        /// Converts an instance of Slot into an instance of GetSlotModelView.
        /// </summary>
        /// <param name="slot">Instance of Slot being converted.</param>
        /// <param name="unit">String representing the unit to which the Slot's dimensions will be converted to.</param>
        /// <returns>Instance of GetSlotModelView.</returns>
        /// <exception cref="System.ArgumentException">Thrown when the provided instance of Slot is null.</exception>
        public static GetSlotModelView fromEntity(Slot slot, string unit)
        {
            if (slot == null)
            {
                throw new ArgumentException(ERROR_NULL_SLOT);
            }

            GetSlotModelView slotModelView = new GetSlotModelView();

            slotModelView.slotId         = slot.Id;
            slotModelView.slotDimensions = CustomizedDimensionsModelViewService.fromEntity(slot.slotDimensions, unit);

            if (slot.customizedProducts.Any())
            {
                slotModelView.customizedProducts = CustomizedProductModelViewService.fromCollection(slot.customizedProducts);
            }

            return(slotModelView);
        }
        /// <summary>
        /// Adds a Slot to a CustomizedProduct.
        /// </summary>
        /// <param name="addSlotModelView">AddSlotModelView with the Slot's information</param>
        /// <returns>An instance of GetCustomizedProductModelView containing updated CustomizedProduct information.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when the CustomizedProduct could not be found.</exception>
        public GetCustomizedProductModelView addSlotToCustomizedProduct(AddSlotModelView addSlotModelView)
        {
            CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories().createCustomizedProductRepository();

            CustomizedProduct customizedProduct = customizedProductRepository.find(addSlotModelView.customizedProductId);

            if (customizedProduct == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_CUSTOMIZED_PRODUCT_BY_ID, addSlotModelView.customizedProductId));
            }

            checkUserToken(customizedProduct, addSlotModelView.userAuthToken);

            CustomizedDimensions customizedDimensions = CustomizedDimensionsModelViewService.fromModelView(addSlotModelView.slotDimensions);

            customizedProduct.addSlot(customizedDimensions);

            customizedProduct = customizedProductRepository.update(customizedProduct);

            return(CustomizedProductModelViewService.fromEntity(customizedProduct));
        }
        /// <summary>
        /// Updates an instance of Slot.
        /// </summary>
        /// <param name="updateSlotModelView">Instance of UpdateSlotModelView.</param>
        /// <returns>Instance of GetCustomizedProductModelView with updated CustomizedProduct information.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when the CustomizedProduct or the Slot could not be found.</exception>
        /// <exception cref="System.ArgumentException">Thrown when none of the CustomizedProduct's properties are updated.</exception>
        public GetCustomizedProductModelView updateSlot(UpdateSlotModelView updateSlotModelView)
        {
            CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories().createCustomizedProductRepository();

            CustomizedProduct customizedProduct = customizedProductRepository.find(updateSlotModelView.customizedProductId);

            if (customizedProduct == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_CUSTOMIZED_PRODUCT_BY_ID, updateSlotModelView.customizedProductId));
            }

            checkUserToken(customizedProduct, updateSlotModelView.userAuthToken);

            Slot slot = customizedProduct.slots.Where(s => s.Id == updateSlotModelView.slotId).SingleOrDefault();

            if (slot == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_SLOT, updateSlotModelView.slotId));
            }

            bool performedAtLeastOneUpdate = false;

            if (updateSlotModelView.dimensions != null)
            {
                CustomizedDimensions updatedDimensions = CustomizedDimensionsModelViewService.fromModelView(updateSlotModelView.dimensions);

                customizedProduct.resizeSlot(slot, updatedDimensions);

                performedAtLeastOneUpdate = true;
            }

            if (!performedAtLeastOneUpdate)
            {
                throw new ArgumentException(ERROR_NO_UPDATE_PERFORMED);
            }

            customizedProduct = customizedProductRepository.update(customizedProduct);

            return(CustomizedProductModelViewService.fromEntity(customizedProduct));
        }
        /// <summary>
        /// Builds an instance of CustomizedProduct based on the given Product with the data in the given instance of AddCustomizedProductModelView.
        /// </summary>
        /// <param name="addCustomizedProductModelView">AddCustomizedProductModelView containing the CustomizedProduct's information.</param>
        /// <param name="product">Instance of Product.</param>
        /// <returns>Built instance of CustomizedProduct.</returns>
        /// <exception cref="System.ArgumentException">
        /// Thrown when the Material referenced by the CustomizedMaterial is not found or when no CustomizedDimensions are provided.
        /// </exception>
        /// <returns>A new instance of CustomizedProduct.</returns>
        private static CustomizedProduct buildCustomizedProduct(AddCustomizedProductModelView addCustomizedProductModelView, Product product)
        {
            CustomizedProductBuilder customizedProductBuilder = null;

            if (addCustomizedProductModelView.customizedDimensions == null)
            {
                throw new ArgumentException(ERROR_NO_CUSTOMIZED_DIMENSIONS);
            }

            CustomizedDimensions customizedProductDimensions = CustomizedDimensionsModelViewService.fromModelView(addCustomizedProductModelView.customizedDimensions);

            if (addCustomizedProductModelView.userAuthToken == null)
            {
                customizedProductBuilder = CustomizedProductBuilder.createCustomizedProduct(addCustomizedProductModelView.reference, product, customizedProductDimensions);
            }
            else
            {
                customizedProductBuilder = CustomizedProductBuilder
                                           .createCustomizedProduct(addCustomizedProductModelView.userAuthToken, addCustomizedProductModelView.reference, product, customizedProductDimensions);
            }

            //build customized product with optional properties if they're defined
            if (addCustomizedProductModelView.customizedMaterial != null)
            {
                CustomizedMaterial customizedMaterial = CreateCustomizedMaterialService.create(addCustomizedProductModelView.customizedMaterial);

                customizedProductBuilder.withMaterial(customizedMaterial);
            }

            if (addCustomizedProductModelView.designation != null)
            {
                customizedProductBuilder.withDesignation(addCustomizedProductModelView.designation);
            }

            return(customizedProductBuilder.build());
        }
        /// <summary>
        /// Updates an instance of CustomizedProduct with the data provided in the given UpdateCustomizedProductModelView.
        /// </summary>
        /// <param name="updateCustomizedProductModelView">Instance of UpdateCustomizedProductModelView containing updated CustomizedProduct information.</param>
        /// <returns>An instance of GetCustomizedProductModelView with the updated CustomizedProduct information.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when the CustomizedProduct could not be found.</exception>
        /// <exception cref="System.ArgumentException">Thrown when none of the CustomizedProduct's properties are updated.</exception>
        public GetCustomizedProductModelView updateCustomizedProduct(UpdateCustomizedProductModelView updateCustomizedProductModelView)
        {
            CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories().createCustomizedProductRepository();

            CustomizedProduct customizedProduct = customizedProductRepository.find(updateCustomizedProductModelView.customizedProductId);

            if (customizedProduct == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_CUSTOMIZED_PRODUCT_BY_ID, updateCustomizedProductModelView.customizedProductId));
            }

            checkUserToken(customizedProduct, updateCustomizedProductModelView.userAuthToken);

            bool performedAtLeastOneUpdate = false;

            if (updateCustomizedProductModelView.reference != null)
            {
                customizedProduct.changeReference(updateCustomizedProductModelView.reference);
                performedAtLeastOneUpdate = true;
            }

            if (updateCustomizedProductModelView.designation != null)
            {
                customizedProduct.changeDesignation(updateCustomizedProductModelView.designation);
                performedAtLeastOneUpdate = true;
            }

            if (updateCustomizedProductModelView.customizedMaterial != null)
            {
                //TODO: check if only the finish or the color are being updated
                CustomizedMaterial customizedMaterial = CreateCustomizedMaterialService.create(updateCustomizedProductModelView.customizedMaterial);

                customizedProduct.changeCustomizedMaterial(customizedMaterial);
                performedAtLeastOneUpdate = true;
            }

            if (updateCustomizedProductModelView.customizedDimensions != null)
            {
                customizedProduct.changeDimensions(CustomizedDimensionsModelViewService.fromModelView(updateCustomizedProductModelView.customizedDimensions));
                performedAtLeastOneUpdate = true;
            }

            if (updateCustomizedProductModelView.customizationStatus == CustomizationStatus.FINISHED)
            {
                customizedProduct.finalizeCustomization();
                performedAtLeastOneUpdate = true;
            }

            if (!performedAtLeastOneUpdate)
            {
                throw new ArgumentException(ERROR_NO_UPDATE_PERFORMED);
            }

            customizedProduct = customizedProductRepository.update(customizedProduct);

            if (customizedProduct == null)
            {
                throw new ArgumentException(ERROR_UNABLE_TO_SAVE_CUSTOMIZED_PRODUCT);
            }

            return(CustomizedProductModelViewService.fromEntity(customizedProduct));
        }