From 117e0a3129674d1d2ca32ce6c7447494d67a3f5a Mon Sep 17 00:00:00 2001 From: Florent Mariotti Date: Tue, 25 Feb 2025 17:09:21 +0100 Subject: [PATCH] clean constants, magic string, ... --- .../magneto/controller/ExportController.java | 18 ++++---- .../core/constants/MagnetoConstants.java | 30 +++++++++++++ .../fr/cgi/magneto/helper/SlideHelper.java | 45 +++++++------------ .../cgi/magneto/model/slides/SlideMedia.java | 5 ++- .../cgi/magneto/model/slides/SlideText.java | 3 +- .../fr/cgi/magneto/service/ExportService.java | 6 +-- .../service/impl/DefaultExportService.java | 24 +++++----- backend/src/main/resources/i18n/fr.json | 2 + 8 files changed, 77 insertions(+), 56 deletions(-) create mode 100644 backend/src/main/java/fr/cgi/magneto/core/constants/MagnetoConstants.java diff --git a/backend/src/main/java/fr/cgi/magneto/controller/ExportController.java b/backend/src/main/java/fr/cgi/magneto/controller/ExportController.java index 14055986..8306ef2c 100644 --- a/backend/src/main/java/fr/cgi/magneto/controller/ExportController.java +++ b/backend/src/main/java/fr/cgi/magneto/controller/ExportController.java @@ -1,20 +1,19 @@ package fr.cgi.magneto.controller; -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import org.entcore.common.controller.ControllerHelper; -import org.entcore.common.user.UserUtils; - import fr.cgi.magneto.core.constants.Field; +import fr.cgi.magneto.helper.I18nHelper; import fr.cgi.magneto.service.ExportService; import fr.cgi.magneto.service.ServiceFactory; import fr.wseduc.rs.ApiDoc; import fr.wseduc.rs.Get; -import fr.wseduc.security.ActionType; -import fr.wseduc.security.SecuredAction; +import fr.wseduc.webutils.I18n; import io.vertx.core.buffer.Buffer; import io.vertx.core.http.HttpServerRequest; +import org.entcore.common.controller.ControllerHelper; +import org.entcore.common.user.UserUtils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; public class ExportController extends ControllerHelper { private final ExportService exportService; @@ -28,7 +27,8 @@ public ExportController(ServiceFactory serviceFactory) { public void exportBoardToPPTX(HttpServerRequest request) { String boardId = request.getParam(Field.BOARDID); UserUtils.getUserInfos(eb, request, user -> { - exportService.exportBoardToPPTX(boardId, user) + I18nHelper i18nHelper = new I18nHelper(getHost(request), I18n.acceptLanguage(request)); + exportService.exportBoardToPPTX(boardId, user, i18nHelper) .onFailure(err -> renderError(request)) .onSuccess(ppt -> { try { diff --git a/backend/src/main/java/fr/cgi/magneto/core/constants/MagnetoConstants.java b/backend/src/main/java/fr/cgi/magneto/core/constants/MagnetoConstants.java new file mode 100644 index 00000000..7716d64e --- /dev/null +++ b/backend/src/main/java/fr/cgi/magneto/core/constants/MagnetoConstants.java @@ -0,0 +1,30 @@ +package fr.cgi.magneto.core.constants; + +public class MagnetoConstants { + // Constantes de mise en page générales + public static final int MARGIN_LEFT = 140; + public static final int MARGIN_TOP_TITLE = 40; + public static final int WIDTH = 1000; + + // Constantes pour les titres + public static final int TITLE_HEIGHT = 70; + public static final Double TITLE_FONT_SIZE = 44.0; + public static final int MAIN_TITLE_HEIGHT = 100; + public static final Double MAIN_TITLE_FONT_SIZE = 100.0; + + // Constantes pour les légendes + public static final int LEGEND_HEIGHT = 70; + public static final int LEGEND_MARGIN_BOTTOM = 20; + public static final Double LEGEND_FONT_SIZE = 16.0; + public static final String LEGEND_FONT_FAMILY = "Roboto"; + + // Constantes pour le contenu + public static final int CONTENT_HEIGHT = 520; + public static final int CONTENT_MARGIN_TOP = 140; + public static final int MAIN_CONTENT_MARGIN_TOP = 300; + public static final Double CONTENT_FONT_SIZE = 36.0; + + // Constantes pour les images + public static final int IMAGE_CONTENT_HEIGHT = 250; + +} diff --git a/backend/src/main/java/fr/cgi/magneto/helper/SlideHelper.java b/backend/src/main/java/fr/cgi/magneto/helper/SlideHelper.java index 60fa2202..3b230308 100644 --- a/backend/src/main/java/fr/cgi/magneto/helper/SlideHelper.java +++ b/backend/src/main/java/fr/cgi/magneto/helper/SlideHelper.java @@ -1,5 +1,6 @@ package fr.cgi.magneto.helper; +import fr.cgi.magneto.core.constants.MagnetoConstants; import org.apache.poi.sl.usermodel.PictureData.PictureType; import org.apache.poi.sl.usermodel.Placeholder; import org.apache.poi.sl.usermodel.PlaceholderDetails; @@ -9,27 +10,11 @@ import java.awt.*; public class SlideHelper { - private static final int MARGIN_LEFT = 140; - private static final int MARGIN_TOP_TITLE = 40; - private static final int WIDTH = 1000; - - private static final int TITLE_HEIGHT = 70; - private static final Double TITLE_FONT_SIZE = 44.0; - - private static final int LEGEND_HEIGHT = 70; - private static final int LEGEND_MARGIN_BOTTOM = 20; - private static final Double LEGEND_FONT_SIZE = 16.0; - private static final String LEGEND_FONT_FAMILY = "Roboto"; - - private static final int CONTENT_HEIGHT = 520; - private static final int CONTENT_MARGIN_TOP = 140; - - private static final int IMAGE_CONTENT_HEIGHT = 250; - + public static XSLFTextBox createTitle(XSLFSlide slide, String title, int titleHeight, Double titleFontSize, TextAlign titleTextAlign) { XSLFTextShape titleShape = slide.createTextBox(); - titleShape.setAnchor(new Rectangle(MARGIN_LEFT, MARGIN_TOP_TITLE, WIDTH, titleHeight)); + titleShape.setAnchor(new Rectangle(MagnetoConstants.MARGIN_LEFT, MagnetoConstants.MARGIN_TOP_TITLE, MagnetoConstants.WIDTH, titleHeight)); PlaceholderDetails phDetails = titleShape.getPlaceholderDetails(); if (phDetails != null) { @@ -52,9 +37,9 @@ public static XSLFTextBox createLegend(XSLFSlide slide, String legendText) { XSLFTextBox legendShape = slide.createTextBox(); int slideHeight = slide.getSlideShow().getPageSize().height; - int legendY = slideHeight - LEGEND_HEIGHT - LEGEND_MARGIN_BOTTOM; + int legendY = slideHeight - MagnetoConstants.LEGEND_HEIGHT - MagnetoConstants.LEGEND_MARGIN_BOTTOM; - legendShape.setAnchor(new Rectangle(MARGIN_LEFT, legendY, WIDTH, LEGEND_HEIGHT)); + legendShape.setAnchor(new Rectangle(MagnetoConstants.MARGIN_LEFT, legendY, MagnetoConstants.WIDTH, MagnetoConstants.LEGEND_HEIGHT)); legendShape.clearText(); legendShape.setText(legendText); @@ -63,15 +48,15 @@ public static XSLFTextBox createLegend(XSLFSlide slide, String legendText) { para.setTextAlign(TextAlign.LEFT); XSLFTextRun run = para.getTextRuns().get(0); - run.setFontSize(LEGEND_FONT_SIZE); - run.setFontFamily(LEGEND_FONT_FAMILY); + run.setFontSize(MagnetoConstants.LEGEND_FONT_SIZE); + run.setFontFamily(MagnetoConstants.LEGEND_FONT_FAMILY); return legendShape; } public static XSLFTextBox createContent(XSLFSlide slide) { XSLFTextBox contentBox = slide.createTextBox(); - contentBox.setAnchor(new Rectangle(MARGIN_LEFT, CONTENT_MARGIN_TOP, WIDTH, CONTENT_HEIGHT)); + contentBox.setAnchor(new Rectangle(MagnetoConstants.MARGIN_LEFT, MagnetoConstants.CONTENT_MARGIN_TOP, MagnetoConstants.WIDTH, MagnetoConstants.CONTENT_HEIGHT)); return contentBox; } @@ -84,16 +69,16 @@ public static XSLFPictureShape createImage(XSLFSlide slide, byte[] pictureData, double imgRatio = (double) imgSize.width / imgSize.height; int newWidth, newHeight; - if (imgRatio > (double) WIDTH / IMAGE_CONTENT_HEIGHT) { - newWidth = WIDTH; - newHeight = (int) (WIDTH / imgRatio); + if (imgRatio > (double) MagnetoConstants.WIDTH / MagnetoConstants.IMAGE_CONTENT_HEIGHT) { + newWidth = MagnetoConstants.WIDTH; + newHeight = (int) (MagnetoConstants.WIDTH / imgRatio); } else { - newHeight = IMAGE_CONTENT_HEIGHT; - newWidth = (int) (IMAGE_CONTENT_HEIGHT * imgRatio); + newHeight = MagnetoConstants.IMAGE_CONTENT_HEIGHT; + newWidth = (int) (MagnetoConstants.IMAGE_CONTENT_HEIGHT * imgRatio); } - int x = MARGIN_LEFT + (WIDTH - newWidth) / 2; - int y = contentMarginTop + (IMAGE_CONTENT_HEIGHT - newHeight) / 2; + int x = MagnetoConstants.MARGIN_LEFT + (MagnetoConstants.WIDTH - newWidth) / 2; + int y = contentMarginTop + (MagnetoConstants.IMAGE_CONTENT_HEIGHT - newHeight) / 2; XSLFPictureShape shape = slide.createPicture(pic); shape.setAnchor(new Rectangle(x, y, newWidth, newHeight)); diff --git a/backend/src/main/java/fr/cgi/magneto/model/slides/SlideMedia.java b/backend/src/main/java/fr/cgi/magneto/model/slides/SlideMedia.java index 646dc45f..ad906c68 100644 --- a/backend/src/main/java/fr/cgi/magneto/model/slides/SlideMedia.java +++ b/backend/src/main/java/fr/cgi/magneto/model/slides/SlideMedia.java @@ -1,5 +1,6 @@ package fr.cgi.magneto.model.slides; +import fr.cgi.magneto.core.constants.MagnetoConstants; import fr.cgi.magneto.helper.SlideHelper; import org.apache.poi.sl.usermodel.TextParagraph; import org.apache.poi.xslf.usermodel.XMLSlideShow; @@ -25,8 +26,8 @@ public Object createApacheSlide() { XMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide = ppt.createSlide(); - SlideHelper.createTitle(slide, title, 70, 44.0, TextParagraph.TextAlign.LEFT); - SlideHelper.createImage(slide, resourceData, fileExtension, 140); + SlideHelper.createTitle(slide, title, MagnetoConstants.TITLE_HEIGHT, MagnetoConstants.TITLE_FONT_SIZE, TextParagraph.TextAlign.LEFT); + SlideHelper.createImage(slide, resourceData, fileExtension, MagnetoConstants.CONTENT_MARGIN_TOP); SlideHelper.createLegend(slide, caption); return slide; diff --git a/backend/src/main/java/fr/cgi/magneto/model/slides/SlideText.java b/backend/src/main/java/fr/cgi/magneto/model/slides/SlideText.java index 583dcdb6..86ee35e1 100644 --- a/backend/src/main/java/fr/cgi/magneto/model/slides/SlideText.java +++ b/backend/src/main/java/fr/cgi/magneto/model/slides/SlideText.java @@ -1,5 +1,6 @@ package fr.cgi.magneto.model.slides; +import fr.cgi.magneto.core.constants.MagnetoConstants; import fr.cgi.magneto.helper.SlideHelper; import org.apache.poi.sl.usermodel.AutoNumberingScheme; import org.apache.poi.sl.usermodel.TextParagraph; @@ -23,7 +24,7 @@ public Object createApacheSlide() { XMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide = ppt.createSlide(); - SlideHelper.createTitle(slide, title, 70, 44.0, TextParagraph.TextAlign.LEFT); + SlideHelper.createTitle(slide, title, MagnetoConstants.TITLE_HEIGHT, MagnetoConstants.TITLE_FONT_SIZE, TextParagraph.TextAlign.LEFT); XSLFTextBox contentBox = SlideHelper.createContent(slide); Document doc = Jsoup.parse(description); diff --git a/backend/src/main/java/fr/cgi/magneto/service/ExportService.java b/backend/src/main/java/fr/cgi/magneto/service/ExportService.java index bbd5959d..d7e32baf 100644 --- a/backend/src/main/java/fr/cgi/magneto/service/ExportService.java +++ b/backend/src/main/java/fr/cgi/magneto/service/ExportService.java @@ -1,10 +1,10 @@ package fr.cgi.magneto.service; +import fr.cgi.magneto.helper.I18nHelper; +import io.vertx.core.Future; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.entcore.common.user.UserInfos; -import io.vertx.core.Future; - public interface ExportService { /** @@ -13,5 +13,5 @@ public interface ExportService { * @param boardId Board identifier * @return Future {@link Future} containing board id */ - Future exportBoardToPPTX(String boardId, UserInfos user); + Future exportBoardToPPTX(String boardId, UserInfos user, I18nHelper i18nHelper); } diff --git a/backend/src/main/java/fr/cgi/magneto/service/impl/DefaultExportService.java b/backend/src/main/java/fr/cgi/magneto/service/impl/DefaultExportService.java index 655406a0..dcbff02b 100644 --- a/backend/src/main/java/fr/cgi/magneto/service/impl/DefaultExportService.java +++ b/backend/src/main/java/fr/cgi/magneto/service/impl/DefaultExportService.java @@ -1,8 +1,10 @@ package fr.cgi.magneto.service.impl; import fr.cgi.magneto.core.constants.Field; +import fr.cgi.magneto.core.constants.MagnetoConstants; import fr.cgi.magneto.core.enums.SlideResourceType; import fr.cgi.magneto.factory.SlideFactory; +import fr.cgi.magneto.helper.I18nHelper; import fr.cgi.magneto.helper.SlideHelper; import fr.cgi.magneto.model.boards.Board; import fr.cgi.magneto.model.cards.Card; @@ -34,7 +36,7 @@ public DefaultExportService(ServiceFactory serviceFactory) { } @Override - public Future exportBoardToPPTX(String boardId, UserInfos user) { + public Future exportBoardToPPTX(String boardId, UserInfos user, I18nHelper i18nHelper) { return serviceFactory.boardService().getBoards(Collections.singletonList(boardId)) .compose(boards -> { if (boards.isEmpty()) { @@ -55,7 +57,7 @@ public Future exportBoardToPPTX(String boardId, UserInfos user) { documentIds.add(imageId); return getBoardDocuments(documentIds); }) - .compose(documents -> createFreeLayoutSlideObjects(board, user, slideShow, documents)) + .compose(documents -> createFreeLayoutSlideObjects(board, user, slideShow, documents, i18nHelper)) .onFailure(err -> { String message = String.format( "[Magneto@%s::exportBoardToPptx] Failed to get documents: %s", @@ -139,7 +141,7 @@ private Future fetchDocumentFile(String documentId, List createFreeLayoutSlideObjects(Board board, UserInfos user, - JsonObject slideShowData, List> documents) { + JsonObject slideShowData, List> documents, I18nHelper i18nHelper) { XMLSlideShow ppt = new XMLSlideShow(); ppt.setPageSize(new java.awt.Dimension(1280, 720)); @@ -152,7 +154,7 @@ private Future createFreeLayoutSlideObjects(Board board, UserInfos SlideFactory slideFactory = new SlideFactory(); // TITRE - XSLFSlide titleApacheSlide = createTitleSlide(board, documents); + XSLFSlide titleApacheSlide = createTitleSlide(board, documents, i18nHelper); ppt.createSlide().importContent(titleApacheSlide); // Utiliser l'ordre des cartes du Board @@ -184,25 +186,25 @@ private Future createFreeLayoutSlideObjects(Board board, UserInfos }); } - private XSLFSlide createTitleSlide(Board board, List> documents) { + private XSLFSlide createTitleSlide(Board board, List> documents, I18nHelper i18nHelper) { XMLSlideShow ppt = new XMLSlideShow(); XSLFSlide slide = ppt.createSlide(); - SlideHelper.createTitle(slide, board.getTitle(), 100, 100.0, TextParagraph.TextAlign.CENTER); + SlideHelper.createTitle(slide, board.getTitle(), MagnetoConstants.MAIN_TITLE_HEIGHT, MagnetoConstants.MAIN_TITLE_FONT_SIZE, TextParagraph.TextAlign.CENTER); XSLFTextBox textBox = SlideHelper.createContent(slide); XSLFTextParagraph paragraph = textBox.addNewTextParagraph(); paragraph.setTextAlign(TextParagraph.TextAlign.CENTER); XSLFTextRun textRun = paragraph.addNewTextRun(); - textRun.setText("Créé par " + board.getOwnerName() + ","); - textRun.setFontSize(36.0); + textRun.setText(i18nHelper.translate("magneto.slideshow.created.by") + board.getOwnerName() + ","); + textRun.setFontSize(MagnetoConstants.CONTENT_FONT_SIZE); XSLFTextParagraph paragraph2 = textBox.addNewTextParagraph(); paragraph2.setTextAlign(TextParagraph.TextAlign.CENTER); XSLFTextRun textRun2 = paragraph2.addNewTextRun(); - textRun2.setText("mis à jour le " + board.getModificationDate()); - textRun2.setFontSize(36.0); + textRun2.setText(i18nHelper.translate("magneto.slideshow.updated.the") + board.getModificationDate()); + textRun2.setFontSize(MagnetoConstants.CONTENT_FONT_SIZE); String imageUrl = board.getImageUrl(); String imageId = imageUrl.substring(imageUrl.lastIndexOf('/') + 1); @@ -214,7 +216,7 @@ private XSLFSlide createTitleSlide(Board board, List> docume Buffer documentBuffer = (Buffer) documentData.get("buffer"); String fileExtension = (String) documentData.get("extension"); if (documentBuffer != null) { - SlideHelper.createImage(slide, documentBuffer.getBytes(), fileExtension, 300); + SlideHelper.createImage(slide, documentBuffer.getBytes(), fileExtension, MagnetoConstants.MAIN_CONTENT_MARGIN_TOP); } } return slide; diff --git a/backend/src/main/resources/i18n/fr.json b/backend/src/main/resources/i18n/fr.json index b35ffa57..52de2a8c 100644 --- a/backend/src/main/resources/i18n/fr.json +++ b/backend/src/main/resources/i18n/fr.json @@ -224,6 +224,8 @@ "magneto.dropzone.overlay.solution": "Veuillez importer un fichier", "magneto.dropzone.overlay.action": "J'ai compris", "magneto.shared.push.notif.body": "a partagé avec vous un nouveau tableau", + "magneto.slideshow.created.by": "Créé par ", + "magneto.slideshow.updated.the": "mis à jour le", "magneto.notify.board.push.notif.body": "vous notifie de changements sur un tableau", "magneto.loading": "Chargement", "magneto.zoom": "Zoom",