From 38e0bd5790b322bcf7637587a706f98ece0427e4 Mon Sep 17 00:00:00 2001 From: akulikov Date: Thu, 1 Sep 2022 18:12:55 +0300 Subject: [PATCH] reports-service created (prototype xml read with XStream) --- clearing-parent/reports-service/pom.xml | 62 +++++++ .../ReportsServiceApplication.java | 21 +++ .../config/ReportsServiceConfig.java | 12 ++ .../controller/DefaultController.java | 27 +++ .../exceptions/ConfigException.java | 6 + .../properties/AProperties.java | 8 + .../services/ReportsService.java | 24 +++ .../xml_test/ChildElementForWrite.java | 74 +++++++++ .../xml_test/CustomLocalDateConverter.java | 32 ++++ .../xml_test/ElementWithAttributeOnly.java | 29 ++++ .../xml_test/RootElementForWrite.java | 155 ++++++++++++++++++ .../src/main/resources/application.properties | 3 + .../src/main/resources/logback.xml | 37 +++++ 13 files changed, 490 insertions(+) create mode 100644 clearing-parent/reports-service/pom.xml create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/ReportsServiceApplication.java create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/config/ReportsServiceConfig.java create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/controller/DefaultController.java create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/exceptions/ConfigException.java create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/properties/AProperties.java create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/services/ReportsService.java create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/ChildElementForWrite.java create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/CustomLocalDateConverter.java create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/ElementWithAttributeOnly.java create mode 100644 clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/RootElementForWrite.java create mode 100644 clearing-parent/reports-service/src/main/resources/application.properties create mode 100644 clearing-parent/reports-service/src/main/resources/logback.xml diff --git a/clearing-parent/reports-service/pom.xml b/clearing-parent/reports-service/pom.xml new file mode 100644 index 000000000..f5c8ebac9 --- /dev/null +++ b/clearing-parent/reports-service/pom.xml @@ -0,0 +1,62 @@ + + + + 4.0.0 + + reports-service + reports-service + Service for creating reports + SPCEX-1.0.0.0 + + + clearing + ru.spcex.clearing + SPCEX-1.0.0.0 + + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-autoconfigure + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + + + com.thoughtworks.xstream + xstream + 1.4.19 + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + ${project.artifactId} + + + + + + \ No newline at end of file diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/ReportsServiceApplication.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/ReportsServiceApplication.java new file mode 100644 index 000000000..9c9f83420 --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/ReportsServiceApplication.java @@ -0,0 +1,21 @@ +package ru.spcex.clearing.reports_service; + +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import ru.spcex.clearing.reports_service.services.ReportsService; + +@SpringBootApplication +public class ReportsServiceApplication { + public static void main(String[] args) { + try { + SpringApplicationBuilder builder = new SpringApplicationBuilder(ReportsServiceApplication.class); + builder.run(args); + } catch (Throwable e) { + LoggerFactory.getLogger(ReportsServiceApplication.class).error("DBF-Loader start failed: {} -> {}", e.getClass().getSimpleName(), e.getMessage()); + System.exit(-1); + } + ReportsService reportsService = new ReportsService(); + reportsService.writeXML(null); + } +} diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/config/ReportsServiceConfig.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/config/ReportsServiceConfig.java new file mode 100644 index 000000000..44345db8c --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/config/ReportsServiceConfig.java @@ -0,0 +1,12 @@ +package ru.spcex.clearing.reports_service.config; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties +@ComponentScan(basePackages = {"ru.spcex.clearing.reports_service"}) +public class ReportsServiceConfig { + +} diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/controller/DefaultController.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/controller/DefaultController.java new file mode 100644 index 000000000..5206e9db4 --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/controller/DefaultController.java @@ -0,0 +1,27 @@ +package ru.spcex.clearing.reports_service.controller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller("/") +public class DefaultController implements InitializingBean { + private final Logger log = LoggerFactory.getLogger(getClass()); + + @RequestMapping(method = RequestMethod.GET, path = "/test", produces = MediaType.TEXT_PLAIN_VALUE) + @ResponseBody + public String processGet() { + log.info("Call test method for exporter controller"); + return "exporter controller test method"; + } + + @Override + public void afterPropertiesSet() throws Exception { + log.info("controller started"); + } +} diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/exceptions/ConfigException.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/exceptions/ConfigException.java new file mode 100644 index 000000000..b51da9820 --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/exceptions/ConfigException.java @@ -0,0 +1,6 @@ +package ru.spcex.clearing.reports_service.exceptions; + +public class ConfigException extends RuntimeException { + public ConfigException(String msg) { super(msg); } + public ConfigException(String msg, Throwable cause) { super(msg, cause); } +} diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/properties/AProperties.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/properties/AProperties.java new file mode 100644 index 000000000..6dac8567b --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/properties/AProperties.java @@ -0,0 +1,8 @@ +package ru.spcex.clearing.reports_service.properties; + +import org.springframework.stereotype.Component; + +@Component("reportsServiceProperties") +public class AProperties { + +} diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/services/ReportsService.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/services/ReportsService.java new file mode 100644 index 000000000..b94cba214 --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/services/ReportsService.java @@ -0,0 +1,24 @@ +package ru.spcex.clearing.reports_service.services; + +import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.io.AbstractDriver; +import ru.spcex.clearing.reports_service.xml_test.ChildElementForWrite; +import ru.spcex.clearing.reports_service.xml_test.CustomLocalDateConverter; +import ru.spcex.clearing.reports_service.xml_test.RootElementForWrite; + +public class ReportsService { + public void writeXML(AbstractDriver driver) { + XStream xStream = driver == null ? new XStream() : new XStream(driver); + + // достаточно только для рутового объекта вызвать обработку аннотаций + xStream.processAnnotations(RootElementForWrite.class); + xStream.registerConverter(new CustomLocalDateConverter(), -100); + + RootElementForWrite elementForWrite = new RootElementForWrite(); + elementForWrite.getTestList().add(new ChildElementForWrite()); + elementForWrite.getTestList().add(new ChildElementForWrite()); + String dataXml = xStream.toXML(elementForWrite); + + System.out.println(dataXml); + } +} diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/ChildElementForWrite.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/ChildElementForWrite.java new file mode 100644 index 000000000..99d3e7cb5 --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/ChildElementForWrite.java @@ -0,0 +1,74 @@ +package ru.spcex.clearing.reports_service.xml_test; + + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; +import com.thoughtworks.xstream.annotations.XStreamConverter; +import com.thoughtworks.xstream.annotations.XStreamOmitField; +import com.thoughtworks.xstream.converters.basic.BooleanConverter; + +// without aliases +public class ChildElementForWrite { + @XStreamAlias("childStr") // alias for field name + private String testChildString = "testChildStr"; + + @XStreamAsAttribute + private Integer testAttributeInt = Integer.MIN_VALUE; + + @XStreamAsAttribute + @XStreamAlias("isBoolean") + @XStreamConverter(value = BooleanConverter.class, booleans = {false}, strings = {"yes", "no"}) + private Boolean testYesAttributeBool = Boolean.TRUE; + +// @XStreamAlias("booleanField") +// @XStreamConverter(value = BooleanConverter.class, booleans = {true, false}, strings = {"yes", "no"}) +// private Boolean testNoFieldBool = Boolean.FALSE; + + private Integer testChildInt = Integer.MAX_VALUE; + + @XStreamOmitField + private String testIgnoredString = "testIgnore"; + + + + public String getTestChildString() { + return testChildString; + } + + public void setTestChildString(String testChildString) { + this.testChildString = testChildString; + } + + public Integer getTestAttributeInt() { + return testAttributeInt; + } + + public void setTestAttributeInt(Integer testAttributeInt) { + this.testAttributeInt = testAttributeInt; + } + + public Integer getTestChildInt() { + return testChildInt; + } + + public void setTestChildInt(Integer testChildInt) { + this.testChildInt = testChildInt; + } + + public String getTestIgnoredString() { + return testIgnoredString; + } + + public void setTestIgnoredString(String testIgnoredString) { + this.testIgnoredString = testIgnoredString; + } + + public Boolean getTestYesAttributeBool() { + return testYesAttributeBool; + } + + public void setTestYesAttributeBool(Boolean testYesAttributeBool) { + this.testYesAttributeBool = testYesAttributeBool; + } + +} diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/CustomLocalDateConverter.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/CustomLocalDateConverter.java new file mode 100644 index 000000000..11a414756 --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/CustomLocalDateConverter.java @@ -0,0 +1,32 @@ +package ru.spcex.clearing.reports_service.xml_test; + +import com.thoughtworks.xstream.converters.Converter; +import com.thoughtworks.xstream.converters.MarshallingContext; +import com.thoughtworks.xstream.converters.UnmarshallingContext; +import com.thoughtworks.xstream.io.HierarchicalStreamReader; +import com.thoughtworks.xstream.io.HierarchicalStreamWriter; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; + +// самый большой минус работы с датами: если у какой то даты отличается формат, для нее нужен кастомный конвертер +public class CustomLocalDateConverter implements Converter { + private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy"); + @Override + public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) { + LocalDate date = (LocalDate) source; + writer.setValue(formatter.format(date)); + writer.addAttribute("formatted", "true"); + } + + @Override + public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { + // not implemented (not used) + return null; + } + + @Override + public boolean canConvert(Class type) { + return LocalDate.class.isAssignableFrom(type); + } +} diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/ElementWithAttributeOnly.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/ElementWithAttributeOnly.java new file mode 100644 index 000000000..9ee1da75d --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/ElementWithAttributeOnly.java @@ -0,0 +1,29 @@ +package ru.spcex.clearing.reports_service.xml_test; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamAsAttribute; + +@XStreamAlias("withAttrsOnly") +public class ElementWithAttributeOnly { + @XStreamAsAttribute + private String attrStr = "attrStr"; + + @XStreamAsAttribute + private Long attrLong = Long.MIN_VALUE; + + public String getAttrStr() { + return attrStr; + } + + public void setAttrStr(String attrStr) { + this.attrStr = attrStr; + } + + public Long getAttrLong() { + return attrLong; + } + + public void setAttrLong(Long attrLong) { + this.attrLong = attrLong; + } +} diff --git a/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/RootElementForWrite.java b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/RootElementForWrite.java new file mode 100644 index 000000000..f8ae35408 --- /dev/null +++ b/clearing-parent/reports-service/src/main/java/ru/spcex/clearing/reports_service/xml_test/RootElementForWrite.java @@ -0,0 +1,155 @@ +package ru.spcex.clearing.reports_service.xml_test; + +import com.thoughtworks.xstream.annotations.XStreamAlias; +import com.thoughtworks.xstream.annotations.XStreamConverter; +import com.thoughtworks.xstream.annotations.XStreamImplicit; + +import java.math.BigDecimal; +import java.time.Instant; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +@XStreamAlias("root") // alias for classname +public class RootElementForWrite { + + @XStreamAlias("string") // alias for field + private String testString = "testString"; + + private BigDecimal testBigDecimal = new BigDecimal("666.666"); + + private Double testDouble = Double.MAX_VALUE; + + private Long testLong = Long.MIN_VALUE; + + private Boolean testBoolean = Boolean.FALSE; + + private LocalDate testLocalDateDefault = LocalDate.now(); + + @XStreamConverter(CustomLocalDateConverter.class) + private LocalDate testLocalDateCustom = LocalDate.now(); + + private LocalDateTime testLocalDateTime = LocalDateTime.now(); + + private OffsetDateTime testOffsetDateTime = OffsetDateTime.now(); + + private Instant testInstant = Instant.now(); + + @XStreamAlias("namedList") + private List testList = new ArrayList<>(); + + @XStreamImplicit + private List testListTwo = Arrays.asList(new ChildElementForWrite(), new ChildElementForWrite()); + + // can marshal without getter/setter + private ElementWithAttributeOnly elementWithAttributeOnly = new ElementWithAttributeOnly(); + + public String getTestString() { + return testString; + } + + public void setTestString(String testString) { + this.testString = testString; + } + + public BigDecimal getTestBigDecimal() { + return testBigDecimal; + } + + public void setTestBigDecimal(BigDecimal testBigDecimal) { + this.testBigDecimal = testBigDecimal; + } + + public Double getTestDouble() { + return testDouble; + } + + public void setTestDouble(Double testDouble) { + this.testDouble = testDouble; + } + + public Long getTestLong() { + return testLong; + } + + public void setTestLong(Long testLong) { + this.testLong = testLong; + } + + public Boolean getTestBoolean() { + return testBoolean; + } + + public void setTestBoolean(Boolean testBoolean) { + this.testBoolean = testBoolean; + } + + public LocalDate getTestLocalDate() { + return testLocalDateCustom; + } + + public void setTestLocalDate(LocalDate testLocalDate) { + this.testLocalDateCustom = testLocalDate; + } + + public LocalDateTime getTestLocalDateTime() { + return testLocalDateTime; + } + + public void setTestLocalDateTime(LocalDateTime testLocalDateTime) { + this.testLocalDateTime = testLocalDateTime; + } + + public OffsetDateTime getTestOffsetDateTime() { + return testOffsetDateTime; + } + + public void setTestOffsetDateTime(OffsetDateTime testOffsetDateTime) { + this.testOffsetDateTime = testOffsetDateTime; + } + + public Instant getTestInstant() { + return testInstant; + } + + public void setTestInstant(Instant testInstant) { + this.testInstant = testInstant; + } + + public List getTestList() { + return testList; + } + + public void setTestList(List testList) { + this.testList = testList; + } + + public List getTestListTwo() { + return testListTwo; + } + + public void setTestListTwo(List testListTwo) { + this.testListTwo = testListTwo; + } + + public LocalDate getTestLocalDateDefault() { + return testLocalDateDefault; + } + + public void setTestLocalDateDefault(LocalDate testLocalDateDefault) { + this.testLocalDateDefault = testLocalDateDefault; + } + + public LocalDate getTestLocalDateCustom() { + return testLocalDateCustom; + } + + public void setTestLocalDateCustom(LocalDate testLocalDateCustom) { + this.testLocalDateCustom = testLocalDateCustom; + } + +} + diff --git a/clearing-parent/reports-service/src/main/resources/application.properties b/clearing-parent/reports-service/src/main/resources/application.properties new file mode 100644 index 000000000..fdc884583 --- /dev/null +++ b/clearing-parent/reports-service/src/main/resources/application.properties @@ -0,0 +1,3 @@ +server.port=8080 +server.servlet.context-path=/reports_service +spring.main.web-application-type=servlet \ No newline at end of file diff --git a/clearing-parent/reports-service/src/main/resources/logback.xml b/clearing-parent/reports-service/src/main/resources/logback.xml new file mode 100644 index 000000000..865483aab --- /dev/null +++ b/clearing-parent/reports-service/src/main/resources/logback.xml @@ -0,0 +1,37 @@ + + + + + UTF-8 + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + ./logs/reports_service.log + + UTF-8 + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + ../logs/reports_service.%i.log + + 1 + 10 + + + 500MB + + + + + + + + + + + + + \ No newline at end of file