Search in sources :

Example 1 with Transformation

use of org.structr.core.Transformation in project structr by structr.

the class CreateNodeCommand method execute.

public T execute(final PropertyMap attributes) throws FrameworkException {
    final DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    final Principal user = securityContext.getUser(false);
    T node = null;
    if (graphDb != null) {
        final NodeFactory<T> nodeFactory = new NodeFactory<>(securityContext);
        final PropertyMap properties = new PropertyMap(attributes);
        final PropertyMap toNotify = new PropertyMap();
        final Object typeObject = properties.get(AbstractNode.type);
        final Class nodeType = getTypeOrGeneric(typeObject);
        final Set<String> labels = TypeProperty.getLabelsForType(nodeType);
        final CreationContainer tmp = new CreationContainer();
        final Date now = new Date();
        final boolean isCreation = true;
        // use user-supplied UUID?
        String uuid = properties.get(GraphObject.id);
        if (uuid == null) {
            // no, create new one
            uuid = getNextUuid();
            properties.put(GraphObject.id, uuid);
        } else {
            // enable UUID validation
            securityContext.uuidWasSetManually(true);
        }
        // use property keys to set property values on creation dummy
        // set default values for common properties in creation query
        GraphObject.id.setProperty(securityContext, tmp, uuid);
        GraphObject.type.setProperty(securityContext, tmp, nodeType.getSimpleName());
        AbstractNode.createdDate.setProperty(securityContext, tmp, now);
        AbstractNode.lastModifiedDate.setProperty(securityContext, tmp, now);
        // default property values
        AbstractNode.visibleToPublicUsers.setProperty(securityContext, tmp, getOrDefault(properties, AbstractNode.visibleToPublicUsers, false));
        AbstractNode.visibleToAuthenticatedUsers.setProperty(securityContext, tmp, getOrDefault(properties, AbstractNode.visibleToAuthenticatedUsers, false));
        AbstractNode.hidden.setProperty(securityContext, tmp, getOrDefault(properties, AbstractNode.hidden, false));
        AbstractNode.deleted.setProperty(securityContext, tmp, getOrDefault(properties, AbstractNode.deleted, false));
        if (user != null) {
            final String userId = user.getProperty(GraphObject.id);
            AbstractNode.createdBy.setProperty(securityContext, tmp, userId);
            AbstractNode.lastModifiedBy.setProperty(securityContext, tmp, userId);
        }
        // prevent double setting of properties
        properties.remove(AbstractNode.id);
        properties.remove(AbstractNode.type);
        properties.remove(AbstractNode.visibleToPublicUsers);
        properties.remove(AbstractNode.visibleToAuthenticatedUsers);
        properties.remove(AbstractNode.hidden);
        properties.remove(AbstractNode.deleted);
        properties.remove(AbstractNode.lastModifiedDate);
        properties.remove(AbstractNode.lastModifiedBy);
        properties.remove(AbstractNode.createdDate);
        properties.remove(AbstractNode.createdBy);
        // move properties to creation container that can be set directly on creation
        tmp.filterIndexableForCreation(securityContext, properties, tmp, toNotify);
        // collect default values and try to set them on creation
        for (final PropertyKey key : StructrApp.getConfiguration().getPropertySet(nodeType, PropertyView.All)) {
            if (key instanceof AbstractPrimitiveProperty && !tmp.hasProperty(key.jsonName())) {
                final Object defaultValue = key.defaultValue();
                if (defaultValue != null) {
                    key.setProperty(securityContext, tmp, defaultValue);
                }
            }
        }
        node = (T) nodeFactory.instantiateWithType(createNode(graphDb, user, labels, tmp.getData()), nodeType, null, isCreation);
        if (node != null) {
            TransactionCommand.nodeCreated(user, node);
            securityContext.disableModificationOfAccessTime();
            node.setProperties(securityContext, properties);
            securityContext.enableModificationOfAccessTime();
            // ensure modification callbacks are called (necessary for validation)
            for (final Entry<PropertyKey, Object> entry : toNotify.entrySet()) {
                final PropertyKey key = entry.getKey();
                final Object value = entry.getValue();
                if (!key.isUnvalidated()) {
                    TransactionCommand.nodeModified(securityContext.getCachedUser(), (AbstractNode) node, key, null, value);
                }
            }
            properties.clear();
            // ensure indexing of newly created node
            node.addToIndex();
            // invalidate UUID cache
            StructrApp.invalidate(uuid);
        }
    }
    if (node != null) {
        // notify node of its creation
        node.onNodeCreation();
        // iterate post creation transformations
        final Set<Transformation<GraphObject>> transformations = StructrApp.getConfiguration().getEntityCreationTransformations(node.getClass());
        for (Transformation<GraphObject> transformation : transformations) {
            transformation.apply(securityContext, node);
        }
    }
    return node;
}
Also used : Transformation(org.structr.core.Transformation) DatabaseService(org.structr.api.DatabaseService) GraphObject(org.structr.core.GraphObject) Date(java.util.Date) PropertyMap(org.structr.core.property.PropertyMap) AbstractPrimitiveProperty(org.structr.core.property.AbstractPrimitiveProperty) GraphObject(org.structr.core.GraphObject) Principal(org.structr.core.entity.Principal) PropertyKey(org.structr.core.property.PropertyKey)

Aggregations

Date (java.util.Date)1 DatabaseService (org.structr.api.DatabaseService)1 GraphObject (org.structr.core.GraphObject)1 Transformation (org.structr.core.Transformation)1 Principal (org.structr.core.entity.Principal)1 AbstractPrimitiveProperty (org.structr.core.property.AbstractPrimitiveProperty)1 PropertyKey (org.structr.core.property.PropertyKey)1 PropertyMap (org.structr.core.property.PropertyMap)1