reportName
This commit is contained in:
parent
8f79cf6056
commit
a8f15eff9d
3 changed files with 42 additions and 23 deletions
|
|
@ -1,12 +1,16 @@
|
||||||
package ru.spcex.clearing.reports.services.builders;
|
package ru.spcex.clearing.reports.services.builders;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import ru.spcex.clearing.reports.reports.AbstractReport;
|
import ru.spcex.clearing.reports.reports.AbstractReport;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
public abstract class DOCXReportBuilder<R extends AbstractReport> extends FileReportBuilder<R> {
|
public abstract class DOCXReportBuilder<R extends AbstractReport> extends FileReportBuilder<R> {
|
||||||
protected DOCXReportBuilder(Class<R> reportClass, Logger log, String filename) throws IOException {
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
super(reportClass, log, filename);
|
|
||||||
|
protected DOCXReportBuilder(Class<R> reportClass, File templateResource) throws IOException {
|
||||||
|
super(reportClass);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,12 @@
|
||||||
package ru.spcex.clearing.reports.services.builders;
|
package ru.spcex.clearing.reports.services.builders;
|
||||||
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import ru.spcex.clearing.reports.reports.AbstractReport;
|
import ru.spcex.clearing.reports.reports.AbstractReport;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
public abstract class FileReportBuilder<R extends AbstractReport> {
|
public abstract class FileReportBuilder<R extends AbstractReport> {
|
||||||
protected final Class<R> reportClass;
|
protected final Class<R> reportClass;
|
||||||
protected final Logger log;
|
|
||||||
protected final String filename;
|
|
||||||
protected final File file;
|
|
||||||
|
|
||||||
protected FileReportBuilder(Class<R> reportClass, Logger log, String filename) throws IOException {
|
protected FileReportBuilder(Class<R> reportClass) {
|
||||||
this.reportClass = reportClass;
|
this.reportClass = reportClass;
|
||||||
this.log = log;
|
|
||||||
this.filename = filename;
|
|
||||||
this.file = new File(filename);
|
|
||||||
if (file.exists()) {
|
|
||||||
if (file.isFile()) {
|
|
||||||
log.warn("Файл {} существует и будет перезаписан", filename);
|
|
||||||
boolean deleteOk = file.delete();
|
|
||||||
if (!deleteOk) throw new IOException("Не удалось удалить файл " + filename);
|
|
||||||
} else throw new IOException(filename + " существует, но не является файлом");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,16 +4,48 @@ import com.thoughtworks.xstream.XStream;
|
||||||
import com.thoughtworks.xstream.io.naming.NoNameCoder;
|
import com.thoughtworks.xstream.io.naming.NoNameCoder;
|
||||||
import com.thoughtworks.xstream.io.xml.XppDriver;
|
import com.thoughtworks.xstream.io.xml.XppDriver;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
import ru.spcex.clearing.reports.reports.AbstractReport;
|
import ru.spcex.clearing.reports.reports.AbstractReport;
|
||||||
|
import ru.spcex.clearing.reports.services.ReportDataCollector;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
public class XMLReportBuilder<R extends AbstractReport> extends FileReportBuilder<R> {
|
||||||
|
private final Logger log = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
public abstract class XMLReportBuilder<R extends AbstractReport> extends FileReportBuilder<R> {
|
|
||||||
protected final XStream xStream;
|
protected final XStream xStream;
|
||||||
|
|
||||||
protected XMLReportBuilder(Class<R> reportClass, Logger log, String filename) throws IOException {
|
protected final ReportDataCollector<R> collector;
|
||||||
super(reportClass, log, filename);
|
|
||||||
|
public XMLReportBuilder(Class<R> reportClass, ReportDataCollector<R> collector) {
|
||||||
|
super(reportClass);
|
||||||
|
this.collector = collector;
|
||||||
this.xStream = new XStream(new XppDriver(new NoNameCoder()));
|
this.xStream = new XStream(new XppDriver(new NoNameCoder()));
|
||||||
xStream.processAnnotations(reportClass);
|
xStream.processAnnotations(reportClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public File createReport(LocalDate startDate, LocalDate endDate) throws IOException {
|
||||||
|
R report = collector.collectReportWithPeriod(startDate, endDate);
|
||||||
|
String reportName = report.getReportName(LocalDateTime.now());
|
||||||
|
|
||||||
|
if (System.getProperty("os.name").contains("windows")) reportName = reportName.replace(":", "_");
|
||||||
|
|
||||||
|
String reportFileName = reportName + ".xml";
|
||||||
|
File reportFile = new File(reportFileName);
|
||||||
|
if (reportFile.exists()) {
|
||||||
|
if (reportFile.isFile()) {
|
||||||
|
log.warn("Файл {} существует и будет перезаписан", reportFileName);
|
||||||
|
boolean deleteOk = reportFile.delete();
|
||||||
|
if (!deleteOk) throw new IOException("Не удалось удалить файл " + reportFileName);
|
||||||
|
} else throw new IOException(reportFileName + " существует, но не является файлом");
|
||||||
|
}
|
||||||
|
try (FileOutputStream fos = new FileOutputStream(reportFile)) {
|
||||||
|
xStream.toXML(report, fos);
|
||||||
|
}
|
||||||
|
return reportFile;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue