Search in sources :

Example 71 with GraphObject

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

the class ListUnattachedNodesCommand method getUnattachedNodes.

/**
 * Return list of nodes which are not attached to a page and have no
 * parent element (no incoming CONTAINS rel)
 *
 * @param app
 * @param securityContext
 * @param webSocketData
 * @return
 * @throws FrameworkException
 */
protected static List<NodeInterface> getUnattachedNodes(final App app, final SecurityContext securityContext, final WebSocketMessage webSocketData) throws FrameworkException {
    final String sortOrder = webSocketData.getSortOrder();
    final String sortKey = webSocketData.getSortKey();
    Query query;
    if (sortKey != null) {
        final PropertyKey sortProperty = StructrApp.key(DOMNode.class, sortKey);
        query = StructrApp.getInstance(securityContext).nodeQuery().includeDeletedAndHidden().sort(sortProperty).order("desc".equals(sortOrder));
    } else {
        query = StructrApp.getInstance(securityContext).nodeQuery().includeDeletedAndHidden();
    }
    query.orTypes(DOMElement.class);
    query.orType(Content.class);
    query.orType(Template.class);
    // do search
    final List<NodeInterface> filteredResults = new LinkedList();
    List<? extends GraphObject> resultList = null;
    try (final Tx tx = app.tx()) {
        resultList = query.getAsList();
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
    }
    if (resultList != null) {
        // determine which of the nodes have no incoming CONTAINS relationships and no page id
        for (GraphObject obj : resultList) {
            if (obj instanceof DOMNode) {
                DOMNode node = (DOMNode) obj;
                if (!node.hasIncomingRelationships(node.getChildLinkType()) && node.getOwnerDocument() == null && !(node instanceof ShadowDocument)) {
                    filteredResults.add(node);
                }
            }
        }
    }
    return filteredResults;
}
Also used : Query(org.structr.core.app.Query) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) GraphObject(org.structr.core.GraphObject) DOMNode(org.structr.web.entity.dom.DOMNode) ShadowDocument(org.structr.web.entity.dom.ShadowDocument) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 72 with GraphObject

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

the class AutocompleteCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final Map<String, Object> data = webSocketData.getNodeData();
    final String id = webSocketData.getId();
    final List<GraphObject> result = new LinkedList<>();
    final String contentType = getOrDefault(data.get("contentType"), "text/plain");
    if (contentType != null) {
        final AbstractHintProvider hintProvider = hintProviders.get(contentType);
        if (hintProvider != null) {
            final String currentToken = getAndTrim(data.get("currentToken"));
            final String previousToken = getAndTrim(data.get("previousToken"));
            final String thirdToken = getAndTrim(data.get("thirdToken"));
            final String type = getAndTrim(data.get("type"));
            final int cursorPosition = getInt(data.get("cursorPosition"));
            final int line = getInt(data.get("line"));
            try {
                final List<GraphObject> hints = hintProvider.getHints(StructrApp.getInstance().get(AbstractNode.class, id), type, currentToken, previousToken, thirdToken, line, cursorPosition);
                result.addAll(hints);
            } catch (FrameworkException fex) {
                logger.warn("", fex);
            }
        } else {
            logger.warn("No HintProvider for content type {}, ignoring.", contentType);
        }
    } else {
        logger.warn("No content type for AutocompleteCommand, ignoring.");
    }
    // set full result list
    webSocketData.setResult(result);
    webSocketData.setRawResultCount(result.size());
    getWebSocket().send(webSocketData, true);
}
Also used : FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) GraphObject(org.structr.core.GraphObject) AbstractHintProvider(org.structr.autocomplete.AbstractHintProvider) GraphObject(org.structr.core.GraphObject)

Example 73 with GraphObject

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

the class FindDuplicateFilesCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final PropertyKey<String> path = StructrApp.key(AbstractFile.class, "path");
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final Query query = StructrApp.getInstance(securityContext).nodeQuery(AbstractFile.class).sort(path);
    try {
        AbstractFile lastFile = null;
        String lastFilepath = null;
        boolean lastWasDupe = false;
        final ArrayList<GraphObject> filesWithSamePath = new ArrayList<>();
        final Iterator it = query.getAsList().iterator();
        while (it.hasNext()) {
            final AbstractNode node = (AbstractNode) it.next();
            try {
                final AbstractFile file = (AbstractFile) node;
                final String currentFilepath = file.getProperty(path);
                // skip the first file as we can not compare it to the previous one
                if (lastFile != null) {
                    if (currentFilepath.equals(lastFilepath)) {
                        if (!lastWasDupe) {
                            // if this is the first duplicate found we need to add both files
                            filesWithSamePath.add(lastFile);
                        }
                        filesWithSamePath.add(file);
                        lastWasDupe = true;
                    } else {
                        lastWasDupe = false;
                    }
                }
                lastFilepath = currentFilepath;
                lastFile = file;
            } catch (ClassCastException cce) {
                logger.warn("Tried casting node '{}' of type '{}' to AbstractFile. Most likely a node type inheriting from File was deleted and an instance remains. Please delete this node or change its type.", node.getUuid(), node.getType());
            }
        }
        // set full result list
        webSocketData.setResult(filesWithSamePath);
        webSocketData.setRawResultCount(filesWithSamePath.size());
        // send only over local connection
        getWebSocket().send(webSocketData, true);
    } catch (FrameworkException fex) {
        logger.warn("Exception occured", fex);
        getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
    }
}
Also used : AbstractFile(org.structr.web.entity.AbstractFile) Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) ArrayList(java.util.ArrayList) GraphObject(org.structr.core.GraphObject) SecurityContext(org.structr.common.SecurityContext) Iterator(java.util.Iterator)

Example 74 with GraphObject

use of org.structr.core.GraphObject 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 75 with GraphObject

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

the class GetCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String nodeId = (String) webSocketData.getNodeData().get("nodeId");
    final String properties = (String) webSocketData.getNodeData().get("properties");
    if (properties != null) {
        securityContext.setCustomView(StringUtils.split(properties, ","));
    }
    final GraphObject graphObject = getGraphObject(webSocketData.getId(), nodeId);
    if (graphObject != null) {
        webSocketData.setResult(Arrays.asList(graphObject));
        // send only over local connection (no broadcast)
        getWebSocket().send(webSocketData, true);
    } else {
    // logger.warn("Node not found for id {}!", webSocketData.getId());
    // Not necessary to send a 404 here
    // getWebSocket().send(MessageBuilder.status().code(404).build(), true);
    }
}
Also used : SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject)

Aggregations

GraphObject (org.structr.core.GraphObject)151 FrameworkException (org.structr.common.error.FrameworkException)58 PropertyKey (org.structr.core.property.PropertyKey)39 LinkedList (java.util.LinkedList)35 SecurityContext (org.structr.common.SecurityContext)25 App (org.structr.core.app.App)25 StructrApp (org.structr.core.app.StructrApp)24 Tx (org.structr.core.graph.Tx)23 List (java.util.List)22 PropertyConverter (org.structr.core.converter.PropertyConverter)22 AbstractNode (org.structr.core.entity.AbstractNode)18 GraphObjectMap (org.structr.core.GraphObjectMap)17 NodeInterface (org.structr.core.graph.NodeInterface)17 LinkedHashSet (java.util.LinkedHashSet)15 PropertyMap (org.structr.core.property.PropertyMap)13 Map (java.util.Map)12 RestMethodResult (org.structr.rest.RestMethodResult)11 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 Result (org.structr.core.Result)9 ArrayList (java.util.ArrayList)8