liquibase.dbdoc.HTMLWriter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-core Show documentation
Show all versions of liquibase-core Show documentation
Liquibase is a tool for managing and executing database changes.
The newest version!
package liquibase.dbdoc;
import liquibase.change.Change;
import liquibase.changelog.ChangeSet;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.exception.DatabaseHistoryException;
import liquibase.resource.OpenOptions;
import liquibase.resource.Resource;
import liquibase.util.LiquibaseUtil;
import liquibase.util.StringUtil;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.DateFormat;
import java.util.Date;
import java.util.List;
public abstract class HTMLWriter {
protected Resource outputDir;
protected Resource baseOutputDir;
protected Database database;
public HTMLWriter(Resource outputDir, Database database) {
this.outputDir = outputDir;
this.baseOutputDir = outputDir;
this.database = database;
}
protected abstract void writeCustomHTML(Writer fileWriter, Object object, List changes, Database database) throws IOException;
private Writer createFileWriter(Object object) throws IOException {
return new OutputStreamWriter(outputDir.resolve(DBDocUtil.toFileName(object.toString().toLowerCase()) + ".html").openOutputStream(new OpenOptions()));
}
public void writeHTML(Object object, List ranChanges, List changesToRun, String changeLog) throws IOException, DatabaseHistoryException, DatabaseException {
try (Writer fileWriter = createFileWriter(object)) {
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("");
}
}
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("").append(title).append(" \n")
.append(" \n");
for (List row : cells) {
fileWriter.append("\n");
for (String cell : row) {
writeTD(fileWriter, cell);
}
fileWriter.append(" \n");
}
fileWriter.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("");
fileWriter.append(title);
fileWriter.append(" \n");
fileWriter.append(" \n");
ChangeSet lastChangeSet = null;
if ((changes == null) || changes.isEmpty()) {
fileWriter.append("None Found ");
} 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("").append(change.getChangeSet().getComments()).append(" ");
}
}
fileWriter.append("\n");
fileWriter.append(" ").append(change.getConfirmationMessage()).append(" ");
}
}
fileWriter.append("
");
fileWriter.append(" ");
}
}