Search in sources :

Example 1 with CompoundToken

use of org.structr.common.error.CompoundToken in project structr by structr.

the class ValidationHelper method areValidCompoundUniqueProperties.

public static synchronized boolean areValidCompoundUniqueProperties(final GraphObject object, final ErrorBuffer errorBuffer, final PropertyKey... keys) {
    if (keys != null && keys.length > 0) {
        final PropertyMap properties = new PropertyMap();
        List<GraphObject> result = null;
        Class type = null;
        for (final PropertyKey key : keys) {
            properties.put(key, object.getProperty(key));
            if (type != null) {
                // set type on first iteration
                type = key.getDeclaringClass();
            }
        }
        if (type == null) {
            // fallback: object type
            type = object.getClass();
        }
        try {
            if (object instanceof NodeInterface) {
                result = StructrApp.getInstance().nodeQuery(type).and(properties).disableSorting().getAsList();
            } else {
                result = StructrApp.getInstance().relationshipQuery(type).and(properties).disableSorting().getAsList();
            }
        } catch (FrameworkException fex) {
            logger.warn("", fex);
        }
        if (result != null) {
            for (final GraphObject foundNode : result) {
                if (foundNode.getId() != object.getId()) {
                    // validation is aborted when the first validation failure occurs, so
                    // we can assume that the object currently exmained is the first
                    // existing object, hence all others get the error message with the
                    // UUID of the first one.
                    errorBuffer.add(new CompoundToken(object.getType(), keys, object.getUuid()));
                    // error!
                    return false;
                }
            }
        }
    }
    // no error
    return true;
}
Also used : CompoundToken(org.structr.common.error.CompoundToken) PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

CompoundToken (org.structr.common.error.CompoundToken)1 FrameworkException (org.structr.common.error.FrameworkException)1 GraphObject (org.structr.core.GraphObject)1 NodeInterface (org.structr.core.graph.NodeInterface)1 PropertyKey (org.structr.core.property.PropertyKey)1 PropertyMap (org.structr.core.property.PropertyMap)1