public bool AddProduct(Product product)
        {
            logger.logInfo("Try to add a new product in db.");
            if (product == null)
            {
                ValidationException e = new ValidationException("Product is null");
                logger.logError(e);
                throw e;
            }
            if (product.Categories == null)
            {
                ValidationException e = new ValidationException("Product's categories are null");
                logger.logError(e);
                throw e;
            }
            CategoryService categoryService = new CategoryService();
            foreach (Category categ in product.Categories)
            {
                if (categoryService.GetCategoryById(categ.IdCategory) == null)
                {
                    EntityDoesNotExistException e = new EntityDoesNotExistException("One or more categories of the product do not exist");
                    logger.logError(e);
                    throw e;
                }
            }
            var validationResults = Validation.Validate<Product>(product);
            if (product.Name != null)
            {
                if (!validationResults.IsValid)
                {
                    ValidationException e = new ValidationException("Invalid product's name/description.");
                    logger.logError(e);
                    throw e;
                }
                else
                {
                    try
                    {
                        DataMapperFactoryMethod.GetCurrentFactory().ProductFactory.AddProduct(product);
                    }
                    catch (DuplicateException duplicateException)
                    {
                        logger.logError(duplicateException);
                        throw duplicateException;
                    }
                }
            }
            else
            {
                ValidationException e = new ValidationException("Invalid product's name - null.");
                logger.logError(e);
                throw e;
            }

            logger.logInfo("Product added successfully!");
            return true;
        }
 public bool AddCategory(Category category)
 {
     logger.logInfo("Try to add a new category in db.");
     if (category == null)
     {
         ValidationException e = new ValidationException("Category is null");
         logger.logError(e);
         throw e;
     }
     var validationResults = Validation.Validate<Category>(category);
     if (category.Name != null)
     {
         if (!validationResults.IsValid)
         {
             ValidationException e = new ValidationException("Invalid category's name/description.");
             logger.logError(e);
            foreach (ValidationResult vr in validationResults)
                 Console.WriteLine(vr.Message);
             throw e;
         }
         else
         {
             try
             {
                 DataMapperFactoryMethod.GetCurrentFactory().CategoryFactory.AddCategory(category);
             }
              catch (DuplicateException duplicateException)
              {
                  logger.logError(duplicateException);
                  throw duplicateException;
              }
              catch (EntityDoesNotExistException entityDoesNotExist)
              {
                  logger.logError(entityDoesNotExist);
                  throw entityDoesNotExist;
              }
              logger.logInfo("Category added successfully!");
         }
     }
     else
     {
         ValidationException e = new ValidationException("Invalid category's name - null.");
         logger.logError(e);
         throw e;
     }
     return true;
 }
 public bool UpdateCategoryDescription(int id, String description)
 {
     logger.logInfo("Try to update category whit the id " + id);
      Category category = this.GetCategoryById(id);
      if (category == null)
      {
          EntityDoesNotExistException e = new EntityDoesNotExistException("Category is null");
          logger.logError(e);
          throw e;
      }
      String oldDescription = category.Description;
      category.Description = description;
      var validationResults = Validation.Validate<Category>(category);
      if (validationResults.IsValid)
      {
          category.Description = oldDescription;
          if (category.Description != description)
          {
              DataMapperFactoryMethod.GetCurrentFactory().CategoryFactory.UpdateCategoryDescription(category, description);
              logger.logInfo("Category successfully updated");
          }
      }
      else
      {
          ValidationException e = new ValidationException("Invalid category's description.");
          logger.logError(e);
          foreach (ValidationResult vr in validationResults)
              Console.WriteLine(vr.Message);
          throw e;
      }
      return true;
 }
 public bool UpdateCategory(int id, String newName)
 {
     logger.logInfo("Try to update category whit the id " + id);
     Category category = this.GetCategoryById(id);
     if (category == null)
     {
         EntityDoesNotExistException e = new EntityDoesNotExistException("Category is null");
         logger.logError(e);
         throw e;
     }
     if (category.Name != newName)
     {
         String oldName = category.Name;
         category.Name = newName;
         var validationResults = Validation.Validate<Category>(category);
         if (newName != null)
         {
             if (validationResults.IsValid)
             {
                 try
                 {
                     category.Name = oldName;
                     DataMapperFactoryMethod.GetCurrentFactory().CategoryFactory.UpdateCategory(category,newName);
                 }
                 catch (DuplicateException duplicateException)
                 {
                     logger.logError(duplicateException);
                     throw duplicateException;
                 }
             }
             else
             {
                 ValidationException e = new ValidationException("Invalid category's name.");
                 logger.logError(e);
                 foreach (ValidationResult vr in validationResults)
                     Console.WriteLine(vr.Message);
                 throw e;
             }
         }
         else
         {
             ValidationException e = new ValidationException("Invalid category's name.");
             logger.logError(e);
             throw e;
         }
         logger.logInfo("Category successfully updated");
     }
     else logger.logInfo("Category has the same name");
     return true;
 }
 public bool UpdateProductDescription(int id, String description)
 {
     logger.logInfo("Try to update product whit the id " + id);
     Product product = this.GetProductById(id);
     if (product == null)
     {
         EntityDoesNotExistException e = new EntityDoesNotExistException("Product is null");
         logger.logError(e);
         throw e;
     }
     if (product.Description != description)
     {
         String oldDesc = product.Description;
         product.Description = description;
         var validationResults = Validation.Validate<Product>(product);
         if (description != null)
         {
             if (validationResults.IsValid)
             {
                 product.Description = oldDesc;
                 try
                 {
                     DataMapperFactoryMethod.GetCurrentFactory().ProductFactory.UpdateProductDescription(product, description);
                 }
                 catch (DuplicateException duplicateException)
                 {
                     logger.logError(duplicateException);
                     throw duplicateException;
                 }
             }
             else
             {
                 ValidationException e = new ValidationException("Invalid product's description.");
                 logger.logError(e);
                 throw e;
             }
         }
         else
         {
             ValidationException e = new ValidationException("Invalid product's description.");
             logger.logError(e);
             throw e;
         }
     }
     else logger.logInfo("Product has the same description");
     logger.logInfo("Product successfully updated");
     return true;
 }