/// <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)); }
/// <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)); }
/// <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> /// 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> /// 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> /// 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); }
/// <summary> /// Retrieves a Collection of all the Products in the Product Repository. /// </summary> /// <returns>An instance of GetAllProductsModelView with all the Products.</returns> /// <exception cref="ResourceNotFoundException">Thrown when no instance of Product exists in the repository.</exception> public GetAllProductsModelView findAllProducts() { IEnumerable <Product> products = PersistenceContext.repositories().createProductRepository().findAll(); if (!products.Any()) { throw new ResourceNotFoundException(ERROR_NO_PRODUCTS_FOUND); } return(ProductModelViewService.fromCollection(products)); }
/// <summary> /// Adds a new instance of Product. /// </summary> /// <param name="addProductMV">AddProductModelView with the product information</param> /// <returns>GetProductModelView with the created product, null if the product was not created</returns> public GetProductModelView addProduct(AddProductModelView addProductMV) { Product newProduct = CreateProductService.create(addProductMV); newProduct = PersistenceContext.repositories().createProductRepository().save(newProduct); //an entity will be null after save if an equal entity was found in the repository if (newProduct == null) { throw new ArgumentException(ERROR_UNABLE_TO_SAVE_PRODUCT); } return(ProductModelViewService.fromEntity(newProduct)); }
/// <summary> /// Adds a Measurement to a Product. /// </summary> /// <param name="addMeasurementToProductMV">AddMeasurementToProductModelView containing the data of the Measurement instance being added.</param> /// <returns>GetProductModelView with updated Product information.</returns> /// <exception cref="ResourceNotFoundException">Thrown when the Product could not be found.</exception> public GetProductModelView addMeasurementToProduct(AddMeasurementModelView addMeasurementToProductMV) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product product = PersistenceContext.repositories().createProductRepository().find(addMeasurementToProductMV.productId); if (product == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addMeasurementToProductMV.productId)); } Measurement measurement = MeasurementModelViewService.fromModelView(addMeasurementToProductMV); product.addMeasurement(measurement); product = productRepository.update(product); return(ProductModelViewService.fromEntity(product)); }
/// <summary> /// Adds a Material to a Product. /// </summary> /// <param name="addMaterialToProductMV">AddMaterialToProductModelView with the material addition information</param> /// <returns>GetProductModelView with updated Product information.</returns> ///<exception cref="ResourceNotFoundException">Thrown when either the Product or the Material could not be found.</exception> public GetProductModelView addMaterialToProduct(AddProductMaterialModelView addMaterialToProductMV) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product productToAddMaterial = productRepository.find(addMaterialToProductMV.productId); if (productToAddMaterial == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, addMaterialToProductMV.productId)); } Material materialBeingAdded = PersistenceContext.repositories().createMaterialRepository().find(addMaterialToProductMV.materialId); if (materialBeingAdded == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_MATERIAL_BY_ID, addMaterialToProductMV.materialId)); } productToAddMaterial.addMaterial(materialBeingAdded); productToAddMaterial = productRepository.update(productToAddMaterial); return(ProductModelViewService.fromEntity(productToAddMaterial)); }
/// <summary> /// Retrieves all possible components for a certain slot of a customized product /// </summary> /// <param name="customizedProductID">customized product to base restrictions on</param> /// <param name="slotID">selected slot</param> /// <returns>possible components for selected slot</returns> public GetPossibleComponentsModelView fetchPossibleComponents(FindPossibleComponentsModelView findComponentsMV) { CustomizedProductRepository customizedProductRepository = PersistenceContext.repositories().createCustomizedProductRepository(); CustomizedProduct customizedProduct = customizedProductRepository.find(findComponentsMV.customizedProductID); if (customizedProduct == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_CUSTOMIZED_PRODUCT_BY_ID, findComponentsMV.customizedProductID)); } Slot slot = customizedProduct.slots.Where(s => s.Id == findComponentsMV.slotID).SingleOrDefault(); if (slot == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_SLOT, findComponentsMV.slotID)); } List <Product> restrictedProducts = (List <Product>)customizedProduct.product.getRestrictedComponents(customizedProduct, slot); if (Collections.isEnumerableNullOrEmpty(restrictedProducts)) { throw new InvalidOperationException(NO_POSSIBLE_COMPONENTS); } return(ProductModelViewService.possibleComponentsFromCollection(restrictedProducts)); }
/// <summary> /// Updates the properties of a product /// </summary> /// <param name="updateProductPropertiesModelView">UpdateProductPropertiesModelView with the data regarding the product update</param> /// <returns>GetProductModelView with the updated product information</returns> public GetProductModelView updateProductProperties(UpdateProductPropertiesModelView updateProductPropertiesModelView) { ProductRepository productRepository = PersistenceContext.repositories().createProductRepository(); Product productBeingUpdated = productRepository.find(updateProductPropertiesModelView.id); if (productBeingUpdated == null) { throw new ResourceNotFoundException(string.Format(ERROR_UNABLE_TO_FIND_PRODUCT_BY_ID, updateProductPropertiesModelView.id)); } //FetchEnsurance.ensureProductFetchWasSuccessful(productBeingUpdated); bool perfomedAtLeastOneUpdate = false; if (updateProductPropertiesModelView.reference != null) { productBeingUpdated.changeProductReference(updateProductPropertiesModelView.reference); perfomedAtLeastOneUpdate = true; } if (updateProductPropertiesModelView.designation != null) { productBeingUpdated.changeProductDesignation(updateProductPropertiesModelView.designation); perfomedAtLeastOneUpdate = true; } if (updateProductPropertiesModelView.modelFilename != null) { productBeingUpdated.changeModelFilename(updateProductPropertiesModelView.modelFilename); perfomedAtLeastOneUpdate = true; } if (updateProductPropertiesModelView.categoryId.HasValue) { ProductCategory categoryToUpdate = PersistenceContext.repositories().createProductCategoryRepository().find(updateProductPropertiesModelView.categoryId.Value); if (categoryToUpdate == null) { throw new ArgumentException(string.Format(ERROR_UNABLE_TO_FIND_CATEGORY_BY_ID, updateProductPropertiesModelView.categoryId.Value)); } productBeingUpdated.changeProductCategory(categoryToUpdate); perfomedAtLeastOneUpdate = true; } //?Should an error be thrown if no update was performed or should it just carry on? //UpdateEnsurance.ensureAtLeastOneUpdateWasPerformed(perfomedAtLeastOneUpdate); if (!perfomedAtLeastOneUpdate) { throw new ArgumentException(ERROR_NO_UPDATE_PERFORMED); } productBeingUpdated = productRepository.update(productBeingUpdated); //the updated product will only be null, if the reference was changed to match a previosuly added product if (productBeingUpdated == null) { throw new ArgumentException(ERROR_UNABLE_TO_UPDATE_PRODUCT); } //UpdateEnsurance.ensureProductUpdateWasSuccessful(productBeingUpdated); return(ProductModelViewService.fromEntity(productBeingUpdated)); }