Search in sources :

Example 51 with GraphObjectMap

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

the class LocalizeFunction method getLocalizedList.

public static List getLocalizedList(final Locale locale, final List<String> keyList, final String domain) throws FrameworkException {
    final ArrayList<GraphObjectMap> resultList = new ArrayList();
    for (final String key : keyList) {
        final GraphObjectMap localizedEntry = new GraphObjectMap();
        resultList.add(localizedEntry);
        localizedEntry.put(new StringProperty("name"), key);
        if (domain == null) {
            localizedEntry.put(new StringProperty("localizedName"), getLocalization(locale, key));
        } else {
            localizedEntry.put(new StringProperty("localizedName"), getLocalization(locale, key, domain));
        }
    }
    return resultList;
}
Also used : GraphObjectMap(org.structr.core.GraphObjectMap) ArrayList(java.util.ArrayList) StringProperty(org.structr.core.property.StringProperty)

Example 52 with GraphObjectMap

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

the class AdvancedSchemaTest method test04SchemaPropertyOrderInBuiltInViews.

@Test
public void test04SchemaPropertyOrderInBuiltInViews() {
    try (final Tx tx = app.tx()) {
        createAdminUser();
        createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    final GenericProperty jsonName = new GenericProperty("jsonName");
    SchemaNode test = null;
    String id = null;
    try (final Tx tx = app.tx()) {
        // create test type
        test = app.create(SchemaNode.class, "Test");
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    try (final Tx tx = app.tx()) {
        // create view with sort order
        final List<SchemaView> list = test.getProperty(SchemaNode.schemaViews);
        // create properties
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "one"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "two"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "three"));
        app.create(SchemaProperty.class, new NodeAttribute<>(SchemaProperty.schemaNode, test), new NodeAttribute<>(SchemaProperty.schemaViews, list), new NodeAttribute<>(SchemaProperty.propertyType, "String"), new NodeAttribute<>(SchemaProperty.name, "four"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("Test");
    final List<PropertyKey> list = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "public"));
    Assert.assertEquals("Invalid number of properties in sorted view", 6, list.size());
    Assert.assertEquals("id", list.get(0).dbName());
    Assert.assertEquals("type", list.get(1).dbName());
    Assert.assertEquals("one", list.get(2).dbName());
    Assert.assertEquals("two", list.get(3).dbName());
    Assert.assertEquals("three", list.get(4).dbName());
    Assert.assertEquals("four", list.get(5).dbName());
    try (final Tx tx = app.tx()) {
        for (final SchemaView testView : test.getProperty(SchemaNode.schemaViews)) {
            // modify sort order
            testView.setProperty(SchemaView.sortOrder, "type, one, id, two, three, four, name");
        }
        // create test entity
        final NodeInterface node = app.create(StructrApp.getConfiguration().getNodeEntityClass("Test"));
        // save UUID for later
        id = node.getUuid();
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    final List<PropertyKey> list2 = new LinkedList<>(StructrApp.getConfiguration().getPropertySet(type, "public"));
    Assert.assertEquals("Invalid number of properties in sorted view", 6, list2.size());
    Assert.assertEquals("id", list2.get(0).dbName());
    Assert.assertEquals("type", list2.get(1).dbName());
    Assert.assertEquals("one", list2.get(2).dbName());
    Assert.assertEquals("two", list2.get(3).dbName());
    Assert.assertEquals("three", list2.get(4).dbName());
    Assert.assertEquals("four", list2.get(5).dbName());
    // test schema resource
    RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).body("result", hasSize(6)).body("result[0].jsonName", equalTo("id")).body("result[1].jsonName", equalTo("type")).body("result[2].jsonName", equalTo("one")).body("result[3].jsonName", equalTo("two")).body("result[4].jsonName", equalTo("three")).body("result[5].jsonName", equalTo("four")).when().get("/_schema/Test/public");
    // test actual REST resource (not easy to extract and verify
    // JSON property order, that's why we're using replaceAll and
    // string comparison..
    final String[] actual = RestAssured.given().contentType("application/json; charset=UTF-8").filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(200)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(201)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(400)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(404)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(422)).filter(ResponseLoggingFilter.logResponseIfStatusCodeIs(500)).headers("X-User", ADMIN_USERNAME, "X-Password", ADMIN_PASSWORD).expect().statusCode(200).when().get("/Test").body().asString().replaceAll("[\\s]+", "").split("[\\W]+");
    // we can only test the actual ORDER of the JSON result object by splitting it on whitespace and validating the resulting array
    assertEquals("Invalid JSON result for sorted property view", "", actual[0]);
    assertEquals("Invalid JSON result for sorted property view", "query_time", actual[1]);
    assertEquals("Invalid JSON result for sorted property view", "0", actual[2]);
    assertEquals("Invalid JSON result for sorted property view", "result_count", actual[4]);
    assertEquals("Invalid JSON result for sorted property view", "1", actual[5]);
    assertEquals("Invalid JSON result for sorted property view", "result", actual[6]);
    assertEquals("Invalid JSON result for sorted property view", "id", actual[7]);
    assertEquals("Invalid JSON result for sorted property view", id, actual[8]);
    assertEquals("Invalid JSON result for sorted property view", "type", actual[9]);
    assertEquals("Invalid JSON result for sorted property view", "Test", actual[10]);
    assertEquals("Invalid JSON result for sorted property view", "one", actual[11]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[12]);
    assertEquals("Invalid JSON result for sorted property view", "two", actual[13]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[14]);
    assertEquals("Invalid JSON result for sorted property view", "three", actual[15]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[16]);
    assertEquals("Invalid JSON result for sorted property view", "four", actual[17]);
    assertEquals("Invalid JSON result for sorted property view", "null", actual[18]);
    assertEquals("Invalid JSON result for sorted property view", "serialization_time", actual[19]);
    // try built-in function
    try {
        final List<GraphObjectMap> list3 = (List) new TypeInfoFunction().apply(new ActionContext(securityContext), null, new Object[] { "Test", "public" });
        Assert.assertEquals("Invalid number of properties in sorted view", 6, list3.size());
        Assert.assertEquals("id", list3.get(0).get(jsonName));
        Assert.assertEquals("type", list3.get(1).get(jsonName));
        Assert.assertEquals("one", list3.get(2).get(jsonName));
        Assert.assertEquals("two", list3.get(3).get(jsonName));
        Assert.assertEquals("three", list3.get(4).get(jsonName));
        Assert.assertEquals("four", list3.get(5).get(jsonName));
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
    // try scripting call
    try {
        final List<GraphObjectMap> list4 = (List) Scripting.evaluate(new ActionContext(securityContext), null, "${type_info('Test', 'public')}", "test");
        Assert.assertEquals("Invalid number of properties in sorted view", 6, list4.size());
        Assert.assertEquals("id", list4.get(0).get(jsonName));
        Assert.assertEquals("type", list4.get(1).get(jsonName));
        Assert.assertEquals("one", list4.get(2).get(jsonName));
        Assert.assertEquals("two", list4.get(3).get(jsonName));
        Assert.assertEquals("three", list4.get(4).get(jsonName));
        Assert.assertEquals("four", list4.get(5).get(jsonName));
    } catch (FrameworkException fex) {
        fex.printStackTrace();
    }
}
Also used : Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) ActionContext(org.structr.schema.action.ActionContext) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) SchemaView(org.structr.core.entity.SchemaView) SchemaNode(org.structr.core.entity.SchemaNode) TypeInfoFunction(org.structr.core.function.TypeInfoFunction) GraphObjectMap(org.structr.core.GraphObjectMap) GenericProperty(org.structr.core.property.GenericProperty) LinkedList(java.util.LinkedList) List(java.util.List) PropertyKey(org.structr.core.property.PropertyKey) NodeInterface(org.structr.core.graph.NodeInterface) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 53 with GraphObjectMap

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

the class SnapshotsCommand method processMessage.

@Override
public void processMessage(final WebSocketMessage webSocketData) {
    final SecurityContext securityContext = getWebSocket().getSecurityContext();
    final App app = StructrApp.getInstance(securityContext);
    final Map<String, Object> data = webSocketData.getNodeData();
    final String mode = (String) data.get("mode");
    final String name = (String) data.get("name");
    final String typesString = (String) data.get("types");
    final List<String> types;
    if (typesString != null) {
        types = Arrays.asList(StringUtils.split(typesString, ","));
    } else {
        types = null;
    }
    if (mode != null) {
        final List<GraphObject> result = new LinkedList<>();
        switch(mode) {
            case "list":
                final List<String> snapshots = SnapshotCommand.listSnapshots();
                if (snapshots != null) {
                    final GraphObjectMap snapshotContainer = new GraphObjectMap();
                    snapshotContainer.put(snapshotsProperty, snapshots);
                    result.add(snapshotContainer);
                }
                break;
            case "get":
                final Path snapshotFile = Paths.get(SnapshotCommand.getSnapshotsPath() + name);
                if (Files.exists(snapshotFile)) {
                    try {
                        final String content = new String(Files.readAllBytes(snapshotFile));
                        // Send content directly
                        getWebSocket().send(MessageBuilder.finished().callback(callback).data("schemaJson", content).build(), true);
                        return;
                    } catch (IOException ex) {
                        LoggerFactory.getLogger(SnapshotsCommand.class.getName()).error("", ex);
                    }
                }
                break;
            default:
                final GraphObjectMap msg = new GraphObjectMap();
                result.add(msg);
                try {
                    app.command(SnapshotCommand.class).execute(mode, name, types);
                    msg.put(statusProperty, "success");
                } catch (Throwable t) {
                    logger.warn("", t);
                    msg.put(statusProperty, t.getMessage());
                }
        }
        // set full result list
        webSocketData.setResult(result);
        webSocketData.setRawResultCount(1);
        getWebSocket().send(webSocketData, true);
    } else {
        getWebSocket().send(MessageBuilder.status().code(422).message("Mode must be one of list, export, add or restore.").build(), true);
    }
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Path(java.nio.file.Path) IOException(java.io.IOException) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) SnapshotCommand(org.structr.rest.maintenance.SnapshotCommand) GraphObjectMap(org.structr.core.GraphObjectMap) SecurityContext(org.structr.common.SecurityContext) GraphObject(org.structr.core.GraphObject)

Example 54 with GraphObjectMap

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

the class SchemaHelper method getSchemaTypeInfo.

// ----- public static methods -----
public static List<GraphObjectMap> getSchemaTypeInfo(final SecurityContext securityContext, final String rawType, final Class type, final String propertyView) throws FrameworkException {
    List<GraphObjectMap> resultList = new LinkedList<>();
    if (type != null) {
        if (propertyView != null) {
            for (final Map.Entry<String, Object> entry : getPropertiesForView(securityContext, type, propertyView).entrySet()) {
                final GraphObjectMap property = new GraphObjectMap();
                for (final Map.Entry<String, Object> prop : ((Map<String, Object>) entry.getValue()).entrySet()) {
                    property.setProperty(new GenericProperty(prop.getKey()), prop.getValue());
                }
                resultList.add(property);
            }
        } else {
            final GraphObjectMap schema = new GraphObjectMap();
            resultList.add(schema);
            String url = "/".concat(rawType);
            schema.setProperty(new StringProperty("url"), url);
            schema.setProperty(new StringProperty("type"), type.getSimpleName());
            schema.setProperty(new StringProperty("className"), type.getName());
            schema.setProperty(new StringProperty("extendsClass"), type.getSuperclass().getName());
            schema.setProperty(new BooleanProperty("isRel"), AbstractRelationship.class.isAssignableFrom(type));
            schema.setProperty(new LongProperty("flags"), SecurityContext.getResourceFlags(rawType));
            Set<String> propertyViews = new LinkedHashSet<>(StructrApp.getConfiguration().getPropertyViewsForType(type));
            // list property sets for all views
            Map<String, Map<String, Object>> views = new TreeMap();
            schema.setProperty(new GenericProperty("views"), views);
            for (final String view : propertyViews) {
                if (!View.INTERNAL_GRAPH_VIEW.equals(view)) {
                    views.put(view, getPropertiesForView(securityContext, type, view));
                }
            }
        }
    }
    return resultList;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) BooleanProperty(org.structr.core.property.BooleanProperty) AbstractRelationship(org.structr.core.entity.AbstractRelationship) StringProperty(org.structr.core.property.StringProperty) TreeMap(java.util.TreeMap) LinkedList(java.util.LinkedList) GraphObjectMap(org.structr.core.GraphObjectMap) LongProperty(org.structr.core.property.LongProperty) GenericProperty(org.structr.core.property.GenericProperty) GraphObject(org.structr.core.GraphObject) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap)

Example 55 with GraphObjectMap

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

the class Function method recursivelyConvertMapToGraphObjectMap.

protected static void recursivelyConvertMapToGraphObjectMap(final GraphObjectMap destination, final Map<String, Object> source, final int depth) {
    if (depth > 20) {
        return;
    }
    for (final Map.Entry<String, Object> entry : source.entrySet()) {
        final ConfigurationProvider provider = StructrApp.getConfiguration();
        final String key = entry.getKey();
        final Object value = entry.getValue();
        if (value instanceof Map) {
            final Map<String, Object> map = (Map<String, Object>) value;
            final GraphObjectMap obj = new GraphObjectMap();
            destination.put(provider.getPropertyKeyForJSONName(obj.getClass(), key), obj);
            recursivelyConvertMapToGraphObjectMap(obj, map, depth + 1);
        } else if (value instanceof Collection) {
            final List list = new LinkedList();
            final Collection collection = (Collection) value;
            for (final Object obj : collection) {
                if (obj instanceof Map) {
                    final GraphObjectMap container = new GraphObjectMap();
                    list.add(container);
                    recursivelyConvertMapToGraphObjectMap(container, (Map<String, Object>) obj, depth + 1);
                } else {
                    list.add(obj);
                }
            }
            destination.put(provider.getPropertyKeyForJSONName(list.getClass(), key), list);
        } else {
            destination.put(value != null ? provider.getPropertyKeyForJSONName(value.getClass(), key) : new StringProperty(key), value);
        }
    }
}
Also used : ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObjectMap(org.structr.core.GraphObjectMap) Collection(java.util.Collection) StringProperty(org.structr.core.property.StringProperty) GraphObject(org.structr.core.GraphObject) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Map(java.util.Map) GraphObjectMap(org.structr.core.GraphObjectMap) LinkedList(java.util.LinkedList)

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