Search in sources :

Example 26 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class AdvancedSchemaTest method test01InheritanceOfFileAttributesToImage.

@Test
public void test01InheritanceOfFileAttributesToImage() {
    try (final Tx tx = app.tx()) {
        createAdminUser();
        ResourceAccessTest.createResourceAccess("_schema", UiAuthenticator.AUTH_USER_GET);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        // Add String property "testFile" to built-in File class
        SchemaNode fileNodeDef = app.nodeQuery(SchemaNode.class).andName("File").getFirst();
        SchemaProperty testFileProperty = app.create(SchemaProperty.class);
        final PropertyMap testFileProperties = new PropertyMap();
        testFileProperties.put(SchemaProperty.name, "testFile");
        testFileProperties.put(SchemaProperty.propertyType, "String");
        testFileProperties.put(SchemaProperty.schemaNode, fileNodeDef);
        testFileProperty.setProperties(testFileProperty.getSecurityContext(), testFileProperties);
        tx.success();
    } catch (Exception ex) {
        logger.error("", ex);
    }
    try (final Tx tx = app.tx()) {
        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).body("result", Matchers.hasSize(count1)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).when().get("/_schema/File/ui");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        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).body("result", Matchers.hasSize(count2)).body("result", Matchers.hasItem(Matchers.allOf(hasEntry("jsonName", "testFile"), hasEntry("declaringClass", "File")))).when().get("/_schema/Image/ui");
        tx.success();
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) SchemaProperty(org.structr.core.entity.SchemaProperty) PropertyMap(org.structr.core.property.PropertyMap) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) FrameworkException(org.structr.common.error.FrameworkException) FrontendTest(org.structr.web.basic.FrontendTest) ResourceAccessTest(org.structr.web.basic.ResourceAccessTest) Test(org.junit.Test)

Example 27 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class ScriptingTest method testSetPropertyWithDynamicNodes.

@Test
public void testSetPropertyWithDynamicNodes() {
    /**
     * This test creates two connected SchemaNodes and tests the script-based
     * association of one instance with several others in the onCreate method.
     */
    final long currentTimeMillis = System.currentTimeMillis();
    Class sourceType = null;
    Class targetType = null;
    PropertyKey targetsProperty = null;
    EnumProperty testEnumProperty = null;
    PropertyKey testBooleanProperty = null;
    PropertyKey testIntegerProperty = null;
    PropertyKey testStringProperty = null;
    PropertyKey testDoubleProperty = null;
    PropertyKey testDateProperty = null;
    Class testEnumType = null;
    // setup phase: create schema nodes
    try (final Tx tx = app.tx()) {
        // create two nodes and associate them with each other
        final SchemaNode sourceNode = createTestNode(SchemaNode.class, "Source");
        final SchemaNode targetNode = createTestNode(SchemaNode.class, "Target");
        final List<SchemaProperty> properties = new LinkedList<>();
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testBoolean"), new NodeAttribute(SchemaProperty.propertyType, "Boolean")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testInteger"), new NodeAttribute(SchemaProperty.propertyType, "Integer")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testString"), new NodeAttribute(SchemaProperty.propertyType, "String")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDouble"), new NodeAttribute(SchemaProperty.propertyType, "Double")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testEnum"), new NodeAttribute(SchemaProperty.propertyType, "Enum"), new NodeAttribute(SchemaProperty.format, "OPEN, CLOSED, TEST")));
        properties.add(createTestNode(SchemaProperty.class, new NodeAttribute(AbstractNode.name, "testDate"), new NodeAttribute(SchemaProperty.propertyType, "Date")));
        sourceNode.setProperty(SchemaNode.schemaProperties, properties);
        final List<SchemaMethod> methods = new LinkedList<>();
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "onCreate"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.targets = Structr.find('Target'); }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest01"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'OPEN'; }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest02"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'CLOSED'; }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest03"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'TEST'; }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest04"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testEnum = 'INVALID'; }")));
        methods.add(createTestNode(SchemaMethod.class, new NodeAttribute(AbstractNode.name, "doTest05"), new NodeAttribute(SchemaMethod.source, "{ var e = Structr.get('this'); e.testBoolean = true; e.testInteger = 123; e.testString = 'testing..'; e.testDouble = 1.2345; e.testDate = new Date(" + currentTimeMillis + "); }")));
        sourceNode.setProperty(SchemaNode.schemaMethods, methods);
        final PropertyMap propertyMap = new PropertyMap();
        propertyMap.put(SchemaRelationshipNode.sourceId, sourceNode.getUuid());
        propertyMap.put(SchemaRelationshipNode.targetId, targetNode.getUuid());
        propertyMap.put(SchemaRelationshipNode.sourceJsonName, "source");
        propertyMap.put(SchemaRelationshipNode.targetJsonName, "targets");
        propertyMap.put(SchemaRelationshipNode.sourceMultiplicity, "*");
        propertyMap.put(SchemaRelationshipNode.targetMultiplicity, "*");
        propertyMap.put(SchemaRelationshipNode.relationshipType, "HAS");
        app.create(SchemaRelationshipNode.class, propertyMap);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        final ConfigurationProvider config = StructrApp.getConfiguration();
        sourceType = config.getNodeEntityClass("Source");
        targetType = config.getNodeEntityClass("Target");
        targetsProperty = StructrApp.key(sourceType, "targets");
        // we need to cast to EnumProperty in order to obtain the dynamic enum type
        testEnumProperty = (EnumProperty) StructrApp.key(sourceType, "testEnum");
        testEnumType = testEnumProperty.getEnumType();
        testBooleanProperty = StructrApp.key(sourceType, "testBoolean");
        testIntegerProperty = StructrApp.key(sourceType, "testInteger");
        testStringProperty = StructrApp.key(sourceType, "testString");
        testDoubleProperty = StructrApp.key(sourceType, "testDouble");
        testDateProperty = StructrApp.key(sourceType, "testDate");
        assertNotNull(sourceType);
        assertNotNull(targetType);
        assertNotNull(targetsProperty);
        // create 5 target nodes
        createTestNodes(targetType, 5);
        // create source node
        createTestNodes(sourceType, 5);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    // check phase: source node should have all five target nodes associated with HAS
    try (final Tx tx = app.tx()) {
        // check all source nodes
        for (final Object obj : app.nodeQuery(sourceType).getAsList()) {
            assertNotNull("Invalid nodeQuery result", obj);
            final GraphObject sourceNode = (GraphObject) obj;
            // test contents of "targets" property
            final Object targetNodesObject = sourceNode.getProperty(targetsProperty);
            assertTrue("Invalid getProperty result for scripted association", targetNodesObject instanceof List);
            final List list = (List) targetNodesObject;
            assertEquals("Invalid getProperty result for scripted association", 5, list.size());
        }
        final GraphObject sourceNode = app.nodeQuery(sourceType).getFirst();
        // set testEnum property to OPEN via doTest01 function call, check result
        sourceNode.invokeMethod("doTest01", Collections.EMPTY_MAP, true);
        assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[0], sourceNode.getProperty(testEnumProperty));
        // set testEnum property to CLOSED via doTest02 function call, check result
        sourceNode.invokeMethod("doTest02", Collections.EMPTY_MAP, true);
        assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[1], sourceNode.getProperty(testEnumProperty));
        // set testEnum property to TEST via doTest03 function call, check result
        sourceNode.invokeMethod("doTest03", Collections.EMPTY_MAP, true);
        assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
        // set testEnum property to INVALID via doTest03 function call, expect previous value & error
        try {
            sourceNode.invokeMethod("doTest04", Collections.EMPTY_MAP, true);
            assertEquals("Invalid setProperty result for EnumProperty", testEnumType.getEnumConstants()[2], sourceNode.getProperty(testEnumProperty));
            fail("Setting EnumProperty to invalid value should result in an Exception!");
        } catch (FrameworkException fx) {
        }
        // test other property types
        sourceNode.invokeMethod("doTest05", Collections.EMPTY_MAP, true);
        assertEquals("Invalid setProperty result for BooleanProperty", true, sourceNode.getProperty(testBooleanProperty));
        assertEquals("Invalid setProperty result for IntegerProperty", 123, sourceNode.getProperty(testIntegerProperty));
        assertEquals("Invalid setProperty result for StringProperty", "testing..", sourceNode.getProperty(testStringProperty));
        assertEquals("Invalid setProperty result for DoubleProperty", 1.2345, sourceNode.getProperty(testDoubleProperty));
        assertEquals("Invalid setProperty result for DateProperty", new Date(currentTimeMillis), sourceNode.getProperty(testDateProperty));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) SchemaProperty(org.structr.core.entity.SchemaProperty) Tx(org.structr.core.graph.Tx) SchemaMethod(org.structr.core.entity.SchemaMethod) FrameworkException(org.structr.common.error.FrameworkException) ConfigurationProvider(org.structr.schema.ConfigurationProvider) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) Date(java.util.Date) SchemaNode(org.structr.core.entity.SchemaNode) PropertyMap(org.structr.core.property.PropertyMap) EnumProperty(org.structr.core.property.EnumProperty) GraphObject(org.structr.core.GraphObject) List(java.util.List) LinkedList(java.util.LinkedList) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 28 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class SchemaHelper method hasSchemaProperty.

private static boolean hasSchemaProperty(final String typeName, final String propertyName) throws FrameworkException {
    final Set<String> visited = new LinkedHashSet<>();
    final Queue<String> types = new LinkedList<>();
    final App app = StructrApp.getInstance();
    types.add(typeName);
    while (!types.isEmpty()) {
        final String type = types.poll();
        if (!visited.contains(type)) {
            visited.add(type);
            final SchemaNode schemaNode = app.nodeQuery(SchemaNode.class).andName(type).getFirst();
            if (schemaNode != null) {
                final SchemaProperty schemaProperty = app.nodeQuery(SchemaProperty.class).and(SchemaProperty.schemaNode, schemaNode).andName(propertyName).getFirst();
                if (schemaProperty != null || hasRelationshipNode(schemaNode, propertyName)) {
                    return true;
                } else {
                    // add superclass AND interfaces
                    String localTypeName = schemaNode.getProperty(SchemaNode.extendsClass);
                    if (localTypeName != null) {
                        localTypeName = cleanTypeName(localTypeName);
                        localTypeName = localTypeName.substring(localTypeName.lastIndexOf(".") + 1);
                        types.add(localTypeName);
                    }
                    final String interfaces = schemaNode.getProperty(SchemaNode.implementsInterfaces);
                    if (StringUtils.isNotBlank(interfaces)) {
                        for (final String iface : collectInterfaces(interfaces)) {
                            String cleaned = cleanTypeName(iface);
                            cleaned = cleaned.substring(cleaned.lastIndexOf(".") + 1);
                            types.add(cleaned);
                        }
                    }
                }
            } else {
                break;
            }
        }
    }
    return false;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) SchemaNode(org.structr.core.entity.SchemaNode) SchemaProperty(org.structr.core.entity.SchemaProperty) LinkedList(java.util.LinkedList)

Example 29 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class StructrCustomProperty method createDatabaseSchema.

@Override
SchemaProperty createDatabaseSchema(final App app, final AbstractSchemaNode schemaNode) throws FrameworkException {
    final SchemaProperty property = super.createDatabaseSchema(app, schemaNode);
    final PropertyMap properties = new PropertyMap();
    properties.put(SchemaProperty.propertyType, Type.Custom.name());
    properties.put(SchemaProperty.fqcn, fqcn);
    property.setProperties(SecurityContext.getSuperUserInstance(), properties);
    return property;
}
Also used : SchemaProperty(org.structr.core.entity.SchemaProperty) PropertyMap(org.structr.core.property.PropertyMap)

Example 30 with SchemaProperty

use of org.structr.core.entity.SchemaProperty in project structr by structr.

the class StructrFunctionProperty method createDatabaseSchema.

@Override
SchemaProperty createDatabaseSchema(final App app, final AbstractSchemaNode schemaNode) throws FrameworkException {
    final SchemaProperty property = super.createDatabaseSchema(app, schemaNode);
    final PropertyMap properties = new PropertyMap();
    final String contentType = getContentType();
    if (contentType != null) {
        switch(contentType) {
            case "application/x-structr-javascript":
            case "application/x-structr-script":
                properties.put(SchemaProperty.propertyType, Type.Function.name());
                break;
            case "application/x-cypher":
                properties.put(SchemaProperty.propertyType, Type.Cypher.name());
        }
    } else {
        // default
        properties.put(SchemaProperty.propertyType, Type.Function.name());
    }
    properties.put(SchemaProperty.readFunction, readFunction);
    properties.put(SchemaProperty.writeFunction, writeFunction);
    property.setProperties(SecurityContext.getSuperUserInstance(), properties);
    return property;
}
Also used : SchemaProperty(org.structr.core.entity.SchemaProperty) PropertyMap(org.structr.core.property.PropertyMap)

Aggregations

SchemaProperty (org.structr.core.entity.SchemaProperty)34 PropertyMap (org.structr.core.property.PropertyMap)23 FrameworkException (org.structr.common.error.FrameworkException)7 SchemaNode (org.structr.core.entity.SchemaNode)7 Tx (org.structr.core.graph.Tx)7 LinkedList (java.util.LinkedList)5 App (org.structr.core.app.App)5 StructrApp (org.structr.core.app.StructrApp)5 Test (org.junit.Test)4 SchemaMethod (org.structr.core.entity.SchemaMethod)4 SchemaView (org.structr.core.entity.SchemaView)4 AbstractSchemaNode (org.structr.core.entity.AbstractSchemaNode)3 FrontendTest (org.structr.web.basic.FrontendTest)3 ResourceAccessTest (org.structr.web.basic.ResourceAccessTest)3 LinkedHashSet (java.util.LinkedHashSet)2 TreeSet (java.util.TreeSet)2 PropertyContainer (org.structr.api.graph.PropertyContainer)2 NodeAttribute (org.structr.core.graph.NodeAttribute)2 StructrPath (org.structr.files.ssh.filesystem.StructrPath)2 URI (java.net.URI)1