Search in sources :

Example 46 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class SystemTest method testConstraintsConcurrently.

@Test
public void testConstraintsConcurrently() {
    try (final Tx tx = app.tx()) {
        app.create(SchemaNode.class, new NodeAttribute(SchemaNode.name, "Item"), new NodeAttribute(SchemaNode.schemaProperties, Arrays.asList(app.create(SchemaProperty.class, new NodeAttribute(SchemaProperty.name, "name"), new NodeAttribute(SchemaProperty.propertyType, "String"), new NodeAttribute(SchemaProperty.unique, true), new NodeAttribute(SchemaProperty.indexed, true)))));
        tx.success();
    } catch (FrameworkException ex) {
        fail("Error creating schema node");
    }
    final Class itemType = StructrApp.getConfiguration().getNodeEntityClass("Item");
    assertNotNull("Error creating schema node", itemType);
    final Runnable worker = new Runnable() {

        @Override
        public void run() {
            int i = 0;
            while (i < 1000) {
                try (final Tx tx = app.tx()) {
                    for (int j = 0; j < 10 && i < 1000; j++) {
                        app.create(itemType, "Item" + StringUtils.leftPad(Integer.toString(i++), 5, "0"));
                    }
                    tx.success();
                } catch (FrameworkException expected) {
                }
            }
        }
    };
    final ExecutorService service = Executors.newFixedThreadPool(10);
    final List<Future> futures = new LinkedList<>();
    for (int i = 0; i < 10; i++) {
        futures.add(service.submit(worker));
    }
    // wait for result of async. operations
    for (final Future future : futures) {
        try {
            future.get();
        } catch (Throwable t) {
            logger.warn("", t);
            fail("Unexpected exception");
        }
    }
    try (final Tx tx = app.tx()) {
        final List<NodeInterface> items = app.nodeQuery(itemType).sort(AbstractNode.name).getAsList();
        int i = 0;
        assertEquals("Invalid concurrent constraint test result", 1000, items.size());
        for (final NodeInterface item : items) {
            assertEquals("Invalid name detected", "Item" + StringUtils.leftPad(Integer.toString(i++), 5, "0"), item.getName());
        }
        tx.success();
    } catch (FrameworkException ex) {
        fail("Unexpected exception");
    }
    service.shutdownNow();
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) SchemaProperty(org.structr.core.entity.SchemaProperty) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) LinkedList(java.util.LinkedList) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test)

Example 47 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class SystemTest method testEnsureCardinalityPerformance.

@Test
public void testEnsureCardinalityPerformance() {
    final List<TestOne> list = new LinkedList<>();
    final int num = 1000;
    // test setup, create a supernode with 10000 relationships
    try (final Tx tx = app.tx()) {
        System.out.println("Creating supernode with " + num + " relationships.");
        list.add(createTestNode(TestOne.class, new NodeAttribute<>(TestOne.manyToManyTestSixs, createTestNodes(TestSix.class, num))));
        tx.success();
    } catch (Throwable fex) {
        fail("Unexpected exception");
    }
    // actual test: test performance of node association on supernode
    try (final Tx tx = app.tx()) {
        for (int i = 0; i < 10; i++) {
            final long t0 = System.currentTimeMillis();
            createTestNode(TestSix.class, new NodeAttribute<>(TestSix.manyToManyTestOnes, list));
            final long t1 = System.currentTimeMillis();
            System.out.println((t1 - t0) + "ms");
        }
        tx.success();
    } catch (Throwable fex) {
        fail("Unexpected exception");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) TestOne(org.structr.core.entity.TestOne) LinkedList(java.util.LinkedList) TestSix(org.structr.core.entity.TestSix) Test(org.junit.Test)

Example 48 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class BasicTest method testNodeCreationWithForcedUuid.

@Test
public void testNodeCreationWithForcedUuid() {
    final String uuid = NodeServiceCommand.getNextUuid();
    TestOne test = null;
    // create object with user context
    try (final Tx tx = app.tx()) {
        test = app.create(TestOne.class, new NodeAttribute<>(AbstractNode.name, "test"), new NodeAttribute<>(GraphObject.id, uuid));
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
    try (final Tx tx = app.tx()) {
        final String uuid1 = test.getProperty(GraphObject.id);
        final String uuid2 = test.getUuid();
        assertEquals("Object creation does not accept provided UUID", uuid, uuid1);
        assertEquals("UUID mismatch in getProperty() and getUuid()", uuid1, uuid2);
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) TestOne(org.structr.core.entity.TestOne) Test(org.junit.Test)

Example 49 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class StructrTypeDefinition method createDatabaseSchema.

AbstractSchemaNode createDatabaseSchema(final App app) throws FrameworkException {
    final Map<String, SchemaProperty> schemaProperties = new TreeMap<>();
    final Map<String, SchemaMethod> schemaMethods = new TreeMap<>();
    final PropertyMap createProperties = new PropertyMap();
    final PropertyMap nodeProperties = new PropertyMap();
    // properties that always need to be set
    createProperties.put(SchemaNode.isInterface, isInterface);
    createProperties.put(SchemaNode.isAbstract, isAbstract);
    createProperties.put(SchemaNode.category, category);
    createProperties.put(SchemaNode.isBuiltinType, SchemaService.DynamicSchemaRootURI.equals(root.getId()));
    final T schemaNode = createSchemaNode(app, createProperties);
    for (final StructrPropertyDefinition property : properties) {
        final SchemaProperty schemaProperty = property.createDatabaseSchema(app, schemaNode);
        if (schemaProperty != null) {
            schemaProperties.put(schemaProperty.getName(), schemaProperty);
        }
    }
    // create views and associate the properties
    for (final Entry<String, Set<String>> view : views.entrySet()) {
        final List<SchemaProperty> viewProperties = new LinkedList<>();
        final List<String> nonGraphProperties = new LinkedList<>();
        for (final String propertyName : view.getValue()) {
            final SchemaProperty property = schemaProperties.get(propertyName);
            if (property != null) {
                viewProperties.add(property);
            } else {
                nonGraphProperties.add(propertyName);
            }
        }
        SchemaView viewNode = app.nodeQuery(SchemaView.class).and(SchemaView.schemaNode, schemaNode).and(SchemaView.name, view.getKey()).getFirst();
        if (viewNode == null) {
            viewNode = app.create(SchemaView.class, new NodeAttribute<>(SchemaView.schemaNode, schemaNode), new NodeAttribute<>(SchemaView.name, view.getKey()));
        }
        final PropertyMap updateProperties = new PropertyMap();
        updateProperties.put(SchemaView.schemaProperties, viewProperties);
        updateProperties.put(SchemaView.nonGraphProperties, StringUtils.join(nonGraphProperties, ", "));
        // update properties of existing or new schema view node
        viewNode.setProperties(SecurityContext.getSuperUserInstance(), updateProperties);
    }
    for (final StructrMethodDefinition method : methods) {
        final SchemaMethod schemaMethod = method.createDatabaseSchema(app, schemaNode);
        if (schemaMethod != null) {
            schemaMethods.put(schemaMethod.getName(), schemaMethod);
        }
    }
    // extends
    if (baseTypeReference != null) {
        final Object def = root.resolveURI(baseTypeReference);
        if (def != null && def instanceof JsonType) {
            final JsonType jsonType = (JsonType) def;
            final String superclassName = "org.structr.dynamic." + jsonType.getName();
            nodeProperties.put(SchemaNode.extendsClass, superclassName);
        } else {
            final Class superclass = StructrApp.resolveSchemaId(baseTypeReference);
            if (superclass != null) {
                if (superclass.isInterface()) {
                    nodeProperties.put(SchemaNode.implementsInterfaces, superclass.getName());
                } else {
                    nodeProperties.put(SchemaNode.extendsClass, superclass.getName());
                }
            } else if ("https://structr.org/v1.1/definitions/FileBase".equals(baseTypeReference.toString())) {
                // FileBase doesn't exist any more, but we need to support it for some time..
                nodeProperties.put(SchemaNode.implementsInterfaces, "org.structr.web.entity.File");
            } else if (!StructrApp.getSchemaBaseURI().relativize(baseTypeReference).isAbsolute()) {
                // resolve internal type referenced in special URI
                final URI base = StructrApp.getSchemaBaseURI().resolve("definitions/");
                final URI type = base.relativize(baseTypeReference);
                final String typeName = type.getPath();
                final String parameters = type.getQuery();
                final Class internal = StructrApp.getConfiguration().getNodeEntityClass(typeName);
                if (internal != null) {
                    nodeProperties.put(SchemaNode.extendsClass, getParameterizedType(internal, parameters));
                }
            }
        }
    }
    // implements
    if (!implementedInterfaces.isEmpty()) {
        final Set<String> interfaces = new LinkedHashSet<>();
        for (final URI implementedInterface : implementedInterfaces) {
            final Object def = root.resolveURI(implementedInterface);
            if (def != null && def instanceof JsonType) {
                final JsonType jsonType = (JsonType) def;
                final String superclassName = "org.structr.dynamic." + jsonType.getName();
                if (jsonType.isInterface()) {
                    interfaces.add(superclassName);
                } else {
                    nodeProperties.put(SchemaNode.extendsClass, superclassName);
                }
            } else {
                final Class superclass = StructrApp.resolveSchemaId(implementedInterface);
                if (superclass != null) {
                    interfaces.add(superclass.getName());
                } else {
                    logger.warn("Unable to resolve built-in type {} against Structr schema", implementedInterface);
                    StructrApp.resolveSchemaId(implementedInterface);
                }
            }
        }
        nodeProperties.put(SchemaNode.implementsInterfaces, StringUtils.join(interfaces, ", "));
    }
    schemaNode.setProperties(SecurityContext.getSuperUserInstance(), nodeProperties);
    return schemaNode;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NodeAttribute(org.structr.core.graph.NodeAttribute) JsonType(org.structr.schema.json.JsonType) SchemaProperty(org.structr.core.entity.SchemaProperty) TreeSet(java.util.TreeSet) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) SchemaMethod(org.structr.core.entity.SchemaMethod) TreeMap(java.util.TreeMap) URI(java.net.URI) LinkedList(java.util.LinkedList) SchemaView(org.structr.core.entity.SchemaView) PropertyMap(org.structr.core.property.PropertyMap)

Example 50 with NodeAttribute

use of org.structr.core.graph.NodeAttribute in project structr by structr.

the class DeploymentTest method test25ExtendedBuiltinTypes.

@Test
public void test25ExtendedBuiltinTypes() {
    // setup
    try (final Tx tx = app.tx()) {
        // create extended folder class
        app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "ExtendedFolder"), new NodeAttribute<>(SchemaNode.extendsClass, "org.structr.dynamic.Folder"));
        // create extended file class
        app.create(SchemaNode.class, new NodeAttribute<>(SchemaNode.name, "ExtendedFile"), new NodeAttribute<>(SchemaNode.extendsClass, "org.structr.dynamic.File"));
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // setup
    try (final Tx tx = app.tx()) {
        final NodeInterface folder1 = app.create(StructrApp.getConfiguration().getNodeEntityClass("ExtendedFolder"), "folder1");
        final NodeInterface folder2 = app.create(StructrApp.getConfiguration().getNodeEntityClass("ExtendedFolder"), new NodeAttribute<>(Folder.name, "folder2"), new NodeAttribute(StructrApp.key(Folder.class, "parent"), folder1));
        app.create(StructrApp.getConfiguration().getNodeEntityClass("ExtendedFile"), new NodeAttribute<>(File.name, "file1.txt"), new NodeAttribute(StructrApp.key(File.class, "parent"), folder1));
        app.create(StructrApp.getConfiguration().getNodeEntityClass("ExtendedFile"), new NodeAttribute<>(File.name, "file2.txt"), new NodeAttribute(StructrApp.key(File.class, "parent"), folder2));
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // test
    compare(calculateHash(), false, true);
}
Also used : NodeAttribute(org.structr.core.graph.NodeAttribute) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) NodeInterface(org.structr.core.graph.NodeInterface) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Aggregations

NodeAttribute (org.structr.core.graph.NodeAttribute)73 Tx (org.structr.core.graph.Tx)55 FrameworkException (org.structr.common.error.FrameworkException)54 Test (org.junit.Test)45 App (org.structr.core.app.App)23 StructrApp (org.structr.core.app.StructrApp)23 StructrUiTest (org.structr.web.StructrUiTest)21 LinkedList (java.util.LinkedList)19 PropertyKey (org.structr.core.property.PropertyKey)17 SchemaNode (org.structr.core.entity.SchemaNode)14 NodeInterface (org.structr.core.graph.NodeInterface)13 Principal (org.structr.core.entity.Principal)12 PropertyMap (org.structr.core.property.PropertyMap)12 StringProperty (org.structr.core.property.StringProperty)10 ConfigurationProvider (org.structr.schema.ConfigurationProvider)10 File (org.structr.web.entity.File)10 Folder (org.structr.web.entity.Folder)9 AbstractFile (org.structr.web.entity.AbstractFile)8 List (java.util.List)7 StructrTest (org.structr.common.StructrTest)7