Search in sources :

Example 91 with PropertyKey

use of org.structr.core.property.PropertyKey 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)

Example 92 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class CompoundToken method toJSON.

@Override
public JsonObject toJSON() {
    final JsonObject token = new JsonObject();
    final JsonArray array = new JsonArray();
    // add all keys that form the compound index
    for (final PropertyKey key : keys) {
        array.add(new JsonPrimitive(key.jsonName()));
    }
    token.add("type", getStringOrNull(getType()));
    token.add("properties", array);
    token.add("token", getStringOrNull(getToken()));
    // optional
    addIfNonNull(token, "detail", getObjectOrNull(getDetail()));
    return token;
}
Also used : JsonArray(com.google.gson.JsonArray) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 93 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class GraphObject method setProperties.

/**
 * Sets the given properties.
 *
 * @param securityContext
 * @param properties
 * @throws FrameworkException
 */
default void setProperties(final SecurityContext securityContext, final PropertyMap properties) throws FrameworkException {
    final CreationContainer container = new CreationContainer(this);
    boolean atLeastOnePropertyChanged = false;
    for (final Entry<PropertyKey, Object> attr : properties.entrySet()) {
        final PropertyKey key = attr.getKey();
        final Object value = attr.getValue();
        if (key.indexable(value)) {
            final Object oldValue = getProperty(key);
            if (!value.equals(oldValue)) {
                atLeastOnePropertyChanged = true;
                // bulk set possible, store in container
                key.setProperty(securityContext, container, value);
                if (isNode()) {
                    if (!key.isUnvalidated()) {
                        TransactionCommand.nodeModified(securityContext.getCachedUser(), (AbstractNode) this, key, getProperty(key), value);
                    }
                    if (key instanceof TypeProperty) {
                        if (this instanceof NodeInterface) {
                            final Class type = StructrApp.getConfiguration().getNodeEntityClass((String) value);
                            TypeProperty.updateLabels(StructrApp.getInstance().getDatabaseService(), (NodeInterface) this, type, true);
                        }
                    }
                } else if (isRelationship()) {
                    if (!key.isUnvalidated()) {
                        TransactionCommand.relationshipModified(securityContext.getCachedUser(), (AbstractRelationship) this, key, getProperty(key), value);
                    }
                    if (key instanceof TypeProperty) {
                        if (this instanceof NodeInterface) {
                            final Class type = StructrApp.getConfiguration().getNodeEntityClass((String) value);
                            TypeProperty.updateLabels(StructrApp.getInstance().getDatabaseService(), (NodeInterface) this, type, true);
                        }
                    }
                }
            }
        } else {
            // bulk set NOT possible, set on entity
            if (key.isSystemInternal()) {
                unlockSystemPropertiesOnce();
            }
            setProperty(key, value);
        }
    }
    if (atLeastOnePropertyChanged) {
        // set primitive values directly for better performance
        getPropertyContainer().setProperties(container.getData());
    }
}
Also used : TypeProperty(org.structr.core.property.TypeProperty) AbstractRelationship(org.structr.core.entity.AbstractRelationship) CreationContainer(org.structr.core.graph.CreationContainer) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface)

Example 94 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class GraphObjectMap method toMap.

public Map<String, Object> toMap() {
    final Map<String, Object> newMap = new LinkedHashMap<>();
    for (final Entry<PropertyKey, Object> entry : properties.entrySet()) {
        final PropertyKey key = entry.getKey();
        final Object value = entry.getValue();
        if (value instanceof GraphObjectMap) {
            newMap.put(key.jsonName(), ((GraphObjectMap) value).toMap());
        } else {
            newMap.put(key.jsonName(), value);
        }
    }
    return newMap;
}
Also used : PropertyKey(org.structr.core.property.PropertyKey) LinkedHashMap(java.util.LinkedHashMap)

Example 95 with PropertyKey

use of org.structr.core.property.PropertyKey in project structr by structr.

the class PathResolvingComparator method resolve.

// ----- private methods -----
private Comparable resolve(final GraphObject obj, final String path) {
    final ConfigurationProvider config = StructrApp.getConfiguration();
    final String[] parts = path.split("[\\.]+");
    GraphObject current = obj;
    int pos = 0;
    for (final String part : parts) {
        final Class type = current.getEntityType();
        final PropertyKey key = config.getPropertyKeyForJSONName(type, part, false);
        if (key == null) {
            logger.warn("Unknown key {} while resolving path {} for sorting.", part, path);
            return null;
        }
        final Object value = current.getProperty(key);
        if (value != null) {
            // last part of path?
            if (++pos == parts.length) {
                if (value instanceof Comparable) {
                    return (Comparable) value;
                }
                logger.warn("Path evaluation result of component {} of type {} in {} cannot be used for sorting.", part, value.getClass().getSimpleName(), path);
                return null;
            }
            if (value instanceof GraphObject) {
                current = (GraphObject) value;
            } else {
                logger.warn("Path component {} of type {} in {} cannot be evaluated further.", part, value.getClass().getSimpleName(), path);
                return null;
            }
        }
    }
    return null;
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Aggregations

PropertyKey (org.structr.core.property.PropertyKey)177 FrameworkException (org.structr.common.error.FrameworkException)108 Test (org.junit.Test)69 NodeInterface (org.structr.core.graph.NodeInterface)62 Tx (org.structr.core.graph.Tx)61 GraphObject (org.structr.core.GraphObject)59 StructrTest (org.structr.common.StructrTest)39 PropertyMap (org.structr.core.property.PropertyMap)37 List (java.util.List)31 Result (org.structr.core.Result)28 ConfigurationProvider (org.structr.schema.ConfigurationProvider)27 SecurityContext (org.structr.common.SecurityContext)26 LinkedList (java.util.LinkedList)22 StringProperty (org.structr.core.property.StringProperty)22 ErrorToken (org.structr.common.error.ErrorToken)20 Map (java.util.Map)18 PropertyConverter (org.structr.core.converter.PropertyConverter)18 NodeAttribute (org.structr.core.graph.NodeAttribute)17 App (org.structr.core.app.App)16 StructrApp (org.structr.core.app.StructrApp)16