From dc1c0b85851bf9230781e0561968f87c4b54dc80 Mon Sep 17 00:00:00 2001 From: FyloZ Date: Mon, 26 Aug 2019 07:26:07 -0400 Subject: [PATCH] Langues back-end --- .../core/ModelBuilder.java | 112 ++++++++++++++++++ .../core/ModelDataType.java | 82 +++++++++++++ .../core/ResponseCode.java | 51 ++++++++ .../core/ResponseCodes.java | 50 -------- .../configuration/LocaleConfiguration.java | 10 +- .../core/utils/ControllerUtils.java | 8 ++ .../services/MixService.java | 13 +- .../web/controller/ImageFilesController.java | 44 ++++--- .../web/controller/IndexController.java | 17 ++- .../web/controller/InventoryController.java | 16 +-- .../web/controller/OthersController.java | 7 +- .../controller/RecipeExplorerController.java | 32 ++--- .../creators/CompanyCreatorController.java | 29 +++-- .../creators/MaterialCreatorController.java | 33 +++--- .../MaterialTypeCreatorController.java | 27 +++-- .../creators/MixCreatorController.java | 61 ++++++---- .../creators/RecipeCreatorController.java | 33 +++--- .../editors/MaterialEditorController.java | 72 ++++++----- .../editors/MixEditorController.java | 65 ++++++---- .../editors/RecipeEditorController.java | 51 ++++---- .../removers/CompanyRemoverController.java | 28 +++-- .../removers/MaterialRemoverController.java | 29 +++-- .../removers/RecipeRemoverController.java | 29 +++-- .../resources/lang/responses_en.properties | 11 +- .../resources/lang/responses_fr.properties | 23 ++-- .../resources/templates/company/creator.html | 3 +- .../resources/templates/company/remover.html | 4 +- src/main/resources/templates/fragments.html | 4 + src/main/resources/templates/index.html | 10 +- .../resources/templates/recipe/created.html | 10 +- .../resources/templates/recipe/editor.html | 9 +- workdir/recipes.mv.db | Bin 32768 -> 40960 bytes 32 files changed, 656 insertions(+), 317 deletions(-) create mode 100644 src/main/java/fyloz/trial/ColorRecipesExplorer/core/ModelBuilder.java create mode 100644 src/main/java/fyloz/trial/ColorRecipesExplorer/core/ModelDataType.java create mode 100644 src/main/java/fyloz/trial/ColorRecipesExplorer/core/ResponseCode.java delete mode 100644 src/main/java/fyloz/trial/ColorRecipesExplorer/core/ResponseCodes.java create mode 100644 src/main/java/fyloz/trial/ColorRecipesExplorer/core/utils/ControllerUtils.java diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ModelBuilder.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ModelBuilder.java new file mode 100644 index 0000000..deb400a --- /dev/null +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ModelBuilder.java @@ -0,0 +1,112 @@ +package fyloz.trial.ColorRecipesExplorer.core; + +import org.springframework.web.servlet.ModelAndView; + +import javax.validation.constraints.NotNull; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class ModelBuilder { + + private static String RESPONSE_PREFIX = "response."; + private static String ERROR_ATTRIBUTE_NAME = "error"; + private static String SUCCESS_ATTRIBUTE_NAME = "success"; + + private ModelAndView model; + private Map modelAttributes; + + public ModelBuilder(String view) { + this(new ModelAndView(view)); + } + + public ModelBuilder(ModelAndView model) { + this.model = model == null ? new ModelAndView() : model; + + modelAttributes = new HashMap<>(); + } + + public ModelBuilder setView(String view) { + model.setViewName(view); + + return this; + } + + public ModelBuilder addAttribute(String attributeName, Object content) { + modelAttributes.put(attributeName, content); + + return this; + } + + public ModelBuilder addResponseCode(ResponseCode responseCode, String... parameters) { + int requiredParametersNumber = responseCode.getParametersNumber(); + int givenParametersNumber = parameters.length; + StringBuilder builder = new StringBuilder(); + + if (requiredParametersNumber != givenParametersNumber) { + throw new IllegalArgumentException(String.format("Mauvais nombre de paramètre dans un réponse de modèle: %s requis, %s fournis", requiredParametersNumber, givenParametersNumber)); + } + + // Construit le code de réponse + builder.append(RESPONSE_PREFIX); + builder.append(responseCode.getCode()); + + // Ajoute les paramètres, si nécessaire + if (requiredParametersNumber > 0) { + builder.append("('"); + + for (int i = 0; i < givenParametersNumber; i++) { + builder.append(parameters[i]); + + if (i < givenParametersNumber - 1) { + builder.append("','"); + } + } + + builder.append("')"); + } + + // Ajoute l'attribut dans le Map + addAttribute(responseCode.getType() == ResponseCode.ResponseCodeType.ERROR ? ERROR_ATTRIBUTE_NAME : SUCCESS_ATTRIBUTE_NAME, builder.toString()); + addAttribute("arg1", "Bathia"); + addAttribute("arg2", "test"); + + return this; + } + + public ModelBuilder addData(ModelDataType modelDataType, @NotNull Object data) { + Class modelDataTypeClass = modelDataType.getDataType(); + Class modelListDataTypeClass = modelDataType.getListDataType(); + Class givenDataTypeClass = data.getClass(); + + // Vérifie le type de l'objet + if (!modelDataTypeClass.equals(givenDataTypeClass)) { + throw new IllegalArgumentException(String.format("L'objet passé en paramètre n'est pas du bon type. Requis: %s, fournis: %s", modelDataTypeClass.getName(), givenDataTypeClass.getName())); + } + + // Si l'objet est une liste, vérifie qu'elle n'est pas vide et qu'elle est du bon type + if (modelDataTypeClass.equals(List.class) && modelListDataTypeClass != null) { + List listData = (List) data; + + if (listData.size() < 1) { + throw new IllegalArgumentException("La liste passée en paramètre ne contient aucun élément"); + } + + Class givenListDataTypeClass = listData.get(0).getClass(); + if (givenDataTypeClass.equals(modelListDataTypeClass)) { + throw new IllegalArgumentException(String.format("La liste passée en paramètre contient des éléments du mauvais type. Requis: %s, fournis: %s", modelListDataTypeClass.getName(), givenListDataTypeClass.getName())); + } + } + + // Ajoute l'attribut dans le Map + addAttribute(modelDataType.getDataTypeName(), data); + + return this; + } + + public ModelAndView build() { + model.addAllObjects(modelAttributes); + + return model; + } +} diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ModelDataType.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ModelDataType.java new file mode 100644 index 0000000..50d23d4 --- /dev/null +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ModelDataType.java @@ -0,0 +1,82 @@ +package fyloz.trial.ColorRecipesExplorer.core; + +import fyloz.trial.ColorRecipesExplorer.model.*; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.stream.Stream; + +public enum ModelDataType { + MATERIAL("material", Material.class), + MATERIALS("materials", ArrayList.class, MATERIAL), + MATERIAL_ID("materialID", Integer.class), + MATERIAL_CODE("materialCode", String.class), + + MATERIAL_TYPE("materialType", MaterialType.class), + MATERIAL_TYPES("materialTypes", ArrayList.class, MATERIAL_TYPE), + + RECIPE("recipe", Recipe.class), + RECIPES("recipes", ArrayList.class, RECIPE), + RECIPE_ID("recipeID", Integer.class), + RECIPE_CODE("recipeCode", String.class), + RECIPE_MAP("recipeMap", HashMap.class), + + RECIPE_STEP("recipeStep", RecipeStep.class), + RECIPE_STEPS("recipeSteps", ArrayList.class, RECIPE_STEP), + + MIX("mix", Mix.class), + MIXES("mixes", ArrayList.class, MIX), + MIX_ID("mixID", Integer.class), + + MIX_TYPE("mixType", MixType.class), + MIX_TYPES("mixTypes", ArrayList.class, MIX_TYPE), + + MIX_QUANTITY("mixQuantity", MixQuantity.class), + MIX_QUANTITIES("mixQuantities", ArrayList.class, MIX_QUANTITY), + + COMPANY("company", Company.class), + COMPANIES("companies", ArrayList.class, COMPANY), + COMPANY_ID("companyID", Integer.class), + COMPANY_NAME("companyName", String.class), + + IMAGE("image", String.class), + IMAGES("images", ArrayList.class, IMAGE); + + private String dataTypeName; + private Class dataType; + private Class listDataType; + + ModelDataType(String dataTypeName, Class dataType) { + this(dataTypeName, dataType, null); + } + + ModelDataType(String dataTypeName, Class dataType, ModelDataType listDataType) { + this.dataTypeName = dataTypeName; + this.dataType = dataType; + + if (listDataType != null) { + this.listDataType = listDataType.getDataType(); + } + } + + public String getDataTypeName() { + return dataTypeName; + } + + public Class getDataType() { + return dataType; + } + + public Class getListDataType() { + return listDataType; + } + + @Override + public String toString() { + return getDataTypeName(); + } + + public static ModelDataType getModelDataTypeFromDataType(Class dataType) { + return Stream.of(values()).filter(r -> r.getDataType().equals(dataType)).findFirst().orElse(null); + } +} diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ResponseCode.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ResponseCode.java new file mode 100644 index 0000000..027938a --- /dev/null +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ResponseCode.java @@ -0,0 +1,51 @@ +package fyloz.trial.ColorRecipesExplorer.core; + +public enum ResponseCode { + SUCCESS_USING_MATERIALS(1, ResponseCodeType.SUCCESS, 0), + SUCCESS_SAVING_RECIPE_INFORMATIONS(2, ResponseCodeType.SUCCESS, 0), + ERROR_SAVING(3, ResponseCodeType.ERROR, 0), + ERROR_SAVING_IMAGE(4, ResponseCodeType.ERROR, 0), + ERROR_SAVING_SIMDUT(5, ResponseCodeType.ERROR, 0), + AUTH_ERROR(6, ResponseCodeType.ERROR, 0), + RECIPE_NOT_FOUND(7, ResponseCodeType.ERROR, 1), + MIX_NOT_FOUND(8, ResponseCodeType.ERROR, 1), + MATERIAL_NOT_FOUND(9, ResponseCodeType.ERROR, 1), + MATERIAL_ALREADY_EXIST(10, ResponseCodeType.ERROR, 1), + MATERIAL_TYPE_ALREADY_EXIST(11, ResponseCodeType.ERROR, 1), + COMPANY_NOT_FOUND(12, ResponseCodeType.ERROR, 1), + COMPANY_ALREADY_EXIST(13, ResponseCodeType.ERROR, 1), + MATERIAL_LINKED(14, ResponseCodeType.ERROR, 1), + COMPANY_LINKED(15, ResponseCodeType.ERROR, 1), + MIX_NOT_ASSOCIATED_WITH_RECIPE(16, ResponseCodeType.ERROR, 2), + NOT_ENOUGH_MATERIAL(17, ResponseCodeType.ERROR, 1), + MIX_TYPE_ALREADY_USED(18, ResponseCodeType.ERROR, 1); + + public static final int MAX_PARAMETERS_NUMBER = 2; + + private int code; + private ResponseCodeType type; + private int parametersNumber; + + ResponseCode(int code, ResponseCodeType type, int parametersNumber) { + this.code = code; + this.type = type; + this.parametersNumber = parametersNumber; + } + + public int getCode() { + return code; + } + + public ResponseCodeType getType() { + return type; + } + + public int getParametersNumber() { + return parametersNumber; + } + + public enum ResponseCodeType { + ERROR, + SUCCESS + } +} diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ResponseCodes.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ResponseCodes.java deleted file mode 100644 index 375faf6..0000000 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/core/ResponseCodes.java +++ /dev/null @@ -1,50 +0,0 @@ -package fyloz.trial.ColorRecipesExplorer.core; - -public enum ResponseCodes { - SUCCESS_USING_MATERIALS(100, ResponseCodeType.SUCCESS, false), - SUCCESS_SAVING_RECIPE_INFORMATIONS(2, ResponseCodeType.SUCCESS, false), - ERROR_SAVING(3, ResponseCodeType.ERROR, false), - ERROR_SAVING_IMAGE(4, ResponseCodeType.ERROR, false), - ERROR_SAVING_SIMDUT(5, ResponseCodeType.ERROR, false), - AUTH_ERROR(6, ResponseCodeType.ERROR, false), - RECIPE_NOT_FOUND(7, ResponseCodeType.ERROR, true), - MIX_NOT_FOUND(8, ResponseCodeType.ERROR, true), - MATERIAL_NOT_FOUND(9, ResponseCodeType.ERROR, true), - MATERIAL_ALREADY_EXIST(10, ResponseCodeType.ERROR, true), - MATERIAL_TYPE_ALREADY_EXIST(11, ResponseCodeType.ERROR, true), - COMPANY_NOT_FOUND(12, ResponseCodeType.ERROR, true), - COMPANY_ALREADY_EXIST(13, ResponseCodeType.ERROR, true), - MATERIAL_LINKED(14, ResponseCodeType.ERROR, true), - COMPANY_LINKED(15, ResponseCodeType.ERROR, true), - MIX_NOT_ASSOCIATED_WITH_RECIPE(16, ResponseCodeType.ERROR, true), - NOT_ENOUGH_MATERIAL(17, ResponseCodeType.ERROR, true), - MIX_TYPE_ALREADY_USED(18, ResponseCodeType.ERROR, true); - - private int code; - private ResponseCodeType type; - private boolean needAdditionalContent; - - ResponseCodes(int code, ResponseCodeType type, boolean needAdditionalContent) { - this.code = code; - this.type = type; - this.needAdditionalContent = needAdditionalContent; - } - - public int getCode() { - return code; - } - - public ResponseCodeType getType() { - return type; - } - - public boolean needAdditionalContent() { - return needAdditionalContent; - } - - - public enum ResponseCodeType { - ERROR, - SUCCESS - } -} diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/core/configuration/LocaleConfiguration.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/configuration/LocaleConfiguration.java index e5c24cf..d42819b 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/core/configuration/LocaleConfiguration.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/configuration/LocaleConfiguration.java @@ -18,11 +18,19 @@ public class LocaleConfiguration implements WebMvcConfigurer { @Bean public MessageSource messageSource() { ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); - messageSource.setBasenames("classpath:lang/messages", "classpath:lang/errors"); +// messageSource.setBasenames("classpath:lang/messages", "classpath:lang/responses"); + messageSource.setBasename("classpath:lang/responses"); messageSource.setDefaultEncoding("UTF-8"); return messageSource; } +// @Bean +// public MessageSource responsesMessageSource() { +// ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); +// messageSource.setBasename("classpath:lang/responses"); +// return messageSource; +// } + @Bean public LocaleResolver localeResolver() { SessionLocaleResolver localeResolver = new SessionLocaleResolver(); diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/core/utils/ControllerUtils.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/utils/ControllerUtils.java new file mode 100644 index 0000000..1e098d7 --- /dev/null +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/core/utils/ControllerUtils.java @@ -0,0 +1,8 @@ +package fyloz.trial.ColorRecipesExplorer.core.utils; + +public class ControllerUtils { + + public static String redirect(String viewName) { + return String.format("redirect:/%s", viewName); + } +} diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/services/MixService.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/services/MixService.java index 09f93b5..bd6d56b 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/services/MixService.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/services/MixService.java @@ -1,5 +1,7 @@ package fyloz.trial.ColorRecipesExplorer.services; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.dao.MixDao; import fyloz.trial.ColorRecipesExplorer.model.*; import org.springframework.beans.factory.annotation.Autowired; @@ -9,9 +11,6 @@ import org.springframework.transaction.annotation.Transactional; import java.util.ArrayList; import java.util.List; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.ERROR_SAVING; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.MATERIAL_NOT_FOUND; - @Service public class MixService extends GenericService { @@ -37,18 +36,20 @@ public class MixService extends GenericService { * @return Le message d'erreur, s'il y a lieu */ @Transactional - public String create(List materials, List quantities, Recipe recipe, MixType mixType) { + public ModelBuilder create(ModelBuilder modelBuilder, List materials, List quantities, Recipe recipe, MixType mixType) { // Crée le mélange en premier pour avoir accès à son ID pour les autres éléments Mix mix = new Mix(recipe, mixType, null); if ((mix = save(mix)) != null) { List mixQuantities = createMixQuantities(mix, materials, quantities); - if (mixQuantities == null) return MATERIAL_NOT_FOUND; + if (mixQuantities == null) { + return modelBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND); + } if (mixQuantityService.saveAll(mixQuantities)) return null; } - return ERROR_SAVING; + return modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING); } /** diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/ImageFilesController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/ImageFilesController.java index 384cd97..109e579 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/ImageFilesController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/ImageFilesController.java @@ -1,6 +1,9 @@ package fyloz.trial.ColorRecipesExplorer.web.controller; import fyloz.trial.ColorRecipesExplorer.ColorRecipesExplorerApplication; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.core.io.ImageHandler; import fyloz.trial.ColorRecipesExplorer.model.Recipe; import fyloz.trial.ColorRecipesExplorer.services.RecipeService; @@ -10,17 +13,19 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.ModelAndView; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; +import static fyloz.trial.ColorRecipesExplorer.core.utils.ControllerUtils.redirect; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.*; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; +import static fyloz.trial.ColorRecipesExplorer.web.StringBank.ERROR_SAVING_IMAGE; +import static fyloz.trial.ColorRecipesExplorer.web.StringBank.RESPONSE_ERROR; @Controller public class ImageFilesController { @@ -62,9 +67,11 @@ public class ImageFilesController { * @return La page à afficher. */ @GetMapping(ADD_IMAGE_SPECIFIC) - public String showPage(Model model, @PathVariable int recipeID) { - model.addAttribute(RECIPE_ID, recipeID); - return ADD_IMAGE; + public ModelAndView showPage(ModelAndView model, @PathVariable int recipeID) { + return new ModelBuilder(model) + .setView(ADD_IMAGE) + .addData(ModelDataType.RECIPE_ID, recipeID) + .build(); } /** @@ -83,37 +90,42 @@ public class ImageFilesController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf * @param recipeID L'identifiant de la recette * @param image L'image uploadée * @return La page à afficher. */ @PostMapping(ADD_IMAGE) - public String addImage(Model model, int recipeID, MultipartFile image) { + public ModelAndView addImage(int recipeID, MultipartFile image) { + ModelBuilder modelBuilder = new ModelBuilder(redirect(EDITOR_RECIPE_SPECIFIC.replace("{recipeID}", String.valueOf(recipeID)))); Recipe recipe = recipeService.getByID(recipeID); + if (recipe == null) { - model.addAttribute(RESPONSE_ERROR, RECIPE_NOT_FOUND); - return showPage(model, recipeID); + modelBuilder.addResponseCode(ResponseCode.RECIPE_NOT_FOUND, String.valueOf(recipeID)); + + return showPage(modelBuilder.build(), recipeID); } ImageHandler imageHandler = new ImageHandler(recipe, recipeService); if (!imageHandler.createFile()) { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING_IMAGE); - return showPage(model, recipeID); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING_IMAGE); + + return showPage(modelBuilder.build(), recipeID); } - model.addAttribute(RECIPE_CODE, recipe.getRecipeCode()); - model.addAttribute(RECIPE_ID, recipe.getRecipeID()); + modelBuilder + .addData(ModelDataType.RECIPE_CODE, recipe.getRecipeCode()) + .addData(ModelDataType.RECIPE_ID, recipe.getRecipeID()); try { // Si je n'utilise pas le path, il cherche un fichier dans les tmp ? image.transferTo(new File(imageHandler.getFile().getAbsolutePath())); - return "redirect:/" + EDITOR_RECIPE_SPECIFIC.replace("{recipeID}", String.valueOf(recipeID)); + return modelBuilder.build(); } catch (IOException e) { ColorRecipesExplorerApplication.logger.error("Erreur inconnue lors de la création d'une image", e); - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING_IMAGE); - return showPage(model, recipeID); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING_IMAGE); + + return showPage(modelBuilder.build(), recipeID); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/IndexController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/IndexController.java index 83fc815..4920e89 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/IndexController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/IndexController.java @@ -1,6 +1,8 @@ package fyloz.trial.ColorRecipesExplorer.web.controller; import fyloz.trial.ColorRecipesExplorer.PasswordValidator; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; import fyloz.trial.ColorRecipesExplorer.model.Company; import fyloz.trial.ColorRecipesExplorer.model.Recipe; import fyloz.trial.ColorRecipesExplorer.services.CompanyService; @@ -8,11 +10,11 @@ import fyloz.trial.ColorRecipesExplorer.services.RecipeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; import java.util.HashMap; import java.util.List; @@ -20,7 +22,6 @@ import java.util.Map; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.INDEX; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.PASSWORD_VALIDATION; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.RECIPES; @Controller public class IndexController { @@ -41,7 +42,7 @@ public class IndexController { * @return La page à afficher. */ @GetMapping({INDEX, "/"}) - public String showPage(Model model) { + public ModelAndView showPage() { List companies = companyService.getAll(); Map> recipes = new HashMap<>(); @@ -49,9 +50,9 @@ public class IndexController { recipes.put(company, recipeService.getByCompany(company)); } - model.addAttribute(RECIPES, recipes); - - return INDEX; + return new ModelBuilder(INDEX) + .addData(ModelDataType.RECIPE_MAP, recipes) + .build(); } /** @@ -65,8 +66,4 @@ public class IndexController { public boolean validatePassword(@RequestBody Map data) { return PasswordValidator.isValid((String) data.get("password")); } - - /** - * Donne acc - */ } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/InventoryController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/InventoryController.java index cb3534a..bd9ea27 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/InventoryController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/InventoryController.java @@ -1,5 +1,7 @@ package fyloz.trial.ColorRecipesExplorer.web.controller; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; import fyloz.trial.ColorRecipesExplorer.model.Material; import fyloz.trial.ColorRecipesExplorer.model.Mix; import fyloz.trial.ColorRecipesExplorer.model.MixQuantity; @@ -11,11 +13,11 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.HashMap; @@ -49,15 +51,14 @@ public class InventoryController { * Modèle de la page: * - materials: Contient la liste de tous les matériaux * - * @param model Le Model injecté par Thymeleaf * @return La page à afficher. */ @GetMapping(INVENTORY) - public String getInventory(Model model) { - model.addAttribute(MATERIALS, materialService.getAll().stream().filter(m -> !m.isMixType()).collect(Collectors.toList())); - model.addAttribute(MATERIAL_TYPES, materialTypeService.getAll()); - - return INVENTORY; + public ModelAndView getInventory(ModelAndView model) { + return new ModelBuilder(INVENTORY) + .addData(ModelDataType.MATERIALS, materialService.getAll().stream().filter(m -> !m.isMixType()).collect(Collectors.toList())) + .addData(ModelDataType.MATERIAL_TYPES, materialTypeService.getAll()) + .build(); } /** @@ -87,6 +88,7 @@ public class InventoryController { @PostMapping(value = USE_INVENTORY, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody @Transactional + // TODO traduits les méthodes JSON public Map consumeMaterials(@RequestBody Map form) { Map response = new HashMap<>(); diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/OthersController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/OthersController.java index 8042e07..3be55f5 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/OthersController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/OthersController.java @@ -1,14 +1,17 @@ package fyloz.trial.ColorRecipesExplorer.web.controller; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.servlet.ModelAndView; +import static fyloz.trial.ColorRecipesExplorer.core.utils.ControllerUtils.redirect; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.TOUCHUP; @Controller public class OthersController { @GetMapping(TOUCHUP) - public String getTouchUpPdf() { - return "redirect:/pdf/touchup.pdf"; + public ModelAndView getTouchUpPdf() { + return new ModelBuilder(redirect("pdf/touchup.pdf")).build(); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/RecipeExplorerController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/RecipeExplorerController.java index e293285..7f0471b 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/RecipeExplorerController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/RecipeExplorerController.java @@ -1,5 +1,8 @@ package fyloz.trial.ColorRecipesExplorer.web.controller; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Mix; import fyloz.trial.ColorRecipesExplorer.model.Recipe; import fyloz.trial.ColorRecipesExplorer.services.MixService; @@ -7,13 +10,10 @@ import fyloz.trial.ColorRecipesExplorer.services.RecipeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; -import java.util.Comparator; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.*; import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @@ -40,27 +40,29 @@ public class RecipeExplorerController { * - mixes: Contient les mélanges associées à la recette * - images: Contient la liste des noms des images associées à la recette * - * @param model Le Model injecté par Thymeleaf * @param recipeID L'identifiant de la recette * @return La page à afficher. */ @GetMapping(EXPLORER_RECIPE_SPECIFIC) - public String showRecipe(Model model, @PathVariable int recipeID) { + public ModelAndView showRecipe(@PathVariable int recipeID) { + ModelBuilder modelBuilder = new ModelBuilder(EXPLORER_RECIPE); Recipe recipe = recipeService.getByID(recipeID); if (recipe == null) { - model.addAttribute(RESPONSE_ERROR, String.format(RECIPE_NOT_FOUND, recipeID)); - return INDEX; + return modelBuilder + .setView(INDEX) + .addResponseCode(ResponseCode.RECIPE_NOT_FOUND, String.valueOf(recipeID)) + .build(); } - List mixes = recipe.getRecipeMixes(); + List mixes = new ArrayList<>(recipe.getRecipeMixes()); // Convertit le PersistentBag en ArrayList mixes.sort(Comparator.comparing(Mix::getMixID)); - model.addAttribute(RECIPE, recipe); - model.addAttribute(MIXES, mixes); - model.addAttribute("images", recipeService.getImageFiles(recipe)); - - return EXPLORER_RECIPE; + return modelBuilder + .addData(ModelDataType.RECIPE, recipe) + .addData(ModelDataType.MIXES, mixes) + .addData(ModelDataType.IMAGES, recipeService.getImageFiles(recipe)) + .build(); } /** diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/CompanyCreatorController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/CompanyCreatorController.java index ef2545d..7fc6e19 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/CompanyCreatorController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/CompanyCreatorController.java @@ -1,20 +1,22 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.creators; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Company; import fyloz.trial.ColorRecipesExplorer.services.CompanyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.CREATOR_COMPANY; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.CREATOR_COMPANY_SUCCESS; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @Controller public class CompanyCreatorController { @@ -32,9 +34,11 @@ public class CompanyCreatorController { * @return La page à afficher. */ @GetMapping(CREATOR_COMPANY) - public String showCreationPage(Model model, Company company) { - model.addAttribute("company", company); - return CREATOR_COMPANY; + public ModelAndView showCreationPage(ModelAndView model, Company company) { + return new ModelBuilder(model) + .setView(CREATOR_COMPANY) + .addData(ModelDataType.COMPANY, company) + .build(); } /** @@ -55,18 +59,21 @@ public class CompanyCreatorController { * @return La page à afficher. */ @PostMapping(value = CREATOR_COMPANY, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public String createCompany(Model model, @ModelAttribute @Valid Company company) { + public ModelAndView createCompany(@ModelAttribute @Valid Company company) { + ModelBuilder modelBuilder = new ModelBuilder(CREATOR_COMPANY_SUCCESS); + if (companyService.isValidForCreation(company)) { if ((company = companyService.save(company)) != null) { - model.addAttribute("companyName", company.getCompanyName()); - return CREATOR_COMPANY_SUCCESS; + return modelBuilder + .addData(ModelDataType.COMPANY_NAME, company.getCompanyName()) + .build(); } else { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING); } } else { - model.addAttribute(RESPONSE_ERROR, String.format(COMPANY_ALREADY_EXIST, company.getCompanyName())); + modelBuilder.addResponseCode(ResponseCode.MIX_NOT_ASSOCIATED_WITH_RECIPE, company.getCompanyName(), null); } - return showCreationPage(model, company); + return showCreationPage(modelBuilder.build(), company); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MaterialCreatorController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MaterialCreatorController.java index 611a7b0..97dfd86 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MaterialCreatorController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MaterialCreatorController.java @@ -1,21 +1,23 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.creators; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Material; import fyloz.trial.ColorRecipesExplorer.services.MaterialService; import fyloz.trial.ColorRecipesExplorer.services.MaterialTypeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.CREATOR_MATERIAL; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.CREATOR_MATERIAL_SUCCESS; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @Controller public class MaterialCreatorController { @@ -35,10 +37,12 @@ public class MaterialCreatorController { * @return La page à afficher. */ @GetMapping(CREATOR_MATERIAL) - public String showCreationPage(Model model, Material material) { - model.addAttribute(MATERIAL, material == null ? new Material() : material); - model.addAttribute(MATERIAL_TYPES, materialTypeService.getAll()); - return CREATOR_MATERIAL; + public ModelAndView showCreationPage(ModelAndView model, Material material) { + return new ModelBuilder(model) + .setView(CREATOR_MATERIAL) + .addData(ModelDataType.MATERIAL, material != null ? material : new Material()) + .addData(ModelDataType.MATERIAL_TYPES, materialTypeService.getAll()) + .build(); } /** @@ -55,29 +59,30 @@ public class MaterialCreatorController { *

* REQUIERT UNE AUTORISATION * - * @param model Le modèle injecté par Thymeleaf * @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 String create(Model model, @Valid Material material, MultipartFile simdut) { + public ModelAndView create(@Valid Material material, MultipartFile simdut) { + ModelBuilder modelBuilder = new ModelBuilder(CREATOR_MATERIAL_SUCCESS); + if (!materialService.exists(material)) { if ((material = materialService.save(material)) != null) { - model.addAttribute(MATERIAL_CODE, material.getMaterialCode()); + modelBuilder.addData(ModelDataType.MATERIAL_CODE, material.getMaterialCode()); if (!materialService.addSimdut(simdut, material)) { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING_SIMDUT); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING_SIMDUT); } - return CREATOR_MATERIAL_SUCCESS; + return modelBuilder.build(); } else { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING); } } else { - model.addAttribute(RESPONSE_ERROR, String.format(MATERIAL_ALREADY_EXIST, material.getMaterialCode())); + modelBuilder.addResponseCode(ResponseCode.MATERIAL_ALREADY_EXIST, material.getMaterialCode()); } - return showCreationPage(model, material); + return showCreationPage(modelBuilder.build(), material); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MaterialTypeCreatorController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MaterialTypeCreatorController.java index d47653e..e135c45 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MaterialTypeCreatorController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MaterialTypeCreatorController.java @@ -1,18 +1,21 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.creators; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.MaterialType; import fyloz.trial.ColorRecipesExplorer.services.MaterialTypeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; +import static fyloz.trial.ColorRecipesExplorer.core.utils.ControllerUtils.redirect; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.CREATOR_MATERIAL_TYPE; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.INDEX; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @Controller public class MaterialTypeCreatorController { @@ -25,23 +28,27 @@ public class MaterialTypeCreatorController { } @GetMapping(CREATOR_MATERIAL_TYPE) - public String showPage(Model model, MaterialType materialType) { - model.addAttribute(MATERIAL_TYPE, materialType == null ? new MaterialType() : materialType); - return CREATOR_MATERIAL_TYPE; + public ModelAndView showPage(ModelAndView model, MaterialType materialType) { + return new ModelBuilder(model) + .setView(CREATOR_MATERIAL_TYPE) + .addData(ModelDataType.MATERIAL_TYPE, materialType == null ? new MaterialType() : materialType) + .build(); } @PostMapping(CREATOR_MATERIAL_TYPE) - public String create(Model model, @Valid MaterialType materialType) { + public ModelAndView create(@Valid MaterialType materialType) { + ModelBuilder modelBuilder = new ModelBuilder(redirect(INDEX)); + if (materialTypeService.isValidForCreation(materialType)) { if (materialTypeService.save(materialType) != null) { - return "redirect:/" + INDEX; + return modelBuilder.build(); } else { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING); } } else { - model.addAttribute(RESPONSE_ERROR, String.format(MIX_TYPE_ALREADY_USED, materialType.getMaterialTypeName())); + modelBuilder.addResponseCode(ResponseCode.MIX_TYPE_ALREADY_USED, materialType.getMaterialTypeName()); } - return showPage(model, materialType); + return showPage(modelBuilder.build(), materialType); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MixCreatorController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MixCreatorController.java index 4771077..b63e950 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MixCreatorController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/MixCreatorController.java @@ -1,5 +1,8 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.creators; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Material; import fyloz.trial.ColorRecipesExplorer.model.MixType; import fyloz.trial.ColorRecipesExplorer.model.Recipe; @@ -10,20 +13,22 @@ import fyloz.trial.ColorRecipesExplorer.services.RecipeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; +import static fyloz.trial.ColorRecipesExplorer.core.utils.ControllerUtils.redirect; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.*; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; +import static fyloz.trial.ColorRecipesExplorer.web.StringBank.MIX_TYPE; +import static fyloz.trial.ColorRecipesExplorer.web.StringBank.RECIPE_ID; @Controller public class MixCreatorController { @@ -50,21 +55,26 @@ public class MixCreatorController { * @return La page à afficher. */ @GetMapping(CREATOR_MIX_SPECIFIC) - public String showCreationPage(Model model, @PathVariable int recipeID) { + public ModelAndView showCreationPage(ModelAndView model, @PathVariable int recipeID) { + ModelBuilder modelBuilder = new ModelBuilder(model) + .setView(CREATOR_MIX); Recipe recipe = recipeService.getByID(recipeID); + if (recipe == null) { - return "redirect:/" + EDITOR_RECIPE; + return modelBuilder + .setView(redirect(EDITOR_RECIPE)) + .build(); } List associatedMixTypes = recipeService.getAssociatedMixesTypes(recipe); // Récupère seulement les produits qui ne sont pas des types de mélange OU que ce type existe dans la recette List materials = materialService.getAll().stream().filter(m -> !m.isMixType() || associatedMixTypes.contains(mixTypeService.getByMaterial(m))).collect(Collectors.toList()); - model.addAttribute(RECIPE, recipe); - model.addAttribute(MATERIALS, materials); - model.addAttribute("materialsJson", materialService.asJson(materials)); // Ajoute les matériaux sous forme de JSON pour utiliser la liste en Javascript - - return CREATOR_MIX; + return modelBuilder + .addData(ModelDataType.RECIPE, recipe) + .addData(ModelDataType.MATERIALS, materials) + .addAttribute("materialsJson", materialService.asJson(materials)) + .build(); } /** @@ -82,12 +92,11 @@ public class MixCreatorController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf - * @param form La formulaire du mélange entré par l'utilisateur + * @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 String createMix(Model model, @RequestBody LinkedMultiValueMap form) { + public ModelAndView createMix(@RequestBody LinkedMultiValueMap form) { String mixTypeName = form.get(MIX_TYPE).get(0); int recipeID = Integer.parseInt(form.get(RECIPE_ID).get(0)); @@ -104,27 +113,33 @@ public class MixCreatorController { } } + ModelBuilder modelBuilder = new ModelBuilder(redirect(EDITOR_RECIPE_SPECIFIC.replaceAll("\\{" + RECIPE_ID + "}", String.valueOf(recipeID)))); + Recipe recipe = recipeService.getByID(recipeID); if (recipe == null) { - model.addAttribute(RESPONSE_ERROR, String.format(RECIPE_NOT_FOUND, recipeID)); - return EDITOR_RECIPE; + return modelBuilder + .addResponseCode(ResponseCode.RECIPE_NOT_FOUND, String.valueOf(recipeID)) + .build(); } MixType mixType = mixTypeService.createByName(mixTypeName); if (mixType == null) { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING); - return EDITOR_RECIPE; + return modelBuilder + .setView(EDITOR_RECIPE) + .addResponseCode(ResponseCode.ERROR_SAVING) + .build(); } else if (recipeService.getAssociatedMixesTypes(recipe).contains(mixType)) { - model.addAttribute(RESPONSE_ERROR, MIX_TYPE_ALREADY_USED); - return EDITOR_RECIPE; + return modelBuilder + .setView(EDITOR_RECIPE) + .addResponseCode(ResponseCode.MIX_TYPE_ALREADY_USED) + .build(); } - String errorMessage = mixService.create(materials, quantities, recipe, mixType); - if (errorMessage != null) { - model.addAttribute(RESPONSE_ERROR, errorMessage); - return showCreationPage(model, recipeID); + ModelBuilder creationResult = mixService.create(modelBuilder, materials, quantities, recipe, mixType); + if (creationResult != null) { + return showCreationPage(modelBuilder.build(), recipeID); } - return "redirect:/" + EDITOR_RECIPE_SPECIFIC.replaceAll("\\{" + RECIPE_ID + "}", String.valueOf(recipeID)); + return modelBuilder.build(); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/RecipeCreatorController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/RecipeCreatorController.java index 9a39729..4fa77db 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/RecipeCreatorController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/creators/RecipeCreatorController.java @@ -1,19 +1,21 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.creators; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Recipe; import fyloz.trial.ColorRecipesExplorer.services.CompanyService; import fyloz.trial.ColorRecipesExplorer.services.RecipeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.CREATOR_RECIPE; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.CREATOR_RECIPE_SUCCESS; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @Controller public class RecipeCreatorController { @@ -34,10 +36,12 @@ public class RecipeCreatorController { * @return La page à afficher. */ @GetMapping(CREATOR_RECIPE) - public String showCreationPage(Model model, Recipe recipe) { - model.addAttribute(COMPANIES, companyService.getAll()); - model.addAttribute(RECIPE, recipe == null ? new Recipe() : recipe); - return CREATOR_RECIPE; + public ModelAndView showCreationPage(ModelAndView model, Recipe recipe) { + return new ModelBuilder(model) + .setView(CREATOR_RECIPE) + .addData(ModelDataType.COMPANIES, companyService.getAll()) + .addData(ModelDataType.RECIPE, recipe == null ? new Recipe() : recipe) + .build(); } /** @@ -52,19 +56,20 @@ public class RecipeCreatorController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf * @param recipe La recette à créer * @return La page à afficher. */ @PostMapping(value = CREATOR_RECIPE) - public String createRecipe(Model model, @Valid Recipe recipe) { + public ModelAndView createRecipe(@Valid Recipe recipe) { + ModelBuilder modelBuilder = new ModelBuilder(CREATOR_RECIPE_SUCCESS); + if ((recipe = recipeService.save(recipe)) != null) { - model.addAttribute(RECIPE_CODE, recipe.getRecipeCode()); - model.addAttribute(RECIPE_ID, recipe.getRecipeID()); - return CREATOR_RECIPE_SUCCESS; - } else { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING); - return showCreationPage(model, recipe); + return modelBuilder + .addData(ModelDataType.RECIPE, recipe) + .build(); } + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING); + + return showCreationPage(modelBuilder.build(), recipe); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/MaterialEditorController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/MaterialEditorController.java index fba3bff..4e02287 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/MaterialEditorController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/MaterialEditorController.java @@ -1,21 +1,24 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.editors; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Material; import fyloz.trial.ColorRecipesExplorer.services.MaterialService; import fyloz.trial.ColorRecipesExplorer.services.MaterialTypeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.servlet.ModelAndView; import java.util.stream.Collectors; +import static fyloz.trial.ColorRecipesExplorer.core.utils.ControllerUtils.redirect; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.*; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @Controller public class MaterialEditorController { @@ -39,10 +42,11 @@ public class MaterialEditorController { * @return La page à afficher. */ @GetMapping(EDITOR_MATERIAL) - public String listMaterials(Model model) { - model.addAttribute(MATERIALS, materialService.getAll().stream().filter(m -> !m.isMixType()).collect(Collectors.toList())); - - return EDITOR_MATERIAL; + public ModelAndView listMaterials(ModelAndView model) { + return new ModelBuilder(model) + .setView(EDITOR_MATERIAL) + .addData(ModelDataType.MATERIALS, materialService.getAll().stream().filter(m -> !m.isMixType()).collect(Collectors.toList())) + .build(); } /** @@ -54,18 +58,21 @@ public class MaterialEditorController { * @return La page à afficher. */ @GetMapping(EDITOR_MATERIAL_SPECIFIC) - public String showEditPage(Model model, @PathVariable int materialID) { + public ModelAndView showEditPage(ModelAndView model, @PathVariable int materialID) { + ModelBuilder modelBuilder = new ModelBuilder(model) + .setView(EDITOR_MATERIAL_EDITOR); Material material = materialService.getByID(materialID); if (material == null) { - model.addAttribute(RESPONSE_ERROR, String.format(MATERIAL_NOT_FOUND, materialID)); + modelBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, String.valueOf(materialID)); - return listMaterials(model); + return listMaterials(modelBuilder.build()); } - model.addAttribute(MATERIAL, material); - model.addAttribute(MATERIAL_TYPES, materialTypeService.getAll()); - return EDITOR_MATERIAL_EDITOR; + return modelBuilder + .addData(ModelDataType.MATERIAL, material) + .addData(ModelDataType.MATERIAL_TYPES, materialTypeService.getAll()) + .build(); } /** @@ -80,10 +87,11 @@ public class MaterialEditorController { * @return La page à afficher. */ @GetMapping(value = EDIT_MATERIAL_SIMDUT_SPECIFIC) - public String chooseSIMDUTFile(Model model, @PathVariable int materialID) { - model.addAttribute(MATERIAL_ID, materialID); - - return EDIT_MATERIAL_SIMDUT; + public ModelAndView chooseSIMDUTFile(ModelAndView model, @PathVariable int materialID) { + return new ModelBuilder(model) + .setView(EDIT_MATERIAL_SIMDUT) + .addData(ModelDataType.MATERIAL_ID, materialID) + .build(); } /** @@ -94,25 +102,28 @@ public class MaterialEditorController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf * @param materialID 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 String saveSIMDUT(Model model, int materialID, MultipartFile simdut) { + public ModelAndView saveSIMDUT(int materialID, MultipartFile simdut) { + ModelBuilder modelBuilder = new ModelBuilder(redirect("material/editor/" + materialID)); Material material = materialService.getByID(materialID); + if (material == null) { - model.addAttribute(RESPONSE_ERROR, MATERIAL_NOT_FOUND); - return chooseSIMDUTFile(model, materialID); + modelBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, String.valueOf(materialID)); + + return chooseSIMDUTFile(modelBuilder.build(), materialID); } if (!materialService.removeSimdut(material) || !materialService.addSimdut(simdut, material)) { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING_SIMDUT); - return chooseSIMDUTFile(model, materialID); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING_SIMDUT); + + return chooseSIMDUTFile(modelBuilder.build(), materialID); } - return "redirect:/material/editor/" + materialID; + return modelBuilder.build(); } /** @@ -129,25 +140,26 @@ public class MaterialEditorController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf * @param material Le produit à mettre à jour * @return La page à afficher. */ @PostMapping(value = EDITOR_MATERIAL, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public String saveEditedMaterial(Model model, Material material) { + public ModelAndView saveEditedMaterial(Material material) { + ModelBuilder modelBuilder = new ModelBuilder(""); int materialID = material.getMaterialID(); if (materialService.getByID(materialID) == null) { - model.addAttribute(RESPONSE_ERROR, String.format(MATERIAL_NOT_FOUND, materialID)); + modelBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, String.valueOf(materialID)); } if ((material = materialService.update(material)) != null) { - model.addAttribute(MATERIAL_CODE, material.getMaterialCode()); + modelBuilder.addData(ModelDataType.MATERIAL_CODE, material.getMaterialCode()); } else { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING); - return showEditPage(model, materialID); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING); + + return showEditPage(modelBuilder.build(), materialID); } - return listMaterials(model); + return listMaterials(modelBuilder.build()); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/MixEditorController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/MixEditorController.java index c0912eb..eb92e3e 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/MixEditorController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/MixEditorController.java @@ -1,5 +1,8 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.editors; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Material; import fyloz.trial.ColorRecipesExplorer.model.Mix; import fyloz.trial.ColorRecipesExplorer.model.MixType; @@ -10,19 +13,20 @@ import fyloz.trial.ColorRecipesExplorer.services.RecipeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.util.LinkedMultiValueMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.servlet.ModelAndView; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; +import static fyloz.trial.ColorRecipesExplorer.core.utils.ControllerUtils.redirect; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.*; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; +import static fyloz.trial.ColorRecipesExplorer.web.StringBank.MIX_ID; @Controller public class MixEditorController { @@ -49,12 +53,16 @@ public class MixEditorController { * @return La page à afficher. */ @GetMapping(EDITOR_MIX_SPECIFIC) - public String showPage(Model model, @PathVariable int mixID) { + public ModelAndView showPage(ModelAndView model, @PathVariable int mixID) { + ModelBuilder modelBuilder = new ModelBuilder(model) + .setView(EDITOR_MIX_SPECIFIC.replaceAll("/\\{" + MIX_ID + "}", "")); Mix mix = mixService.getByID(mixID); // Renvoie l'utilisateur à la page d'édition des recettes si le mélange n'est pas trouvé if (mix == null) { - return "redirect:/" + EDITOR_RECIPE; + return modelBuilder + .setView(redirect(EDITOR_RECIPE)) + .build(); } List materials = new ArrayList<>(); @@ -65,12 +73,12 @@ public class MixEditorController { } } - model.addAttribute(MIX, mix); - model.addAttribute("mixJson", mixService.asJson(mix)); - model.addAttribute(RECIPE_CODE, mix.getRecipe().getRecipeCode()); - model.addAttribute("materialsJson", materialService.asJson(materials)); - - return EDITOR_MIX_SPECIFIC.replaceAll("/\\{" + MIX_ID + "}", ""); + return modelBuilder + .addData(ModelDataType.MIX, mix) + .addData(ModelDataType.RECIPE_CODE, mix.getRecipe().getRecipeCode()) + .addAttribute("mixJson", materialService.asJson(mix)) + .addAttribute("materialsJson", materialService.asJson(materials)) + .build(); } /** @@ -87,25 +95,29 @@ public class MixEditorController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf * @param form Le formulaire du mélange entré par l'utilisateur * @return La page à afficher. */ @PostMapping(value = EDITOR_MIX, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public String saveMix(Model model, @RequestBody LinkedMultiValueMap form) { + public ModelAndView saveMix(@RequestBody LinkedMultiValueMap form) { + ModelBuilder modelBuilder = new ModelBuilder(""); int mixID = Integer.parseInt((String) form.get(MIX_ID).get(0)); Mix mix = mixService.getByID(mixID); if (mix == null) { - model.addAttribute(RESPONSE_ERROR, String.format(MIX_NOT_FOUND, mixID)); - return showPage(model, mixID); + modelBuilder.addResponseCode(ResponseCode.MIX_NOT_FOUND, String.valueOf(mixID)); + + return showPage(modelBuilder.build(), mixID); } + modelBuilder.setView(redirect(EDITOR_RECIPE_SPECIFIC.replace("{recipeID}", String.valueOf(mix.getRecipe().getRecipeID())))); + List materials = new ArrayList<>(); List quantities = new ArrayList<>(); for (String key : form.keySet()) { LinkedList value = (LinkedList) form.get(key); + // TODO Laisser ? assert value != null : "Une valeur du formulaire d'édition d'un mélange est nulle."; if (key.equals("product")) { @@ -116,26 +128,33 @@ public class MixEditorController { } if (!mixService.edit(mix, materials, quantities)) { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING); - return showPage(model, mixID); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING); + + return showPage(modelBuilder.build(), mixID); } - return "redirect:/" + EDITOR_RECIPE_SPECIFIC.replace("{recipeID}", String.valueOf(mix.getRecipe().getRecipeID())); + return modelBuilder.build(); } @GetMapping(REMOVER_MIX_SPECIFIC) - public String deleteMix(Model model, @PathVariable int mixID) { + public ModelAndView deleteMix(@PathVariable int mixID) { + ModelBuilder modelBuilder = new ModelBuilder(""); Mix mix = mixService.getByID(mixID); + if (mix == null) { - model.addAttribute(RESPONSE_ERROR, MIX_NOT_FOUND); - return showPage(model, mixID); + modelBuilder.addResponseCode(ResponseCode.MIX_NOT_FOUND, String.valueOf(mixID)); + + return showPage(modelBuilder.build(), mixID); } + modelBuilder.setView(redirect(EDITOR_RECIPE_SPECIFIC.replace("{recipeID}", String.valueOf(mix.getRecipe().getRecipeID())))); + if (!mixService.deleteMix(mix)) { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING); - return showPage(model, mixID); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING); + + return showPage(modelBuilder.build(), mixID); } - return "redirect:/" + EDITOR_RECIPE_SPECIFIC.replace("{recipeID}", String.valueOf(mix.getRecipe().getRecipeID())); + return modelBuilder.build(); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/RecipeEditorController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/RecipeEditorController.java index 668adf5..a1d4cf6 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/RecipeEditorController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/editors/RecipeEditorController.java @@ -1,22 +1,25 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.editors; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Recipe; import fyloz.trial.ColorRecipesExplorer.services.CompanyService; import fyloz.trial.ColorRecipesExplorer.services.RecipeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.servlet.ModelAndView; import javax.validation.Valid; +import java.util.ArrayList; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.*; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @Controller public class RecipeEditorController { @@ -40,10 +43,10 @@ public class RecipeEditorController { * @return La page à afficher. */ @GetMapping(EDITOR_RECIPE) - public String listRecipes(Model model) { - model.addAttribute(RECIPES, recipeService.getRecipesByCompany()); - - return EDITOR_RECIPE; + public ModelAndView listRecipes(ModelAndView model) { + return new ModelBuilder(model) + .addData(ModelDataType.RECIPE_MAP, recipeService.getRecipesByCompany()) + .build(); } /** @@ -63,21 +66,24 @@ public class RecipeEditorController { * @return La page à afficher. */ @GetMapping(EDITOR_RECIPE_SPECIFIC) - public String showEditPage(Model model, @PathVariable int recipeID) { + public ModelAndView showEditPage(ModelAndView model, @PathVariable int recipeID) { + ModelBuilder modelBuilder = new ModelBuilder(model) + .setView(EDITOR_RECIPE_EDITOR); Recipe recipe = recipeService.getByID(recipeID); if (recipe == null) { - model.addAttribute(RESPONSE_ERROR, String.format(RECIPE_NOT_FOUND, recipeID)); - return listRecipes(model); + modelBuilder.addResponseCode(ResponseCode.RECIPE_NOT_FOUND, String.valueOf(recipeID)); + + return listRecipes(modelBuilder.build()); } - model.addAttribute(RECIPE, recipe); - model.addAttribute("recipeJSON", recipeService.asJson(recipe)); - model.addAttribute(COMPANIES, companyService.getAll()); - model.addAttribute(MIXES, recipeService.getSortedMixes(recipe)); - model.addAttribute("images", recipeService.getImageFiles(recipe)); - - return EDITOR_RECIPE_EDITOR; + return modelBuilder + .addData(ModelDataType.RECIPE, recipe) + .addData(ModelDataType.COMPANIES, companyService.getAll()) + .addData(ModelDataType.MIXES, new ArrayList<>(recipeService.getSortedMixes(recipe))) // Convertit le PersistentBag en ArrayList + .addData(ModelDataType.IMAGES, recipeService.getImageFiles(recipe)) + .addAttribute("recipeJSON", recipeService.asJson(recipe)) + .build(); } /** @@ -94,23 +100,24 @@ public class RecipeEditorController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf * @param recipe La recette à modifier * @param form Le formulaire entré par l'utilisateur * @return La page à afficher. */ @PostMapping(value = EDITOR_RECIPE, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public String saveRecipe(Model model, @Valid Recipe recipe, @RequestBody MultiValueMap form) { + public ModelAndView saveRecipe(@Valid Recipe recipe, @RequestBody MultiValueMap form) { + ModelBuilder modelBuilder = new ModelBuilder(""); int recipeID = recipe.getRecipeID(); if (recipeService.getByID(recipeID) == null) { - model.addAttribute(RESPONSE_ERROR, String.format(RECIPE_NOT_FOUND, recipeID)); - return listRecipes(model); + modelBuilder.addResponseCode(ResponseCode.RECIPE_NOT_FOUND, String.valueOf(recipeID)); + + return listRecipes(modelBuilder.build()); } recipe = recipeService.createAndSetSteps(recipe, form); - model.addAttribute(RECIPE_CODE, recipe.getRecipeCode()); - return listRecipes(model); + modelBuilder.addData(ModelDataType.RECIPE_CODE, recipe.getRecipeCode()); + return listRecipes(modelBuilder.build()); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/CompanyRemoverController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/CompanyRemoverController.java index 1c2bec6..30d9ae2 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/CompanyRemoverController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/CompanyRemoverController.java @@ -1,17 +1,19 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.removers; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Company; import fyloz.trial.ColorRecipesExplorer.services.CompanyService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.ModelAndView; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.REMOVER_COMPANY; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.REMOVER_COMPANY_SPECIFIC; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @Controller public class CompanyRemoverController { @@ -33,10 +35,11 @@ public class CompanyRemoverController { * @return La page à afficher */ @GetMapping(REMOVER_COMPANY) - public String showPage(Model model) { - model.addAttribute(COMPANIES, companyService.getAll()); - - return REMOVER_COMPANY; + public ModelAndView showPage(ModelAndView model) { + return new ModelBuilder(model) + .setView(REMOVER_COMPANY) + .addData(ModelDataType.COMPANIES, companyService.getAll()) + .build(); } /** @@ -54,24 +57,25 @@ public class CompanyRemoverController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf * @param companyID L'identifiant de la bannière * @return La page à afficher */ @PostMapping(REMOVER_COMPANY_SPECIFIC) - public String removeCompany(Model model, @PathVariable int companyID) { + public ModelAndView removeCompany(@PathVariable int companyID) { + ModelBuilder modelBuilder = new ModelBuilder(""); Company company = companyService.getByID(companyID); + if (companyService.exists(company)) { if (companyService.deleteIfNotLinked(company)) { - model.addAttribute("successCompanyName", company.getCompanyName()); + modelBuilder.addData(ModelDataType.COMPANY_NAME, company.getCompanyName()); } else { - model.addAttribute(RESPONSE_ERROR, String.format(COMPANY_LINKED, company.getCompanyName())); + modelBuilder.addResponseCode(ResponseCode.COMPANY_LINKED, company.getCompanyName()); } } else { - model.addAttribute(RESPONSE_ERROR, String.format(COMPANY_NOT_FOUND, companyID)); + modelBuilder.addResponseCode(ResponseCode.COMPANY_NOT_FOUND, String.valueOf(companyID)); } - return showPage(model); + return showPage(modelBuilder.build()); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/MaterialRemoverController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/MaterialRemoverController.java index f8edee6..599a3c8 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/MaterialRemoverController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/MaterialRemoverController.java @@ -1,19 +1,21 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.removers; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Material; import fyloz.trial.ColorRecipesExplorer.services.MaterialService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.ModelAndView; import java.util.stream.Collectors; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.REMOVER_MATERIAL; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.REMOVER_MATERIAL_SPECIFIC; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @Controller public class MaterialRemoverController { @@ -32,10 +34,11 @@ public class MaterialRemoverController { * @return La page à afficher. */ @GetMapping(REMOVER_MATERIAL) - public String showPage(Model model) { - model.addAttribute(MATERIALS, materialService.getAll().stream().filter(m -> !m.isMixType()).collect(Collectors.toList())); - - return REMOVER_MATERIAL; + public ModelAndView showPage(ModelAndView model) { + return new ModelBuilder(model) + .setView(REMOVER_MATERIAL) + .addData(ModelDataType.MATERIALS, materialService.getAll().stream().filter(m -> !m.isMixType()).collect(Collectors.toList())) + .build(); } /** @@ -53,28 +56,28 @@ public class MaterialRemoverController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf * @param materialID L'identifiant du produit à supprimer * @return La page à afficher. */ @PostMapping(REMOVER_MATERIAL_SPECIFIC) - public String removeMaterial(Model model, @PathVariable int materialID) { + public ModelAndView removeMaterial(@PathVariable int materialID) { + ModelBuilder modelBuilder = new ModelBuilder(""); Material material = materialService.getByID(materialID); if (material == null) { - model.addAttribute(RESPONSE_ERROR, String.format(MATERIAL_NOT_FOUND, materialID)); + modelBuilder.addResponseCode(ResponseCode.MIX_NOT_FOUND, String.valueOf(materialID)); } else { if (materialService.deleteIfNotLinked(material)) { - model.addAttribute(MATERIAL_CODE, material.getMaterialCode()); + modelBuilder.addData(ModelDataType.MATERIAL_CODE, material.getMaterialCode()); if (!materialService.removeSimdut(material)) { - model.addAttribute(RESPONSE_ERROR, ERROR_SAVING_SIMDUT); + modelBuilder.addResponseCode(ResponseCode.ERROR_SAVING_SIMDUT); } } else { - model.addAttribute(RESPONSE_ERROR, String.format(MATERIAL_LINKED, material.getMaterialCode())); + modelBuilder.addResponseCode(ResponseCode.MATERIAL_LINKED, material.getMaterialCode()); } } - return showPage(model); + return showPage(modelBuilder.build()); } } diff --git a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/RecipeRemoverController.java b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/RecipeRemoverController.java index d23698d..05e2424 100644 --- a/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/RecipeRemoverController.java +++ b/src/main/java/fyloz/trial/ColorRecipesExplorer/web/controller/removers/RecipeRemoverController.java @@ -1,16 +1,18 @@ package fyloz.trial.ColorRecipesExplorer.web.controller.removers; +import fyloz.trial.ColorRecipesExplorer.core.ModelBuilder; +import fyloz.trial.ColorRecipesExplorer.core.ModelDataType; +import fyloz.trial.ColorRecipesExplorer.core.ResponseCode; import fyloz.trial.ColorRecipesExplorer.model.Recipe; import fyloz.trial.ColorRecipesExplorer.services.RecipeService; import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.ModelAndView; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.REMOVER_RECIPE; import static fyloz.trial.ColorRecipesExplorer.web.PagesPaths.REMOVER_RECIPE_SPECIFIC; -import static fyloz.trial.ColorRecipesExplorer.web.StringBank.*; @Controller public class RecipeRemoverController { @@ -28,10 +30,11 @@ public class RecipeRemoverController { * @return La page à afficher. */ @GetMapping(REMOVER_RECIPE) - public String listRecipes(Model model) { - model.addAttribute(RECIPES, recipeService.getRecipesByCompany()); - - return REMOVER_RECIPE; + public ModelAndView listRecipes(ModelAndView model) { + return new ModelBuilder(model) + .setView(REMOVER_RECIPE) + .addData(ModelDataType.RECIPES, recipeService.getRecipesByCompany()) + .build(); } /** @@ -49,23 +52,25 @@ public class RecipeRemoverController { *

* REQUIERT UNE AUTORISATION * - * @param model Le Model injecté par Thymeleaf * @param recipeID L'identifiant de la recette * @return La page à afficher. */ @PostMapping(REMOVER_RECIPE_SPECIFIC) - public String removeRecipe(Model model, @PathVariable int recipeID) { + public ModelAndView removeRecipe(@PathVariable int recipeID) { + ModelBuilder modelBuilder = new ModelBuilder(""); Recipe recipe = recipeService.getByID(recipeID); // Affiche un erreur si le recette n'est pas trouvée if (recipe == null) { - model.addAttribute(RESPONSE_ERROR, String.format(RECIPE_NOT_FOUND, recipeID)); - return listRecipes(model); + modelBuilder.addResponseCode(ResponseCode.RECIPE_NOT_FOUND, String.valueOf(recipeID)); + + return listRecipes(modelBuilder.build()); } + // TODO erreur dans cette méthode recipeService.deleteRecipe(recipe); - model.addAttribute(RECIPE_CODE, recipe.getRecipeCode()); - return listRecipes(model); + modelBuilder.addData(ModelDataType.RECIPE_CODE, recipe.getRecipeCode()); + return listRecipes(modelBuilder.build()); } } diff --git a/src/main/resources/lang/responses_en.properties b/src/main/resources/lang/responses_en.properties index 0f2939c..85f91d1 100644 --- a/src/main/resources/lang/responses_en.properties +++ b/src/main/resources/lang/responses_en.properties @@ -1,5 +1,14 @@ response.1=The quantities of each material used have been deducted from the inventory -response.2=The recipe's informations have been saved +response.10=There is already a material with the code {0} +response.11=There is already a material type named {0} +response.12=Any banner with the identifier {0} has been found +response.13=There is already a banner named {0} +response.14=The material {0} is linked to one or more recipes, delete them first +response.15=The banner {0} is linked to one or more recipes, delete them first +response.16=The mix with the identifier {0} is not linked to the recipe with the identifier {1} +response.17=There is not enough {0} in inventory for this recipe +response.18=This recipe already contains a mix of the type {0} +response.2=The recipe''s informations have been saved response.3=An error has occurred while saving response.4=An error has occurred while saving the image response.5=An error has occurred while saving the SIMDUT file diff --git a/src/main/resources/lang/responses_fr.properties b/src/main/resources/lang/responses_fr.properties index 5fa1b4d..3f58875 100644 --- a/src/main/resources/lang/responses_fr.properties +++ b/src/main/resources/lang/responses_fr.properties @@ -1,9 +1,18 @@ -response.1=Les quantités de chaque produits utilisés ont été déduites de l'inventaire +response.1=Les quantités de chaque produits utilisés ont été déduites de l''inventaire +response.10=Il y a déjà un produit ayant le code {0} +response.11=Il y a déjà un type de produit s''appellant {0} +response.12=Aucune bannière ayant l'identifiant {0} n''a été trouvée +response.13=Il y a déjà une bannière s''appellant {0} +response.14=Le produit {0} est lié à une ou plusieurs recettes, veuillez les supprimer d'abord +response.15=La bannière {0} est liée à une ou plusieurs recettes, veuillez les supprimer d'abord +response.16=Le mélange ayant l''identifiant {0} n''est pas associé à la recette ayant l''identifiant {1} +response.17=Il n''y a pas assez de {0} en inventaire pour cette recette +response.18=Cette recette contient déjà un mélange du type {0} response.2=Les informations de la recette ont été sauvegardées -response.3=Une erreur est survenue lors de l'enregistrement -response.4=Une erreur est survenue lors de l'enregistrement de l'image -response.5=Une erreur est survenue lors de l'enregistrement du fichier SIMDUT +response.3=Une erreur est survenue lors de l''enregistrement +response.4=Une erreur est survenue lors de l''enregistrement de l''image +response.5=Une erreur est survenue lors de l''enregistrement du fichier SIMDUT response.6=Votre mot de passe n'est pas valide -response.7=Aucune recette ayant l'identifiant {0} n'a été trouvée -response.8=Aucun mélange ayant l'identifiant {0} n'a été trouvé -response.9=Aucun produit ayant l'identifiant {0} n'a été trouvé \ No newline at end of file +response.7=Aucune recette ayant l''identifiant {0} n''a été trouvée +response.8=Aucun mélange ayant l''identifiant {0} n''a été trouvé +response.9=Aucun produit ayant l''identifiant {0} n''a été trouvé \ No newline at end of file diff --git a/src/main/resources/templates/company/creator.html b/src/main/resources/templates/company/creator.html index b33e2be..ab0b494 100644 --- a/src/main/resources/templates/company/creator.html +++ b/src/main/resources/templates/company/creator.html @@ -12,7 +12,8 @@

-

+ +

diff --git a/src/main/resources/templates/company/remover.html b/src/main/resources/templates/company/remover.html index 0e47154..26419d2 100644 --- a/src/main/resources/templates/company/remover.html +++ b/src/main/resources/templates/company/remover.html @@ -26,8 +26,8 @@

-

+

diff --git a/src/main/resources/templates/fragments.html b/src/main/resources/templates/fragments.html index 2851498..3a795a6 100644 --- a/src/main/resources/templates/fragments.html +++ b/src/main/resources/templates/fragments.html @@ -50,6 +50,10 @@
+
+

+
+

diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index fddc05f..1a1339f 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -49,19 +49,19 @@

- - + +

- + - + @@ -75,7 +75,7 @@ - + diff --git a/src/main/resources/templates/recipe/created.html b/src/main/resources/templates/recipe/created.html index b647cec..756396c 100644 --- a/src/main/resources/templates/recipe/created.html +++ b/src/main/resources/templates/recipe/created.html @@ -1,10 +1,7 @@ - - - - + @@ -15,8 +12,9 @@

-

- +

+
diff --git a/src/main/resources/templates/recipe/editor.html b/src/main/resources/templates/recipe/editor.html index 2120dd9..7cd3a41 100644 --- a/src/main/resources/templates/recipe/editor.html +++ b/src/main/resources/templates/recipe/editor.html @@ -29,17 +29,18 @@ th:text="#{recipe.success.edit(${recipeCode})}">

- +

-
+
- @@ -51,7 +52,7 @@
- +
diff --git a/workdir/recipes.mv.db b/workdir/recipes.mv.db index 3cff0f9bfb89130afd568728968415345ef1109c..c09d90acd4861f506c1bcefb8ed9ea06015b7a7c 100644 GIT binary patch delta 4870 zcmeH~O>Epm6vyoe+p?Q)N@);Dl%%8yVMF~fw#Qa!LX$L2MM=4o1|-U?cTA#eHq|C+ zgFwpCL&E{2wj;r%5(h-YrBu}lAta=xmvBIWN^n3(^nirKsayadabaxl?u_@Nmqb0F z?8Tn-*z^3}`~Cmpd>-UC!Ii#-v}s@MAK1?hNiRc|-C2@zG?KOrTX%2aJFO3#P{3^) z{tShFhdX9WQ%Pp#E|AmsO|s;uc5(rqcc4Ysan;dXtR&BeS5nE`;^MNCGLYi`u;M}^ zxjeT(92Fv>nL09(7f5btZZYd<{?l)r8+bY)NmAGFy#`s98qczGr877-ymqhSso0Z} z)O54AwXoH5qY0?L6jp|h0RZj7SBKwk1sbN4BS(Su6I~y%fVMz4N4@~fYxLr11~hlj z-$p-;HNVxIkm!}+#|xjla9IW|G5YM-QPA=_ogbS4E$`8-u|uHc2E9La5X2$fH~tEU zW11N!Aby?xFn+ivp5NafwS(SRYdh$R{VcbGgRu?3qV#{qA9ZNo#5jnr7G@__WzhH! z{q^`UkjT+PlL|;o(6f^(AaRF&Kk4B~PIVQ=SQU?Xo zLv&_(W6dJXrRG4MqG-Yh3nN?6p;^9Wt2RP>r0Wn0BM&Kj1TBT%r?7xke$7B=jW2Jf z#HqH66yc$1C@$z}kS}kg6=8XcL`MYS@`6Zph5L|hxww~KG*z3&ja<9<(8AEmXIif2 zzr5D7cZdO!Ol20`c}ElS%>FF%?5Y}OR1wC}nR7nr;dC>ZAz4QaF-0)I6hS4KC9BJh zU4&IHv5H}h>O#*`iMXoG);QAdvpO>TSJ29@j!F{xkhd}$%kSRp3R<~)pnvdlsf%~A zsgyg}wD>9Vn_5QnGM*@@%^HlV4cala`9LnUw`mnKWfe2!tJ^6iW=!Yk{i(J!q(zg4 zE0bo|LIUa-tI3t@$`VNjisxrhB+RtRgqf%q0;&#Sj{Y&#dIIUu7^({w)Xnd50ZVA( zlr0txF)VM(EW3zt8JG6}>6xrRB;swqOJp4)FYOI+Ny@R1wF@CJS71 zJDnx0Y6&?08{jyz8O4!}<rlh^>m8KtUdiVhnaQn|=&wq(oz=mWNuJsw+q*Vhp}sM5Nu| z@?g@FxuFxt7jfZbttuS~cUn;mVDE+^vSXGs!1v}*1GYpB5Pf*pmCHL6(UpJS6KVj5 zTBK31WQ0vE4RKKeDpfwzfE`kHK?D8_Et%PK#FC|`CEu)P$tDNi3c=S!@X5pQazt|Ne>Z~$#iz0P8-wp2Wg?TPS>PpVbF%t5N z1nTS*)(CWq)Zq&K4yiMuT3RLAxR#b0x|>RijpUPE+6ez0QkSowE+2JXtDf_U8(|Kh z9Rg^G0RF!>#e(zlLRYaFI`1~5Q;-M0s*%qd#O1*bop*;mn@SpBmo?y9Q3DL#_q!Uv ab0KO#$$2@{f(Br&0UpG}uq>1DIsG3C?Mzev delta 2343 zcmc&#UuYaf7~h@qyzAYiXQ;KUiOuC|Qk!lrvpaLQvrEaJ=GvqhY6XdwDs^{mHVL`h zmFp!26cU429|UU$q>qXipQ?n#2VW$8u;P=5g4hQi1VIqQ2cLwBvv=3qU7M?*R0o!C zKjxeHX1?$D`_1|`SYHR1vk{8yw+0UH6-IH&tWM`y&7{~+Edv#{tn2B!2DsJ(n_h&$ zuUq@i>IJBj7gpFAYmQwsRYO^_E|?f&VOLF5vY>LIIb$oeYPD|KkYOl>1u4!e^@Sy7 zs)!+|swz`fSnc9MwPI@C?px;%Kbnvv=@I2pM3$xG?r}+iy$u^V_x=8!=mAMeeUzDX zZ)C2e0DRS59zG5LLhdIcZ=?Ytd}?eIpjY^%F&!Yxx5qvPsX_kYcp0Q7_>bf7M^l-u zgv2ioKj^+U@tO?M&+(&^qaYpQi<4(T`YV28auB3{aPl}v9_52m z5F~r~vs23;d6j=TAI(6_d$h_u4s1f>??i#`6#LYdvEziSJf5j3ASgw?+Gu64ArqCL*>TI@+ z1IWS=_|6C?9D#O-aDROwCCgv9w`Mlvs3H9>_3*SaG`!nOo@*x$yD02p{^9A(HLONN zeZ_~xhq`WqnKGcu}q}N&4W5jA_AS(6N4Y$ZkkWK@W1=aA+O{me@)cs>_Beu2v z&HYz0u_3`|#V%LrqNxReVE6)LVS+RpBSHMx^PV&{!wX88RZKOcD+LUd3R|t4dP`lj zwz$xp;inaXS#6Qy*nlT4W6B3j?rlpFdAk$$B1dk9xM%p>v;LlY$C59#$rvntxRzdVIo|>v9`0I^3R7(q~77f7Wod#^u0z#c(v+KyWonT}C y1K5=A8EkwpxB2MC&~yMT|B7b+18Cfz2VUO+EPnR`V4=SFl%R$3d0i77e)3Q1&bCVc