Search in sources :

Example 1 with Notion

use of org.structr.core.notion.Notion in project structr by structr.

the class TypeResource method identifyStartNode.

private NodeInterface identifyStartNode(final Relation template, final Map<String, Object> properties) throws FrameworkException {
    final Property<String> sourceIdProperty = template.getSourceIdProperty();
    final Class sourceType = template.getSourceType();
    final Notion notion = template.getStartNodeNotion();
    notion.setType(sourceType);
    PropertyKey startNodeIdentifier = notion.getPrimaryPropertyKey();
    if (startNodeIdentifier != null) {
        Object identifierValue = properties.get(startNodeIdentifier.dbName());
        properties.remove(sourceIdProperty.dbName());
        return (NodeInterface) notion.getAdapterForSetter(securityContext).adapt(identifierValue);
    }
    return null;
}
Also used : Notion(org.structr.core.notion.Notion) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface)

Example 2 with Notion

use of org.structr.core.notion.Notion in project structr by structr.

the class AggregatorProperty method getProperty.

@Override
public List<T> getProperty(SecurityContext securityContext, GraphObject currentObject, boolean applyConverter, final Predicate<GraphObject> predicate) {
    if (currentObject != null && currentObject instanceof AbstractNode) {
        NodeInterface sourceNode = (NodeInterface) currentObject;
        List<NodeInterface> nodes = new LinkedList<>();
        // 1. step: add all nodes
        for (Property property : aggregation.getAggregationProperties()) {
            Object obj = sourceNode.getProperty(property);
            if (obj != null && obj instanceof Collection) {
                nodes.addAll((Collection) obj);
            }
        }
        // 2. step: sort nodes according to comparator
        Comparator<NodeInterface> comparator = aggregation.getComparator();
        if (nodes.isEmpty() && comparator != null) {
            Collections.sort(nodes, comparator);
        }
        // 3. step: apply notions depending on type
        List results = new LinkedList();
        try {
            for (NodeInterface node : nodes) {
                Notion notion = aggregation.getNotionForType(node.getClass());
                if (notion != null) {
                    results.add(notion.getAdapterForGetter(securityContext).adapt(node));
                } else {
                    results.add(node);
                }
            }
        } catch (Throwable t) {
            logger.warn("", t);
        }
        return results;
    }
    return Collections.emptyList();
}
Also used : AbstractNode(org.structr.core.entity.AbstractNode) Collection(java.util.Collection) Notion(org.structr.core.notion.Notion) GraphObject(org.structr.core.GraphObject) List(java.util.List) LinkedList(java.util.LinkedList) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 3 with Notion

use of org.structr.core.notion.Notion in project structr by structr.

the class StaticRelationshipResource method doPost.

@Override
public RestMethodResult doPost(final Map<String, Object> propertySet) throws FrameworkException {
    final GraphObject sourceNode = typedIdResource.getEntity();
    RestMethodResult result = null;
    if (sourceNode != null && propertyKey != null && propertyKey instanceof RelationProperty) {
        final RelationProperty relationProperty = (RelationProperty) propertyKey;
        final Class sourceNodeType = sourceNode.getClass();
        NodeInterface newNode = null;
        if (propertyKey.isReadOnly()) {
            logger.info("Read-only property on {}: {}", new Object[] { sourceNodeType, typeResource.getRawType() });
            return null;
        }
        // fetch notion
        final Notion notion = relationProperty.getNotion();
        final PropertyKey primaryPropertyKey = notion.getPrimaryPropertyKey();
        // apply notion if the property set contains the ID property as the only element
        if (primaryPropertyKey != null && propertySet.containsKey(primaryPropertyKey.jsonName()) && propertySet.size() == 1) {
        // FIXME: what happens here?
        } else {
            // the notion can not deserialize objects with a single key, or the POSTed propertySet did not contain a key to deserialize,
            // so we create a new node from the POSTed properties and link the source node to it. (this is the "old" implementation)
            newNode = typeResource.createNode(propertySet);
            if (newNode != null) {
                relationProperty.addSingleElement(securityContext, sourceNode, newNode);
            }
        }
        if (newNode != null) {
            result = new RestMethodResult(HttpServletResponse.SC_CREATED);
            result.addHeader("Location", buildLocationHeader(newNode));
            return result;
        }
    } else {
        final Class entityType = typedIdResource.getTypeResource().getEntityClass();
        final String methodName = typeResource.getRawType();
        try {
            final String source = SchemaMethodResource.findMethodSource(entityType, methodName);
            result = SchemaMethodResource.invoke(securityContext, typedIdResource.getEntity(), source, propertySet, methodName);
        } catch (IllegalPathException ex) {
            // try direct invocation of the schema method on the node type
            try {
                result = SchemaMethodResource.wrapInResult(typedIdResource.getEntity().invokeMethod(methodName, propertySet, true));
            } catch (Throwable t) {
                logger.warn("Unable to execute {}.{}: {}", entityType.getSimpleName(), methodName, t.getMessage());
            }
        }
    }
    if (result == null) {
        throw new IllegalPathException("Illegal path");
    } else {
        return result;
    }
}
Also used : IllegalPathException(org.structr.rest.exception.IllegalPathException) RelationProperty(org.structr.core.property.RelationProperty) Notion(org.structr.core.notion.Notion) GraphObject(org.structr.core.GraphObject) RestMethodResult(org.structr.rest.RestMethodResult) NodeInterface(org.structr.core.graph.NodeInterface) PropertyKey(org.structr.core.property.PropertyKey)

Example 4 with Notion

use of org.structr.core.notion.Notion in project structr by structr.

the class TypeResource method identifyEndNode.

private NodeInterface identifyEndNode(final Relation template, final Map<String, Object> properties) throws FrameworkException {
    final Property<String> targetIdProperty = template.getTargetIdProperty();
    final Class targetType = template.getTargetType();
    final Notion notion = template.getEndNodeNotion();
    notion.setType(targetType);
    final PropertyKey endNodeIdentifier = notion.getPrimaryPropertyKey();
    if (endNodeIdentifier != null) {
        Object identifierValue = properties.get(endNodeIdentifier.dbName());
        properties.remove(targetIdProperty.dbName());
        return (NodeInterface) notion.getAdapterForSetter(securityContext).adapt(identifierValue);
    }
    return null;
}
Also used : Notion(org.structr.core.notion.Notion) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface)

Aggregations

GraphObject (org.structr.core.GraphObject)4 NodeInterface (org.structr.core.graph.NodeInterface)4 Notion (org.structr.core.notion.Notion)4 PropertyKey (org.structr.core.property.PropertyKey)3 Collection (java.util.Collection)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 AbstractNode (org.structr.core.entity.AbstractNode)1 RelationProperty (org.structr.core.property.RelationProperty)1 RestMethodResult (org.structr.rest.RestMethodResult)1 IllegalPathException (org.structr.rest.exception.IllegalPathException)1