use of com.axway.ats.common.dbaccess.snapshot.TableDescription 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);
// this timestamp comes when user takes a snapshot from database
dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_METADATA_TIME, DatabaseSnapshotUtils.dateToString(snapshot.metadataTimestamp));
// this timestamp now
snapshot.contentTimestamp = System.currentTimeMillis();
dbNode.setAttribute(DatabaseSnapshotUtils.ATTR_CONTENT_TIME, DatabaseSnapshotUtils.dateToString(snapshot.contentTimestamp));
doc.appendChild(dbNode);
// append table descriptions
for (TableDescription tableDescription : snapshot.tables) {
Element tableNode = doc.createElement(DatabaseSnapshotUtils.NODE_TABLE);
dbNode.appendChild(tableNode);
tableDescription.toXmlNode(doc, tableNode);
// if the current table is in the skipTableContent we will do not need to get its all data
if (snapshot.skipTableContent.contains(tableDescription.getName())) {
// no rows have to be saved, so we skip this iteration
continue;
}
List<String> valuesList = new ArrayList<String>();
valuesList.addAll(snapshot.loadTableData(snapshot.name, tableDescription, snapshot.skipRulesPerTable, snapshot.skipRows, null, null));
for (String values : valuesList) {
Element rowNode = doc.createElement(DatabaseSnapshotUtils.NODE_ROW);
if (!skipRow(snapshot.skipRows.get(tableDescription.getName()), values)) {
rowNode.setTextContent(values);
}
tableNode.appendChild(rowNode);
}
}
// append skip rules
for (String tableName : snapshot.skipRulesPerTable.keySet()) {
Element skipRuleNode = doc.createElement(DatabaseSnapshotUtils.NODE_SKIP_RULE);
dbNode.appendChild(skipRuleNode);
skipRuleNode.setAttribute(DatabaseSnapshotUtils.ATT_SKIP_RULE_TABLE, tableName);
snapshot.skipRulesPerTable.get(tableName).toXmlNode(doc, skipRuleNode);
}
// save the XML file
try {
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setIndent(4);
format.setLineWidth(1000);
XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(backupFile)), format);
serializer.serialize(doc);
} catch (Exception e) {
throw new DatabaseSnapshotException("Error saving " + backupFile, e);
}
log.info("Save database snapshot into file " + backupFile + " - END");
return doc;
}
Aggregations