Search in sources :

Example 6 with TableDescription

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;
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) Element(org.w3c.dom.Element) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) OutputFormat(org.apache.xml.serialize.OutputFormat) Document(org.w3c.dom.Document) File(java.io.File) TableDescription(com.axway.ats.common.dbaccess.snapshot.TableDescription) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException) DatabaseSnapshotException(com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)

Aggregations

TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)6 ArrayList (java.util.ArrayList)4 PublicAtsApi (com.axway.ats.common.PublicAtsApi)2 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)2 IndexNameMatcher (com.axway.ats.common.dbaccess.snapshot.IndexNameMatcher)1 EqualityState (com.axway.ats.common.dbaccess.snapshot.equality.EqualityState)1 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 OracleDbProvider (com.axway.ats.core.dbaccess.oracle.OracleDbProvider)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 OutputFormat (org.apache.xml.serialize.OutputFormat)1 XMLSerializer (org.apache.xml.serialize.XMLSerializer)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1