/// <summary>
        /// Transforms an enumerable of components dto into components
        /// </summary>
        /// <param name="componentsDTO">IEnumerable with the components dto</param>
        /// <returns>IEnumerable with the transformed components dto</returns>
        public IEnumerable <Product> transform(IEnumerable <ComponentDTO> componentsDTO)
        {
            IEnumerable <ProductDTO> productsDTO = extractProductsDTOFromComponentsDTO(componentsDTO);
            List <Product>           products    = new List <Product>(PersistenceContext.repositories().createProductRepository().fetchProductsByID(productsDTO));

            return(products);
        }
        /// <summary>
        /// Finds a Product's Collection of Material.
        /// </summary>
        /// <param name="fetchProductDTO">DTO containing information used for querying.</param>
        /// <returns>GetAllMaterialsModelView with all of the elements in the Product's Collection of Material.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when the Product could not be found.</exception>
        public GetAllMaterialsModelView findProductMaterials(FetchProductDTO fetchProductDTO)
        {
            Product product = PersistenceContext.repositories().createProductRepository().find(fetchProductDTO.id);

            if (product == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, fetchProductDTO.id));
            }

            List <Material> pricedMaterials = new List <Material>();

            if (fetchProductDTO.pricedMaterialsOnly)
            {
                MaterialPriceTableRepository materialPriceTableRepository = PersistenceContext.repositories().createMaterialPriceTableRepository();
                foreach (ProductMaterial productMaterial in product.productMaterials)
                {
                    if (materialPriceTableRepository.fetchCurrentMaterialPrice(productMaterial.materialId) != null)
                    {
                        pricedMaterials.Add(productMaterial.material);
                    }
                }
            }

            if (fetchProductDTO.pricedMaterialsOnly)
            {
                return(pricedMaterials.Count == 0 ? throw new ResourceNotFoundException(NO_PRICED_MATERIALS) : MaterialModelViewService.fromCollection(pricedMaterials));
            }

            return(MaterialModelViewService.fromCollection(product.productMaterials.Select(pm => pm.material)));
        }
        /// <summary>
        /// Deletes an instance of Restriction from a Product's Material.
        /// </summary>
        /// <param name="deleteRestrictionFromProductMaterialMV">DeleteRestrictionFromProductMaterialModelView containing the Product's, the Materials's and the Restriction's persistence identifiers.</param>
        /// <exception cref="ResourceNotFoundException">Thrown when the Product, the Material or the Restriction could not be found.</exception>
        public void deleteRestrictionFromProductMaterial(DeleteProductMaterialRestrictionModelView deleteRestrictionFromProductMaterialMV)
        {
            ProductRepository productRepository = PersistenceContext.repositories().createProductRepository();

            Product product = productRepository.find(deleteRestrictionFromProductMaterialMV.productId);

            if (product == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteRestrictionFromProductMaterialMV.productId));
            }

            ProductMaterial productMaterial = product.productMaterials.
                                              Where(pm => pm.materialId == deleteRestrictionFromProductMaterialMV.materialId).SingleOrDefault();

            if (productMaterial == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MATERIAL_BY_ID, deleteRestrictionFromProductMaterialMV.materialId));
            }

            Restriction restriction = productMaterial.restrictions.Where(r => r.Id == deleteRestrictionFromProductMaterialMV.restrictionId).SingleOrDefault();

            if (restriction == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_RESTRICTION_BY_ID, deleteRestrictionFromProductMaterialMV.restrictionId));
            }

            productMaterial.restrictions.Remove(restriction);

            product = productRepository.update(product);
        }
        /// <summary>
        /// Finds a Product by an identifier, be it a business identifier or a persistence identifier.
        /// </summary>
        /// <param name="fetchProductDTO">DTO containing information used for querying and data conversion.</param>
        /// <returns>An instance of GetProductModelView with the Product's information.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when the Product is not found.</exception>
        public GetProductModelView findProduct(FetchProductDTO fetchProductDTO)
        {
            Product product = null;

            //if no reference value is specified, search by id
            if (Strings.isNullOrEmpty(fetchProductDTO.reference))
            {
                product = PersistenceContext.repositories().createProductRepository().find(fetchProductDTO.id);

                if (product == null)
                {
                    throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, fetchProductDTO.id));
                }
            }
            else
            {
                product = PersistenceContext.repositories().createProductRepository().find(fetchProductDTO.reference);

                if (product == null)
                {
                    throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_REFERENCE, fetchProductDTO.reference));
                }
            }

            return(ProductModelViewService.fromEntity(product, fetchProductDTO.productDTOOptions.requiredUnit));
        }
        /// <summary>
        /// Adds a Restriction to a Product's complementary Product.
        /// </summary>
        /// <param name="addRestrictionToProductComponentMV">AddRestrictionToProductComponentModelView containing the data of the Restriction instance being added.</param>
        /// <returns>GetProductModelView with updated Product information.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when either of the Products could not be found.</exception>
        public GetProductModelView addRestrictionToProductComponent(AddComponentRestrictionModelView addRestrictionToProductComponentMV)
        {
            ProductRepository productRepository = PersistenceContext.repositories().createProductRepository();
            Product           parentProduct     = productRepository.find(addRestrictionToProductComponentMV.fatherProductId);

            if (parentProduct == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addRestrictionToProductComponentMV.fatherProductId));
            }

            //filter product's components rather than accessing the repository
            Product childProduct = parentProduct.components
                                   .Where(component => component.complementaryProduct.Id == addRestrictionToProductComponentMV.childProductId)
                                   .Select(component => component.complementaryProduct).SingleOrDefault();

            if (childProduct == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addRestrictionToProductComponentMV.childProductId));
            }

            Algorithm algorithm = new AlgorithmFactory().createAlgorithm(addRestrictionToProductComponentMV.restriction.algorithmId);

            if (addRestrictionToProductComponentMV.restriction.inputValues != null)
            {
                algorithm.setInputValues(InputValueModelViewService.toDictionary(addRestrictionToProductComponentMV.restriction.inputValues));
            }
            algorithm.ready();
            Restriction restriction = new Restriction(addRestrictionToProductComponentMV.restriction.description, algorithm);

            parentProduct.addComplementaryProductRestriction(childProduct, restriction);
            parentProduct = productRepository.update(parentProduct);
            return(ProductModelViewService.fromEntity(parentProduct));
        }
        /// <summary>
        /// Deletes a Restriction from a Product's Measurement.
        /// </summary>
        /// <param name="deleteRestrictionFromProductMeasurementMV">DeleteRestrictionFromProductMeasurementModelView with the Product's, the Measurement's and the Restriction's persistence identifier.</param>
        /// <exception cref="ResourceNotFoundException">Thrown when the Product, the Measurement or the Restriction could not be found.</exception>
        public void deleteRestrictionFromProductMeasurement(DeleteMeasurementRestrictionModelView deleteRestrictionFromProductMeasurementMV)
        {
            ProductRepository productRepository = PersistenceContext.repositories().createProductRepository();
            Product           product           = productRepository.find(deleteRestrictionFromProductMeasurementMV.productId);

            if (product == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteRestrictionFromProductMeasurementMV.productId));
            }

            Measurement measurement = product.productMeasurements.Where(pm => pm.measurementId == deleteRestrictionFromProductMeasurementMV.measurementId)
                                      .Select(pm => pm.measurement).SingleOrDefault();

            if (measurement == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MEASUREMENT_BY_ID, deleteRestrictionFromProductMeasurementMV.measurementId));
            }

            Restriction restriction = measurement.restrictions.Where(r => r.Id == deleteRestrictionFromProductMeasurementMV.restrictionId).SingleOrDefault();

            if (restriction == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_RESTRICTION_BY_ID, deleteRestrictionFromProductMeasurementMV.restrictionId));
            }

            measurement.restrictions.Remove(restriction);
            product = productRepository.update(product);
        }
        /// <summary>
        /// Creates a new instance of CommercialCatalogue with the data in the given instance of AddCommercialCatalogueModelView.
        /// </summary>
        /// <param name="addCommercialCatalogueModelView">AddCommercialCatalogueModelView with the CommercialCatalogue's data.</param>
        /// <returns>An instance of CommercialCatalogue.</returns>
        /// <exception cref="System.ArgumentException">
        /// Thrown when no CustomizedProductCollection or CustomizedProduct could be found with the provided identifiers.
        /// </exception>
        public static CommercialCatalogue create(AddCommercialCatalogueModelView addCommercialCatalogueModelView)
        {
            string reference   = addCommercialCatalogueModelView.reference;
            string designation = addCommercialCatalogueModelView.designation;

            //check if catalogue collections were specified
            if (!addCommercialCatalogueModelView.catalogueCollections.Any())
            {
                return(new CommercialCatalogue(reference, designation));
            }
            else
            {
                //create repositories so that customized products and collections can be fetched
                CustomizedProductCollectionRepository customizedProductCollectionRepository = PersistenceContext
                                                                                              .repositories().createCustomizedProductCollectionRepository();

                CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories()
                                                                          .createCustomizedProductRepository();

                List <CatalogueCollection> catalogueCollections = new List <CatalogueCollection>();

                foreach (AddCatalogueCollectionModelView addCatalogueCollectionModelView in addCommercialCatalogueModelView.catalogueCollections)
                {
                    CatalogueCollection catalogueCollection = CreateCatalogueCollectionService.create(addCatalogueCollectionModelView);

                    catalogueCollections.Add(catalogueCollection);
                }

                return(new CommercialCatalogue(reference, designation, catalogueCollections));
            }
        }
        /// <summary>
        /// Adds a complementary Product to a Product.
        /// </summary>
        /// <param name="addComponentToProductMV">AddComponentToProductModelView containing the data of the complementary Product being added.</param>
        /// <returns>GetProductModelView with updated Product information.</returns>
        /// <exception cref="ResourceNotFoundException">Throw when either of the Products could not be found.</exception>
        public GetProductModelView addComponentToProduct(AddComponentModelView addComponentToProductMV)
        {
            ProductRepository productRepository     = PersistenceContext.repositories().createProductRepository();
            Product           productToAddComponent = productRepository.find(addComponentToProductMV.fatherProductId);

            if (productToAddComponent == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addComponentToProductMV.fatherProductId));
            }

            Product componentBeingAdded = productRepository.find(addComponentToProductMV.childProductId);

            if (componentBeingAdded == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addComponentToProductMV.childProductId));
            }

            if (addComponentToProductMV.mandatory)
            {
                productToAddComponent.addMandatoryComplementaryProduct(componentBeingAdded);
            }
            else
            {
                productToAddComponent.addComplementaryProduct(componentBeingAdded);
            }

            productToAddComponent = productRepository.update(productToAddComponent);

            return(ProductModelViewService.fromEntity(productToAddComponent));
        }
        /// <summary>
        /// Deletes an instance of CustomizedProduct.
        /// </summary>
        /// <param name="deleteCustomizedProductModelView">Instance of DeleteCustomizedProductModelView containing the CustomizedProduct's identifier.</param>
        /// <exception cref="ResourceNotFoundException">Thrown when no CustomizedProduct could be found with the given identifier.</exception>
        public void deleteCustomizedProduct(DeleteCustomizedProductModelView deleteCustomizedProductModelView)
        {
            CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories().createCustomizedProductRepository();

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

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

            checkUserToken(customizedProduct, deleteCustomizedProductModelView.userAuthToken);

            //check if it's a sub customized product
            if (customizedProduct.insertedInSlotId.HasValue)
            {
                CustomizedProduct parent = customizedProductRepository.findCustomizedProductBySlot(customizedProduct.insertedInSlot);
                Slot insertedInSlot      = customizedProduct.insertedInSlot;

                //remove the sub customized product and update parent
                parent.removeCustomizedProduct(customizedProduct, insertedInSlot);
                customizedProductRepository.update(parent);
            }
            else
            {
                //if it's a base product, just deactivate it
                customizedProductRepository.remove(customizedProduct);
            }
        }
        /// <summary>
        /// Deletes a slot from a CustomizedProduct.
        /// </summary>
        /// <param name="deleteSlotModelView">Instance of DeleteSlotModelView containing both the CustomizedProduct's and Slot's identifiers.</param>
        /// <exception cref="ResourceNotFoundException">Thrown when either no CustomizedProduct or Slot could be found with the given identifiers.</exception>
        public void deleteSlot(DeleteSlotModelView deleteSlotModelView)
        {
            CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories().createCustomizedProductRepository();

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

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

            checkUserToken(customizedProduct, deleteSlotModelView.userAuthToken);

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

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

            customizedProduct.removeSlot(slot);

            customizedProductRepository.update(customizedProduct);
        }
예제 #11
0
        /// <summary>
        /// Deletes a customized product from a customized product collection
        /// </summary>
        /// <param name="modelView"> model view with the necessary information to perform the request</param>
        public static void delete(DeleteCustomizedProductFromCustomizedProductCollectionModelView modelView)
        {
            CustomizedProductCollectionRepository customizedProductCollectionRepository =
                PersistenceContext.repositories()
                .createCustomizedProductCollectionRepository();

            CustomizedProductCollection customizedProductCollection =
                customizedProductCollectionRepository.find(modelView.customizedProductCollectionId);

            checkIfCustomizedProductCollectionWasFound(customizedProductCollection, modelView.customizedProductCollectionId);

            CustomizedProduct customizedProduct =
                PersistenceContext.repositories()
                .createCustomizedProductRepository()
                .find(modelView.customizedProductId);

            if (customizedProduct == null)
            {
                throw new ArgumentException(
                          string.Format(
                              UNABLE_TO_FIND_CUSTOMIZED_PRODUCT,
                              modelView.customizedProductId)
                          );
            }

            customizedProductCollection.removeCustomizedProduct(customizedProduct);

            customizedProductCollectionRepository.update(customizedProductCollection);
        }
예제 #12
0
        /// <summary>
        /// Updates basic information of a material
        /// </summary>
        /// <param name="updateMaterialDTO">UpdateMaterialDTO with the data regarding the material update</param>
        /// <returns>boolean true if the update was successful, false if not</returns>
        public bool updateMaterialBasicInformation(UpdateMaterialDTO updateMaterialDTO)
        {
            MaterialRepository materialRepository   = PersistenceContext.repositories().createMaterialRepository();
            Material           materialBeingUpdated = materialRepository.find(updateMaterialDTO.id);
            bool updatedWithSuccess       = true;
            bool perfomedAtLeastOneUpdate = false;

            if (updateMaterialDTO.reference != null)
            {
                updatedWithSuccess      &= materialBeingUpdated.changeReference(updateMaterialDTO.reference);
                perfomedAtLeastOneUpdate = true;
            }
            if (updateMaterialDTO.designation != null)
            {
                updatedWithSuccess      &= materialBeingUpdated.changeDesignation(updateMaterialDTO.designation);
                perfomedAtLeastOneUpdate = true;
            }
            if (updateMaterialDTO.image != null)
            {
                updatedWithSuccess      &= materialBeingUpdated.changeImage(updateMaterialDTO.image);
                perfomedAtLeastOneUpdate = true;
            }
            if (!perfomedAtLeastOneUpdate || !updatedWithSuccess)
            {
                return(false);
            }
            updatedWithSuccess &= materialRepository.update(materialBeingUpdated) != null;
            return(updatedWithSuccess);
        }
예제 #13
0
        /// <summary>
        /// Updates the Material on the MaterialRepository given its data.
        /// </summary>
        /// <param name="materialDTO">DTO that holds all info about the Material</param>
        /// <returns>DTO that represents the updated Material</returns>
        //TODO: user might not want to update all aspects of the material, and as such the dto might be incomplete; check which fields are empty
        public MaterialDTO updateMaterial(MaterialDTO materialDTO)
        {
            MaterialRepository repository = PersistenceContext.repositories().createMaterialRepository();
            Material           material   = repository.find(materialDTO.id);

            if (material == null)
            {
                return(null);
            }

            material.changeReference(materialDTO.reference);
            material.changeDesignation(materialDTO.designation);
            material.changeImage(materialDTO.image);

            foreach (ColorDTO colorDTO in materialDTO.colors)
            {
                string name  = colorDTO.name;
                byte   red   = colorDTO.red;
                byte   green = colorDTO.green;
                byte   blue  = colorDTO.blue;
                byte   alpha = colorDTO.alpha;
                material.addColor(Color.valueOf(name, red, green, blue, alpha));
            }

            foreach (FinishDTO finishDTO in materialDTO.finishes)
            {
                material.addFinish(Finish.valueOf(finishDTO.description, finishDTO.shininess));
            }
            Material mat = repository.update(material);

            return(mat == null ? null : mat.toDTO());
        }
예제 #14
0
        /// <summary>
        /// Fetches a Material from the MaterialRepository given its ID.
        /// </summary>
        /// <param name = "materialID">the Material's ID</param>
        /// <returns>DTO that represents the Material</returns>
        public MaterialDTO findMaterialByID(long materialID, bool pricedFinishesOnly)
        {
            Material material = PersistenceContext.repositories().createMaterialRepository().find(materialID);

            if (material == null)
            {
                throw new ResourceNotFoundException(ERROR_MATERIAL_NOT_FOUND);
            }

            if (pricedFinishesOnly)
            {
                FinishPriceTableRepository finishPriceTableRepository =
                    PersistenceContext.repositories().createFinishPriceTableRepository();
                List <Finish> pricedFinishes = new List <Finish>();
                foreach (Finish finish in material.Finishes)
                {
                    if (finishPriceTableRepository.fetchCurrentMaterialFinishPrice(finish.Id) != null)
                    {
                        pricedFinishes.Add(finish);
                    }
                }

                if (pricedFinishes.Count == 0)
                {
                    throw new ResourceNotFoundException(NO_PRICED_FINISHES);
                }

                material.Finishes.Clear();
                material.Finishes.AddRange(pricedFinishes);
                return(material.toDTO());
            }

            return(PersistenceContext.repositories().createMaterialRepository().find(materialID).toDTO());
        }
예제 #15
0
        /// <summary>
        /// Retrieves all the CustomizedProducts added to the CatalogueCollection.
        /// </summary>
        /// <param name="findCatalogueCollectionModelView">FindCatalogueCollectionModelView with the CommercialCatalogue's and CatalogueCollection's identifiers.</param>
        /// <returns>Instance of GetAllCustomizedProductsModelView representing all of the CatalogueCollectionProduct's.</returns>
        /// <exception cref="ResourceNotFoundException">
        /// Thrown when no CommercialCatalogue or CatalogueCollection could be found with the provided identifiers or when the IEnumerable of CatalogueCollectionProduct is empty.
        /// </exception>
        public GetAllCustomizedProductsModelView findCatalogueCollectionProducts(FindCatalogueCollectionModelView findCatalogueCollectionModelView)
        {
            CommercialCatalogueRepository catalogueRepository = PersistenceContext.repositories().createCommercialCatalogueRepository();

            CommercialCatalogue commercialCatalogue = catalogueRepository.find(findCatalogueCollectionModelView.commercialCatalogueId);

            if (commercialCatalogue == null)
            {
                throw new ResourceNotFoundException(string.Format(CATALOGUE_NOT_FOUND_BY_ID, findCatalogueCollectionModelView.commercialCatalogueId));
            }

            CatalogueCollection catalogueCollection = commercialCatalogue.catalogueCollectionList
                                                      .Where(cc => cc.customizedProductCollectionId == findCatalogueCollectionModelView.customizedProductCollectionId).SingleOrDefault();

            if (catalogueCollection == null)
            {
                throw new ResourceNotFoundException(string.Format(
                                                        CATALOGUE_COLLECTION_NOT_FOUND_BY_ID, findCatalogueCollectionModelView.customizedProductCollectionId,
                                                        findCatalogueCollectionModelView.commercialCatalogueId)
                                                    );
            }

            IEnumerable <CustomizedProduct> customizedProducts = catalogueCollection.catalogueCollectionProducts.Select(ccp => ccp.customizedProduct).ToList();

            if (!customizedProducts.Any())
            {
                throw new ResourceNotFoundException(CATALOGUE_COLLECTION_PRODUCTS_NOT_FOUND);
            }

            return(CustomizedProductModelViewService.fromCollection(customizedProducts));
        }
        /// <summary>
        /// Finds a Product's Material's Collection of Restriction.
        /// </summary>
        /// <param name="productMaterialModelView">GetProductMaterialModelView with the Product and Material persistence identifiers.</param>
        /// <returns>An instance of GetAllRestrictionsModelView containing the information of all the Materials's restrictions.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when either the Product or the Material could not be found.</exception>
        public GetAllRestrictionsModelView findMaterialRestrictions(FindProductMaterialModelView productMaterialModelView)
        {
            Product product = PersistenceContext.repositories().createProductRepository().find(productMaterialModelView.productId);

            if (product == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, productMaterialModelView.productId));
            }

            ProductMaterial productMaterial = product.productMaterials
                                              .Where(pm => pm.materialId == productMaterialModelView.materialId).SingleOrDefault();

            if (productMaterial == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MATERIAL_BY_ID, productMaterialModelView.materialId));
            }

            //if no restrictions are found, throw an exception so that a 404 code is sent
            if (!productMaterial.restrictions.Any())
            {
                throw new ResourceNotFoundException(ERROR_UNABLE_TO_FIND_RESTRICTIONS);
            }

            return(RestrictionModelViewService.fromCollection(productMaterial.restrictions));
        }
예제 #17
0
        /// <summary>
        /// Removes an instance of CatalogueCollectionProduct from a CommercialCatalogue's CatalogueCollection.
        /// </summary>
        /// <param name="deleteCatalogueCollectionProductModelView">DeleteCatalogueCollectionProductModelView with the CustomizedProduct's, CustomizedProductCollection's and CommercialCatalogue's identifiers.</param>
        ///<exception cref="ResourceNotFoundException">Thrown when no CommercialCatalogue, CatalogueCollection or CatalogueCollectionProduct could be found.</exception>
        public void deleteCatalogueCollectionProduct(DeleteCatalogueCollectionProductModelView deleteCatalogueCollectionProductModelView)
        {
            CommercialCatalogueRepository catalogueRepository = PersistenceContext.repositories().createCommercialCatalogueRepository();

            CommercialCatalogue commercialCatalogue = catalogueRepository.find(deleteCatalogueCollectionProductModelView.commercialCatalogueId);

            if (commercialCatalogue == null)
            {
                throw new ResourceNotFoundException(string.Format(CATALOGUE_NOT_FOUND_BY_ID, deleteCatalogueCollectionProductModelView.commercialCatalogueId));
            }

            CatalogueCollection catalogueCollection = commercialCatalogue.catalogueCollectionList
                                                      .Where(cc => cc.customizedProductCollection.Id == deleteCatalogueCollectionProductModelView.customizedProductCollectionId).SingleOrDefault();

            if (catalogueCollection == null)
            {
                throw new ResourceNotFoundException(string.Format(
                                                        CATALOGUE_COLLECTION_NOT_FOUND_BY_ID, deleteCatalogueCollectionProductModelView.customizedProductCollectionId, deleteCatalogueCollectionProductModelView.commercialCatalogueId
                                                        ));
            }

            CustomizedProduct customizedProduct = catalogueCollection.catalogueCollectionProducts
                                                  .Where(ccp => ccp.customizedProductId == deleteCatalogueCollectionProductModelView.customizedProductId)
                                                  .Select(ccp => ccp.customizedProduct).SingleOrDefault();

            if (customizedProduct == null)
            {
                throw new ResourceNotFoundException(string.Format(
                                                        CUSTOMIZED_PRODUCT_NOT_FOUND_BY_ID, deleteCatalogueCollectionProductModelView.customizedProductCollectionId, deleteCatalogueCollectionProductModelView.customizedProductId
                                                        ));
            }

            catalogueCollection.removeCustomizedProduct(customizedProduct);
            catalogueRepository.update(commercialCatalogue);
        }
        /// <summary>
        /// Adds a Restriction to a Product's Measurement.
        /// </summary>
        /// <param name="addRestrictionToProductMeasurementMV">AddRestrictionToProductMeasurementModelView with the Product's and Measurement's persistence identifiers
        /// as well as the Restriction's data.</param>
        /// <returns>GetProductModelView with updated Product information.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when either the Product or the Measurement could not be found.</exception>
        public GetProductModelView addRestrictionToProductMeasurement(AddMeasurementRestrictionModelView addRestrictionToProductMeasurementMV)
        {
            ProductRepository productRepository = PersistenceContext.repositories().createProductRepository();

            Product product = productRepository.find(addRestrictionToProductMeasurementMV.productId);

            if (product == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addRestrictionToProductMeasurementMV.productId));
            }

            Measurement measurement = product.productMeasurements
                                      .Where(pm => pm.measurementId == addRestrictionToProductMeasurementMV.measurementId).Select(pm => pm.measurement).SingleOrDefault();

            if (measurement == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MEASUREMENT_BY_ID, addRestrictionToProductMeasurementMV.measurementId));
            }

            Algorithm algorithm = new AlgorithmFactory().createAlgorithm(addRestrictionToProductMeasurementMV.restriction.algorithmId);

            if (addRestrictionToProductMeasurementMV.restriction.inputValues != null)
            {
                algorithm.setInputValues(InputValueModelViewService.toDictionary(addRestrictionToProductMeasurementMV.restriction.inputValues));
            }
            algorithm.ready();
            Restriction restriction = new Restriction(addRestrictionToProductMeasurementMV.restriction.description, algorithm);

            product.addMeasurementRestriction(measurement, restriction);

            product = productRepository.update(product);

            return(ProductModelViewService.fromEntity(product));
        }
예제 #19
0
        /// <summary>
        /// Updates a customized product collection's basic information
        /// </summary>
        /// <param name="modelView">model view with the updates for a customized product collection</param>
        /// <returns>Updated customized product collection or throws an exception if an error occurs</returns>
        public static CustomizedProductCollection update(UpdateCustomizedProductCollectionModelView modelView)
        {
            CustomizedProductCollectionRepository customizedProductCollectionRepository =
                PersistenceContext.repositories()
                .createCustomizedProductCollectionRepository();


            CustomizedProductCollection customizedProductCollection =
                customizedProductCollectionRepository.find(modelView.customizedProductCollectionId);

            checkIfCustomizedProductCollectionWasFound(
                customizedProductCollection, modelView.customizedProductCollectionId
                );

            customizedProductCollection.changeName(modelView.name);

            CustomizedProductCollection updatedCustomizedProductCollection =
                customizedProductCollectionRepository.update(customizedProductCollection);

            checkIfUpdatedCustomizedProductCollectionWasSaved(
                updatedCustomizedProductCollection, modelView.customizedProductCollectionId
                );

            return(updatedCustomizedProductCollection);
        }
        /// <summary>
        /// Adds a Restriction to a Product's Material.
        /// </summary>
        /// <param name="addRestrictionModelView">AddRestrictionToProductMaterialModelView containing the data of the Restriction instance being added.</param>
        /// <returns>GetProductModelView with updated Product information.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when the Product or the Material could not be found.</exception>
        public GetProductModelView addRestrictionToProductMaterial(AddProductMaterialRestrictionModelView addRestrictionModelView)
        {
            ProductRepository productRepository = PersistenceContext.repositories().createProductRepository();
            Product           product           = productRepository.find(addRestrictionModelView.productId);

            if (product == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addRestrictionModelView.productId));
            }

            //filter product's materials rather than accessing the repository
            Material material = product.productMaterials
                                .Where(productMaterial => productMaterial.materialId == addRestrictionModelView.materialId)
                                .Select(productMaterial => productMaterial.material).SingleOrDefault();

            if (material == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MATERIAL_BY_ID, addRestrictionModelView.materialId));
            }

            Algorithm algorithm = new AlgorithmFactory().createAlgorithm(addRestrictionModelView.restriction.algorithmId);

            if (addRestrictionModelView.restriction.inputValues != null)
            {
                algorithm.setInputValues(InputValueModelViewService.toDictionary(addRestrictionModelView.restriction.inputValues));
            }
            algorithm.ready();
            Restriction restriction = new Restriction(addRestrictionModelView.restriction.description, algorithm);

            product.addMaterialRestriction(material, restriction);
            product = productRepository.update(product);
            return(ProductModelViewService.fromEntity(product));
        }
예제 #21
0
        /// <summary>
        /// Adds complementary Products to an instance of Product.
        /// </summary>
        /// <param name="product">Product to which the complementary Products will be added.</exception>
        /// <param name="componentModelViews">ModelViews containing component information.</param>
        /// <returns>Product updated with a list of complementary Products.</returns>
        /// <exception cref="System.ArgumentException">Thrown when any complementary Product could not be found.</exception>
        private static Product addComplementaryProducts(Product product, IEnumerable <AddComponentModelView> componentModelViews)
        {
            ProductRepository productRepository = PersistenceContext.repositories().createProductRepository();

            foreach (AddComponentModelView addComponentToProductModelView in componentModelViews)
            {
                if (addComponentToProductModelView == null)
                {
                    throw new ArgumentException(ERROR_NULL_COMPONENT);
                }

                Product complementaryProduct = productRepository.find(addComponentToProductModelView.childProductId);

                if (complementaryProduct == null)
                {
                    throw new ArgumentException(string.Format(ERROR_PRODUCT_NOT_FOUND, addComponentToProductModelView.childProductId));
                }

                if (addComponentToProductModelView.mandatory)
                {
                    product.addMandatoryComplementaryProduct(complementaryProduct);
                }
                else
                {
                    product.addComplementaryProduct(complementaryProduct);
                }
            }

            return(product);
        }
        /// <summary>
        /// Deletes an instance of Restriction from a Product's Component.
        /// </summary>
        /// <param name="deleteRestrictionFromProductComponentMV">DeleteRestrictionFromProductComponentDTO with the restriction deletion information</param>
        /// <exception cref="ResourceNotFoundException">Thrown when either of the Products or the Restriction could not be found.</exception>
        public void deleteRestrictionFromProductComponent(DeleteComponentRestrictionModelView deleteRestrictionFromProductComponentMV)
        {
            ProductRepository productRepository = PersistenceContext.repositories().createProductRepository();
            Product           productWithComponentBeingDeletedRestriction = productRepository.find(deleteRestrictionFromProductComponentMV.fatherProductId);

            if (productWithComponentBeingDeletedRestriction == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteRestrictionFromProductComponentMV.fatherProductId));
            }

            Component productComponentBeingDeletedRestriction = productWithComponentBeingDeletedRestriction.components
                                                                .Where(component => component.complementaryProduct.Id == deleteRestrictionFromProductComponentMV.childProductId).SingleOrDefault();

            if (productComponentBeingDeletedRestriction == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, deleteRestrictionFromProductComponentMV.childProductId));
            }

            Restriction restriction = productComponentBeingDeletedRestriction.restrictions
                                      .Where(r => r.Id == deleteRestrictionFromProductComponentMV.restrictionId).SingleOrDefault();

            if (restriction == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_RESTRICTION_BY_ID, deleteRestrictionFromProductComponentMV.restrictionId));
            }

            productComponentBeingDeletedRestriction.restrictions.Remove(restriction);

            productWithComponentBeingDeletedRestriction = productRepository.update(productWithComponentBeingDeletedRestriction);
        }
        public GetComponentModelView findProductComponent(FindComponentModelView findComponentModelView)
        {
            ProductRepository productRepository = PersistenceContext.repositories().createProductRepository();

            Product product = productRepository.find(findComponentModelView.fatherProductId);

            if (product == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, findComponentModelView.fatherProductId));
            }

            //if no components are found, throw an exception so that a 404 code is sent
            if (!product.components.Any())
            {
                throw new ResourceNotFoundException(ERROR_UNABLE_TO_FIND_COMPONENTS);
            }

            Component component = product.components.Where(c => c.complementaryProductId == findComponentModelView.childProductId).SingleOrDefault();

            if (component == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, findComponentModelView.childProductId));
            }

            return(ComponentModelViewService.fromEntity(component, findComponentModelView.unit));
        }
        /// <summary>
        /// Finds a Product's Measurement's Collection of Restriction.
        /// </summary>
        /// <param name="productMeasurementModelView">GetProductMeasurementModelView with the Product's and the Measurement's persistence identifier.</param>
        /// <returns>An instance of GetAllRestrictionsModelView containing the information of all the Measurement's restrictions.</returns>
        /// <exception cref="ResourceNotFoundException">Thrown when either the Product or the Measurement could not be found.</exception>
        public GetAllRestrictionsModelView findMeasurementRestrictions(FindMeasurementModelView productMeasurementModelView)
        {
            Product product = PersistenceContext.repositories().createProductRepository().find(productMeasurementModelView.productId);

            if (product == null)
            {
                throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, productMeasurementModelView.productId));
            }

            //filter through the product's measurements
            Measurement measurement = product.productMeasurements
                                      .Where(productMeasurement => productMeasurement.measurementId == productMeasurementModelView.measurementId)
                                      .Select(productMeasurement => productMeasurement.measurement).SingleOrDefault();

            if (measurement == null)
            {
                throw new ResourceNotFoundException(
                          string.Format(ERROR_UNABLE_TO_FIND_MEASUREMENT_BY_ID, productMeasurementModelView.measurementId)
                          );
            }

            //if no restrictions are found, throw an exception so that a 404 code is sent
            if (!measurement.restrictions.Any())
            {
                throw new ResourceNotFoundException(ERROR_UNABLE_TO_FIND_RESTRICTIONS);
            }

            return(RestrictionModelViewService.fromCollection(measurement.restrictions));
        }
        public static GetCurrentMaterialFinishPriceModelView fromMaterialFinish(GetCurrentMaterialFinishPriceModelView modelView, IHttpClientFactory clientFactory)
        {
            Material material = PersistenceContext.repositories().createMaterialRepository().find(modelView.finish.materialId);

            if (material == null)
            {
                throw new ResourceNotFoundException(string.Format(MATERIAL_NOT_FOUND, modelView.finish.materialId));
            }

            MaterialPriceTableEntry currentMaterialPrice = PersistenceContext.repositories().createMaterialPriceTableRepository().fetchCurrentMaterialPrice(modelView.finish.materialId);

            if (currentMaterialPrice == null)
            {
                throw new ResourceNotFoundException(string.Format(NO_CURRENT_MATERIAL_PRICE, modelView.finish.materialId));
            }

            foreach (Finish finish in material.Finishes)
            {
                if (finish.Id == modelView.finish.id)
                {
                    FinishPriceTableEntry currentMaterialFinishPrice = PersistenceContext.repositories().createFinishPriceTableRepository().fetchCurrentMaterialFinishPrice(modelView.finish.id);

                    if (currentMaterialFinishPrice == null)
                    {
                        throw new ResourceNotFoundException(string.Format(NO_CURRENT_FINISH_PRICE, modelView.finish.id, modelView.finish.materialId));
                    }

                    GetCurrentMaterialFinishPriceModelView currentMaterialFinishPriceModelView = new GetCurrentMaterialFinishPriceModelView();
                    currentMaterialFinishPriceModelView.finish                  = new GetMaterialFinishModelView();
                    currentMaterialFinishPriceModelView.currentPrice            = new PriceModelView();
                    currentMaterialFinishPriceModelView.timePeriod              = new TimePeriodModelView();
                    currentMaterialFinishPriceModelView.tableEntryId            = currentMaterialFinishPrice.Id;
                    currentMaterialFinishPriceModelView.finish.materialId       = material.Id;
                    currentMaterialFinishPriceModelView.finish.id               = finish.Id;
                    currentMaterialFinishPriceModelView.finish.description      = finish.description;
                    currentMaterialFinishPriceModelView.finish.shininess        = finish.shininess;
                    currentMaterialFinishPriceModelView.timePeriod.startingDate = LocalDateTimePattern.GeneralIso.Format(currentMaterialFinishPrice.timePeriod.startingDate);
                    currentMaterialFinishPriceModelView.timePeriod.endingDate   = LocalDateTimePattern.GeneralIso.Format(currentMaterialFinishPrice.timePeriod.endingDate);
                    if (modelView.currentPrice.currency == null || modelView.currentPrice.area == null)
                    {
                        currentMaterialFinishPriceModelView.currentPrice.value    = currentMaterialFinishPrice.price.value;
                        currentMaterialFinishPriceModelView.currentPrice.area     = CurrencyPerAreaConversionService.getBaseArea();
                        currentMaterialFinishPriceModelView.currentPrice.currency = CurrencyPerAreaConversionService.getBaseCurrency();
                    }
                    else
                    {
                        Task <double> convertedValueTask =
                            new CurrencyPerAreaConversionService(clientFactory)
                            .convertDefaultCurrencyPerAreaToCurrencyPerArea(currentMaterialFinishPrice.price.value, modelView.currentPrice.currency, modelView.currentPrice.area);
                        convertedValueTask.Wait();
                        currentMaterialFinishPriceModelView.currentPrice.value    = convertedValueTask.Result;
                        currentMaterialFinishPriceModelView.currentPrice.currency = modelView.currentPrice.currency;
                        currentMaterialFinishPriceModelView.currentPrice.area     = modelView.currentPrice.area;
                    }
                    return(currentMaterialFinishPriceModelView);
                }
            }
            throw new ResourceNotFoundException(string.Format(FINISH_NOT_FOUND, modelView.finish.id, modelView.finish.materialId));
        }
        /// <summary>
        /// Transforms a ComponentDTO into a component
        /// </summary>
        /// <param name="componentDTO">ComponentDTO with the component information</param>
        /// <returns>Component with the transformed component dto</returns>
        public Component transform(ComponentDTO componentDTO)
        {
            ProductDTO productDTO = componentDTO.product;
            Product    product    = PersistenceContext.repositories().createProductRepository().find(productDTO.id);

            //TODO:RESTRICTIONS ARE STILL IN DEVELOPMENT
            // new Component(product);
            return(null);
        }
        /// <summary>
        /// Ensures that a product category is a leaf
        /// </summary>
        /// <param name="productCategory">ProductCategory with the product category being ensured that is leaf</param>
        public static void ensureProductCategoryIsLeaf(ProductCategory productCategory)
        {
            IEnumerable <ProductCategory> productCategories = PersistenceContext.repositories().createProductCategoryRepository().findSubCategories(productCategory);

            if (!Collections.isEnumerableNullOrEmpty(productCategories))
            {
                throw new InvalidOperationException(INVALID_PRODUCT_CATEGORY_SUBCATEGORIES_FETCH);
            }
        }
        /// <summary>
        /// Retrieves a ModelView representation of the instance of ProductCategory with a matching database identifier.
        /// </summary>
        /// <param name="id">Database identifier.</param>
        /// <returns>ModelView representation of the ProductCategory with the matching database identifier
        /// or null if no ProductCategory with a matching id was found.</returns>
        public GetProductCategoryModelView findByDatabaseId(long id)
        {
            ProductCategory category = PersistenceContext.repositories().createProductCategoryRepository().find(id);

            if (category == null)
            {   //category might not exist
                throw new ArgumentException(ERROR_CATEGORY_NOT_FOUND_ID);
            }

            return(ProductCategoryModelViewService.fromEntity(category));
        }
        /// <summary>
        /// Retrieves a ModelView representation of the instance of ProductCategory with a matching business identifier (name).
        /// </summary>
        /// <param name="name">Business identifier (name).</param>
        /// <returns>ModelView representation of the instance of ProductCategory with a matching business identifier.</returns>
        public GetProductCategoryModelView findByName(string name)
        {
            ProductCategory category = PersistenceContext.repositories().createProductCategoryRepository().find(name);

            if (category == null)
            {   //category might not exist
                throw new ArgumentException(ERROR_CATEGORY_NOT_FOUND_NAME);
            }

            return(ProductCategoryModelViewService.fromEntity(category));
        }
        /// <summary>
        /// Retrieves all instances of ProductCategory that are leaves.
        /// </summary>
        /// <returns>GetAllProductCategoriesModelView with data regarding all of the leaf ProductCategory.</returns>
        /// <exception cref="ResourceNotFoundException">Throw when no leaf ProductCategory is found.</exception>
        public GetAllProductCategoriesModelView findLeaves()
        {
            IEnumerable <ProductCategory> leaves = PersistenceContext.repositories().createProductCategoryRepository().findLeaves();

            if (!leaves.Any())
            {
                throw new ResourceNotFoundException(ERROR_NO_CATEGORIES_FOUND);
            }

            return(ProductCategoryModelViewService.fromCollection(leaves));
        }