From a1cc594acdb30c3bb09990cb43e8e3e123ed9bf6 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Tue, 18 Feb 2020 19:32:48 -0500 Subject: [PATCH] Continue transition vers exceptions + DTO --- .../core/exception/SimdutException.java | 12 ++ .../model/EntityAlreadyExistsException.java | 19 ++- .../core/exception/model/ModelException.java | 15 +- .../core/io/response/ResponseCode.java | 1 + .../core/model/MaterialType.java | 2 + .../core/model/dto/MaterialTypeEditorDto.java | 12 ++ .../core/services/files/FilesService.java | 61 ++++++++ .../core/services/model/MaterialService.java | 105 ++++++++----- .../services/model/MaterialTypeService.java | 92 +++++++---- .../dao/MaterialTypeDao.java | 8 +- .../creators/MaterialCreatorController.java | 50 ++---- .../MaterialTypeCreatorController.java | 18 +-- .../creators/MixCreatorController.java | 67 ++------ .../creators/RecipeCreatorController.java | 28 +--- .../editors/MaterialEditorController.java | 147 +++++------------- .../editors/MaterialTypeEditorController.java | 82 ++++------ .../resources/lang/responses_en.properties | 1 + .../resources/lang/responses_fr.properties | 1 + 18 files changed, 357 insertions(+), 364 deletions(-) create mode 100644 src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/SimdutException.java create mode 100644 src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MaterialTypeEditorDto.java diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/SimdutException.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/SimdutException.java new file mode 100644 index 0000000..3551703 --- /dev/null +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/SimdutException.java @@ -0,0 +1,12 @@ +package dev.fyloz.trial.colorrecipesexplorer.core.exception; + +import dev.fyloz.trial.colorrecipesexplorer.core.model.Material; + +public class SimdutException extends RuntimeException { + + private Material material; + + public SimdutException(Material material) { + this.material = material; + } +} diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/model/EntityAlreadyExistsException.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/model/EntityAlreadyExistsException.java index c6296dc..86cfb8c 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/model/EntityAlreadyExistsException.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/model/EntityAlreadyExistsException.java @@ -1,10 +1,18 @@ package dev.fyloz.trial.colorrecipesexplorer.core.exception.model; import dev.fyloz.trial.colorrecipesexplorer.core.model.IModel; +import lombok.Getter; +import lombok.NonNull; +@Getter public class EntityAlreadyExistsException extends ModelException { + @NonNull private IdentifierType identifierType; + + private String identifierName; + + @NonNull private Object requestedId; public EntityAlreadyExistsException(Class type, IdentifierType identifierType, Object requestedId) { @@ -13,11 +21,10 @@ public class EntityAlreadyExistsException extends ModelException { this.requestedId = requestedId; } - public IdentifierType getIdentifierType() { - return identifierType; - } - - public Object getRequestedId() { - return requestedId; + public EntityAlreadyExistsException(Class type, IdentifierType identifierType, String identifierName, Object requestedId) { + super(type); + this.identifierType = identifierType; + this.identifierName = identifierName != null ? identifierName : identifierType.getName(); + this.requestedId = requestedId; } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/model/ModelException.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/model/ModelException.java index 33f0556..1938320 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/model/ModelException.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/exception/model/ModelException.java @@ -21,7 +21,18 @@ public class ModelException extends RuntimeException { } public enum IdentifierType { - ID, - NAME + ID("id"), + NAME("name"), + OTHER(""); + + private String name; + + IdentifierType(String name) { + this.name = name; + } + + public String getName() { + return name; + } } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/io/response/ResponseCode.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/io/response/ResponseCode.java index d0f3855..c05a05d 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/io/response/ResponseCode.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/io/response/ResponseCode.java @@ -35,6 +35,7 @@ public enum ResponseCode { SUCCESS_SAVING_COMPANY(32, ResponseCodeType.SUCCESS, 1), SUCCESS_DELETING_RECIPE(33, ResponseCodeType.SUCCESS, 1), SUCCESS_DELETING_MATERIAL_TYPE(34, ResponseCodeType.SUCCESS, 1), + RECIPE_ALREADY_EXIST(35, ResponseCodeType.ERROR, 1), // HTTP Errors _500(100, ResponseCodeType.ERROR, 0), diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MaterialType.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MaterialType.java index b626a49..cf5d922 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MaterialType.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/MaterialType.java @@ -17,6 +17,8 @@ public class MaterialType implements IModel { public static final MaterialType DEFAULT_MATERIAL_TYPE; public static final MaterialType BASE_MATERIAL_TYPE; + public static final String IDENTIFIER_PREFIX_NAME = "prefix"; + static { DEFAULT_MATERIAL_TYPE = new MaterialType("Aucun", "", false); BASE_MATERIAL_TYPE = new MaterialType("Base", "BAS", false); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MaterialTypeEditorDto.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MaterialTypeEditorDto.java new file mode 100644 index 0000000..5b8288b --- /dev/null +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/dto/MaterialTypeEditorDto.java @@ -0,0 +1,12 @@ +package dev.fyloz.trial.colorrecipesexplorer.core.model.dto; + +import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType; +import lombok.Data; + +@Data +public class MaterialTypeEditorDto { + + private String oldName; + private MaterialType materialType; + +} diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/FilesService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/FilesService.java index 016b034..a1a9f82 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/FilesService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/FilesService.java @@ -1,18 +1,25 @@ package dev.fyloz.trial.colorrecipesexplorer.core.services.files; +import dev.fyloz.trial.colorrecipesexplorer.ColorRecipesExplorerApplication; +import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.io.ResourceLoader; import org.springframework.stereotype.Service; import org.springframework.util.FileCopyUtils; +import org.springframework.web.multipart.MultipartFile; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; @Service public class FilesService { private ResourceLoader resources; + private Logger logger = ColorRecipesExplorerApplication.LOGGER; @Autowired public FilesService(ResourceLoader resources) { @@ -47,4 +54,58 @@ public class FilesService { return new String(data, StandardCharsets.UTF_8); } + /** + * Écrit un fichier Multipart sur le disque. + * + * @param multipartFile Le fichier à écrire + * @param path Le chemin vers le fichier + * @return Si le fichier a bien été créé + */ + public boolean writeMultiPartFile(MultipartFile multipartFile, String path) { + if (multipartFile.getSize() <= 0) return true; + + try { + File file = createFile(path); + multipartFile.transferTo(file.toPath()); + return true; + } catch (IOException ex) { + ColorRecipesExplorerApplication.LOGGER.error("Impossible d'écrire un fichier Multipart: " + ex.getMessage()); + return false; + } + } + + /** + * Crée un fichier sur le disque. + * + * @param path Le chemin vers le fichier + * @return Le fichier créé + * @throws IOException La création du fichier échoue + */ + public File createFile(String path) throws IOException { + File file = new File(path); + + if (!file.exists() || file.isDirectory()) { + Files.createDirectories(file.getParentFile().toPath()); + Files.createFile(file.toPath()); + } + + return file; + } + + /** + * Supprime un fichier sur le disque. + * + * @param path Le chemin vers le fichier + * @throws IOException La suppression du fichier échoue + */ + public void deleteFile(String path) { + File file = new File(path); + + try { + if (file.exists() && !file.isDirectory()) Files.delete(file.toPath()); + } catch (IOException ex) { + logger.error("Impossible de supprimer un fichier: " + ex.getMessage()); + } + } + } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialService.java index 5a5884d..1221801 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialService.java @@ -1,16 +1,20 @@ package dev.fyloz.trial.colorrecipesexplorer.core.services.model; +import dev.fyloz.trial.colorrecipesexplorer.ColorRecipesExplorerApplication; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.SimdutException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException; import dev.fyloz.trial.colorrecipesexplorer.core.io.file.FileHandler; import dev.fyloz.trial.colorrecipesexplorer.core.model.Material; import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType; import dev.fyloz.trial.colorrecipesexplorer.core.services.GenericService; +import dev.fyloz.trial.colorrecipesexplorer.core.services.files.FilesService; import dev.fyloz.trial.colorrecipesexplorer.dao.MaterialDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; -import java.io.File; -import java.io.IOException; +import javax.validation.constraints.NotNull; import java.util.ArrayList; import java.util.Comparator; import java.util.List; @@ -21,11 +25,13 @@ import java.util.stream.Collectors; public class MaterialService extends GenericService { private MixQuantityService mixQuantityService; + private FilesService filesService; @Autowired - public MaterialService(MaterialDao materialDao, MixQuantityService mixQuantityService) { - super(materialDao); + public MaterialService(MaterialDao materialDao, MixQuantityService mixQuantityService, FilesService filesService) { + super(materialDao, Material.class); this.mixQuantityService = mixQuantityService; + this.filesService = filesService; } public Optional getByName(String name) { @@ -38,12 +44,39 @@ public class MaterialService extends GenericService { return dao.findAllByMaterialType(materialType); } + /** + * Récupère tous les produits qui ne sont pas des types de mélange. + * + * @return Tous les produits qui ne sont pas des types de mélange + */ + public List getAllNotMixType() { + return getAll() + .stream() + .filter(m -> !m.isMixType()) + .collect(Collectors.toList()); + } + public List getAllOrdered() { return getAll().stream() .sorted(Comparator.comparing(Material::getName)) .collect(Collectors.toList()); } + public Material save(@NotNull Material material, MultipartFile file) { + addSimdut(file, material); + + return super.save(material); + } + + @Override + public Material update(Material material) { + Optional materialByCode = dao.findByName(material.getName()); + if (materialByCode.isPresent() && !material.getId().equals(materialByCode.get().getId())) + throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.NAME, material.getName()); + + return super.update(material); + } + /** * Vérifie si un produit est lié à un ou plusieurs mélanges. * @@ -54,12 +87,11 @@ public class MaterialService extends GenericService { return mixQuantityService.existsByMaterial(material); } - public boolean deleteIfNotLinked(Material material) { + @Deprecated(since = "1.3.0", forRemoval = true) + public void deleteIfNotLinked(Material material) { if (!isLinkedToMixes(material)) { - return delete(material); + delete(material); } - - return false; } @Override @@ -75,11 +107,6 @@ public class MaterialService extends GenericService { return super.isValidForUpdate(material) && (materialByCode.isEmpty() || material.getId().equals(materialByCode.get().getId())); } - @Deprecated(since = "1.2.0") - public List getAllBySearchString(String searchString) { - return dao.findAllByNameContainingIgnoreCase(searchString).stream().filter(m -> !m.isMixType()).collect(Collectors.toList()); - } - /** * Crée un FileHandler pour le produit passé en paramètre. * @@ -91,6 +118,19 @@ public class MaterialService extends GenericService { return new FileHandler(filename, FileHandler.FileContext.SIMDUT, FileHandler.FileExtension.PDF); } + /** + * Récupère le chemin vers le fichier SIMDUT d'un produit. + * + * @param material Le produit + * @return Le chemin vers le fichier SIMDUT du produit + */ + private String getSimdutPath(Material material) { + return String.format("%s/simdut/%s_%s.pdf", + ColorRecipesExplorerApplication.UPLOAD_LOCATION, + material.getId(), + material.getName()); + } + /** * Crée le fichier SIMDUT sur le disque et y transfert le contenu du MultipartFile passé en paramètre. *

@@ -99,24 +139,20 @@ public class MaterialService extends GenericService { * * @param simdut Le contenu du fichier SIMDUT * @param material Le produit du SIMDUT - * @return Si le fichier SIMDUT à bien été créé ou non. */ - public boolean addSimdut(MultipartFile simdut, Material material) { - if (simdut.getSize() <= 0) return false; + public void addSimdut(MultipartFile simdut, Material material) { + if (filesService.writeMultiPartFile(simdut, getSimdutPath(material))) throw new SimdutException(material); + } - FileHandler fileHandler = getFileHandlerForMaterial(material); - - if (!fileHandler.createFile()) { - return false; - } - - try { - simdut.transferTo(new File(fileHandler.getFile().getAbsolutePath())); - return true; - } catch (IOException e) { - logger.error("Erreur lors du transfert d'un fichier SIMDUT vers le disque", e); - return false; - } + /** + * Met à jour le fichier SIMDUT sur le disque. + * + * @param simdut Le contenu du fichier SIMDUT mis à jour + * @param material Le produit du SIMDUT + */ + public void updateSimdut(MultipartFile simdut, Material material) { + removeSimdut(material); + addSimdut(simdut, material); } /** @@ -125,15 +161,8 @@ public class MaterialService extends GenericService { * Cette méthode retournera true si le fichier SIMDUT du produit n'existe pas ou si la suppression est faîtes. * * @param material Le produit dont on veut supprimer le fichier SIMDUT - * @return Si la suppression du fichier SIMDUT s'est effectuée. */ - public boolean removeSimdut(Material material) { - FileHandler fileHandler = getFileHandlerForMaterial(material); - - if (fileHandler.getFile().exists()) { - return fileHandler.deleteFile(); - } - - return true; + public void removeSimdut(Material material) { + filesService.deleteFile(getSimdutPath(material)); } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialTypeService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialTypeService.java index d7285c0..ed27da5 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialTypeService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MaterialTypeService.java @@ -1,13 +1,15 @@ package dev.fyloz.trial.colorrecipesexplorer.core.services.model; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException; import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType; +import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MaterialTypeEditorDto; import dev.fyloz.trial.colorrecipesexplorer.core.services.GenericService; import dev.fyloz.trial.colorrecipesexplorer.dao.MaterialTypeDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.Optional; - @Service public class MaterialTypeService extends GenericService { @@ -15,7 +17,7 @@ public class MaterialTypeService extends GenericService materialTypeByName = dao.findByName(materialType.getName()); + MaterialType materialTypeByName = dao.findByName(materialType.getName()); - return materialTypeByName.isEmpty() || materialType.getId().equals(materialTypeByName.get().getId()); + return materialType.getId().equals(materialTypeByName.getId()); } + @Deprecated(since = "1.3.0", forRemoval = true) public boolean isValidForUpdatePrefix(MaterialType materialType) { - Optional materialTypeByPrefix = dao.findByPrefix(materialType.getPrefix()); + MaterialType materialTypeByPrefix = dao.findByPrefix(materialType.getPrefix()); - return materialTypeByPrefix.isEmpty() || materialType.getId().equals(materialTypeByPrefix.get().getId()); + return materialType.getId().equals(materialTypeByPrefix.getId()); } - public Optional getByName(String name) { + /** + * Récupère un type de produit par son nom. + * + * @param name Le nom du type de produit à récupérer + * @return Le type de produit correspondant au nom. + */ + public MaterialType getByName(String name) { return dao.findByName(name); } - // TODO Utilisé ? - public Optional getDefaultMaterialType() { - return getByName(MaterialType.DEFAULT_MATERIAL_TYPE.getName()); + /** + * Récupère un type de produit par son préfixe. + * + * @param prefix Le préfixe du type de produit à récupérer + * @return Le type de produit correspondant au préfixe + */ + public MaterialType getByPrefix(String prefix) { + return dao.findByPrefix(prefix); } + /** + * Vérifie si un type de produit correspondant à un nom existe. + * + * @param name Le nom à vérifier + * @return Si un type de produit ayant le nom existe + */ public boolean existsByName(String name) { - return getByName(name).isPresent(); + return dao.existsByName(name); + } + + /** + * Vérifie si un type de produit correspondant à un préfixe existe. + * + * @param prefix Le préfixe à vérifier + * @return Si un type de produit ayant le préfixe existe + */ + public boolean existsByPrefix(String prefix) { + return dao.existsByPrefix(prefix); } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialTypeDao.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialTypeDao.java index 4902a20..ea3b539 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialTypeDao.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialTypeDao.java @@ -7,8 +7,12 @@ import java.util.Optional; public interface MaterialTypeDao extends JpaRepository { - Optional findByName(String name); + MaterialType findByName(String name); - Optional findByPrefix(String prefix); + boolean existsByName(String name); + + MaterialType findByPrefix(String prefix); + + boolean existsByPrefix(String prefix); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MaterialCreatorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MaterialCreatorController.java index 96b1b86..b8a641a 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MaterialCreatorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MaterialCreatorController.java @@ -1,5 +1,7 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller.creators; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.SimdutException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType; @@ -15,7 +17,6 @@ import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; -import java.util.Optional; import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.CREATOR_MATERIAL; @@ -40,48 +41,21 @@ public class MaterialCreatorController { .build(); } - /** - * Permet à l'utilisateur de créer un produit. - *

- * La création échouera si: - * - L'utilisateur n'est pas autorisé à effectuer cette action - * - Le produit existe déjà - * - Une erreur est survenue lors de la sauvegarde dans la base de données - *

- * Modèle de la page: - * - error: Contient le message d'erreur, s'il y a lieu - * - materialCode: Contient le code de produit du produit créé - *

- * REQUIERT UNE AUTORISATION - * - * @param material Le produit à créer - * @param simdut Le fichier SIMDUT du produit (optionnel) - * @return La page à afficher. - */ @PostMapping(value = CREATOR_MATERIAL, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ModelAndView create(@Valid Material material, MultipartFile simdut) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); - if (!materialService.exists(material)) { - Optional savedMaterial = materialService.save(material); - - if (savedMaterial.isPresent()) { - material = savedMaterial.get(); - - modelResponseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL, material.getName()); - - if (simdut.getSize() > 0 && !materialService.addSimdut(simdut, material)) { - modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING_SIMDUT); - } - - return showCreationPage(modelResponseBuilder - .addResponseData(ResponseDataType.MATERIAL_CODE, material.getName()) - .build(), null); - } else { - modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING); - } - } else { + try { + materialService.save(material, simdut); + return showCreationPage( + modelResponseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL, material.getName()).build(), + null + ); + } catch (EntityAlreadyExistsException ex) { modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_ALREADY_EXIST, material.getName()); + } catch (SimdutException ex) { + modelResponseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL, material.getName()); + modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING_SIMDUT); } return showCreationPage(modelResponseBuilder.build(), material); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MaterialTypeCreatorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MaterialTypeCreatorController.java index b5af8d5..db2a948 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MaterialTypeCreatorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MaterialTypeCreatorController.java @@ -1,5 +1,6 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller.creators; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType; @@ -40,16 +41,13 @@ public class MaterialTypeCreatorController { public ModelAndView create(@Valid MaterialType materialType) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(ControllerUtils.redirect(INDEX)); - if (materialTypeService.isValidForCreation(materialType)) { - Optional optionalMaterialType = materialTypeService.save(materialType); - if (optionalMaterialType.isPresent()) { - return getPage(modelResponseBuilder - .addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL_TYPE, optionalMaterialType.get().getName()) - .build(), null); - } else { - modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING); - } - } else { + try { + materialTypeService.save(materialType); + return getPage( + modelResponseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL_TYPE, materialType.getName()).build(), + null + ); + } catch (EntityAlreadyExistsException ex) { modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_TYPE_ALREADY_EXIST, materialType.getName()); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MixCreatorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MixCreatorController.java index 0ae17fc..e3ad702 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MixCreatorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/MixCreatorController.java @@ -1,12 +1,12 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller.creators; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType; import dev.fyloz.trial.colorrecipesexplorer.core.model.Recipe; import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MixCreationFormDto; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.*; -import dev.fyloz.trial.colorrecipesexplorer.core.utils.ControllerUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; @@ -17,7 +17,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; -import java.util.Optional; import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.*; @@ -27,7 +26,6 @@ public class MixCreatorController { private MixService mixService; private RecipeService recipeService; private MaterialService materialService; - private MixTypeService mixTypeService; private MaterialTypeService materialTypeService; @Autowired @@ -35,63 +33,32 @@ public class MixCreatorController { this.mixService = mixService; this.recipeService = recipeService; this.materialService = materialService; - this.mixTypeService = mixTypeService; this.materialTypeService = materialTypeService; } - /** - * Affiche la page de création d'un mélange. - * Cette méthode requiert l'identifiant d'une recette dans l'URL. - * - * @param model Le Model injecté par Thymeleaf - * @param id L'identifiant de la recette - * @return La page à afficher. - */ @GetMapping(CREATOR_MIX_SPECIFIC) - public ModelAndView showCreationPage(ModelAndView model, @PathVariable Long id) { + public ModelAndView getPage(ModelAndView model, @PathVariable Long id) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(model) .withView(CREATOR_MIX); - Optional optionalRecipe = recipeService.getById(id); - if (optionalRecipe.isEmpty()) { - return modelResponseBuilder - .withView(ControllerUtils.redirect(EDITOR_RECIPE)) - .build(); + try { + Recipe recipe = recipeService.getById(id); + + modelResponseBuilder + .addResponseData(ResponseDataType.RECIPE, recipe) + .addResponseData(ResponseDataType.MATERIAL_TYPES, materialTypeService.getAll()) + .addAttribute("materialsJson", materialService.asJson(mixService.getAvailableMaterialsForNewMix(recipe))); + + if (materialService.getAll().isEmpty()) + modelResponseBuilder.addResponseData(ResponseDataType.BLOCK_BUTTON, true); + } catch (EntityNotFoundException ex) { + modelResponseBuilder + .withRedirect(EDITOR_RECIPE); } - Recipe recipe = optionalRecipe.get(); - - ModelResponseBuilder responseBuilder = modelResponseBuilder - .addResponseData(ResponseDataType.RECIPE, recipe) - .addResponseData(ResponseDataType.MATERIAL_TYPES, materialTypeService.getAll()) - .addAttribute("materialsJson", materialService.asJson(mixService.getAvailableMaterialsForNewMix(recipe))); - - if (materialService.getAll().isEmpty()) { - responseBuilder.addResponseCode(ResponseCode.NO_MATERIAL) - .addResponseData(ResponseDataType.BLOCK_BUTTON, true); - } - - return responseBuilder.build(); + return modelResponseBuilder.build(); } - /** - * Permet à l'utilisateur de créer un mélange. - *

- * La création échouera si: - * - L'utilisateur n'est pas autorisé à exécuter cette action - * - La recette n'existe pas - * - Un des produits n'existe pas - * - Une erreur est survenue lors de la sauvegarde du type de mélange - * - Une erreur est survenue lors de la sauvegarde du mélange - *

- * Modèle de la page: - * - error: Contient le message d'erreur, s'il y a lieu - *

- * REQUIERT UNE AUTORISATION - * - * @param form La formulaire du mélange entré par l'utilisateur - * @return La page à afficher. - */ @PostMapping(value = CREATOR_MIX, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public ModelAndView createMix(@ModelAttribute @Valid MixCreationFormDto formDto) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); @@ -104,7 +71,7 @@ public class MixCreatorController { ModelResponseBuilder mixCreationResponse = mixService.create(formDto, recipe); if (mixCreationResponse != null) - return showCreationPage(mixCreationResponse.build(), formDto.getRecipe().getId()); + return getPage(mixCreationResponse.build(), formDto.getRecipe().getId()); return modelResponseBuilder .withRedirect(EDITOR_RECIPE_SPECIFIC, recipe.getId()) diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/RecipeCreatorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/RecipeCreatorController.java index 21b2d0d..f3214e7 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/RecipeCreatorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/RecipeCreatorController.java @@ -13,7 +13,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; -import java.util.Optional; import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.CREATOR_RECIPE; import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.EDITOR_RECIPE_SPECIFIC; @@ -45,33 +44,12 @@ public class RecipeCreatorController { return responseBuilder.build(); } - /** - * Permet à l'utilisateur de créer une nouvelle recette. - *

- * La création échouera si: - * - L'utilisateur n'est pas autorisé à exécuter cette action - * - Une erreur est survenue lors de la sauvegarde dans la base de données - *

- * Modèle de la page: - * - error: Contient le message d'erreur, s'il y a lieu - *

- * REQUIERT UNE AUTORISATION - * - * @param recipe La recette à créer - * @return La page à afficher. - */ - @PostMapping(value = CREATOR_RECIPE) + @PostMapping(CREATOR_RECIPE) public ModelAndView createRecipe(@Valid Recipe recipe) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); - Optional savedRecipe = recipeService.save(recipe); - if (savedRecipe.isPresent()) { - return modelResponseBuilder - .withRedirect(EDITOR_RECIPE_SPECIFIC, savedRecipe.get().getId()) - .build(); - } - modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING); + long id = recipeService.save(recipe).getId(); - return showCreationPage(modelResponseBuilder.build(), recipe); + return modelResponseBuilder.withRedirect(EDITOR_RECIPE_SPECIFIC, id).build(); } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MaterialEditorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MaterialEditorController.java index aa11fe3..7c4d14f 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MaterialEditorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MaterialEditorController.java @@ -1,12 +1,14 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller.editors; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.SimdutException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType; import dev.fyloz.trial.colorrecipesexplorer.core.model.Material; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialTypeService; -import dev.fyloz.trial.colorrecipesexplorer.core.utils.ControllerUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; @@ -16,9 +18,6 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; -import java.util.Optional; -import java.util.stream.Collectors; - import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.*; @Controller @@ -33,107 +32,50 @@ public class MaterialEditorController { this.materialTypeService = materialTypeService; } - /** - * Affiche la page de tous les produits. - *

- * Modèle de la page: - * - materials: La liste de tous les produits - * - * @param model Le Model injecté par Thymeleaf - * @return La page à afficher. - */ @GetMapping(EDITOR_MATERIAL) - public ModelAndView listMaterials(ModelAndView model) { + public ModelAndView getPage(ModelAndView model) { return new ModelResponseBuilder(model) .withView(EDITOR_MATERIAL) - .addResponseData(ResponseDataType.MATERIALS, materialService.getAll().stream().filter(m -> !m.isMixType()).collect(Collectors.toList())) + .addResponseData(ResponseDataType.MATERIALS, materialService.getAllNotMixType()) .build(); } @GetMapping(EDITOR_MATERIAL_SPECIFIC) - public ModelAndView showEditPage(ModelAndView model, @PathVariable Long id, Material material) { - ModelResponseBuilder responseBuilder = new ModelResponseBuilder(model) - .withView(EDITOR_MATERIAL_EDITOR); + public ModelAndView getEditPage(ModelAndView model, @PathVariable Long id, Material material) { + ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(model).withView(EDITOR_MATERIAL_EDITOR); - if (material.getName() == null) { - Optional optionalMaterial = materialService.getById(id); - - if (optionalMaterial.isEmpty()) { - return listMaterials( - responseBuilder - .addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, id) - .build() - ); - } - - material = optionalMaterial.get(); + try { + if (material.getName() == null) material = materialService.getById(id); + return modelResponseBuilder + .addResponseData(ResponseDataType.MATERIAL, material) + .addResponseData(ResponseDataType.MATERIAL_TYPES, materialTypeService.getAll()) + .build(); + } catch (EntityNotFoundException ex) { + modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, id); } - return responseBuilder - .addResponseData(ResponseDataType.MATERIAL, material) - .addResponseData(ResponseDataType.MATERIAL_TYPES, materialTypeService.getAll()) - .build(); + return getPage(modelResponseBuilder.build()); } - /** - * Permet à l'utilisateur de modifier un produit. - *

- * La modification échouera si: - * - L'utilisateur n'est pas autorisé à exécuter cette action - * - Il n'y a aucun produit avec l'identifiant passé en paramètre - * - Une erreur est survenue lors de la mise à jour dans la base de données - *

- * Modèle de la page: - * - error: Contient le message d'erreur, s'il y a lieu - * - materialCode: Contient le code du produit mit à jour - *

- * REQUIERT UNE AUTORISATION - * - * @param material Le produit à mettre à jour - * @return La page à afficher. - */ @PostMapping(value = EDITOR_MATERIAL, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) public ModelAndView saveEditedMaterial(Material material) { - ModelResponseBuilder responseBuilder = new ModelResponseBuilder(""); - Long id = material.getId(); + ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); - if (!materialService.existsById(id)) { - responseBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, id); - } else if (!materialService.isValidForUpdate(material)) { - responseBuilder.addResponseCode(ResponseCode.MATERIAL_ALREADY_EXIST, material.getName()); + try { + material = materialService.update(material); - return showEditPage( - responseBuilder - .addResponseCode(ResponseCode.MATERIAL_ALREADY_EXIST, material.getName()) - .build(), - id, material); - } else { - Optional updatedMaterial = materialService.update(material); - if (updatedMaterial.isPresent()) { - responseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL, updatedMaterial.get().getName()); - } else { - return showEditPage( - responseBuilder - .addResponseCode(ResponseCode.ERROR_SAVING) - .build(), - id, null); - } + return getPage(modelResponseBuilder + .addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL, material.getName()) + .build()); + } catch (EntityNotFoundException ex) { + modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, material.getId()); + } catch (EntityAlreadyExistsException ex) { + modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_ALREADY_EXIST, material.getName()); } - return listMaterials(responseBuilder.build()); + return getEditPage(modelResponseBuilder.build(), material.getId(), material); } - /** - * Affiche la page d'upload d'un fichier SIMDUT pour un produit - * Cette méthode requiert l'identifiant du produit dans l'URL - *

- * Modèle de la page: - * - id: Contient l'identifiant du produit - * - * @param model Le Model injecté par Thymeleaf - * @param id L'identifiant du produit à modifier le SIMDUT - * @return La page à afficher. - */ @GetMapping(value = EDIT_MATERIAL_SIMDUT_SPECIFIC) public ModelAndView chooseSIMDUTFile(ModelAndView model, @PathVariable Long id) { return new ModelResponseBuilder(model) @@ -142,35 +84,20 @@ public class MaterialEditorController { .build(); } - /** - * Sauvegarde le fichier SIMDUT d'un produit. - *

- * Modèle de la page: - * - error: Contient le message d'erreur, s'il y a lieu - *

- * REQUIERT UNE AUTORISATION - * - * @param id L'identifiant du produit - * @param simdut Le fichier SIMDUT uploadé - * @return La page à afficher. - */ @PostMapping(value = EDIT_MATERIAL_SIMDUT, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) public ModelAndView saveSIMDUT(Long id, MultipartFile simdut) { - ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(ControllerUtils.redirect("material/editor/" + id)); + ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder().withRedirect(EDITOR_MATERIAL_SPECIFIC, id); - Optional optionalMaterial = materialService.getById(id); - if (optionalMaterial.isEmpty()) { - return chooseSIMDUTFile(modelResponseBuilder - .addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, id) - .build(), - id); - } - - Material material = optionalMaterial.get(); - if (!materialService.removeSimdut(material) || !materialService.addSimdut(simdut, material)) { + try { + Material material = materialService.getById(id); + materialService.updateSimdut(simdut, material); + } catch (EntityNotFoundException ex) { + return chooseSIMDUTFile( + modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, id).build(), + id + ); + } catch (SimdutException ex) { modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING_SIMDUT); - - return chooseSIMDUTFile(modelResponseBuilder.build(), id); } return modelResponseBuilder.build(); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MaterialTypeEditorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MaterialTypeEditorController.java index 3ffa6c1..bc7cddc 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MaterialTypeEditorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MaterialTypeEditorController.java @@ -1,9 +1,13 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller.editors; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityAlreadyExistsException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseCode; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType; import dev.fyloz.trial.colorrecipesexplorer.core.model.MaterialType; +import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.MaterialTypeEditorDto; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialTypeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; @@ -13,8 +17,6 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.servlet.ModelAndView; -import java.util.Optional; - import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.*; @Controller @@ -28,7 +30,7 @@ public class MaterialTypeEditorController { } @GetMapping(EDITOR_MATERIAL_TYPE) - public ModelAndView listMaterialTypes(ModelAndView model) { + public ModelAndView getPage(ModelAndView model) { return new ModelResponseBuilder(model) .withView(EDITOR_MATERIAL_TYPE) .addResponseData(ResponseDataType.MATERIAL_TYPES, materialTypeService.getAll()) @@ -36,67 +38,37 @@ public class MaterialTypeEditorController { } @GetMapping(EDITOR_MATERIAL_TYPE_SPECIFIC) - public ModelAndView showEditPage(ModelAndView model, @PathVariable Long id, MaterialType materialType) { - ModelResponseBuilder responseBuilder = new ModelResponseBuilder(model) - .withView(EDITOR_MATERIAL_TYPE_EDITOR); + public ModelAndView getEditPage(ModelAndView model, @PathVariable Long id, MaterialType materialType) { + ModelResponseBuilder responseBuilder = new ModelResponseBuilder(model).withView(EDITOR_MATERIAL_TYPE_EDITOR); - if (materialType.getName() == null) { - Optional optionalMaterialType = materialTypeService.getById(id); + try { + if (materialType == null) materialType = materialTypeService.getById(id); - if (optionalMaterialType.isEmpty()) { - return listMaterialTypes( - responseBuilder - .addResponseCode(ResponseCode.MATERIAL_TYPE_NOT_FOUND, id) - .build() - ); - } - - materialType = optionalMaterialType.get(); + return responseBuilder + .addResponseData(ResponseDataType.MATERIAL_TYPE, materialType) + .build(); + } catch (EntityNotFoundException ex) { + return getPage(responseBuilder.addResponseCode(ResponseCode.MATERIAL_TYPE_NOT_FOUND, id).build()); } - - return responseBuilder - .addResponseData(ResponseDataType.MATERIAL_TYPE, materialType) - .build(); } @PostMapping(value = EDITOR_MATERIAL_TYPE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public ModelAndView saveEditedMaterialType(MaterialType materialType) { - ModelResponseBuilder responseBuilder = new ModelResponseBuilder(""); + public ModelAndView updateMaterialType(MaterialTypeEditorDto materialTypeDto) { + ModelResponseBuilder responseBuilder = new ModelResponseBuilder(); - // L'ID est 0 lors de la désérialisation, il faut utiliser le nom unique - String materialTypeName = materialType.getName(); - Optional optionalMaterialType = materialTypeService.getByName(materialTypeName); + try { + materialTypeService.update(materialTypeDto); - if (optionalMaterialType.isEmpty()) { - responseBuilder.addResponseCode(ResponseCode.MATERIAL_TYPE_NOT_FOUND, materialTypeName); - } else { - materialType = optionalMaterialType.get(); - if (!materialTypeService.isValidForUpdateName(materialType)) { - return showEditPage( - responseBuilder - .addResponseCode(ResponseCode.MATERIAL_TYPE_ALREADY_EXIST, materialType.getName()) - .build(), - materialType.getId(), materialType); - } else if (!materialTypeService.isValidForUpdatePrefix(materialType)) { - return showEditPage( - responseBuilder - .addResponseCode(ResponseCode.MATERIAL_TYPE_ALREADY_EXIST_PREFIX, materialType.getPrefix()) - .build(), - materialType.getId(), materialType); - } else { - Optional updatedMaterialType = materialTypeService.update(materialType); - if (updatedMaterialType.isPresent()) { - responseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL_TYPE, updatedMaterialType.get().getName()); - } else { - return showEditPage( - responseBuilder - .addResponseCode(ResponseCode.ERROR_SAVING) - .build(), - materialType.getId(), materialType); - } - } + return getPage(responseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL_TYPE, materialTypeDto.getMaterialType().getName()).build()); + } catch (EntityNotFoundException ex) { + return getPage(responseBuilder.addResponseCode(ResponseCode.MATERIAL_TYPE_NOT_FOUND, materialTypeDto.getOldName()).build()); + } catch (EntityAlreadyExistsException ex) { + if (ModelException.IdentifierType.NAME.equals(ex.getIdentifierType())) + responseBuilder.addResponseCode(ResponseCode.MATERIAL_TYPE_ALREADY_EXIST, materialTypeDto.getMaterialType().getName()); + else if (ex.getIdentifierName().equals(MaterialType.IDENTIFIER_PREFIX_NAME)) + responseBuilder.addResponseCode(ResponseCode.MATERIAL_TYPE_ALREADY_EXIST_PREFIX, materialTypeDto.getMaterialType().getPrefix()); } - return listMaterialTypes(responseBuilder.build()); + return getEditPage(responseBuilder.build(), materialTypeDto.getMaterialType().getId(), materialTypeDto.getMaterialType()); } } diff --git a/src/main/resources/lang/responses_en.properties b/src/main/resources/lang/responses_en.properties index 41f63b1..7f310ee 100644 --- a/src/main/resources/lang/responses_en.properties +++ b/src/main/resources/lang/responses_en.properties @@ -35,3 +35,4 @@ response.31=The material {0} has been deleted response.32=The banner {0} has been saved response.33=The recipe {0} has been deleted response.34=The material type {0} has been deleted +response.35=There is already a recipe with the ID {0} diff --git a/src/main/resources/lang/responses_fr.properties b/src/main/resources/lang/responses_fr.properties index e0e061f..73065b1 100644 --- a/src/main/resources/lang/responses_fr.properties +++ b/src/main/resources/lang/responses_fr.properties @@ -35,3 +35,4 @@ response.31=Le produit {0} a bien été supprimée response.32=La bannière {0} a bien été sauvegardée response.33=La recette {0} a bien été supprimée response.34=Le type de produit {0} a bien été supprimé +response.35=Il y a déjà une recette avec l''ID {0}