Search in sources :

Example 11 with Schema

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

the class DbObjectXMLTransformer method addAttributes.

/**
 * Add class-specific attributes.
 *
 * @param dbObject DbObject
 * @param attribs AttributesImpl
 */
private void addAttributes(DbObject dbObject, AttributesImpl attribs) {
    if (dbObject instanceof Schema) {
        Schema schema = (Schema) dbObject;
        attribs.addAttribute("", "", XML.ATTR_DB_PREFIX, "CDATA", schema.getDbPrefix());
        attribs.addAttribute("", "", XML.ATTR_VERSION, "CDATA", Integer.toString(schema.getVersion()));
        attribs.addAttribute("", "", XML.ATTR_TABLE_COLUMN_ORDER, "CDATA", Boolean.toString(schema.isCheckTableColumnOrder()));
    } else if (dbObject instanceof Index) {
        Index index = (Index) dbObject;
        attribs.addAttribute("", "", XML.ATTR_UNIQUE, "CDATA", Boolean.toString(index.isUnique()));
    } else if (dbObject instanceof Column) {
        Column column = (Column) dbObject;
        attribs.addAttribute("", "", XML.ATTR_ORDER, "CDATA", Integer.toString(column.getOrder()));
    }
}
Also used : Column(org.alfresco.util.schemacomp.model.Column) Schema(org.alfresco.util.schemacomp.model.Schema) Index(org.alfresco.util.schemacomp.model.Index)

Example 12 with Schema

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

the class DbToXML method execute.

public void execute() {
    ExportDb exporter = new ExportDb(context);
    exporter.setNamePrefix(namePrefix);
    exporter.setDbSchemaName(dbSchemaName);
    exporter.execute();
    Schema schema = exporter.getSchema();
    // Write to a string buffer and then write the results to a file
    // since we need to write windows line endings - and even passing in a suitable
    // PrintWriter to StreamResult does not seem to result in the correct line endings.
    StringWriter stringWriter = new StringWriter();
    SchemaToXML schemaToXML = new SchemaToXML(schema, new StreamResult(stringWriter));
    schemaToXML.execute();
    writeToFile(stringWriter.getBuffer().toString());
}
Also used : StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Schema(org.alfresco.util.schemacomp.model.Schema)

Example 13 with Schema

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

the class ExportDb method execute.

private void execute(Connection con, int schemaVersion) throws Exception {
    final DatabaseMetaData dbmd = con.getMetaData();
    String schemaName = databaseMetaDataHelper.getSchema(con);
    schema = new Schema(schemaName, namePrefix, schemaVersion, true);
    String[] prefixFilters = namePrefixFilters(dbmd);
    for (String filter : prefixFilters) {
        extractSchema(dbmd, schemaName, filter);
    }
}
Also used : Schema(org.alfresco.util.schemacomp.model.Schema) DatabaseMetaData(java.sql.DatabaseMetaData)

Example 14 with Schema

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

the class XMLToSchema method startElement.

@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
    lastText.setLength(0);
    if (qName.equals(XML.EL_SCHEMA)) {
        String name = atts.getValue(XML.ATTR_NAME);
        String dbPrefix = atts.getValue(XML.ATTR_DB_PREFIX);
        int version = Integer.parseInt(atts.getValue(XML.ATTR_VERSION));
        String attrTableColumnOrder = atts.getValue(XML.ATTR_TABLE_COLUMN_ORDER);
        // Should column order be checked for tables?
        boolean compareTableColOrder = attrTableColumnOrder != null ? Boolean.parseBoolean(attrTableColumnOrder) : true;
        schema = new Schema(name, dbPrefix, version, compareTableColOrder);
        stack.push(schema);
    } else if (qName.equals(XML.EL_TABLE)) {
        stack.push(new Table(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_COLUMN)) {
        Column column = new Column(atts.getValue(XML.ATTR_NAME));
        if (atts.getValue(XML.ATTR_ORDER) != null) {
            int order = Integer.parseInt(atts.getValue(XML.ATTR_ORDER));
            column.setOrder(order);
        }
        column.setCompareOrder(schema.isCheckTableColumnOrder());
        stack.push(column);
    } else if (qName.equals(XML.EL_COLUMN_NAME)) {
        if (stack.peek() instanceof PrimaryKey && atts.getValue(XML.ATTR_ORDER) != null) {
            PrimaryKey pk = (PrimaryKey) stack.peek();
            Integer columnOrder = Integer.parseInt(atts.getValue(XML.ATTR_ORDER));
            pk.getColumnOrders().add(columnOrder);
        }
    } else if (qName.equals(XML.EL_PRIMARY_KEY)) {
        stack.push(new PrimaryKey(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_FOREIGN_KEY)) {
        stack.push(new ForeignKey(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_INDEX)) {
        Index index = new Index(atts.getValue(XML.ATTR_NAME));
        boolean unique = Boolean.parseBoolean(atts.getValue(XML.ATTR_UNIQUE));
        index.setUnique(unique);
        stack.push(index);
    } else if (qName.equals(XML.EL_SEQUENCE)) {
        stack.push(new Sequence(atts.getValue(XML.ATTR_NAME)));
    } else if (qName.equals(XML.EL_VALIDATOR)) {
        String className = atts.getValue(XML.ATTR_CLASS);
        DbValidator validator = null;
        try {
            validator = (DbValidator) Class.forName(className).newInstance();
        } catch (Throwable e) {
            throw new RuntimeException("Couldn't create validator, class: " + className, e);
        }
        stack.push(validator);
    } else if (qName.equals(XML.EL_PROPERTY)) {
        String name = atts.getValue(XML.ATTR_NAME);
        stack.push(name);
    }
}
Also used : Table(org.alfresco.util.schemacomp.model.Table) Schema(org.alfresco.util.schemacomp.model.Schema) PrimaryKey(org.alfresco.util.schemacomp.model.PrimaryKey) Index(org.alfresco.util.schemacomp.model.Index) Sequence(org.alfresco.util.schemacomp.model.Sequence) ForeignKey(org.alfresco.util.schemacomp.model.ForeignKey) Column(org.alfresco.util.schemacomp.model.Column) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator)

Example 15 with Schema

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

the class SchemaVersionValidator method validate.

@Override
public void validate(DbObject referenceObj, DbObject targetObj, DiffContext ctx) {
    Schema reference = (Schema) referenceObj;
    Schema target = (Schema) targetObj;
    if (target.getVersion() < reference.getVersion()) {
        DbProperty targetProperty = new DbProperty(target, "version");
        String message = I18NUtil.getMessage("system.schema_comp.schema_version_validator", reference.getVersion());
        ctx.getComparisonResults().add(new ValidationResult(targetProperty, message));
    }
}
Also used : Schema(org.alfresco.util.schemacomp.model.Schema) ValidationResult(org.alfresco.util.schemacomp.ValidationResult) DbProperty(org.alfresco.util.schemacomp.DbProperty)

Aggregations

Schema (org.alfresco.util.schemacomp.model.Schema)20 Table (org.alfresco.util.schemacomp.model.Table)11 Test (org.junit.Test)9 PrimaryKey (org.alfresco.util.schemacomp.model.PrimaryKey)7 Index (org.alfresco.util.schemacomp.model.Index)6 Sequence (org.alfresco.util.schemacomp.model.Sequence)5 DbValidator (org.alfresco.util.schemacomp.validator.DbValidator)5 Column (org.alfresco.util.schemacomp.model.Column)4 DbObject (org.alfresco.util.schemacomp.model.DbObject)4 BufferedReader (java.io.BufferedReader)3 StringReader (java.io.StringReader)3 ForeignKey (org.alfresco.util.schemacomp.model.ForeignKey)3 StringWriter (java.io.StringWriter)2 StreamResult (javax.xml.transform.stream.StreamResult)2 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