Search in sources :

Example 26 with GraphObjectMap

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

the class GetFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    final SecurityContext securityContext = ctx.getSecurityContext();
    try {
        if (!arrayHasLengthAndAllElementsNotNull(sources, 2)) {
            return "";
        }
        final String keyName = sources[1].toString();
        GraphObject dataObject = null;
        // handle GraphObject
        if (sources[0] instanceof GraphObject) {
            dataObject = (GraphObject) sources[0];
        }
        // handle first element of a list of graph objects
        if (sources[0] instanceof List) {
            final List list = (List) sources[0];
            final int size = list.size();
            if (size == 1) {
                final Object value = list.get(0);
                if (value != null) {
                    if (value instanceof GraphObject) {
                        dataObject = (GraphObject) list.get(0);
                    } else {
                        return "get(): first element of collection is of type " + value.getClass() + " which is not supported.";
                    }
                } else {
                    return "get(): first element of collection is null.";
                }
            }
        }
        // handle map separately
        if (sources[0] instanceof Map && !(sources[0] instanceof GraphObjectMap)) {
            final Map map = (Map) sources[0];
            return map.get(keyName);
        }
        // handle request object
        if (sources[0] instanceof HttpServletRequest) {
            final HttpServletRequest request = (HttpServletRequest) sources[0];
            return request.getParameter(keyName);
        }
        if (dataObject != null) {
            final PropertyKey key = StructrApp.key(dataObject.getClass(), keyName);
            if (key != null) {
                final PropertyConverter inputConverter = key.inputConverter(securityContext);
                Object value = dataObject.getProperty(key);
                if (inputConverter != null) {
                    return inputConverter.revert(value);
                }
                return dataObject.getProperty(key);
            }
            return "";
        } else {
            return ERROR_MESSAGE_GET_ENTITY;
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) PropertyConverter(org.structr.core.converter.PropertyConverter) List(java.util.List) GraphObject(org.structr.core.GraphObject) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey)

Example 27 with GraphObjectMap

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

the class KeysFunction method apply.

@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
    try {
        if (sources == null) {
            throw new IllegalArgumentException();
        }
        if (sources.length == 2 && sources[0] != null && sources[0] instanceof GraphObject && sources[1] != null) {
            final Set<String> keys = new LinkedHashSet<>();
            final GraphObject source = (GraphObject) sources[0];
            for (final PropertyKey key : source.getPropertyKeys(sources[1].toString())) {
                keys.add(key.jsonName());
            }
            return new LinkedList<>(keys);
        } else if (sources.length == 1 && sources[0] != null && sources[0] instanceof GraphObjectMap) {
            return new LinkedList<>(((GraphObjectMap) sources[0]).keySet());
        } else if (sources.length == 1 && sources[0] != null && sources[0] instanceof Map) {
            return new LinkedList<>(((Map) sources[0]).keySet());
        } else {
            return null;
        }
    } catch (final IllegalArgumentException e) {
        logParameterError(caller, sources, ctx.isJavaScriptContext());
        return usage(ctx.isJavaScriptContext());
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) GraphObjectMap(org.structr.core.GraphObjectMap) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) PropertyKey(org.structr.core.property.PropertyKey) LinkedList(java.util.LinkedList)

Example 28 with GraphObjectMap

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

the class CypherTest method testCypherResultWrapping.

@Test
public void testCypherResultWrapping() {
    try (final Tx tx = app.tx()) {
        List<TestOne> testOnes = createTestNodes(TestOne.class, 10);
        List<TestSix> testSixs = createTestNodes(TestSix.class, 10);
        for (final TestOne testOne : testOnes) {
            testOne.setProperty(TestOne.manyToManyTestSixs, testSixs);
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.warn("", ex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH (n:TestOne) RETURN DISTINCT n");
        assertEquals("Invalid wrapped cypher query result", 10, result.size());
        for (final GraphObject obj : result) {
            System.out.println(obj);
            assertEquals("Invalid wrapped cypher query result", TestOne.class, obj.getClass());
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH (n:TestOne)-[r]-(m:TestSix) RETURN DISTINCT  n, r, m ");
        final Iterator<GraphObject> it = result.iterator();
        assertEquals("Invalid wrapped cypher query result", 300, result.size());
        while (it.hasNext()) {
            // n
            assertEquals("Invalid wrapped cypher query result", TestOne.class, it.next().getClass());
            // r
            assertEquals("Invalid wrapped cypher query result", SixOneManyToMany.class, it.next().getClass());
            // m
            assertEquals("Invalid wrapped cypher query result", TestSix.class, it.next().getClass());
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN p ");
        assertEquals("Invalid wrapped cypher query result", 100, result.size());
        for (final GraphObject obj : result) {
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN { nodes: nodes(p), rels: relationships(p) } ");
        assertEquals("Invalid wrapped cypher query result", 100, result.size());
        for (final GraphObject obj : result) {
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
            final Object nodes = obj.getProperty(new StringProperty("nodes"));
            final Object rels = obj.getProperty(new StringProperty("rels"));
            assertTrue("Invalid wrapped cypher query result", nodes instanceof Collection);
            assertTrue("Invalid wrapped cypher query result", rels instanceof Collection);
            final Iterator it = ((Collection) nodes).iterator();
            while (it.hasNext()) {
                assertEquals("Invalid wrapped cypher query result", TestOne.class, it.next().getClass());
                assertEquals("Invalid wrapped cypher query result", TestSix.class, it.next().getClass());
            }
            for (final Object node : ((Collection) rels)) {
                assertEquals("Invalid wrapped cypher query result", SixOneManyToMany.class, node.getClass());
            }
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN DISTINCT { path: p, value: 12 } ");
        assertEquals("Invalid wrapped cypher query result", 100, result.size());
        final Iterator it = result.iterator();
        while (it.hasNext()) {
            final Object path = it.next();
            final Object value = it.next();
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, path.getClass());
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, value.getClass());
            assertEquals("Invalid wrapped cypher query result", 12L, ((GraphObjectMap) value).getProperty(new StringProperty("value")));
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN { nodes: { x : { y : { z : nodes(p) } } } } ");
        assertEquals("Invalid wrapped cypher query result", 100, result.size());
        for (final GraphObject obj : result) {
            assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());
            final Object nodes = obj.getProperty(new StringProperty("nodes"));
            assertTrue("Invalid wrapped cypher query result", nodes instanceof GraphObjectMap);
            final Object x = ((GraphObjectMap) nodes).getProperty(new StringProperty("x"));
            assertTrue("Invalid wrapped cypher query result", x instanceof GraphObjectMap);
            final Object y = ((GraphObjectMap) x).getProperty(new StringProperty("y"));
            assertTrue("Invalid wrapped cypher query result", y instanceof GraphObjectMap);
            final Object z = ((GraphObjectMap) y).getProperty(new StringProperty("z"));
            assertTrue("Invalid wrapped cypher query result", z instanceof Collection);
        }
        tx.success();
    } catch (FrameworkException ex) {
        logger.error("", ex);
    }
/*
		try (final Tx tx = app.tx()) {

			final List<GraphObject> result = app.command(CypherQueryCommand.class).execute("MATCH p = (n:TestOne)-[r]-(m:TestSix) RETURN p");

			assertEquals("Invalid wrapped cypher query result", 100, result.size());

			for (final GraphObject obj : result) {

				assertEquals("Invalid wrapped cypher query result", GraphObjectMap.class, obj.getClass());

				final Object paths = obj.getProperty(new StringProperty("p"));

				assertTrue("Invalid wrapped cypher query result", paths instanceof Iterable);

				final Iterator it = ((Iterable)paths).iterator();
				while (it.hasNext()) {

					assertEquals("Invalid wrapped cypher query result", TestOne.class, it.next().getClass());		// n
					assertEquals("Invalid wrapped cypher query result", SixOneManyToMany.class, it.next().getClass());	// r
					assertEquals("Invalid wrapped cypher query result", TestSix.class, it.next().getClass());		// m
				}
			}

			tx.success();


		} catch (FrameworkException ex) {
			logger.error("", ex);
		}
		*/
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) GraphObject(org.structr.core.GraphObject) GraphObjectMap(org.structr.core.GraphObjectMap) Iterator(java.util.Iterator) Collection(java.util.Collection) GraphObject(org.structr.core.GraphObject) TestOne(org.structr.core.entity.TestOne) CypherQueryCommand(org.structr.core.graph.CypherQueryCommand) TestSix(org.structr.core.entity.TestSix) Test(org.junit.Test)

Example 29 with GraphObjectMap

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

the class AbstractHintProvider method getHints.

public List<GraphObject> getHints(final GraphObject currentEntity, final String type, final String currentToken, final String previousToken, final String thirdToken, final int cursorLine, final int cursorPosition) {
    final List<Hint> allHints = getAllHints(currentEntity, currentToken, previousToken, thirdToken);
    final List<GraphObject> hints = new LinkedList<>();
    int maxNameLength = 0;
    if (StringUtils.isBlank(currentToken) || startChars.contains(currentToken)) {
        // display all possible hints
        for (final Hint hint : allHints) {
            final GraphObjectMap item = new GraphObjectMap();
            String functionName = getFunctionName(hint.getReplacement());
            if (hint.mayModify()) {
                item.put(text, visitReplacement(functionName));
            } else {
                item.put(text, functionName);
            }
            item.put(displayText, getFunctionName(hint.getName()) + " - " + textOrPlaceholder(hint.shortDescription()));
            addPosition(item, hint, cursorLine, cursorPosition, cursorPosition);
            if (functionName.length() > maxNameLength) {
                maxNameLength = functionName.length();
            }
            hints.add(item);
        }
    } else {
        final int currentTokenLength = currentToken.length();
        for (final Hint hint : allHints) {
            final String functionName = getFunctionName(hint.getName());
            final String replacement = hint.getReplacement();
            if (functionName.startsWith(currentToken) || (currentToken.length() > 2 && functionName.contains(currentToken))) {
                final GraphObjectMap item = new GraphObjectMap();
                if (hint.mayModify()) {
                    item.put(text, visitReplacement(replacement));
                } else {
                    item.put(text, replacement);
                }
                item.put(displayText, getFunctionName(hint.getName()) + " - " + textOrPlaceholder(hint.shortDescription()));
                addPosition(item, hint, cursorLine, cursorPosition - currentTokenLength, cursorPosition);
                if (functionName.length() > maxNameLength) {
                    maxNameLength = functionName.length();
                }
                hints.add(item);
            }
        }
    }
    alignHintDescriptions(hints, maxNameLength);
    return hints;
}
Also used : Hint(org.structr.schema.action.Hint) GraphObjectMap(org.structr.core.GraphObjectMap) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) Hint(org.structr.schema.action.Hint)

Example 30 with GraphObjectMap

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

the class AbstractHintProvider method addPosition.

// ----- private methods -----
private void addPosition(final GraphObjectMap item, final Hint hint, final int cursorLine, final int replaceFrom, final int replaceTo) {
    final GraphObjectMap fromObject = new GraphObjectMap();
    final GraphObjectMap toObject = new GraphObjectMap();
    fromObject.put(line, cursorLine);
    fromObject.put(ch, replaceFrom);
    toObject.put(line, cursorLine);
    toObject.put(ch, replaceTo);
    item.put(from, fromObject);
    item.put(to, toObject);
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap)

Aggregations

GraphObjectMap (org.structr.core.GraphObjectMap)60 LinkedList (java.util.LinkedList)27 GraphObject (org.structr.core.GraphObject)24 Map (java.util.Map)18 FrameworkException (org.structr.common.error.FrameworkException)16 StringProperty (org.structr.core.property.StringProperty)15 GenericProperty (org.structr.core.property.GenericProperty)13 PropertyKey (org.structr.core.property.PropertyKey)12 List (java.util.List)11 Result (org.structr.core.Result)10 SecurityContext (org.structr.common.SecurityContext)8 IntProperty (org.structr.core.property.IntProperty)8 RestMethodResult (org.structr.rest.RestMethodResult)8 ArrayList (java.util.ArrayList)6 Collection (java.util.Collection)6 Test (org.junit.Test)6 ConfigurationProvider (org.structr.schema.ConfigurationProvider)6 HashMap (java.util.HashMap)5 LinkedHashSet (java.util.LinkedHashSet)5 Tx (org.structr.core.graph.Tx)5