Search in sources :

Example 81 with GraphObject

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

the class CreateRelationshipCommand method createRelationship.

private synchronized <A extends NodeInterface, B extends NodeInterface, R extends Relation<A, B, ?, ?>> R createRelationship(final A fromNode, final B toNode, final Class<R> relType, final PropertyMap attributes) throws FrameworkException {
    // disable updating access time when creating relationships
    securityContext.disableModificationOfAccessTime();
    final RelationshipFactory<R> factory = new RelationshipFactory(securityContext);
    final PropertyMap properties = new PropertyMap(attributes);
    final CreationContainer tmp = new CreationContainer();
    final R template = instantiate(relType);
    final Node startNode = fromNode.getNode();
    final Node endNode = toNode.getNode();
    final Date now = new Date();
    final Principal user = securityContext.getCachedUser();
    template.ensureCardinality(securityContext, fromNode, toNode);
    // date properties need converter
    AbstractRelationship.createdDate.setProperty(securityContext, tmp, now);
    AbstractRelationship.lastModifiedDate.setProperty(securityContext, tmp, now);
    // set initial properties manually (caution, this can only be used for primitive properties!)
    tmp.getData().put(GraphObject.id.jsonName(), getNextUuid());
    tmp.getData().put(GraphObject.type.jsonName(), relType.getSimpleName());
    tmp.getData().put(AbstractRelationship.relType.jsonName(), template.name());
    tmp.getData().put(AbstractRelationship.sourceId.jsonName(), fromNode.getUuid());
    tmp.getData().put(AbstractRelationship.targetId.jsonName(), toNode.getUuid());
    tmp.getData().put(AbstractRelationship.visibleToPublicUsers.jsonName(), false);
    tmp.getData().put(AbstractRelationship.visibleToAuthenticatedUsers.jsonName(), false);
    tmp.getData().put(AbstractRelationship.cascadeDelete.jsonName(), template.getCascadingDeleteFlag());
    if (user != null) {
        tmp.getData().put(AbstractRelationship.createdBy.jsonName(), user.getUuid());
    }
    // create relationship including initial properties
    final Relationship rel = startNode.createRelationshipTo(endNode, template, tmp.getData());
    final R newRel = factory.instantiateWithType(rel, relType, null, true);
    if (newRel != null) {
        newRel.setProperties(securityContext, properties);
        // notify transaction handler
        TransactionCommand.relationshipCreated(user, newRel);
        // notify relationship of its creation
        newRel.onRelationshipCreation();
        // iterate post creation transformations
        for (Transformation<GraphObject> transformation : StructrApp.getConfiguration().getEntityCreationTransformations(newRel.getClass())) {
            transformation.apply(securityContext, newRel);
        }
    }
    // enable access time update again for subsequent calls
    securityContext.enableModificationOfAccessTime();
    return newRel;
}
Also used : PropertyMap(org.structr.core.property.PropertyMap) Node(org.structr.api.graph.Node) AbstractRelationship(org.structr.core.entity.AbstractRelationship) Relationship(org.structr.api.graph.Relationship) GraphObject(org.structr.core.GraphObject) Date(java.util.Date) Principal(org.structr.core.entity.Principal)

Example 82 with GraphObject

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

the class CypherQueryCommand method execute.

public List<GraphObject> execute(String query, Map<String, Object> parameters, boolean includeHiddenAndDeleted, boolean publicOnly) throws FrameworkException {
    DatabaseService graphDb = (DatabaseService) arguments.get("graphDb");
    RelationshipFactory relFactory = new RelationshipFactory(securityContext);
    NodeFactory nodeFactory = new NodeFactory(securityContext);
    List<GraphObject> resultList = new LinkedList<>();
    // graphdb can be null..
    if (graphDb != null) {
        try (final NativeResult result = graphDb.execute(query, parameters != null ? parameters : Collections.emptyMap())) {
            while (result.hasNext()) {
                final Map<String, Object> row = result.next();
                for (Entry<String, Object> entry : row.entrySet()) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    final Object obj = handleObject(nodeFactory, relFactory, key, value, includeHiddenAndDeleted, publicOnly, 0);
                    if (obj != null) {
                        if (obj instanceof GraphObject) {
                            resultList.add((GraphObject) obj);
                        } else if (obj instanceof Collection) {
                            final List<Object> nonGraphObjectResult = new LinkedList<>();
                            for (final Object item : ((Collection) obj)) {
                                if (item instanceof GraphObject) {
                                    resultList.add((GraphObject) item);
                                } else {
                                    nonGraphObjectResult.add(item);
                                }
                            }
                            if (!nonGraphObjectResult.isEmpty()) {
                                // Wrap non-graph-objects in simple list
                                final GraphObjectMap graphObject = new GraphObjectMap();
                                graphObject.setProperty(new GenericProperty(key), nonGraphObjectResult);
                                resultList.add(graphObject);
                            }
                        } else {
                            logger.warn("Unable to handle Cypher query result object of type {}, ignoring.", obj.getClass().getName());
                        }
                    }
                }
            }
        }
    }
    return resultList;
}
Also used : NativeResult(org.structr.api.NativeResult) DatabaseService(org.structr.api.DatabaseService) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) List(java.util.List) LinkedList(java.util.LinkedList)

Example 83 with GraphObject

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

the class ReplaceFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
            return null;
        }
        final String template = sources[0].toString();
        GraphObject node = null;
        if (sources[1] instanceof GraphObject) {
            node = (GraphObject) sources[1];
        }
        if (sources[1] instanceof List) {
            final List list = (List) sources[1];
            if (list.size() == 1 && list.get(0) instanceof GraphObject) {
                node = (GraphObject) list.get(0);
            }
        }
        if (node != null) {
            // recursive replacement call, be careful here
            return Scripting.replaceVariables(ctx, node, template);
        }
        return "";
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : List(java.util.List) GraphObject(org.structr.core.GraphObject)

Example 84 with GraphObject

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

the class SortFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    if (sources == null || sources.length == 0) {
        return null;
    }
    // Default sort key
    String sortKey = "name";
    if (sources.length > 1 && sources[1] instanceof String) {
        sortKey = (String) sources[1];
    }
    if (sources.length >= 1) {
        if (sources[0] instanceof List) {
            final List list = (List) sources[0];
            final Iterator iterator = list.iterator();
            if (iterator.hasNext()) {
                final Object firstElement = iterator.next();
                if (firstElement instanceof GraphObject) {
                    final Class type = firstElement.getClass();
                    final PropertyKey key = StructrApp.key(type, sortKey);
                    final boolean descending = sources.length == 3 && sources[2] != null && "true".equals(sources[2].toString());
                    if (key != null) {
                        List<GraphObject> sortCollection = (List<GraphObject>) list;
                        Collections.sort(sortCollection, new GraphObjectComparator(key, descending));
                        return sortCollection;
                    }
                } else if (firstElement instanceof String) {
                    final String[] stringArray = (String[]) list.toArray(new String[list.size()]);
                    Arrays.sort(stringArray);
                    return Arrays.asList(stringArray);
                }
            }
        }
    } else {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
    }
    return sources[0];
}
Also used : Iterator(java.util.Iterator) List(java.util.List) GraphObject(org.structr.core.GraphObject) GraphObjectComparator(org.structr.common.GraphObjectComparator) GraphObject(org.structr.core.GraphObject) PropertyKey(org.structr.core.property.PropertyKey)

Example 85 with GraphObject

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

the class StructrPropertyValueChannel method getConvertedPropertyValue.

// ----- public static methods -----
public static String getConvertedPropertyValue(final SecurityContext securityContext, final GraphObject graphObject, final PropertyKey key) throws IOException, FrameworkException {
    final PropertyConverter inputConverter = key.inputConverter(securityContext);
    Object actualValue = graphObject.getProperty(key);
    if (inputConverter != null) {
        actualValue = inputConverter.revert(actualValue);
    }
    if (actualValue != null) {
        return actualValue.toString();
    }
    return "";
}
Also used : PropertyConverter(org.structr.core.converter.PropertyConverter) 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