diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplication.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplication.java index 77728d4..dd2a30a 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplication.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/ColorRecipesExplorerApplication.java @@ -1,6 +1,5 @@ package dev.fyloz.trial.colorrecipesexplorer; -import dev.fyloz.trial.colorrecipesexplorer.core.io.file.FileHandler; import dev.fyloz.trial.colorrecipesexplorer.core.services.PasswordService; import dev.fyloz.trial.colorrecipesexplorer.core.services.files.FilesService; import org.slf4j.Logger; @@ -12,7 +11,6 @@ import org.springframework.context.MessageSource; import java.io.File; import java.io.IOException; -import java.nio.file.Files; import java.util.List; @SpringBootApplication @@ -54,8 +52,8 @@ public class ColorRecipesExplorerApplication { String filePath = String.format("%s/%s.txt", UPLOAD_LOCATION, USERS_FILE_NAME); try { - if(filesService.fileExists(filePath)) filesService.createFile(filePath); - List fileContent = filesService.readFileAsStrings(filePath); + if(filesService.exists(filePath)) filesService.create(filePath); + List fileContent = filesService.readAsStrings(filePath); if (fileContent.size() < 1) { LOGGER.warn("Aucun mot de passe trouvé. Il sera impossible d'utiliser certaines fonctionnalités de l'application."); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Recipe.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Recipe.java index 74ff6ab..60ecf28 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Recipe.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/model/Recipe.java @@ -6,7 +6,9 @@ import org.hibernate.validator.constraints.Length; import javax.persistence.*; import javax.validation.constraints.NotNull; +import java.util.ArrayList; import java.util.Collection; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @@ -52,12 +54,34 @@ public class Recipe implements IModel { @OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL) private List recipeSteps; + /** + * Récupère les mélanges triés par leur identifiant. + * + * @return Les mélanges triés par leur identifiant + */ + public List getMixesSortedById() { + List sortedMixes = new ArrayList<>(mixes); + sortedMixes.sort(Comparator.comparing(Mix::getId)); + return sortedMixes; + } + + /** + * Récupère les types de mélange des mélanges de la recette. + * + * @return Les types de mélange contenus dans la recette + */ public Collection getMixTypes() { return mixes.stream() .map(Mix::getMixType) .collect(Collectors.toList()); } + /** + * Vérifie si la recette contient un type de mélange. + * + * @param mixType Le type de mélange + * @return Si la recette contient le type de mélange + */ public boolean hasMixType(MixType mixType) { return getMixTypes().contains(mixType); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/GenericService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/GenericService.java index 47b3818..6ba789a 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/GenericService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/GenericService.java @@ -30,6 +30,16 @@ public class GenericService> this.type = type; } + @Override + public boolean exists(T entity) { + return entity != null && entity.getId() != null && existsById(entity.getId()); + } + + @Override + public boolean existsById(Long id) { + return dao.existsById(id); + } + @Override public T getById(Long id) { Optional found = dao.findById(id); @@ -106,16 +116,6 @@ public class GenericService> return entity.getId() != null && existsById(entity.getId()); } - @Override - public boolean exists(T entity) { - return entity != null && entity.getId() != null && existsById(entity.getId()); - } - - @Override - public boolean existsById(Long id) { - return dao.existsById(id); - } - /** * Transforme un objet en Json. * diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/IGenericService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/IGenericService.java index 5864586..d308b2b 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/IGenericService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/IGenericService.java @@ -7,6 +7,17 @@ import java.util.List; public interface IGenericService { + @Deprecated(since = "1.3.0", forRemoval = true) + boolean exists(T entity); + + /** + * Vérifie si une entité correspondant à un identifiant existe. + * + * @param id L'identifiant de l'entité + * @return Si un entité correspondant à l'identifiant existe + */ + boolean existsById(Long id); + /** * Récupère toutes les entités de type T. * @@ -66,15 +77,4 @@ public interface IGenericService { * @param entities Les entités à supprimer */ void deleteAll(List entities); - - @Deprecated(since = "1.3.0", forRemoval = true) - boolean exists(T entity); - - /** - * Vérifie si une entité correspondant à un identifiant existe. - * - * @param id L'identifiant de l'entité - * @return Si un entité correspondant à l'identifiant existe - */ - boolean existsById(Long id); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/TouchUpKitService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/TouchUpKitService.java index c72b7d8..a0f4065 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/TouchUpKitService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/TouchUpKitService.java @@ -21,12 +21,22 @@ public class TouchUpKitService { this.resourceLoader = resourceLoader; } - public byte[] getPdfForJobNumber(String jobNumber) throws IOException { - return new PdfBuilder(resourceLoader, true, FONT_SIZE) - .addLine(TOUCH_UP_FR, true, 0) - .addLine(TOUCH_UP_EN, true, 0) - .addLine(jobNumber, false, 10) - .build(); + /** + * Génère un PDF de kit de retouche pour une job. + * + * @param jobNumber La job + * @return Le PDF de kit de retouche pour la job + */ + public byte[] generatePdfForJobNumber(String jobNumber) { + try { + return new PdfBuilder(resourceLoader, true, FONT_SIZE) + .addLine(TOUCH_UP_FR, true, 0) + .addLine(TOUCH_UP_EN, true, 0) + .addLine(jobNumber, false, 10) + .build(); + } catch (IOException ex) { + throw new RuntimeException(String.format("Impossible de générer un PDF de kit de retouche pour la job '%s': %s", jobNumber, ex.getMessage())); + } } } 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 64fff49..eae4782 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 @@ -33,7 +33,7 @@ public class FilesService { * @param path Le chemin vers la fichier (sans classpath:, depuis le dossier resources) * @return Le contenu du fichier */ - public String readResourceFile(String path) { + public String readResource(String path) { String fullPath = String.format("classpath:%s", path); try (InputStream stream = resources.getResource(fullPath).getInputStream()) { return readInputStreamAsString(stream); @@ -62,7 +62,7 @@ public class FilesService { * @return Le contenu du fichier dans un tableau de Byte * @throws IOException La lecture du fichier a échoué */ - public byte[] readFileAsBytes(String path) throws IOException { + public byte[] readAsBytes(String path) throws IOException { return Files.readAllBytes(Paths.get(path)); } @@ -73,7 +73,7 @@ public class FilesService { * @return Le contenu du fichier dans une liste de String * @throws IOException La lecture du fichier a échoué */ - public List readFileAsStrings(String path) throws IOException { + public List readAsStrings(String path) throws IOException { return Files.readAllLines(Paths.get(path)); } @@ -84,11 +84,11 @@ public class FilesService { * @param path Le chemin vers le fichier * @return Si le fichier a bien été créé */ - public boolean writeMultiPartFile(MultipartFile multipartFile, String path) { + public boolean write(MultipartFile multipartFile, String path) { if (multipartFile.getSize() <= 0) return true; try { - File file = createFile(path); + File file = create(path); multipartFile.transferTo(file.toPath()); return true; } catch (IOException ex) { @@ -104,7 +104,7 @@ public class FilesService { * @return Le fichier créé * @throws IOException La création du fichier échoue */ - public File createFile(String path) throws IOException { + public File create(String path) throws IOException { File file = getFile(path); if (!file.exists() || file.isDirectory()) { @@ -121,7 +121,7 @@ public class FilesService { * @param path Le chemin vers le fichier * @throws IOException La suppression du fichier échoue */ - public void deleteFile(String path) { + public void delete(String path) { File file = getFile(path); try { @@ -137,7 +137,7 @@ public class FilesService { * @param path Le chemin vers le fichier * @return Si le fichier existe */ - public boolean fileExists(String path) { + public boolean exists(String path) { File file = getFile(path); return file.exists() && !file.isDirectory(); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/ImagesService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/ImagesService.java index 5da78b2..76ba379 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/ImagesService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/ImagesService.java @@ -22,22 +22,46 @@ public class ImagesService { this.filesService = filesService; } - public byte[] readImage(String name) { + /** + * Lit une image. + * + * @param name Le nom de l'image + * @return Le contenu de l'image + */ + public byte[] read(String name) { try { - return filesService.readFileAsBytes(getPath(name)); + return filesService.readAsBytes(getPath(name)); } catch (IOException ex) { throw new RuntimeException("Erreur lors de la lecture d'une image: " + ex.getMessage()); } } - public boolean writeMultipartImage(MultipartFile image, String name) { - return filesService.writeMultiPartFile(image, getPath(name)); + /** + * Écrit des données image sur le disque. + * + * @param image Le contenu du fichier + * @param name Le nom de l'image + * @return Si l'écriture du fichier s'est achevée + */ + public boolean write(MultipartFile image, String name) { + return filesService.write(image, getPath(name)); } - public void deleteImage(String name) { - filesService.deleteFile(getPath(name)); + /** + * Supprime un fichier image. + * + * @param name Le nom de l'image + */ + public void delete(String name) { + filesService.delete(getPath(name)); } + /** + * Vérifie si un InputStream contient une image. + * + * @param input L'InputStream + * @return Si l'InputStream contient une image + */ public boolean isImage(InputStream input) { try { return !(ImageIO.read(input) == null); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/MarkdownFilesService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/MarkdownFilesService.java index e38aee8..1a9c580 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/MarkdownFilesService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/MarkdownFilesService.java @@ -22,8 +22,8 @@ public class MarkdownFilesService { * @param path Le chemin vers la fichier (sans classpath:, depuis le dossier resources) * @return Le MarkDown rendu en HTML */ - public String renderMarkdown(String path) { - String fileContent = filesService.readResourceFile(path); + public String render(String path) { + String fileContent = filesService.readResource(path); Parser parser = Parser.builder().build(); Node document = parser.parse(fileContent); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/SimdutService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/SimdutService.java index 8d3f0dc..b7caba0 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/SimdutService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/SimdutService.java @@ -1,10 +1,13 @@ package dev.fyloz.trial.colorrecipesexplorer.core.services.files; import dev.fyloz.trial.colorrecipesexplorer.ColorRecipesExplorerApplication; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.SimdutException; import dev.fyloz.trial.colorrecipesexplorer.core.model.Material; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; import java.io.IOException; @@ -17,24 +20,49 @@ public class SimdutService { private MaterialService materialService; @Autowired - public SimdutService(FilesService filesService, MaterialService materialService) { + public SimdutService(FilesService filesService) { this.filesService = filesService; + } + + @Autowired + @Lazy + public void setMaterialService(MaterialService materialService) { this.materialService = materialService; } + /** + * Vérifie si un produit a un fichier SIMDUT. + * + * @param material Le produit + * @return Si le produit a un fichier SIMDUT + */ + public boolean exists(Material material) { + return filesService.exists(getPath(material)); + } + + /** + * Vérifie si le produit correspondant à un identifiant a un fichier SIMDUT. + * + * @param id L'identifiant du produit + * @return si le produit correspondant à l'identifiant a un fichier SIMDUT + */ + public boolean exists(Long id) { + return exists(materialService.getById(id)); + } + /** * Lit le fichier SIMDUT d'un produit et retourne son contenu. * * @param material Le produit * @return Le contenu du fichier SIMDUT du produit */ - public byte[] readSimdutForMaterial(Material material) { + public byte[] read(Material material) { String path = getPath(material); - if (filesService.fileExists(path)) return new byte[0]; + if (filesService.exists(path)) return new byte[0]; try { - return filesService.readFileAsBytes(path); + return filesService.readAsBytes(path); } catch (IOException ex) { throw new RuntimeException("Impossible de lire un fichier SIMDUT: " + ex.getMessage()); } @@ -46,32 +74,58 @@ public class SimdutService { * @param id L'identifiant du produit * @return Le contenu du fichier SIMDUT du produit correspondant à l'identifiant */ - public byte[] readSimdutForMaterialId(Long id) { - return readSimdutForMaterial(materialService.getById(id)); + public byte[] read(Long id) { + return read(materialService.getById(id)); } /** - * Vérifie si un produit a un fichier SIMDUT. + * Écrit le fichier SIMDUT d'un produit. * * @param material Le produit - * @return Si le produit a un fichier SIMDUT + * @param simdut Le contenu du fichier SIMDUT à écrire */ - public boolean simdutExistsForMaterial(Material material) { - return filesService.fileExists(getPath(material)); + public void write(Material material, MultipartFile simdut) { + if (!filesService.write(simdut, getPath(material))) throw new SimdutException(material); } /** - * Vérifie si le produit correspondant à un identifiant a un fichier SIMDUT. + * Met à jour le fichier SIMDUT d'un produit * - * @param id L'identifiant du produit - * @return si le produit correspondant à l'identifiant a un fichier SIMDUT + * @param simdut Le contenu du fichier SIMDUT mis à jour + * @param material Le produit du SIMDUT */ - public boolean simdutExistsForMaterialId(Long id) { - return simdutExistsForMaterial(materialService.getById(id)); + public void update(MultipartFile simdut, Material material) { + delete(material); + write(material, simdut); } + /** + * Supprime le fichier SIMDUT pour un produit. + * + * @param material Le produit + */ + public void delete(Material material) { + filesService.delete(getPath(material)); + } + + /** + * 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 getPath(Material material) { - return String.format("%s/%s/%s", ColorRecipesExplorerApplication.UPLOAD_LOCATION, SIMDUT_DIRECTORY, materialService.getSimdutFileName(material)); + return String.format("%s/%s/%s", ColorRecipesExplorerApplication.UPLOAD_LOCATION, SIMDUT_DIRECTORY, getSimdutFileName(material)); + } + + /** + * Récupère le nom du fichier SIMDUT d'un produit. + * + * @param material Le produit + * @return Le nom du fichier SIMDUT du produit + */ + public String getSimdutFileName(Material material) { + return String.format("%s_%s", material.getId(), material.getName()); } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/XlsService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/XlsService.java index 94051c9..2efb7cd 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/XlsService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/files/XlsService.java @@ -29,7 +29,7 @@ public class XlsService { * @param recipe La recette * @return Le fichier XLS de la recette */ - public byte[] generateXlsForRecipe(Recipe recipe) { + public byte[] generate(Recipe recipe) { return new XlsxExporter().generate(recipe); } @@ -39,8 +39,8 @@ public class XlsService { * @param id L'identifiant de la recette * @return Le fichier XLS de la recette */ - public byte[] generateXlsForRecipeId(Long id) { - return generateXlsForRecipe(recipeService.getById(id)); + public byte[] generate(Long id) { + return generate(recipeService.getById(id)); } /** @@ -48,13 +48,13 @@ public class XlsService { * * @return Le fichier ZIP contenant tous les fichiers XLS */ - public byte[] generateXlsForAllRecipes() { + public byte[] generateForAll() { ColorRecipesExplorerApplication.LOGGER.info("Exportation de toutes les couleurs en XLS"); Collection recipes = recipeService.getAll(); try (ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); ZipOutputStream zipOutput = new ZipOutputStream(byteOutput)) { for (Recipe recipe : recipes) { - byte[] recipeXLS = generateXlsForRecipe(recipe); + byte[] recipeXLS = generate(recipe); zipOutput.putNextEntry(new ZipEntry(String.format("%s_%s.xlsx", recipe.getCompany().getName(), recipe.getName()))); zipOutput.write(recipeXLS, 0, recipeXLS.length); zipOutput.closeEntry(); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/CompanyService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/CompanyService.java index ec3536b..7d59650 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/CompanyService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/CompanyService.java @@ -1,29 +1,32 @@ 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.EntityLinkedException; +import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.ModelException; import dev.fyloz.trial.colorrecipesexplorer.core.model.Company; import dev.fyloz.trial.colorrecipesexplorer.core.services.GenericService; import dev.fyloz.trial.colorrecipesexplorer.dao.CompanyDao; -import dev.fyloz.trial.colorrecipesexplorer.dao.RecipeDao; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; +import javax.validation.constraints.NotNull; + @Service public class CompanyService extends GenericService { - private RecipeDao recipeDao; // Utilise le Dao pour éviter une erreur au démarrage, citant une récursion (CompanyService -> RecipeService -> CompanyService -> ...) + private RecipeService recipeService; @Autowired - public CompanyService(CompanyDao companyDao, RecipeDao recipeDao) { + public CompanyService(CompanyDao companyDao) { super(companyDao, Company.class); - this.recipeDao = recipeDao; } - @Override - public void delete(Company entity) { - if (isLinkedToRecipes(entity)) throw new EntityLinkedException(type); - - super.delete(entity); + // Pour éviter les dépendances circulaires + @Autowired + @Lazy + public void setRecipeService(RecipeService recipeService) { + this.recipeService = recipeService; } /** @@ -43,7 +46,22 @@ public class CompanyService extends GenericService { * @return Si la bannière est liée à une recette */ public boolean isLinkedToRecipes(Company company) { - return !recipeDao.findAllByCompany(company).isEmpty(); + return recipeService.existsByCompany(company); + } + + @Override + public Company save(@NotNull Company entity) { + if (existsByName(entity.getName())) + throw new EntityAlreadyExistsException(type, ModelException.IdentifierType.NAME, entity.getName()); + + return super.save(entity); + } + + @Override + public void delete(Company entity) { + if (isLinkedToRecipes(entity)) throw new EntityLinkedException(type); + + super.delete(entity); } @Deprecated(since = "1.3.0", forRemoval = true) 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 13432fb..0f33be7 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,15 +1,12 @@ 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.EntityNotFoundException; 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.core.services.files.SimdutService; import dev.fyloz.trial.colorrecipesexplorer.dao.MaterialDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -17,7 +14,6 @@ import org.springframework.web.multipart.MultipartFile; import javax.validation.constraints.NotNull; import java.util.ArrayList; -import java.util.Comparator; import java.util.List; import java.util.Optional; import java.util.stream.Collectors; @@ -26,19 +22,23 @@ import java.util.stream.Collectors; public class MaterialService extends GenericService { private MixQuantityService mixQuantityService; - private FilesService filesService; + private SimdutService simdutService; @Autowired - public MaterialService(MaterialDao materialDao, MixQuantityService mixQuantityService, FilesService filesService) { + public MaterialService(MaterialDao materialDao, MixQuantityService mixQuantityService, SimdutService simdutService) { super(materialDao, Material.class); this.mixQuantityService = mixQuantityService; - this.filesService = filesService; + this.simdutService = simdutService; } - public List getAllByMaterialType(MaterialType materialType) { - if (materialType == null) return new ArrayList<>(); - - return dao.findAllByMaterialType(materialType); + /** + * Vérifie si un produit est lié à un ou plusieurs mélanges. + * + * @param material Le produit à vérifier. + * @return Si le produit est lié à d'autres mélanges. + */ + public boolean isLinkedToMixes(Material material) { + return mixQuantityService.existsByMaterial(material); } /** @@ -53,10 +53,26 @@ public class MaterialService extends GenericService { .collect(Collectors.toList()); } - public List getAllOrdered() { - return getAll().stream() - .sorted(Comparator.comparing(Material::getName)) - .collect(Collectors.toList()); + /** + * Vérifie si des produits sont d'un type de produit + * + * @param materialType Le type de produit + * @return Si des produits sont du type de produit + */ + public boolean existsByMaterialType(MaterialType materialType) { + return dao.existsByMaterialType(materialType); + } + + /** + * Récupère tous les produits qui sont d'un type de produit. + * + * @param materialType Le type de produit des produits + * @return Tous les produits qui sont du type de produit + */ + public List getAllByMaterialType(MaterialType materialType) { + if (materialType == null) return new ArrayList<>(); + + return dao.findAllByMaterialType(materialType); } /** @@ -72,8 +88,13 @@ public class MaterialService extends GenericService { return found.get(); } + @Override + public Material save(@NotNull Material entity) { + throw new UnsupportedOperationException("Cette méthode n'est pas supportée pour les produits. Utiliser MaterialService::save(Material, MultipartFile)"); + } + public Material save(@NotNull Material material, MultipartFile file) { - addSimdut(file, material); + simdutService.write(material, file); return super.save(material); } @@ -87,20 +108,17 @@ public class MaterialService extends GenericService { return super.update(material); } - @Override - public void delete(Material material) { - removeSimdut(material); - super.delete(material); + public Material update(Material material, MultipartFile simdut) { + simdutService.update(simdut, material); + + return update(material); } - /** - * Vérifie si un produit est lié à un ou plusieurs mélanges. - * - * @param material Le produit à vérifier. - * @return Si le produit est lié à d'autres mélanges. - */ - public boolean isLinkedToMixes(Material material) { - return mixQuantityService.existsByMaterial(material); + @Override + public void delete(Material material) { + simdutService.delete(material); + + super.delete(material); } @Deprecated(since = "1.3.0", forRemoval = true) @@ -109,56 +127,4 @@ public class MaterialService extends GenericService { delete(material); } } - - /** - * 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. - *

- * Cette méthode retournera true si le simdut est null ou si le transfert du fichier vers le disque a fonctionné. - * Cette méthode retournera false si la création du fichier échoue ou si une erreur survient lors du transfert. - * - * @param simdut Le contenu du fichier SIMDUT - * @param material Le produit du SIMDUT - */ - public void addSimdut(MultipartFile simdut, Material material) { - if (filesService.writeMultiPartFile(simdut, getSimdutPath(material))) throw new SimdutException(material); - } - - /** - * 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); - } - - /** - * Supprime le fichier SIMDUT pour un produit. - *

- * 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 - */ - public void removeSimdut(Material material) { - filesService.deleteFile(getSimdutPath(material)); - } - - public String getSimdutFileName(Material material) { - return String.format("%s_%s", material.getId(), material.getName()); - } } 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 ed27da5..6bb43a6 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 @@ -21,8 +21,54 @@ public class MaterialTypeService extends GenericService { } @Transactional - public void create(MixFormDto formDto) { + public void save(MixFormDto formDto) { Mix mix = new MixBuilder(mixTypeService, materialService) .withDto(formDto) .build(); @@ -90,7 +89,7 @@ public class MixService extends GenericService { } @Transactional - public void edit(Mix mix, MixFormDto formDto) { + public void update(Mix mix, MixFormDto formDto) { mix = new MixBuilder(mixTypeService, materialService) .withMix(mix) .withDto(formDto) @@ -116,26 +115,4 @@ public class MixService extends GenericService { public void deleteById(Long id) { delete(getById(id)); } - - /** - * Crée une liste de MixQuantity, pour la création d'un mélange - * - * @param mix Le mélange associé aux MixQuantity - * @param materials Les matériaux - * @param quantities Les quantités - * @return La liste des MixQuantity créés. - */ - public List createMixQuantities(Mix mix, List materials, List quantities) { - List mixQuantities = new ArrayList<>(); - - for (int i = 0; i < materials.size(); i++) { - Material material = materials.get(i); - float quantity = quantities.get(i); - - MixQuantity mixQuantity = new MixQuantity(mix, material, quantity); - mixQuantities.add(mixQuantity); - } - - return mixQuantities; - } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixTypeService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixTypeService.java index f82d28e..77cae0c 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixTypeService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/MixTypeService.java @@ -54,7 +54,7 @@ public class MixTypeService extends GenericService { * @param materialType Le type de produit du type de mélange * @return Le type de mélange créé */ - public MixType createByName(String name, MaterialType materialType) { + public MixType save(String name, MaterialType materialType) { Material mixTypeMaterial = new Material(name, 0f, true, materialType); MixType mixType = new MixType(name, mixTypeMaterial); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/RecipeService.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/RecipeService.java index 6855b8f..0eb2e74 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/RecipeService.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/services/model/RecipeService.java @@ -1,6 +1,8 @@ package dev.fyloz.trial.colorrecipesexplorer.core.services.model; -import dev.fyloz.trial.colorrecipesexplorer.core.model.*; +import dev.fyloz.trial.colorrecipesexplorer.core.model.Company; +import dev.fyloz.trial.colorrecipesexplorer.core.model.Mix; +import dev.fyloz.trial.colorrecipesexplorer.core.model.Recipe; import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.RecipeEditorFormDto; import dev.fyloz.trial.colorrecipesexplorer.core.model.dto.RecipeExplorerFormDto; import dev.fyloz.trial.colorrecipesexplorer.core.services.GenericService; @@ -9,7 +11,6 @@ import dev.fyloz.trial.colorrecipesexplorer.dao.RecipeDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.MultiValueMap; import java.io.File; import java.util.*; @@ -33,25 +34,23 @@ public class RecipeService extends GenericService { } /** - * Récupère la liste des recettes associées à une compagnie. + * Vérifie s'il y a une recette liée à une compagnie. * - * @param company La compagnie. - * @return La liste des recettes associées à cette compagnie. + * @param company La compagnie + * @return S'il y a une recette liée à la compagnie */ - public List getByCompany(Company company) { - return dao.findAllByCompany(company); + public boolean existsByCompany(Company company) { + return dao.existsByCompany(company); } /** - * Récupère une liste triée des mélanges pour une recette. + * Récupère toutes les recettes pour une compagnie. * - * @param recipe La recette dont on veut récupérer les mélanges - * @return Une liste triée des mélanges. + * @param company La compagnie + * @return Toutes les recettes pour la compagnie */ - public List getSortedMixes(Recipe recipe) { - List mixes = recipe.getMixes(); - mixes.sort(Comparator.comparing(Mix::getId)); - return new ArrayList<>(mixes); // Convertit le PersistentBag en ArrayList + public Collection getAllByCompany(Company company) { + return dao.findAllByCompany(company); } /** @@ -59,8 +58,9 @@ public class RecipeService extends GenericService { * * @return Un Map contenant les recettes classées par compagnie. */ - public Map> getRecipesByCompany() { - return mappedByCompany(getAll()); + public Map> getAllMappedByCompany() { + return companyService.getAll().stream() + .collect(Collectors.toMap(c -> c, this::getAllByCompany)); } /** @@ -69,11 +69,14 @@ public class RecipeService extends GenericService { * @param recipeDto Les informations de la recette à mettre à jour * @return La recette mise à jour */ + @Transactional public Recipe updateRecipeAndSteps(RecipeEditorFormDto recipeDto) { Recipe recipe = recipeDto.getRecipe(); - List steps = stepService.createAllForRecipe(recipe, recipeDto.getStep()); - return setSteps(recipe, steps); + stepService.deleteAll(recipe.getRecipeSteps()); + recipe.setRecipeSteps(stepService.createAllForRecipe(recipe, recipeDto.getStep())); + + return update(recipe); } /** @@ -121,7 +124,12 @@ public class RecipeService extends GenericService { return Arrays.stream(allImages).map(File::getName).collect(Collectors.toList()); } - // TODO test + /** + * Récupère l'index de la prochaine image pour une recette. + * + * @param recipe La recette + * @return L'index de la prochaine image pour la recette + */ public int getNextImageIndex(Recipe recipe) { String imageName = getImageFileName(recipe); List allImages = getImageFiles(recipe); @@ -130,66 +138,42 @@ public class RecipeService extends GenericService { } /** - * Récupère les mélanges d'une recette triés selon leur identifiant. + * Récupère le nom des fichiers image d'une recette. * * @param recipe La recette - * @return Les mélanges de la recette triés selon leur identifiant + * @return Le nom des fichiers image de la recette */ - public Collection getMixesSortedById(Recipe recipe) { - return getMixesSorted(recipe, Comparator.comparing(Mix::getId)); - } - - /** - * Récupère les mélanges d'une recette triés selon un comparateur. - * - * @param recipe La recette - * @param comparator Le comparateur, ou la méthode de triage - * @return Les mélanges de la recette triés selon le comparateur - */ - public Collection getMixesSorted(Recipe recipe, Comparator comparator) { - return recipe - .getMixes() - .parallelStream() - .sorted(comparator) - .collect(Collectors.toList()); - } - - private Map> mappedByCompany(List recipes) { - List companies = companyService.getAll(); - Map> mappedRecipes = new HashMap<>(); - - for (Company c : companies) { - List recipesForCompany = recipes.stream().filter(r -> r.getCompany().equals(c)).collect(Collectors.toList()); - - if (recipesForCompany.size() > 0) { - mappedRecipes.put(c, recipesForCompany); - } - } - - return mappedRecipes; - } - - @Transactional - protected Recipe setSteps(Recipe recipe, List steps) { - stepService.deleteAll(recipe.getRecipeSteps()); - recipe.setRecipeSteps(steps); - - return update(recipe); - } - - private void removeAllFiles(Recipe recipe) { - getImageFiles(recipe).forEach(f -> imagesService.deleteImage(f)); - } - public String getImageFileName(Recipe recipe) { return String.format("%s_%s", recipe.getId(), recipe.getName()); } + /** + * Récupère le nom du fichier image d'une recette avec le prochain index disponible. + * + * @param recipe La recette + * @return Le nom du fichier image de la recette avec le prochain index disponible + */ public String getImageFileNameWithIndex(Recipe recipe) { return getImageFileNameWithIndex(recipe, getNextImageIndex(recipe)); } + /** + * Récupère le nom du fichier image d'une recette avec un index. + * + * @param recipe La recette + * @param index L'index du fichier image + * @return Le nom du fichier image de la recette avec l'index + */ public String getImageFileNameWithIndex(Recipe recipe, int index) { return String.format("%s-%s", getImageFileName(recipe), index); } + + /** + * Supprime tous les fichiers image d'une recette. + * + * @param recipe La recette + */ + private void removeAllFiles(Recipe recipe) { + getImageFiles(recipe).forEach(f -> imagesService.delete(f)); + } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java index 18e1e17..9679eb7 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/core/utils/MixBuilder.java @@ -37,7 +37,7 @@ public class MixBuilder { public MixBuilder withDto(MixFormDto dto) { if (this.mixType == null) { - this.mixType = mixTypeService.createByName(dto.getMixTypeName(), dto.getMaterialType()); + this.mixType = mixTypeService.save(dto.getMixTypeName(), dto.getMaterialType()); } else { this.mixType.setName(dto.getMixTypeName()); this.mixType.getMaterial().setMaterialType(dto.getMaterialType()); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialDao.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialDao.java index 7d440a3..d16a3b7 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialDao.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/MaterialDao.java @@ -14,7 +14,6 @@ public interface MaterialDao extends JpaRepository { List findAllByMaterialType(MaterialType materialType); - List findAllByNameContainingIgnoreCase(String stringSearch); + boolean existsByMaterialType(MaterialType materialType); - boolean existsByName(String name); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/RecipeDao.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/RecipeDao.java index 88ea1e0..eb7ddfb 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/RecipeDao.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/dao/RecipeDao.java @@ -15,6 +15,6 @@ public interface RecipeDao extends JpaRepository { Recipe findByName(String name); - @Query("select r from Recipe r where upper(r.name) like %?1% or upper(r.description) like %?1%") - List findAllByRecipeDescriptionContainsOrRecipeCodeContains(String searchWordUpperCase); + boolean existsByCompany(Company company); + } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/WebsitePaths.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/WebsitePaths.java index 182e487..fde0547 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/WebsitePaths.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/WebsitePaths.java @@ -3,8 +3,6 @@ package dev.fyloz.trial.colorrecipesexplorer.web; public class WebsitePaths { // Autres public static final String INDEX = "index"; - public static final String SEARCH = "search"; - public static final String SEARCH_INVENTORY = "inventory/search"; public static final String SIMDUT_FILES = "simdut/{id}"; public static final String PASSWORD_VALIDATION = "password/valid"; public static final String RECIPE_XLS = "recipe/xls/{id}"; @@ -35,7 +33,6 @@ public class WebsitePaths { public static final String EXPLORER_RECIPE = "recipe/explore"; public static final String EXPLORER_RECIPE_SPECIFIC = "recipe/explore/{id}"; public static final String CREATOR_RECIPE = "recipe/creator"; - public static final String CREATOR_RECIPE_SUCCESS = "recipe/created"; public static final String EDITOR_RECIPE = "recipe/editor"; public static final String EDITOR_RECIPE_SPECIFIC = "recipe/editor/{id}"; public static final String EDITOR_RECIPE_EDITOR = "recipe/edit"; @@ -44,7 +41,6 @@ public class WebsitePaths { // Compagnies public static final String CREATOR_COMPANY = "company/creator"; - public static final String CREATOR_COMPANY_SUCCESS = "company/created"; public static final String REMOVER_COMPANY = "company/remover"; public static final String REMOVER_COMPANY_SPECIFIC = "company/remover/{id}"; @@ -52,7 +48,6 @@ public class WebsitePaths { public static final String CREATOR_MATERIAL = "material/creator"; public static final String EDIT_MATERIAL_SIMDUT = "material/simdut"; public static final String EDIT_MATERIAL_SIMDUT_SPECIFIC = "material/simdut/{id}"; - public static final String CREATOR_MATERIAL_SUCCESS = "material/created"; public static final String REMOVER_MATERIAL = "material/remover"; public static final String REMOVER_MATERIAL_SPECIFIC = "material/remover/{id}"; public static final String EDITOR_MATERIAL = "material/editor"; diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/IndexController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/IndexController.java index 3a4fa86..e9c6ace 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/IndexController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/IndexController.java @@ -1,10 +1,7 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller; -import dev.fyloz.trial.colorrecipesexplorer.core.io.response.JSONResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType; -import dev.fyloz.trial.colorrecipesexplorer.core.model.Company; -import dev.fyloz.trial.colorrecipesexplorer.core.model.Recipe; import dev.fyloz.trial.colorrecipesexplorer.core.services.PasswordService; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.RecipeService; import org.springframework.beans.factory.annotation.Autowired; @@ -13,10 +10,7 @@ import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.ModelAndView; -import java.util.HashMap; -import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.INDEX; import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.PASSWORD_VALIDATION; @@ -39,7 +33,7 @@ public class IndexController { @GetMapping({INDEX, "/"}) public ModelAndView getPage() { return new ModelResponseBuilder(INDEX) - .addResponseData(ResponseDataType.RECIPE_MAP, recipeService.getRecipesByCompany()) + .addResponseData(ResponseDataType.RECIPE_MAP, recipeService.getAllMappedByCompany()) .build(); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/OthersController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/OthersController.java index 0994de8..9baaa92 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/OthersController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/OthersController.java @@ -59,6 +59,6 @@ public class OthersController { @GetMapping(value = UPDATES_GET, produces = MediaType.TEXT_HTML_VALUE) @ResponseBody public ResponseEntity getUpdates() { - return new ResponseEntity<>(markdownService.renderMarkdown("updates.md"), HttpStatus.OK); + return new ResponseEntity<>(markdownService.render("updates.md"), HttpStatus.OK); } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/RecipeExplorerController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/RecipeExplorerController.java index b94f0da..84788bb 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/RecipeExplorerController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/RecipeExplorerController.java @@ -40,7 +40,7 @@ public class RecipeExplorerController { return modelResponseBuilder .addResponseData(ResponseDataType.RECIPE, recipe) - .addResponseData(ResponseDataType.MIXES, recipeService.getMixesSortedById(recipe)) + .addResponseData(ResponseDataType.MIXES, recipe.getMixesSortedById()) .addResponseData(ResponseDataType.IMAGES, recipeService.getImageFiles(recipe)) .build(); } catch (EntityNotFoundException e) { diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/CompanyCreatorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/CompanyCreatorController.java index 2a3e92a..768900f 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/CompanyCreatorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/creators/CompanyCreatorController.java @@ -29,7 +29,7 @@ public class CompanyCreatorController { } @GetMapping(CREATOR_COMPANY) - public ModelAndView showCreationPage(ModelAndView model, Company company) { + public ModelAndView getPage(ModelAndView model, Company company) { return new ModelResponseBuilder(model) .withView(CREATOR_COMPANY) .addResponseData(ResponseDataType.COMPANY, company) @@ -48,6 +48,6 @@ public class CompanyCreatorController { modelResponseBuilder.addResponseCode(ResponseCode.COMPANY_ALREADY_EXIST, company.getName()); } - return showCreationPage(modelResponseBuilder.build(), company); + return getPage(modelResponseBuilder.build(), company); } } 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 b8a641a..1cc4eac 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 @@ -33,7 +33,7 @@ public class MaterialCreatorController { } @GetMapping(CREATOR_MATERIAL) - public ModelAndView showCreationPage(ModelAndView model, Material material) { + public ModelAndView getPage(ModelAndView model, Material material) { return new ModelResponseBuilder(model) .withView(CREATOR_MATERIAL) .addResponseData(ResponseDataType.MATERIAL, material != null ? material : new Material()) @@ -42,12 +42,12 @@ public class MaterialCreatorController { } @PostMapping(value = CREATOR_MATERIAL, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public ModelAndView create(@Valid Material material, MultipartFile simdut) { + public ModelAndView createMaterial(@Valid Material material, MultipartFile simdut) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); try { materialService.save(material, simdut); - return showCreationPage( + return getPage( modelResponseBuilder.addResponseCode(ResponseCode.SUCCESS_SAVING_MATERIAL, material.getName()).build(), null ); @@ -58,6 +58,6 @@ public class MaterialCreatorController { modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING_SIMDUT); } - return showCreationPage(modelResponseBuilder.build(), material); + return getPage(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 db2a948..9ca54bb 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 @@ -38,7 +38,7 @@ public class MaterialTypeCreatorController { } @PostMapping(CREATOR_MATERIAL_TYPE) - public ModelAndView create(@Valid MaterialType materialType) { + public ModelAndView createMaterialType(@Valid MaterialType materialType) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(ControllerUtils.redirect(INDEX)); try { 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 8bb0ed0..15f44d1 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 @@ -65,7 +65,7 @@ public class MixCreatorController { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); try { - mixService.create(formDto); + mixService.save(formDto); return modelResponseBuilder .withRedirect(EDITOR_RECIPE_SPECIFIC, formDto.getRecipe().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 f3214e7..cc06232 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 @@ -30,7 +30,7 @@ public class RecipeCreatorController { } @GetMapping(CREATOR_RECIPE) - public ModelAndView showCreationPage(ModelAndView model, Recipe recipe) { + public ModelAndView getPage(ModelAndView model, Recipe recipe) { ModelResponseBuilder responseBuilder = new ModelResponseBuilder(model) .withView(CREATOR_RECIPE) .addResponseData(ResponseDataType.COMPANIES, companyService.getAll()) 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 7c4d14f..80f4721 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 @@ -58,7 +58,7 @@ public class MaterialEditorController { } @PostMapping(value = EDITOR_MATERIAL, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE) - public ModelAndView saveEditedMaterial(Material material) { + public ModelAndView updateMaterial(Material material) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); try { @@ -77,7 +77,7 @@ public class MaterialEditorController { } @GetMapping(value = EDIT_MATERIAL_SIMDUT_SPECIFIC) - public ModelAndView chooseSIMDUTFile(ModelAndView model, @PathVariable Long id) { + public ModelAndView getSimdutPage(ModelAndView model, @PathVariable Long id) { return new ModelResponseBuilder(model) .withView(EDIT_MATERIAL_SIMDUT) .addResponseData(ResponseDataType.MATERIAL_ID, id) @@ -85,14 +85,14 @@ public class MaterialEditorController { } @PostMapping(value = EDIT_MATERIAL_SIMDUT, consumes = MediaType.MULTIPART_FORM_DATA_VALUE) - public ModelAndView saveSIMDUT(Long id, MultipartFile simdut) { + public ModelAndView updateSimdut(Long id, MultipartFile simdut) { ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder().withRedirect(EDITOR_MATERIAL_SPECIFIC, id); try { Material material = materialService.getById(id); - materialService.updateSimdut(simdut, material); + materialService.update(material, simdut); } catch (EntityNotFoundException ex) { - return chooseSIMDUTFile( + return getSimdutPage( modelResponseBuilder.addResponseCode(ResponseCode.MATERIAL_NOT_FOUND, id).build(), id ); diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MixEditorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MixEditorController.java index feaf9a6..17ac905 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MixEditorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/MixEditorController.java @@ -62,29 +62,11 @@ public class MixEditorController { try { Mix mix = mixService.getById(id); - mixService.edit(mix, formDto); + mixService.update(mix, formDto); } catch (EntityNotFoundException ex) { return getPage(modelResponseBuilder.addResponseCode(ResponseCode.MIX_NOT_FOUND, id).build(), id); } return modelResponseBuilder.build(); } - - @GetMapping(REMOVER_MIX_SPECIFIC) - public ModelAndView deleteMix(@PathVariable Long id) { - ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); - - try { - Mix mix = mixService.getById(id); - mixService.delete(mix); - - return modelResponseBuilder - .withRedirect(EDITOR_RECIPE_SPECIFIC, mix.getRecipe().getId()) - .build(); - } catch (EntityNotFoundException ex) { - modelResponseBuilder.addResponseCode(ResponseCode.MIX_NOT_FOUND, id); - } - - return getPage(modelResponseBuilder.build(), id); - } } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/RecipeEditorController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/RecipeEditorController.java index e9f0e8d..1dc8e13 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/RecipeEditorController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/editors/RecipeEditorController.java @@ -10,7 +10,6 @@ import dev.fyloz.trial.colorrecipesexplorer.core.services.model.CompanyService; import dev.fyloz.trial.colorrecipesexplorer.core.services.model.RecipeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; -import org.springframework.lang.Nullable; import org.springframework.stereotype.Controller; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.GetMapping; @@ -36,7 +35,7 @@ public class RecipeEditorController { public ModelAndView getPage(ModelAndView model) { return new ModelResponseBuilder(model) .withView(EDITOR_RECIPE) - .addResponseData(ResponseDataType.RECIPE_MAP, recipeService.getRecipesByCompany()) + .addResponseData(ResponseDataType.RECIPE_MAP, recipeService.getAllMappedByCompany()) .build(); } @@ -50,7 +49,7 @@ public class RecipeEditorController { modelResponseBuilder .addResponseData(ResponseDataType.RECIPE, recipe) .addResponseData(ResponseDataType.COMPANIES, companyService.getAll()) - .addResponseData(ResponseDataType.MIXES, recipeService.getSortedMixes(recipe)) + .addResponseData(ResponseDataType.MIXES, recipe.getMixesSortedById()) .addResponseData(ResponseDataType.IMAGES, recipeService.getImageFiles(recipe)) .addResponseData(ResponseDataType.RECIPE_JSON, recipeService.asJson(recipe)); } catch (EntityNotFoundException ex) { diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/ImageFilesController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/ImageFilesController.java index 1dd3ed1..fcd4d92 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/ImageFilesController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/ImageFilesController.java @@ -14,7 +14,6 @@ import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.util.MultiValueMap; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.servlet.ModelAndView; @@ -40,7 +39,7 @@ public class ImageFilesController { public ResponseEntity getImage(@PathVariable String image) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAGE_JPEG); - return new ResponseEntity<>(imagesService.readImage(image), headers, HttpStatus.OK); + return new ResponseEntity<>(imagesService.read(image), headers, HttpStatus.OK); } @GetMapping(ADD_IMAGE_SPECIFIC) @@ -61,7 +60,7 @@ public class ImageFilesController { } else { Recipe recipe = recipeService.getById(id); - if (imagesService.writeMultipartImage(image, recipeService.getImageFileNameWithIndex(recipe))) + if (imagesService.write(image, recipeService.getImageFileNameWithIndex(recipe))) return modelResponseBuilder.build(); else modelResponseBuilder.addResponseCode(ResponseCode.ERROR_SAVING_IMAGE); } @@ -75,7 +74,7 @@ public class ImageFilesController { @GetMapping(value = DELETE_IMAGE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map deleteImage(@PathVariable String image) { - imagesService.deleteImage(image); + imagesService.delete(image); return new JSONResponseBuilder().build(); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/SIMDUTFilesController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/SIMDUTFilesController.java index 0a71e14..50d27f7 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/SIMDUTFilesController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/SIMDUTFilesController.java @@ -2,7 +2,6 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller.files; import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundException; import dev.fyloz.trial.colorrecipesexplorer.core.services.files.SimdutService; -import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MaterialService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -33,8 +32,8 @@ public class SIMDUTFilesController { HttpHeaders headers = new HttpHeaders(); try { - if (simdutService.simdutExistsForMaterialId(id)) { - byte[] simdutContent = simdutService.readSimdutForMaterialId(id); + if (simdutService.exists(id)) { + byte[] simdutContent = simdutService.read(id); headers.setContentType(MediaType.APPLICATION_PDF); return new ResponseEntity<>(simdutContent, headers, HttpStatus.OK); } else { @@ -50,7 +49,7 @@ public class SIMDUTFilesController { @PostMapping(SIMDUT_FILES) public ResponseEntity getFile(@PathVariable Long id) { try { - return ResponseEntity.status(simdutService.simdutExistsForMaterialId(id) ? HttpStatus.FOUND : HttpStatus.NOT_FOUND).build(); + return ResponseEntity.status(simdutService.exists(id) ? HttpStatus.FOUND : HttpStatus.NOT_FOUND).build(); } catch (EntityNotFoundException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/TouchUpKitController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/TouchUpKitController.java index a190e34..a6ca7ec 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/TouchUpKitController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/TouchUpKitController.java @@ -2,8 +2,8 @@ package dev.fyloz.trial.colorrecipesexplorer.web.controller.files; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ModelResponseBuilder; import dev.fyloz.trial.colorrecipesexplorer.core.io.response.ResponseDataType; -import dev.fyloz.trial.colorrecipesexplorer.core.services.model.RecipeService; import dev.fyloz.trial.colorrecipesexplorer.core.services.TouchUpKitService; +import dev.fyloz.trial.colorrecipesexplorer.core.services.model.RecipeService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -11,11 +11,8 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; 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.servlet.ModelAndView; -import java.io.IOException; - import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.*; @Controller @@ -34,19 +31,19 @@ public class TouchUpKitController { public ModelAndView getPage(ModelAndView model) { return new ModelResponseBuilder(model) .withView(TOUCHUP) - .addResponseData(ResponseDataType.RECIPE_MAP, recipeService.getRecipesByCompany()) + .addResponseData(ResponseDataType.RECIPE_MAP, recipeService.getAllMappedByCompany()) .build(); } @PostMapping(value = TOUCHUP_PDF, produces = MediaType.APPLICATION_PDF_VALUE) - public ResponseEntity getTouchUpKitPdf(@RequestBody String jobNumber) throws IOException { - return new ResponseEntity<>(touchUpKitService.getPdfForJobNumber(jobNumber.replace("jobNumber=", "")), HttpStatus.FOUND); + public ResponseEntity getTouchUpKitPdf(String jobNumber) { + return new ResponseEntity<>(touchUpKitService.generatePdfForJobNumber(jobNumber), HttpStatus.FOUND); } @PostMapping(value = TOUCHUP_PTOUCH) - public ModelAndView getTouchUpKitPtouch(@RequestBody String jobNumber) { + public ModelAndView getTouchUpKitPtouch(String jobNumber) { return new ModelResponseBuilder(TOUCHUP_PTOUCH_PAGE) - .addAttribute("jobNumber", jobNumber.replace("jobNumber=", "")) + .addAttribute("jobNumber", jobNumber) .build(); } diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/XlsExporterController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/XlsExporterController.java index 84dd5ed..b9e324a 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/XlsExporterController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/files/XlsExporterController.java @@ -32,7 +32,7 @@ public class XlsExporterController { HttpHeaders headers = new HttpHeaders(); try { - byte[] xlsContent = xlsService.generateXlsForRecipeId(id); + byte[] xlsContent = xlsService.generate(id); return ResponseEntity.ok() .headers(headers) @@ -49,7 +49,7 @@ public class XlsExporterController { public ResponseEntity getAllXls() throws IOException { HttpHeaders headers = new HttpHeaders(); - byte[] allXlsContent = xlsService.generateXlsForAllRecipes(); + byte[] allXlsContent = xlsService.generateForAll(); return ResponseEntity.ok() .headers(headers) diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/removers/MixRemoverController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/removers/MixRemoverController.java new file mode 100644 index 0000000..87fde33 --- /dev/null +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/removers/MixRemoverController.java @@ -0,0 +1,43 @@ +package dev.fyloz.trial.colorrecipesexplorer.web.controller.removers; + +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.model.Mix; +import dev.fyloz.trial.colorrecipesexplorer.core.services.model.MixService; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.servlet.ModelAndView; + +import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.*; + +@Controller +public class MixRemoverController { + + private MixService mixService; + + public MixRemoverController(MixService mixService) { + this.mixService = mixService; + } + + @GetMapping(REMOVER_MIX_SPECIFIC) + public ModelAndView removeMix(@PathVariable Long id) { + ModelResponseBuilder modelResponseBuilder = new ModelResponseBuilder(); + + try { + Mix mix = mixService.getById(id); + mixService.delete(mix); + + return modelResponseBuilder + .withRedirect(EDITOR_RECIPE_SPECIFIC, mix.getRecipe().getId()) + .build(); + } catch (EntityNotFoundException ex) { + modelResponseBuilder.addResponseCode(ResponseCode.MIX_NOT_FOUND, id); + } + + return modelResponseBuilder + .withRedirect(EDITOR_MIX_SPECIFIC, id) + .build(); + } +} diff --git a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/removers/RecipeRemoverController.java b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/removers/RecipeRemoverController.java index ff4ee7b..287983b 100644 --- a/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/removers/RecipeRemoverController.java +++ b/src/main/java/dev/fyloz/trial/colorrecipesexplorer/web/controller/removers/RecipeRemoverController.java @@ -4,7 +4,6 @@ import dev.fyloz.trial.colorrecipesexplorer.core.exception.model.EntityNotFoundE 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.services.model.RecipeService; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @@ -12,8 +11,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.REMOVER_RECIPE; import static dev.fyloz.trial.colorrecipesexplorer.web.WebsitePaths.REMOVER_RECIPE_SPECIFIC; @@ -30,7 +27,7 @@ public class RecipeRemoverController { public ModelAndView getPage(ModelAndView model) { return new ModelResponseBuilder(model) .withView(REMOVER_RECIPE) - .addResponseData(ResponseDataType.RECIPE_MAP, recipeService.getRecipesByCompany()) + .addResponseData(ResponseDataType.RECIPE_MAP, recipeService.getAllMappedByCompany()) .build(); }