use of com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns in project ats-framework by Axway.
the class DatabaseSnapshotBackupUtils method saveToFile.
/**
* Save a snapshot into a file
* @param snapshot the snapshot to save
* @param backupFile the backup file name
* @return the XML document
*/
public Document saveToFile(DatabaseSnapshot snapshot, String backupFile) {
log.info("Save database snapshot into file " + backupFile + " - START");
// create the directory if does not exist
File dirPath = new File(IoUtils.getFilePath(backupFile));
if (!dirPath.exists()) {
dirPath.mkdirs();
}
Document doc;
try {
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
} catch (Exception e) {
throw new DatabaseSnapshotException("Error creating DOM parser for " + backupFile, e);
}
// TODO - add DTD or schema for manual creation and easy validation
Element dbNode = doc.createElement(DatabaseSnapshotUtils.NODE_DB_SNAPSHOT);
dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_SNAPSHOT_NAME, snapshot.name);
// the timestamp comes when user takes a snapshot from database
dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_METADATA_TIME, DatabaseSnapshotUtils.dateToString(snapshot.metadataTimestamp));
// the timestamp now
snapshot.contentTimestamp = System.currentTimeMillis();
dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_CONTENT_TIME, DatabaseSnapshotUtils.dateToString(snapshot.contentTimestamp));
doc.appendChild(dbNode);
// append all table data
for (TableDescription tableDescription : snapshot.tables) {
Element tableNode = doc.createElement(DatabaseSnapshotUtils.NODE_TABLE);
// append table meta data
dbNode.appendChild(tableNode);
tableDescription.toXmlNode(doc, tableNode);
// check if table content is to be skipped
SkipContent skipTableContentOption = snapshot.skipContentPerTable.get(tableDescription.getName().toLowerCase());
if (skipTableContentOption != null) {
// we skip the table content
if (skipTableContentOption.isRememberNumberOfRows()) {
// ... but we want to persist the number of rows
int numberRows = snapshot.loadTableLength(snapshot.name, tableDescription, null, null);
tableNode.setAttribute(DatabaseSnapshotUtils.ATTR_TABLE_NUMBER_ROWS, String.valueOf(numberRows));
}
continue;
}
// append table content
List<String> valuesList = snapshot.loadTableData(snapshot.name, tableDescription, snapshot.skipColumnsPerTable, snapshot.skipRowsPerTable, null, null);
for (String values : valuesList) {
Element rowNode = doc.createElement(DatabaseSnapshotUtils.NODE_ROW);
rowNode.setTextContent(StringUtils.escapeNonPrintableAsciiCharacters(values));
tableNode.appendChild(rowNode);
}
}
// append any skip table content rules
for (SkipContent skipContent : snapshot.skipContentPerTable.values()) {
skipContent.toXmlNode(doc, dbNode);
}
// append any skip table column rules
for (SkipColumns skipColumns : snapshot.skipColumnsPerTable.values()) {
skipColumns.toXmlNode(doc, dbNode);
}
// append any skip index attribute rules
for (SkipIndexAttributes skipIndexAttributes : snapshot.skipIndexAttributesPerTable.values()) {
skipIndexAttributes.toXmlNode(doc, dbNode);
}
// append any skip table row rules
for (SkipRows skipRows : snapshot.skipRowsPerTable.values()) {
skipRows.toXmlNode(doc, dbNode);
}
// save the XML file
OutputStream fos = null;
try {
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setIndent(4);
format.setLineWidth(1000);
fos = new FileOutputStream(new File(backupFile));
XMLSerializer serializer = new XMLSerializer(fos, format);
serializer.serialize(doc);
} catch (Exception e) {
throw new DatabaseSnapshotException("Error saving " + backupFile, e);
} finally {
IoUtils.closeStream(fos, "Error closing IO stream to file used for database snapshot backup " + backupFile);
}
log.info("Save database snapshot into file " + backupFile + " - END");
return doc;
}
Aggregations