Search in sources :

Example 91 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class DeleteNodeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Boolean recursive = (Boolean) webSocketData.getNodeData().get("recursive");
    final NodeInterface obj = getNode(webSocketData.getId());
    if (obj != null) {
        deleteNode(getWebSocket(), obj, recursive);
    }
}
Also used : NodeInterface(org.structr.core.graph.NodeInterface)

Example 92 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class DeleteNodeCommand method deleteNode.

protected static void deleteNode(final StructrWebSocket ws, final NodeInterface obj, final Boolean recursive) {
    final SecurityContext securityContext = ws.getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);
    try (final Tx tx = app.tx()) {
        if (!(obj.isGranted(Permission.delete, securityContext))) {
            logger.warn("No delete permission for {} on {}", new Object[] { ws.getCurrentUser().toString(), obj.toString() });
            ws.send(MessageBuilder.status().message("No delete permission").code(400).build(), true);
            tx.success();
            return;
        }
    } catch (FrameworkException ex) {
        logger.warn("", ex);
    }
    if (Boolean.TRUE.equals(recursive)) {
        // Remove all child nodes first
        try {
            final List<NodeInterface> filteredResults = new LinkedList<>();
            if (obj instanceof DOMNode) {
                DOMNode node = (DOMNode) obj;
                filteredResults.addAll(DOMNode.getAllChildNodes(node));
            } else if (obj instanceof LinkedTreeNode) {
                LinkedTreeNode node = (LinkedTreeNode) obj;
                filteredResults.addAll(node.getAllChildNodes());
            }
            for (NodeInterface node : filteredResults) {
                app.delete(node);
            }
        } catch (FrameworkException fex) {
            logger.warn("Exception occured", fex);
            ws.send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
        } catch (DOMException dex) {
            logger.warn("DOMException occured.", dex);
            ws.send(MessageBuilder.status().code(422).message(dex.getMessage()).build(), true);
        }
    }
    try {
        app.delete(obj);
    } catch (FrameworkException fex) {
        logger.warn("Unable to delete node(s)", fex);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) DOMException(org.w3c.dom.DOMException) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedTreeNode(org.structr.core.entity.LinkedTreeNode) SecurityContext(org.structr.common.SecurityContext) DOMNode(org.structr.web.entity.dom.DOMNode) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 93 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class RemoveFromCollectionCommand method processMessage.

// ~--- methods --------------------------------------------------------
@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final String keyString = (String) webSocketData.getNodeData().get("key");
    if (keyString == null) {
        logger.error("Unable to remove given object from collection: key is null");
        getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }
    final String idToRemove = (String) webSocketData.getNodeData().get("idToRemove");
    if (idToRemove == null) {
        logger.error("Unable to remove given object from collection: idToRemove is null");
        getWebSocket().send(MessageBuilder.status().code(400).build(), true);
    }
    GraphObject obj = getNode(webSocketData.getId());
    if (obj != null) {
        if (!((AbstractNode) obj).isGranted(Permission.write, getWebSocket().getSecurityContext())) {
            getWebSocket().send(MessageBuilder.status().message("No write permission").code(400).build(), true);
            logger.warn("No write permission for {} on {}", new Object[] { getWebSocket().getCurrentUser().toString(), obj.toString() });
            return;
        }
    }
    if (obj == null) {
        // No node? Try to find relationship
        obj = getRelationship(webSocketData.getId());
    }
    GraphObject objToRemove = getNode(idToRemove);
    if (obj != null && objToRemove != null) {
        try {
            PropertyKey key = StructrApp.key(obj.getClass(), keyString);
            if (key != null) {
                List collection = (List) obj.getProperty(key);
                collection.remove(objToRemove);
                obj.setProperties(obj.getSecurityContext(), new PropertyMap(key, collection));
                if (obj instanceof NodeInterface) {
                    TransactionCommand.registerNodeCallback((NodeInterface) obj, callback);
                } else if (obj instanceof RelationshipInterface) {
                    TransactionCommand.registerRelCallback((RelationshipInterface) obj, callback);
                }
            }
        } catch (FrameworkException ex) {
            logger.error("Unable to set properties: {}", ((FrameworkException) ex).toString());
            getWebSocket().send(MessageBuilder.status().code(400).build(), true);
        }
    } else {
        logger.warn("Graph object with uuid {} not found.", webSocketData.getId());
        getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) FrameworkException(org.structr.common.error.FrameworkException) RelationshipInterface(org.structr.core.graph.RelationshipInterface) List(java.util.List) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface)

Example 94 with NodeInterface

use of org.structr.core.graph.NodeInterface in project structr by structr.

the class ValidationHelper method isValidUniqueProperty.

public static synchronized boolean isValidUniqueProperty(final GraphObject object, final PropertyKey key, final ErrorBuffer errorBuffer) {
    if (key != null) {
        final Object value = object.getProperty(key);
        if (value != null) {
            // validation will only be executed for non-null values
            List<GraphObject> result = null;
            // use declaring class for inheritance-aware uniqueness
            Class type = key.getDeclaringClass();
            if (type == null || (AbstractNode.name.equals(key) && NodeInterface.class.equals(type))) {
                // fallback: object type
                type = object.getClass();
            }
            try {
                if (object instanceof NodeInterface) {
                    result = StructrApp.getInstance().nodeQuery(type).and(key, value).getAsList();
                } else {
                    result = StructrApp.getInstance().relationshipQuery(type).and(key, value).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 UniqueToken(object.getType(), key, object.getUuid()));
                        // error!
                        return false;
                    }
                }
            }
        }
    }
    // no error
    return true;
}
Also used : UniqueToken(org.structr.common.error.UniqueToken) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) NodeInterface(org.structr.core.graph.NodeInterface)

Example 95 with NodeInterface

use of org.structr.core.graph.NodeInterface 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

NodeInterface (org.structr.core.graph.NodeInterface)186 FrameworkException (org.structr.common.error.FrameworkException)120 Tx (org.structr.core.graph.Tx)116 Test (org.junit.Test)81 PropertyKey (org.structr.core.property.PropertyKey)61 LinkedList (java.util.LinkedList)36 StructrTest (org.structr.common.StructrTest)29 PropertyMap (org.structr.core.property.PropertyMap)26 TestOne (org.structr.core.entity.TestOne)24 List (java.util.List)23 GraphObject (org.structr.core.GraphObject)22 App (org.structr.core.app.App)21 StructrApp (org.structr.core.app.StructrApp)21 GenericNode (org.structr.core.entity.GenericNode)21 Before (org.junit.Before)18 Result (org.structr.core.Result)18 AbstractRelationship (org.structr.core.entity.AbstractRelationship)16 Random (java.util.Random)15 RelationshipInterface (org.structr.core.graph.RelationshipInterface)14 ErrorToken (org.structr.common.error.ErrorToken)12