Search in sources :

Example 6 with SkipColumns

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

Aggregations

SkipColumns (com.axway.ats.action.dbaccess.snapshot.rules.SkipColumns)6 SkipContent (com.axway.ats.action.dbaccess.snapshot.rules.SkipContent)4 SkipRows (com.axway.ats.action.dbaccess.snapshot.rules.SkipRows)3 DatabaseSnapshotException (com.axway.ats.common.dbaccess.snapshot.DatabaseSnapshotException)3 TableDescription (com.axway.ats.common.dbaccess.snapshot.TableDescription)3 SkipIndexAttributes (com.axway.ats.action.dbaccess.snapshot.rules.SkipIndexAttributes)2 File (java.io.File)2 ArrayList (java.util.ArrayList)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 IndexMatcher (com.axway.ats.common.dbaccess.snapshot.IndexMatcher)1 DatabaseEqualityState (com.axway.ats.common.dbaccess.snapshot.equality.DatabaseEqualityState)1 PostgreSqlDbProvider (com.axway.ats.core.dbaccess.postgresql.PostgreSqlDbProvider)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 OutputFormat (org.apache.xml.serialize.OutputFormat)1 XMLSerializer (org.apache.xml.serialize.XMLSerializer)1