Search in sources :

Example 6 with Label

use of org.structr.api.graph.Label in project structr by structr.

the class MaintenanceTest method testSyncCommandInheritance.

@Test
public void testSyncCommandInheritance() {
    try {
        // create test nodes
        final List<TestEleven> testNodes = createTestNodes(TestEleven.class, 10);
        final String tenantIdentifier = app.getDatabaseService().getTenantIdentifier();
        int labelCount = 7;
        // one additional label
        if (tenantIdentifier != null) {
            labelCount += 1;
        }
        try (final Tx tx = app.tx()) {
            for (final TestEleven node : testNodes) {
                Iterable<Label> labels = node.getNode().getLabels();
                assertEquals(labelCount, Iterables.count(labels));
                for (final Label label : labels) {
                    System.out.print(label.name() + " ");
                }
                System.out.println();
                final Set<String> names = Iterables.toSet(labels).stream().map(Label::name).collect(Collectors.toSet());
                assertEquals("Number of labels must be 7", labelCount, names.size());
                assertTrue("Set of labels must contain AbstractNode", names.contains("AbstractNode"));
                assertTrue("Set of labels must contain NodeInterface", names.contains("NodeInterface"));
                assertTrue("Set of labels must contain AccessControllable", names.contains("AccessControllable"));
                assertTrue("Set of labels must contain CMISInfo", names.contains("CMISInfo"));
                assertTrue("Set of labels must contain CMISItemInfo", names.contains("CMISItemInfo"));
                assertTrue("Set of labels must contain TestOne", names.contains("TestOne"));
                assertTrue("Set of labels must contain TestEleven", names.contains("TestEleven"));
                if (tenantIdentifier != null) {
                    assertTrue("Set of labels must contain custom tenant identifier if set", names.contains(tenantIdentifier));
                }
            }
            tx.success();
        }
        // test export
        app.command(SyncCommand.class).execute(toMap("mode", "export", "file", EXPORT_FILENAME));
        final Path exportFile = Paths.get(EXPORT_FILENAME);
        assertTrue("Export file doesn't exist!", Files.exists(exportFile));
        // stop existing and start new database
        stopSystem();
        startSystem();
        // test import
        app.command(SyncCommand.class).execute(toMap("mode", "import", "file", EXPORT_FILENAME));
        final DatabaseService db = app.getDatabaseService();
        try (final Tx tx = app.tx()) {
            final Result<TestEleven> result = app.nodeQuery(TestEleven.class).getResult();
            assertEquals(10, result.size());
            for (final TestEleven node : result.getResults()) {
                Iterable<Label> labels = node.getNode().getLabels();
                final Set<Label> set = new HashSet<>(Iterables.toList(labels));
                assertEquals(labelCount, set.size());
                assertTrue("First label has to be AbstractNode", set.contains(db.forName(Label.class, "AbstractNode")));
                assertTrue("Second label has to be NodeInterface", set.contains(db.forName(Label.class, "NodeInterface")));
                assertTrue("Third label has to be AccessControllable", set.contains(db.forName(Label.class, "AccessControllable")));
                assertTrue("Fourth label has to be CMISInfo", set.contains(db.forName(Label.class, "CMISInfo")));
                assertTrue("Firth label has to be CMISItemInfo", set.contains(db.forName(Label.class, "CMISItemInfo")));
                assertTrue("Sixth label has to be TestEleven", set.contains(db.forName(Label.class, "TestEleven")));
                assertTrue("Seventh label has to be TestOne", set.contains(db.forName(Label.class, "TestOne")));
                if (tenantIdentifier != null) {
                    assertTrue("Set of labels must contain custom tenant identifier if set", set.contains(db.forName(Label.class, tenantIdentifier)));
                }
            }
            tx.success();
        }
        // clean-up after test
        Files.delete(exportFile);
    } catch (Exception ex) {
        ex.printStackTrace();
        logger.warn("", ex);
        fail("Unexpected exception.");
    }
}
Also used : Path(java.nio.file.Path) TestEleven(org.structr.core.entity.TestEleven) Tx(org.structr.core.graph.Tx) SyncCommand(org.structr.core.graph.SyncCommand) Label(org.structr.api.graph.Label) DatabaseService(org.structr.api.DatabaseService) FrameworkException(org.structr.common.error.FrameworkException) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 7 with Label

use of org.structr.api.graph.Label in project structr by structr.

the class TypeProperty method updateLabels.

public static void updateLabels(final DatabaseService graphDb, final NodeInterface node, final Class newType, final boolean removeUnused) {
    final Set<Label> intersection = new LinkedHashSet<>();
    final Set<Label> toRemove = new LinkedHashSet<>();
    final Set<Label> toAdd = new LinkedHashSet<>();
    final Node dbNode = node.getNode();
    // include optional tenant identifier when modifying labels
    final String tenantIdentifier = graphDb.getTenantIdentifier();
    if (tenantIdentifier != null) {
        toAdd.add(graphDb.forName(Label.class, tenantIdentifier));
    }
    // collect labels that are already present on a node
    for (final Label label : dbNode.getLabels()) {
        toRemove.add(label);
    }
    // collect new labels
    for (final Class supertype : SearchCommand.typeAndAllSupertypes(newType)) {
        final String supertypeName = supertype.getName();
        if (supertypeName.startsWith("org.structr.") || supertypeName.startsWith("com.structr.")) {
            toAdd.add(graphDb.forName(Label.class, supertype.getSimpleName()));
        }
    }
    // calculate intersection
    intersection.addAll(toAdd);
    intersection.retainAll(toRemove);
    // calculate differences
    toAdd.removeAll(intersection);
    toRemove.removeAll(intersection);
    if (removeUnused) {
        // remove difference
        for (final Label remove : toRemove) {
            dbNode.removeLabel(remove);
        }
    }
    // add difference
    for (final Label add : toAdd) {
        dbNode.addLabel(add);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Node(org.structr.api.graph.Node) Label(org.structr.api.graph.Label)

Example 8 with Label

use of org.structr.api.graph.Label in project structr by structr.

the class BoltDatabaseService method getOrCreateLabel.

public Label getOrCreateLabel(final String name) {
    Label label = labelCache.get(name);
    if (label == null) {
        label = new LabelImpl(name);
        labelCache.put(name, label);
    }
    return label;
}
Also used : Label(org.structr.api.graph.Label)

Aggregations

Label (org.structr.api.graph.Label)8 LinkedHashSet (java.util.LinkedHashSet)4 Test (org.junit.Test)3 DatabaseService (org.structr.api.DatabaseService)3 StructrTest (org.structr.common.StructrTest)3 FrameworkException (org.structr.common.error.FrameworkException)3 Tx (org.structr.core.graph.Tx)3 Node (org.structr.api.graph.Node)2 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 Transaction (org.structr.api.Transaction)1 SessionTransaction (org.structr.bolt.SessionTransaction)1 GraphObject (org.structr.core.GraphObject)1 Group (org.structr.core.entity.Group)1 TestEleven (org.structr.core.entity.TestEleven)1 TestFive (org.structr.core.entity.TestFive)1