All Downloads are FREE. Search and download functionalities are using the official Maven repository.

liquibase.dbdoc.HTMLWriter Maven / Gradle / Ivy

There is a newer version: 4.30.0
Show newest version
package liquibase.dbdoc;

import liquibase.change.Change;
import liquibase.changelog.ChangeSet;
import liquibase.GlobalConfiguration;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.exception.DatabaseHistoryException;
import liquibase.util.LiquibaseUtil;
import liquibase.util.StringUtil;

import java.io.*;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;

public abstract class HTMLWriter {
    protected File outputDir;
    protected Database database;

    public HTMLWriter(File outputDir, Database database) {
        this.outputDir = outputDir;
        this.database = database;
        if (!outputDir.exists()) {
            outputDir.mkdirs();
        }
    }

    protected abstract void writeCustomHTML(Writer fileWriter, Object object, List changes, Database database) throws IOException;

    private Writer createFileWriter(Object object) throws IOException {
        return new OutputStreamWriter(new FileOutputStream(new File(outputDir, DBDocUtil.toFileName(object.toString().toLowerCase()) + ".html")), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
    }

    public void writeHTML(Object object, List ranChanges, List changesToRun, String changeLog) throws IOException, DatabaseHistoryException, DatabaseException {
        Writer fileWriter = createFileWriter(object);


        try {
            fileWriter.append("");
            writeHeader(object, fileWriter);
            fileWriter.append("");

            fileWriter.append("

").append(createTitle(object)).append("

\n"); writeBody(fileWriter, object, ranChanges, changesToRun); writeFooter(fileWriter, changeLog); fileWriter.append(""); fileWriter.append(""); } finally { fileWriter.close(); } } private void writeFooter(Writer fileWriter, String changeLog) throws IOException { fileWriter.append("
Generated: "); fileWriter.append(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(new Date())); fileWriter.append("
Against: "); fileWriter.append(database.toString()); fileWriter.append("
Change Log: "); fileWriter.append(changeLog); fileWriter.append("

Generated By: "); fileWriter.append("Liquibase ").append(LiquibaseUtil .getBuildVersionInfo()).append(""); } protected void writeBody(Writer fileWriter, Object object, List ranChanges, List changesToRun) throws IOException, DatabaseHistoryException, DatabaseException { writeCustomHTML(fileWriter, object, ranChanges, database); writeChanges("Pending Changes", fileWriter, changesToRun); writeChanges("Past Changes", fileWriter, ranChanges); } protected void writeTable(String title, List> cells, Writer fileWriter) throws IOException { fileWriter.append("

"); int colspan = 0; if (cells.isEmpty()) { colspan = 0; } else { colspan = cells.get(0).size(); } fileWriter.append("

\n") .append("\n").append("\n") .append("\n"); for (List row : cells) { fileWriter.append("\n"); for (String cell : row) { writeTD(fileWriter, cell); } fileWriter.append("\n"); } fileWriter.append("
\n").append("").append(title).append("
\n"); } private void writeTD(Writer fileWriter, String filePath) throws IOException { fileWriter.append("\n"); fileWriter.append(filePath); fileWriter.append("\n"); } private void writeHeader(Object object, Writer fileWriter) throws IOException { String title = createTitle(object); fileWriter.append("") .append("").append(title).append("") .append("") .append("") .append(""); } protected abstract String createTitle(Object object); protected void writeChanges(String title, Writer fileWriter, List changes) throws IOException, DatabaseHistoryException, DatabaseException { fileWriter.append("

\n"); fileWriter.append("\n"); fileWriter.append("\n"); fileWriter.append("\n"); ChangeSet lastChangeSet = null; if ((changes == null) || changes.isEmpty()) { fileWriter.append(""); } else { for (Change change : changes) { if (!change.getChangeSet().equals(lastChangeSet)) { lastChangeSet = change.getChangeSet(); fileWriter.append("\n"); writeTD(fileWriter, ""+change.getChangeSet().getFilePath()+""); writeTD(fileWriter, change.getChangeSet().getId()); writeTD(fileWriter, ""+ StringUtil.escapeHtml(change.getChangeSet().getAuthor().toLowerCase())+""); ChangeSet.RunStatus runStatus = database.getRunStatus(change.getChangeSet()); if (runStatus.equals(ChangeSet.RunStatus.NOT_RAN)) { String anchor = change.getChangeSet().toString(false).replaceAll("\\W","_"); writeTD(fileWriter, "NOT YET RAN [SQL]"); } else if (runStatus.equals(ChangeSet.RunStatus.INVALID_MD5SUM)) { writeTD(fileWriter, "INVALID MD5SUM"); } else if (runStatus.equals(ChangeSet.RunStatus.ALREADY_RAN)) { writeTD(fileWriter, "Executed "+ DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT).format(database.getRanDate(change.getChangeSet()))); } else if (runStatus.equals(ChangeSet.RunStatus.RUN_AGAIN)) { writeTD(fileWriter, "Executed, WILL RUN AGAIN"); } else { throw new RuntimeException("Unknown run status: "+runStatus); } fileWriter.append(""); if (StringUtil.trimToNull(change.getChangeSet().getComments()) != null) { fileWriter.append(""); } } fileWriter.append("\n"); fileWriter.append(""); } } fileWriter.append("
\n"); fileWriter.append(""); fileWriter.append(title); fileWriter.append("
None Found
").append(change.getChangeSet().getComments()).append("
          ").append(change.getConfirmationMessage()).append("
"); fileWriter.append(" 

"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy