|
| 1 | +package org.cpicpgx.exporter; |
| 2 | + |
| 3 | +import org.apache.commons.csv.CSVFormat; |
| 4 | +import org.apache.commons.csv.CSVPrinter; |
| 5 | +import org.cpicpgx.db.ConnectionFactory; |
| 6 | +import org.cpicpgx.model.FileType; |
| 7 | +import org.slf4j.Logger; |
| 8 | +import org.slf4j.LoggerFactory; |
| 9 | + |
| 10 | +import java.io.FileWriter; |
| 11 | +import java.lang.invoke.MethodHandles; |
| 12 | +import java.nio.file.Path; |
| 13 | +import java.sql.Connection; |
| 14 | +import java.sql.ResultSet; |
| 15 | + |
| 16 | +public class ChangelogExporter extends BaseExporter { |
| 17 | + private static final Logger sf_logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); |
| 18 | + private static final String FILE_NAME = "data.tsv"; |
| 19 | + private static final String SQL_STMT = "select date as \"Date of Change\", type as \"Type of Data\", entityname as \"Subject\", note as \"Note of Change\" from change_log_view order by date desc, type, entityname, note"; |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + ChangelogExporter exporter = new ChangelogExporter(); |
| 23 | + try { |
| 24 | + exporter.parseArgs(args); |
| 25 | + exporter.export(); |
| 26 | + } catch (Exception e) { |
| 27 | + sf_logger.error("Error exporting changelog", e); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public FileType getFileType() { |
| 33 | + return FileType.CHANGELOG; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void export() throws Exception { |
| 38 | + Path filePath = this.directory.resolve(FILE_NAME); |
| 39 | + try ( |
| 40 | + FileWriter fileWriter = new FileWriter(filePath.toFile()); |
| 41 | + Connection conn = ConnectionFactory.newConnection(); |
| 42 | + ResultSet rs = conn.prepareStatement(SQL_STMT).executeQuery(); |
| 43 | + CSVPrinter printer = CSVFormat.TDF.builder() |
| 44 | + .setHeader(rs) |
| 45 | + .build().print(fileWriter) |
| 46 | + ) { |
| 47 | + printer.printRecords(rs); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
0 commit comments