Search in sources :

Example 1 with ExportDb

use of org.alfresco.util.schemacomp.ExportDb in project alfresco-repository by Alfresco.

the class SchemaBootstrap method attemptValidateSchema.

/**
 * Validates and compares current DB schema with schema reference definition, specified in <code>referenceResource</code> parameter.<br />
 * <br />
 * The method supports two mechanisms to report validation results:
 * <ol>
 * <li>using an external output stream, specified as <code>out</code>;</li>
 * <li>using specially created {@link FileOutputStream}, which represents temporary file with name, formatted in accordance with <code>outputFileNameTemplate</code> template.</li>
 * </ol>
 * It is necessary to take care about freeing resources of output stream in case of the 1st approach.<br />
 * <b>N.B.:</b> The method only writes messages of the report. And it <b>doesn't flush and doesn't close</b> the specified output stream!<br />
 * <br />
 *
 * @param referenceResource - {@link Resource} instance, which determines file of reference schema
 * @param outputFileNameTemplate - {@link String} value, which determines template of temporary filename for validation report. <b>It can't be <code>null</code> if
 *        <code>out</code> is <code>null</code></b>!
 * @param out - {@link PrintWriter} instance, which represents an external output stream for writing a validation report. This stream is never closed or flushed. <b>It can't be
 *        <code>null</code> if <code>outputFileNameTemplate</code> is <code>null</code></b>!
 * @return {@link Integer} value, which determines amount of errors or warnings that were detected during validation
 */
private int attemptValidateSchema(Resource referenceResource, String outputFileNameTemplate, PrintWriter out) {
    Date startTime = new Date();
    InputStream is = null;
    try {
        is = new BufferedInputStream(referenceResource.getInputStream());
    } catch (IOException e) {
        throw new RuntimeException("Unable to open schema reference file: " + referenceResource);
    }
    XMLToSchema xmlToSchema = new XMLToSchema(is);
    xmlToSchema.parse();
    Schema reference = xmlToSchema.getSchema();
    ExportDb exporter = new ExportDb(dataSource, dialect, descriptorService, databaseMetaDataHelper);
    exporter.setDbSchemaName(dbSchemaName);
    // Ensure that the database objects we're validating are filtered
    // by the same prefix as the reference file.
    exporter.setNamePrefix(reference.getDbPrefix());
    exporter.execute();
    Schema target = exporter.getSchema();
    SchemaComparator schemaComparator = new SchemaComparator(reference, target, dialect);
    schemaComparator.validateAndCompare();
    Results results = schemaComparator.getComparisonResults();
    Object[] outputFileNameParams = new Object[] { dialect.getClass().getSimpleName(), reference.getDbPrefix() };
    PrintWriter pw = null;
    File outputFile = null;
    try {
        if (out == null) {
            String outputFileName = MessageFormat.format(outputFileNameTemplate, outputFileNameParams);
            outputFile = TempFileProvider.createTempFile(outputFileName, ".txt");
            try {
                pw = new PrintWriter(outputFile, SchemaComparator.CHAR_SET);
            } catch (FileNotFoundException error) {
                throw new RuntimeException("Unable to open file for writing: " + outputFile);
            } catch (UnsupportedEncodingException error) {
                throw new RuntimeException("Unsupported char set: " + SchemaComparator.CHAR_SET, error);
            }
        } else {
            pw = out;
        }
        // Populate the file with details of the comparison's results.
        for (Result result : results) {
            pw.print(result.describe());
            pw.print(SchemaComparator.LINE_SEPARATOR);
        }
    } finally {
        // We care only about output streams for reporting, which are created specially for current reference resource...
        if (null == out) {
            pw.close();
        }
    }
    if (results.size() == 0) {
        LogUtil.info(logger, INFO_SCHEMA_COMP_ALL_OK, referenceResource);
    } else {
        int numProblems = results.size();
        if (outputFile == null) {
            LogUtil.warn(logger, WARN_SCHEMA_COMP_PROBLEMS_FOUND_NO_FILE, numProblems);
        } else {
            LogUtil.warn(logger, WARN_SCHEMA_COMP_PROBLEMS_FOUND, numProblems, outputFile);
        }
    }
    Date endTime = new Date();
    long durationMillis = endTime.getTime() - startTime.getTime();
    LogUtil.debug(logger, DEBUG_SCHEMA_COMP_TIME_TAKEN, durationMillis);
    return results.size();
}
Also used : SchemaComparator(org.alfresco.util.schemacomp.SchemaComparator) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Schema(org.alfresco.util.schemacomp.model.Schema) XMLToSchema(org.alfresco.util.schemacomp.XMLToSchema) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Date(java.util.Date) Savepoint(java.sql.Savepoint) Result(org.alfresco.util.schemacomp.Result) XMLToSchema(org.alfresco.util.schemacomp.XMLToSchema) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) BufferedInputStream(java.io.BufferedInputStream) Results(org.alfresco.util.schemacomp.Results) File(java.io.File) ExportDb(org.alfresco.util.schemacomp.ExportDb) PrintWriter(java.io.PrintWriter)

Aggregations

BufferedInputStream (java.io.BufferedInputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PrintWriter (java.io.PrintWriter)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Savepoint (java.sql.Savepoint)1 Date (java.util.Date)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 ExportDb (org.alfresco.util.schemacomp.ExportDb)1 Result (org.alfresco.util.schemacomp.Result)1 Results (org.alfresco.util.schemacomp.Results)1 SchemaComparator (org.alfresco.util.schemacomp.SchemaComparator)1 XMLToSchema (org.alfresco.util.schemacomp.XMLToSchema)1 Schema (org.alfresco.util.schemacomp.model.Schema)1