Search in sources :

Example 86 with PropertyKey

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

the class ListSchemaPropertiesCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final String view = (String) webSocketData.getNodeData().get("view");
    final String id = webSocketData.getId();
    final List<GraphObject> result = new LinkedList();
    if (view != null) {
        if (id != null) {
            AbstractNode schemaObject = getNode(id);
            if (schemaObject != null) {
                final ConfigurationProvider config = StructrApp.getConfiguration();
                String typeName = schemaObject.getProperty(AbstractNode.name);
                if (typeName == null && schemaObject instanceof SchemaRelationshipNode) {
                    typeName = ((SchemaRelationshipNode) schemaObject).getClassName();
                }
                Class type = config.getNodeEntityClass(typeName);
                if (type == null || GenericNode.class.equals(type)) {
                    type = config.getRelationshipEntityClass(typeName);
                }
                if (type != null) {
                    final Set<PropertyKey> allProperties = config.getPropertySet(type, PropertyView.All);
                    final Set<PropertyKey> viewProperties = config.getPropertySet(type, view);
                    final Set<PropertyKey> parentProperties = config.getPropertySet(type.getSuperclass(), view);
                    for (final PropertyKey key : allProperties) {
                        final String declaringClass = key.getDeclaringClass() != null ? key.getDeclaringClass().getSimpleName() : "GraphObject";
                        final String propertyName = key.jsonName();
                        final GraphObjectMap property = new GraphObjectMap();
                        final Class valueType = key.valueType();
                        String valueTypeName = "Unknown";
                        boolean _isDisabled = false;
                        if (valueType != null) {
                            valueTypeName = valueType.getSimpleName();
                        }
                        // (since it has to be configured there instead of locally)
                        if (parentProperties.contains(key)) {
                            _isDisabled = true;
                        }
                        property.put(AbstractNode.name, propertyName);
                        property.put(isSelected, viewProperties.contains(key));
                        property.put(isDisabled, _isDisabled);
                        property.put(SchemaProperty.propertyType, valueTypeName);
                        property.put(SchemaProperty.notNull, key.isNotNull());
                        property.put(SchemaProperty.unique, key.isUnique());
                        property.put(SchemaProperty.isDynamic, key.isDynamic());
                        property.put(SchemaProperty.declaringClass, declaringClass);
                        // store in result
                        result.add(property);
                    }
                } else {
                    getWebSocket().send(MessageBuilder.status().code(404).message("Type " + typeName + " not found.").build(), true);
                }
            } else {
                getWebSocket().send(MessageBuilder.status().code(404).message("Schema node with ID " + id + " not found.").build(), true);
            }
        } else {
            getWebSocket().send(MessageBuilder.status().code(422).message("LIST_SCHEMA_PROPERTIES needs an ID.").build(), true);
        }
    } else {
        getWebSocket().send(MessageBuilder.status().code(422).message("LIST_SCHEMA_PROPERTIES needs a view name in nodeData.").build(), true);
    }
    webSocketData.setView(PropertyView.Ui);
    webSocketData.setResult(result);
    webSocketData.setRawResultCount(1);
    // send only over local connection
    getWebSocket().send(webSocketData, true);
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GenericNode(org.structr.core.entity.GenericNode) GraphObject(org.structr.core.GraphObject) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 87 with PropertyKey

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

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

the class GetByTypeCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final String rawType = (String) webSocketData.getNodeData().get("type");
    final String properties = (String) webSocketData.getNodeData().get("properties");
    final boolean includeDeletedAndHidden = (Boolean) webSocketData.getNodeData().get("includeDeletedAndHidden");
    final Class type = SchemaHelper.getEntityClassForRawType(rawType);
    if (type == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Type " + rawType + " not found").build(), true);
        return;
    }
    if (properties != null) {
        securityContext.setCustomView(StringUtils.split(properties, ","));
    }
    final String sortOrder = webSocketData.getSortOrder();
    final String sortKey = webSocketData.getSortKey();
    final int pageSize = webSocketData.getPageSize();
    final int page = webSocketData.getPage();
    final Query query = StructrApp.getInstance(securityContext).nodeQuery(type).includeDeletedAndHidden(includeDeletedAndHidden);
    if (sortKey != null) {
        final PropertyKey sortProperty = StructrApp.key(type, sortKey);
        if (sortProperty != null) {
            query.sort(sortProperty).order("desc".equals(sortOrder));
        }
    }
    // for image lists, suppress thumbnails
    if (type.equals(Image.class)) {
        query.and(StructrApp.key(Image.class, "isThumbnail"), false);
    }
    try {
        // do search
        Result result = query.getResult();
        // save raw result count
        int resultCountBeforePaging = result.size();
        // set full result list
        webSocketData.setResult(PagingHelper.subList(result.getResults(), pageSize, page));
        webSocketData.setRawResultCount(resultCountBeforePaging);
        // 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 : Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) SecurityContext(org.structr.common.SecurityContext) Image(org.structr.web.entity.Image) PropertyKey(org.structr.core.property.PropertyKey) Result(org.structr.core.Result)

Example 89 with PropertyKey

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

the class QueryCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final Map<String, Object> nodeData = webSocketData.getNodeData();
    final String rawType = (String) nodeData.get("type");
    final String properties = (String) nodeData.get("properties");
    final Boolean exact = (Boolean) nodeData.get("exact");
    final Class type = SchemaHelper.getEntityClassForRawType(rawType);
    if (type == null) {
        getWebSocket().send(MessageBuilder.status().code(404).message("Type " + rawType + " not found").build(), true);
        return;
    }
    final String sortKey = webSocketData.getSortKey();
    final int pageSize = webSocketData.getPageSize();
    final int page = webSocketData.getPage();
    final Query query = StructrApp.getInstance(securityContext).nodeQuery(type).page(page).pageSize(pageSize);
    if (sortKey != null) {
        final PropertyKey sortProperty = StructrApp.key(type, sortKey);
        final String sortOrder = webSocketData.getSortOrder();
        query.sort(sortProperty).order("desc".equals(sortOrder));
    }
    if (properties != null) {
        try {
            final Gson gson = new GsonBuilder().create();
            final Map<String, Object> querySource = gson.fromJson(properties, new TypeToken<Map<String, Object>>() {
            }.getType());
            final PropertyMap queryMap = PropertyMap.inputTypeToJavaType(securityContext, type, querySource);
            final boolean inexactQuery = exact != null && exact == false;
            // add properties to query
            for (final Entry<PropertyKey, Object> entry : queryMap.entrySet()) {
                query.and(entry.getKey(), entry.getValue(), !inexactQuery);
            }
        } catch (FrameworkException fex) {
            logger.warn("Exception occured", fex);
            getWebSocket().send(MessageBuilder.status().code(fex.getStatus()).message(fex.getMessage()).build(), true);
            return;
        }
    }
    try {
        // do search
        final Result result = query.getResult();
        // save raw result count
        // filteredResults.size();
        int resultCountBeforePaging = result.getRawResultCount();
        // set full result list
        webSocketData.setResult(result.getResults());
        webSocketData.setRawResultCount(resultCountBeforePaging);
        // 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 : Query(org.structr.core.app.Query) FrameworkException(org.structr.common.error.FrameworkException) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Result(org.structr.core.Result) PropertyMap(org.structr.core.property.PropertyMap) TypeToken(com.google.gson.reflect.TypeToken) SecurityContext(org.structr.common.SecurityContext) PropertyKey(org.structr.core.property.PropertyKey)

Example 90 with PropertyKey

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

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