Search in sources :

Example 6 with DbValidator

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

the class DefaultComparisonUtils method compareSimple.

@Override
public void compareSimple(DbProperty leftProperty, DbProperty rightProperty, DiffContext ctx) {
    // Check whether the leftProperty should be compared to the rightProperty
    DbObject leftDbObject = leftProperty.getDbObject();
    if (leftDbObject.hasValidators()) {
        for (DbValidator validator : leftDbObject.getValidators()) {
            if (validator.validates(leftProperty.getPropertyName())) {
                // Don't perform differencing on this property - a validator will handle it.
                return;
            }
        }
    }
    Where where = null;
    Object left = leftProperty.getPropertyValue();
    checkNotDbObject(left);
    Object right = rightProperty.getPropertyValue();
    checkNotDbObject(right);
    if (left == right) {
        // Same object, or both nulls
        where = Where.IN_BOTH_NO_DIFFERENCE;
    } else if (left == null) {
        // right can't be null, or left == right would have been true
        where = Where.ONLY_IN_TARGET;
    } else if (right == null) {
        // left can't be null, or left == right would have been true
        where = Where.ONLY_IN_REFERENCE;
    } else {
        // neither are null
        boolean objectsAreEqual;
        // Strings are compared case-insensitively, e.g. table names.
        if (left instanceof String && right instanceof String) {
            objectsAreEqual = ((String) left).equalsIgnoreCase((String) right);
        } else {
            objectsAreEqual = left.equals(right);
        }
        if (objectsAreEqual) {
            where = Where.IN_BOTH_NO_DIFFERENCE;
        } else {
            where = Where.IN_BOTH_BUT_DIFFERENCE;
        }
    }
    ctx.getComparisonResults().add(where, leftProperty, rightProperty);
}
Also used : DbObject(org.alfresco.util.schemacomp.model.DbObject) DbObject(org.alfresco.util.schemacomp.model.DbObject) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator) Where(org.alfresco.util.schemacomp.Difference.Where)

Example 7 with DbValidator

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

the class DefaultComparisonUtils method compareSimpleCollections.

@Override
public void compareSimpleCollections(DbProperty leftProp, DbProperty rightProp, DiffContext ctx) {
    // Check whether the leftProperty should be compared to the rightProperty
    DbObject leftDbObject = leftProp.getDbObject();
    if (leftDbObject.hasValidators()) {
        for (DbValidator validator : leftDbObject.getValidators()) {
            if (validator.validates(leftProp.getPropertyName())) {
                // Don't perform differencing on this property - a validator will handle it.
                return;
            }
        }
    }
    @SuppressWarnings("unchecked") Collection<? extends Object> leftCollection = (Collection<? extends Object>) leftProp.getPropertyValue();
    @SuppressWarnings("unchecked") Collection<? extends Object> rightCollection = (Collection<? extends Object>) rightProp.getPropertyValue();
    ArrayList<? extends Object> leftList = new ArrayList<Object>(leftCollection);
    ArrayList<? extends Object> rightList = new ArrayList<Object>(rightCollection);
    Results differences = ctx.getComparisonResults();
    for (int leftIndex = 0; leftIndex < leftList.size(); leftIndex++) {
        Object leftObj = leftList.get(leftIndex);
        DbProperty leftIndexedProp = new DbProperty(leftProp.getDbObject(), leftProp.getPropertyName(), leftIndex);
        int rightIndex;
        if ((rightIndex = rightList.indexOf(leftObj)) != -1) {
            // The same valued object in the right hand collection as in the left.
            // Note: it isn't possible to determine a result of Where.IN_BOTH_BUT_DIFFERENCE
            // with a 'simple' value — as there is no way of knowing if the term represents the same value
            // (e.g. two strings {red_value, green_value}, are these meant to be the same or different?)
            DbProperty rightIndexedProp = new DbProperty(rightProp.getDbObject(), rightProp.getPropertyName(), rightIndex);
            differences.add(Where.IN_BOTH_NO_DIFFERENCE, leftIndexedProp, rightIndexedProp);
        } else {
            // No equivalent object in the right hand collection.
            // Using rightIndexedProperty would result in index out of bounds error.
            differences.add(Where.ONLY_IN_REFERENCE, leftIndexedProp, rightProp);
        }
    }
    // Identify objects in the right collection but not the left
    for (int rightIndex = 0; rightIndex < rightList.size(); rightIndex++) {
        Object rightObj = rightList.get(rightIndex);
        if (!leftCollection.contains(rightObj)) {
            DbProperty rightIndexedProp = new DbProperty(rightProp.getDbObject(), rightProp.getPropertyName(), rightIndex);
            // No equivalent object in the left hand collection.
            differences.add(Where.ONLY_IN_TARGET, leftProp, rightIndexedProp);
        }
    }
}
Also used : DbObject(org.alfresco.util.schemacomp.model.DbObject) ArrayList(java.util.ArrayList) Collection(java.util.Collection) DbObject(org.alfresco.util.schemacomp.model.DbObject) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator)

Example 8 with DbValidator

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

the class DbObjectXMLTransformer method transformValidators.

/**
 * @param validators List<DbValidator>
 * @throws SAXException
 */
private void transformValidators(List<DbValidator> validators) throws SAXException {
    if (validators.size() > 0) {
        simpleStartTag(XML.EL_VALIDATORS);
        for (DbValidator dbv : validators) {
            final AttributesImpl attribs = new AttributesImpl();
            attribs.addAttribute("", "", XML.ATTR_CLASS, "CDATA", dbv.getClass().getName());
            xmlOut.startElement("", "", XML.EL_VALIDATOR, attribs);
            if (dbv.getPropertyNames().size() > 0) {
                simpleStartTag(XML.EL_PROPERTIES);
                for (String propName : dbv.getPropertyNames()) {
                    final AttributesImpl propAttrs = new AttributesImpl();
                    propAttrs.addAttribute("", "", XML.ATTR_NAME, "CDATA", propName);
                    xmlOut.startElement("", "", XML.EL_PROPERTY, propAttrs);
                    String propValue = dbv.getProperty(propName);
                    char[] chars = propValue.toCharArray();
                    xmlOut.characters(chars, 0, chars.length);
                    simpleEndTag(XML.EL_PROPERTY);
                }
                simpleEndTag(XML.EL_PROPERTIES);
            }
            simpleEndTag(XML.EL_VALIDATOR);
        }
        simpleEndTag(XML.EL_VALIDATORS);
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator)

Example 9 with DbValidator

use of org.alfresco.util.schemacomp.validator.DbValidator 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 10 with DbValidator

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

the class DefaultComparisonUtils method compareSimpleOrderedLists.

@Override
public void compareSimpleOrderedLists(DbProperty refProp, DbProperty targetProp, DiffContext ctx) {
    checkPropertyContainsList(refProp);
    checkPropertyContainsList(targetProp);
    // Check whether the leftProperty should be compared to the rightProperty
    DbObject leftDbObject = refProp.getDbObject();
    if (leftDbObject.hasValidators()) {
        for (DbValidator validator : leftDbObject.getValidators()) {
            if (validator.validates(refProp.getPropertyName())) {
                // Don't perform differencing on this property - a validator will handle it.
                return;
            }
        }
    }
    @SuppressWarnings("unchecked") ArrayList<Object> refList = new ArrayList<Object>((List<Object>) refProp.getPropertyValue());
    @SuppressWarnings("unchecked") ArrayList<Object> targetList = new ArrayList<Object>((List<Object>) targetProp.getPropertyValue());
    Results differences = ctx.getComparisonResults();
    int maxSize = Math.max(refList.size(), targetList.size());
    for (int i = 0; i < maxSize; i++) {
        if (i < refList.size() && i < targetList.size()) {
            DbProperty refIndexedProp = new DbProperty(refProp.getDbObject(), refProp.getPropertyName(), i);
            DbProperty targetIndexedProp = new DbProperty(targetProp.getDbObject(), targetProp.getPropertyName(), i);
            if (refList.get(i).equals(targetList.get(i))) {
                differences.add(Where.IN_BOTH_NO_DIFFERENCE, refIndexedProp, targetIndexedProp);
            } else {
                differences.add(Where.IN_BOTH_BUT_DIFFERENCE, refIndexedProp, targetIndexedProp);
            }
        } else if (i < refList.size()) {
            DbProperty indexedProp = new DbProperty(refProp.getDbObject(), refProp.getPropertyName(), i);
            differences.add(Where.ONLY_IN_REFERENCE, indexedProp, null);
        } else {
            DbProperty indexedProp = new DbProperty(targetProp.getDbObject(), targetProp.getPropertyName(), i);
            // No equivalent object in the reference collection.
            differences.add(Where.ONLY_IN_TARGET, null, indexedProp);
        }
    }
}
Also used : DbObject(org.alfresco.util.schemacomp.model.DbObject) ArrayList(java.util.ArrayList) DbObject(org.alfresco.util.schemacomp.model.DbObject) DbValidator(org.alfresco.util.schemacomp.validator.DbValidator)

Aggregations

DbValidator (org.alfresco.util.schemacomp.validator.DbValidator)15 DbObject (org.alfresco.util.schemacomp.model.DbObject)9 Index (org.alfresco.util.schemacomp.model.Index)8 Table (org.alfresco.util.schemacomp.model.Table)8 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)6 PrimaryKey (org.alfresco.util.schemacomp.model.PrimaryKey)6 Column (org.alfresco.util.schemacomp.model.Column)5 ForeignKey (org.alfresco.util.schemacomp.model.ForeignKey)5 Schema (org.alfresco.util.schemacomp.model.Schema)5 Sequence (org.alfresco.util.schemacomp.model.Sequence)5 BufferedReader (java.io.BufferedReader)3 StringReader (java.io.StringReader)3 AbstractDbObject (org.alfresco.util.schemacomp.model.AbstractDbObject)3 NameValidator (org.alfresco.util.schemacomp.validator.NameValidator)2 Collection (java.util.Collection)1 MySQLInnoDBDialect (org.alfresco.repo.domain.dialect.MySQLInnoDBDialect)1 Where (org.alfresco.util.schemacomp.Difference.Where)1 Before (org.junit.Before)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1