From 62abb0d58b74a0f0469459d02f244689985f7141 Mon Sep 17 00:00:00 2001 From: AKurakin Date: Fri, 13 Jan 2023 11:14:14 +0300 Subject: [PATCH] =?UTF-8?q?reports-service=20=20=D0=BF=D0=BE=D1=84=D0=B8?= =?UTF-8?q?=D0=BA=D1=81=D0=B8=D0=BB=20=D0=B7=D0=B0=D0=BF=D1=83=D1=81=D0=BA?= =?UTF-8?q?=20=D0=B8=D0=B7=20jar=20(=D0=BD=D0=B5=20=D0=BC=D0=BE=D0=B3=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=87=D0=B8=D1=82=D0=B0=D1=82=D1=8C=20=D1=80?= =?UTF-8?q?=D0=B5=D1=81=D1=83=D1=80=D1=81=D1=8B=20=D0=BA=D0=B0=D0=BA=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB),=20=D1=83=D0=B1=D1=80=D0=B0=D0=BB=20?= =?UTF-8?q?=D1=82=D0=BE=D0=BC=D0=BA=D0=B0=D1=82=20=D1=81=D0=B5=D1=80=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=20(=D1=82=D0=B0=D0=BA=D0=BE=D0=B5=20=D0=BA=D0=BE?= =?UTF-8?q?=D0=B3=D0=B4=D0=B0-=D1=82=D0=BE=20=D0=B1=D1=8B=D0=BB=D0=BE=20?= =?UTF-8?q?=D0=BD=D0=B5=D1=81=D0=BA=D0=BE=D0=BB=D1=8C=D0=BA=D0=BE=20=D0=BB?= =?UTF-8?q?=D0=B5=D1=82=20=D0=BD=D0=B0=D0=B7=D0=B0=D0=B4=20=D1=81=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D0=BE=D0=BC=20report-srvc)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clearing-parent/reports-service/pom.xml | 6 ++++++ clearing-parent/reports-service/readme.md | 7 ++++++- .../notifications/NotificationBuilder.java | 17 +++++++++-------- .../clearing/reports/services/DOCXService.java | 14 ++++++++------ .../src/main/resources/application.properties | 3 --- .../reports/services/DOCXServiceTest.java | 2 +- 6 files changed, 30 insertions(+), 19 deletions(-) diff --git a/clearing-parent/reports-service/pom.xml b/clearing-parent/reports-service/pom.xml index 457291ada..969ee84ed 100644 --- a/clearing-parent/reports-service/pom.xml +++ b/clearing-parent/reports-service/pom.xml @@ -24,6 +24,12 @@ org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-tomcat + + org.springframework.boot diff --git a/clearing-parent/reports-service/readme.md b/clearing-parent/reports-service/readme.md index 995cd56a8..4ab9323ef 100644 --- a/clearing-parent/reports-service/readme.md +++ b/clearing-parent/reports-service/readme.md @@ -48,4 +48,9 @@ DAILY - все ежедневные reports-service.kafka-consumer - группа настроек для подключения к очереди kafka -и другие настройки. \ No newline at end of file +и другие настройки. + +Рабочие папки +------------- + +* Из настройки reports-service.ReportModuleOut=./report_out - путь к папке для результатов генерации отчётов diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/notifications/NotificationBuilder.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/notifications/NotificationBuilder.java index 56cdad6b8..9773f4a1e 100644 --- a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/notifications/NotificationBuilder.java +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/notifications/NotificationBuilder.java @@ -1,19 +1,20 @@ package ru.spcex.clearing.reports.notifications; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import ru.spcex.clearing.reports.exceptions.ConfigException; import ru.spcex.clearing.reports.services.DOCXService; import java.io.File; import java.net.URL; import java.time.LocalDate; -import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.TextStyle; -import java.util.HashMap; import java.util.Locale; import java.util.Map; public abstract class NotificationBuilder { + protected final Logger log = LoggerFactory.getLogger(getClass()); protected final DateTimeFormatter filenameDateTimeFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy'T'HH:mm:ss"); protected final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); protected final DOCXService docxService; @@ -41,17 +42,17 @@ public abstract class NotificationBuilder { } - protected File getTemplateFile() { - File templateFile; + protected URL getTemplateFile() { String templateResourceName = getTemplateResourceName(); + URL resource = null; try { - URL resource = getClass().getClassLoader().getResource(templateResourceName); + resource = getClass().getClassLoader().getResource(templateResourceName); if (resource == null) throw new Exception(); - templateFile = new File(resource.toURI()); } catch (Exception e) { - throw new ConfigException("Can't read template resource " + templateResourceName, e); + throw new ConfigException("Can't read template resource " + templateResourceName +" (URL \""+resource+"\")", e); } - return templateFile; + log.info("Use resource: {}", resource); + return resource; } protected String normalizeFilenameForOS(String filename) { diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/services/DOCXService.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/services/DOCXService.java index 02e1813eb..67cd092ea 100644 --- a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/services/DOCXService.java +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports/services/DOCXService.java @@ -5,6 +5,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.*; +import java.net.URL; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -43,7 +44,7 @@ public class DOCXService { /** * Файл шаблона */ - private File templateFile; + private URL templateFile; /** * Флаг инициализации @@ -53,13 +54,14 @@ public class DOCXService { /** * (ре)Инициализация сервиса */ - public boolean init(File templateFile, String keyPattern) { + public boolean init(URL templateFile, String keyPattern) { initialized = false; - if (templateFile == null || !templateFile.exists() || !templateFile.isFile()) { - log.warn("Invalid templateFile"); + if (templateFile == null /*|| !templateFile.exists() || !templateFile.isFile()*/) { + log.warn("Invalid templateFile {}", templateFile); return false; } this.templateFile = templateFile; + log.debug("{} use template from {}", getClass().getSimpleName(), templateFile); if (keyPattern != null) { try { this.keyPattern = Pattern.compile(keyPattern); @@ -108,7 +110,7 @@ public class DOCXService { } } - try (InputStream inputStream = new FileInputStream(templateFile); + try (InputStream inputStream = templateFile.openStream(); //new FileInputStream(templateFile); OutputStream outputStream = new FileOutputStream(outputFile)) { XWPFDocument doc = new XWPFDocument(inputStream); @@ -125,7 +127,7 @@ public class DOCXService { boolean checkOk = checkIntersection(valuesMap, notPresentInMap); if (!checkOk) { log.error("Can't insert values in file {}. Keys not in map: {}.", - templateFile.getAbsolutePath(), + templateFile.toExternalForm()/*templateFile.getAbsolutePath()*/, String.join(", ", notPresentInMap)); return false; } diff --git a/clearing-parent/reports-service/src/main/resources/application.properties b/clearing-parent/reports-service/src/main/resources/application.properties index ffa1ebe50..ed2aa2053 100644 --- a/clearing-parent/reports-service/src/main/resources/application.properties +++ b/clearing-parent/reports-service/src/main/resources/application.properties @@ -1,6 +1,3 @@ -server.port=8080 -server.servlet.context-path=/reports_service -spring.main.web-application-type=servlet reports-service.kafka-consumer.bootstrap-servers=localhost:9092 diff --git a/clearing-parent/reports-service/src/test/java/ru/spcex/clearing/reports/services/DOCXServiceTest.java b/clearing-parent/reports-service/src/test/java/ru/spcex/clearing/reports/services/DOCXServiceTest.java index ffd640744..2cfff9e9d 100644 --- a/clearing-parent/reports-service/src/test/java/ru/spcex/clearing/reports/services/DOCXServiceTest.java +++ b/clearing-parent/reports-service/src/test/java/ru/spcex/clearing/reports/services/DOCXServiceTest.java @@ -19,7 +19,7 @@ class DOCXServiceTest { valuesMap.put("${test3}", "======== TEST 3 ========="); DOCXService service = new DOCXService(); - service.init(new File(getClass().getClassLoader().getResource("REC.ACTV_TEST.docx").getPath()), null); + service.init(getClass().getClassLoader().getResource("REC.ACTV_TEST.docx"), null); service.insertValues(new File("output.docx"), valuesMap, false);