Search in sources :

Example 51 with SchemaNode

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

the class ValidationTest method testInheritedStringPropertyUniqueness.

@Test
public void testInheritedStringPropertyUniqueness() {
    try (final Tx tx = app.tx()) {
        final SchemaNode testType = app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "Test"), new NodeAttribute<>(new StringProperty("_testUnique"), "String!"));
        app.create(SchemaNode.class, new NodeAttribute<>(AbstractNode.name, "TestDerived"), new NodeAttribute<>(SchemaNode.extendsClass, "org.structr.dynamic.Test"));
        tx.success();
    } catch (FrameworkException fex) {
        fex.printStackTrace();
        fail("Unexpected exception.");
    }
    final Class testType = StructrApp.getConfiguration().getNodeEntityClass("TestDerived");
    if (testType != null) {
        final PropertyKey key = StructrApp.key(testType, "testUnique");
        if (key != null) {
            Settings.CypherDebugLogging.setValue(true);
            try (final Tx tx = app.tx()) {
                // key must be unique, but can empty
                app.create(testType, new NodeAttribute<>(key, "unique"));
                app.create(testType, new NodeAttribute<>(key, ""));
                tx.success();
            } catch (FrameworkException fex) {
                fex.printStackTrace();
                fail("Unexpected exception.");
            }
            Settings.CypherDebugLogging.setValue(true);
            for (int i = 0; i < 5; i++) {
                try (final Tx tx = app.tx()) {
                    app.create(testType, new NodeAttribute<>(key, "unique"));
                    tx.success();
                    fail("Uniqueness constraint violated!");
                } catch (FrameworkException fex) {
                    final List<ErrorToken> tokens = fex.getErrorBuffer().getErrorTokens();
                    final ErrorToken token = tokens.get(0);
                    assertEquals("Invalid uniqueness validation result", 1, tokens.size());
                    assertEquals("Invalid uniqueness validation result", 422, fex.getStatus());
                    assertEquals("Invalid uniqueness validation result", "testUnique", token.getProperty());
                    assertEquals("Invalid uniqueness validation result", "TestDerived", token.getType());
                    assertEquals("Invalid uniqueness validation result", "already_taken", token.getToken());
                }
            }
        }
    }
}
Also used : SchemaNode(org.structr.core.entity.SchemaNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) StringProperty(org.structr.core.property.StringProperty) List(java.util.List) ErrorToken(org.structr.common.error.ErrorToken) PropertyKey(org.structr.core.property.PropertyKey) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 52 with SchemaNode

use of org.structr.core.entity.SchemaNode 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 53 with SchemaNode

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

the class SchemaHelper method cleanUnusedDynamicGrants.

public static void cleanUnusedDynamicGrants(final List<SchemaNode> existingSchemaNodes) {
    try {
        final List<DynamicResourceAccess> existingDynamicGrants = StructrApp.getInstance().nodeQuery(DynamicResourceAccess.class).getAsList();
        final Set<String> existingSchemaNodeNames = new HashSet<>();
        for (final SchemaNode schemaNode : existingSchemaNodes) {
            existingSchemaNodeNames.add(schemaNode.getResourceSignature());
        }
        for (final DynamicResourceAccess grant : existingDynamicGrants) {
            boolean foundAllParts = true;
            final String sig;
            try {
                sig = grant.getResourceSignature();
            } catch (NotFoundException nfe) {
                logger.debug("Unable to get signature from grant");
                continue;
            }
            // Try to find schema nodes for all parts of the grant signature
            final String[] parts = StringUtils.split(sig, "/");
            if (parts != null) {
                for (final String sigPart : parts) {
                    if ("/".equals(sigPart) || sigPart.startsWith("_")) {
                        continue;
                    }
                    // If one of the signature parts doesn't have an equivalent existing schema node, remove it
                    foundAllParts &= existingSchemaNodeNames.contains(sigPart);
                }
            }
            if (!foundAllParts) {
                logger.info("Did not find all parts of signature, will be removed: {}, ", new Object[] { sig });
                removeDynamicGrants(sig);
            }
        }
    } catch (Throwable t) {
        logger.warn("", t);
    }
}
Also used : DynamicResourceAccess(org.structr.core.entity.DynamicResourceAccess) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) SchemaNode(org.structr.core.entity.SchemaNode) NotFoundException(org.structr.api.NotFoundException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 54 with SchemaNode

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

the class SchemaHelper method reloadSchema.

public static boolean reloadSchema(final ErrorBuffer errorBuffer, final String initiatedBySessionId) {
    try {
        final App app = StructrApp.getInstance();
        final List<SchemaNode> existingSchemaNodes = app.nodeQuery(SchemaNode.class).getAsList();
        cleanUnusedDynamicGrants(existingSchemaNodes);
        for (final SchemaNode schemaNode : existingSchemaNodes) {
            createDynamicGrants(schemaNode.getResourceSignature());
        }
        for (final SchemaRelationshipNode schemaRelationship : StructrApp.getInstance().nodeQuery(SchemaRelationshipNode.class).getAsList()) {
            createDynamicGrants(schemaRelationship.getResourceSignature());
            createDynamicGrants(schemaRelationship.getInverseResourceSignature());
        }
    } catch (Throwable t) {
        logger.warn("", t);
    }
    return SchemaService.reloadSchema(errorBuffer, initiatedBySessionId);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) AbstractSchemaNode(org.structr.core.entity.AbstractSchemaNode) SchemaNode(org.structr.core.entity.SchemaNode) SchemaRelationshipNode(org.structr.core.entity.SchemaRelationshipNode)

Aggregations

SchemaNode (org.structr.core.entity.SchemaNode)54 Tx (org.structr.core.graph.Tx)44 FrameworkException (org.structr.common.error.FrameworkException)41 Test (org.junit.Test)34 App (org.structr.core.app.App)18 StructrApp (org.structr.core.app.StructrApp)18 NodeAttribute (org.structr.core.graph.NodeAttribute)14 LinkedList (java.util.LinkedList)13 PropertyMap (org.structr.core.property.PropertyMap)13 NodeInterface (org.structr.core.graph.NodeInterface)12 PropertyKey (org.structr.core.property.PropertyKey)11 StructrTest (org.structr.common.StructrTest)10 SchemaMethod (org.structr.core.entity.SchemaMethod)10 SchemaRelationshipNode (org.structr.core.entity.SchemaRelationshipNode)9 StringProperty (org.structr.core.property.StringProperty)9 List (java.util.List)8 ConfigurationProvider (org.structr.schema.ConfigurationProvider)8 SchemaProperty (org.structr.core.entity.SchemaProperty)7 LinkedHashSet (java.util.LinkedHashSet)6 FrontendTest (org.structr.web.basic.FrontendTest)6