Search in sources :

Example 1 with Label

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

the class DefaultFactoryDefinition method determineNodeType.

@Override
public Class determineNodeType(final Node node) {
    // check deletion first
    if (TransactionCommand.isDeleted(node)) {
        return null;
    }
    final String type = GraphObject.type.dbName();
    if (node.hasProperty(type)) {
        final Object obj = node.getProperty(type);
        if (obj != null) {
            final Class nodeType = StructrApp.getConfiguration().getNodeEntities().get(obj.toString());
            if (nodeType != null) {
                return nodeType;
            }
        }
    } else {
        if (externalNodeTypeName == null) {
            // try to determine external node
            // type name from configuration
            externalNodeTypeName = Settings.ForeignTypeName.getValue();
        }
        if (externalNodeTypeName != null && node.hasProperty(externalNodeTypeName)) {
            Object typeObj = node.getProperty(externalNodeTypeName);
            if (typeObj != null) {
                // String externalNodeType = typeObj.toString();
                // initialize dynamic type
                // genericNodeExtender.getType(externalNodeType);
                // return dynamic type
                final Class dynamicType = StructrApp.getConfiguration().getNodeEntityClass(typeObj.toString());
                if (dynamicType != null) {
                    return dynamicType;
                }
            }
        }
        final Iterable<Label> labels = node.getLabels();
        if (labels != null) {
            final List<String> sortedLabels = Iterables.toList(Iterables.map(new LabelExtractor(), labels));
            Collections.sort(sortedLabels);
            final String typeName = StringUtils.join(sortedLabels, "");
            // return dynamic type
            final Class dynamicType = StructrApp.getConfiguration().getNodeEntityClass(typeName);
            if (dynamicType != null) {
                return dynamicType;
            }
        }
    }
    return getGenericNodeType();
}
Also used : Label(org.structr.api.graph.Label) GraphObject(org.structr.core.GraphObject)

Example 2 with Label

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

the class NodeInfo method extractTypes.

private void extractTypes(final Node node) {
    // first try: labels
    // AM 2015-06-26: Changed the behaviour here: In case of multiple labels, don't put them all
    // into the set of potential types but rather create a combined type.
    // E.g. a node with the two labels 'Person' and 'Entity' will get a type 'EntityPerson'
    final List<String> labelStrings = new ArrayList<>();
    for (final Label label : node.getLabels()) {
        labelStrings.add(label.name());
    }
    // Collections.sort(labelStrings);
    addType(StringUtils.join(labelStrings, ""));
    // second try: type attribute
    if (node.hasProperty("type")) {
        final String type = node.getProperty("type").toString();
        addType(type.replaceAll("[\\W]+", ""));
    }
    if (types.isEmpty() && !properties.keySet().isEmpty()) {
        // fifth try: analyze properties
        final StringBuilder buf = new StringBuilder("NodeWith");
        for (final String key : properties.keySet()) {
            buf.append(StringUtils.capitalize(key));
        }
        types.add(buf.toString());
    }
}
Also used : ArrayList(java.util.ArrayList) Label(org.structr.api.graph.Label)

Example 3 with Label

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

the class MaintenanceTest method testBulkCreateLabelsCommand.

@Test
public void testBulkCreateLabelsCommand() {
    try {
        final DatabaseService graphDb = app.getDatabaseService();
        final Set<Label> expectedLabels = new LinkedHashSet<>();
        expectedLabels.add(graphDb.forName(Label.class, "Principal"));
        expectedLabels.add(graphDb.forName(Label.class, "Group"));
        expectedLabels.add(graphDb.forName(Label.class, "AccessControllable"));
        expectedLabels.add(graphDb.forName(Label.class, "AbstractNode"));
        expectedLabels.add(graphDb.forName(Label.class, "NodeInterface"));
        expectedLabels.add(graphDb.forName(Label.class, "CMISInfo"));
        expectedLabels.add(graphDb.forName(Label.class, "CMISItemInfo"));
        if (graphDb.getTenantIdentifier() != null) {
            expectedLabels.add(graphDb.forName(Label.class, graphDb.getTenantIdentifier()));
        }
        // intentionally create raw Neo4j transaction and create nodes in there
        try (Transaction tx = graphDb.beginTx()) {
            for (int i = 0; i < 100; i++) {
                final Node test = graphDb.createNode(Collections.EMPTY_SET, Collections.EMPTY_MAP);
                // set ID and type so that the rebuild index command identifies it as a Structr node.
                test.setProperty("type", "Group");
                test.setProperty("id", UUID.randomUUID().toString().replace("-", ""));
            }
            // this is important.... :)
            tx.success();
        }
        /*
			 * This test will fail with the new Neo4j 3.0 Bolt interface, because
			 * there is no separation between a (Lucene-based) index and the
			 * database values any more. Nodes are selected by their 'type'
			 * property and will always be found even if NOT created using Structr
			 * methods.
			// nodes should not be found yet..
			try (final Tx tx = app.tx()) {

				// check nodes, we should find no Groups here
				assertEquals(0, app.nodeQuery(Group.class).getResult().size());
			}
			 */
        // test rebuild index and create labels
        // app.command(BulkRebuildIndexCommand.class).execute(new LinkedHashMap<>());
        app.command(BulkCreateLabelsCommand.class).execute(new LinkedHashMap<>());
        // nodes should now be visible to Structr
        try (final Tx tx = app.tx()) {
            // check nodes, we should find 100 Groups here
            assertEquals(100, app.nodeQuery(Group.class).getResult().size());
            // check nodes
            for (final Group group : app.nodeQuery(Group.class)) {
                final Set<Label> labels = Iterables.toSet(group.getNode().getLabels());
                assertEquals("Invalid number of labels", expectedLabels.size(), labels.size());
                assertTrue("Invalid labels found", labels.containsAll(expectedLabels));
            }
            tx.success();
        }
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception.");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Group(org.structr.core.entity.Group) Transaction(org.structr.api.Transaction) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) BulkCreateLabelsCommand(org.structr.core.graph.BulkCreateLabelsCommand) Node(org.structr.api.graph.Node) Label(org.structr.api.graph.Label) DatabaseService(org.structr.api.DatabaseService) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 4 with Label

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

the class PropertyTest method testModifyType.

// ----- type property tests -----
@Test
public void testModifyType() {
    final DatabaseService db = StructrApp.getInstance().getDatabaseService();
    final Set<Label> labelsBefore = new LinkedHashSet<>();
    final Set<Label> labelsAfter = new LinkedHashSet<>();
    String id = null;
    labelsBefore.add(db.forName(Label.class, AccessControllable.class.getSimpleName()));
    labelsBefore.add(db.forName(Label.class, TestFour.class.getSimpleName()));
    labelsAfter.add(db.forName(Label.class, AccessControllable.class.getSimpleName()));
    labelsAfter.add(db.forName(Label.class, TestFive.class.getSimpleName()));
    try (final Tx tx = app.tx()) {
        // create entity of type TestFour
        final TestFour testEntity = createTestNode(TestFour.class);
        // check if node exists
        assertNotNull(testEntity);
        // check labels before type change
        assertTrue(Iterables.toSet(testEntity.getNode().getLabels()).containsAll(labelsBefore));
        // save ID for later use
        id = testEntity.getUuid();
        // change type to TestFive
        // system properties have to be unlocked now, admin rights are not enough anymore
        testEntity.unlockSystemPropertiesOnce();
        testEntity.setProperty(GraphObject.type, TestFive.class.getSimpleName());
        // commit transaction
        tx.success();
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        final TestFive testEntity = app.get(TestFive.class, id);
        assertNotNull(testEntity);
        // check labels after type change
        assertTrue(Iterables.toSet(testEntity.getNode().getLabels()).containsAll(labelsAfter));
    } catch (FrameworkException fex) {
        logger.warn("", fex);
        fail("Unexpected exception");
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TestFour(org.structr.core.entity.TestFour) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Label(org.structr.api.graph.Label) DatabaseService(org.structr.api.DatabaseService) TestFive(org.structr.core.entity.TestFive) StructrTest(org.structr.common.StructrTest) Test(org.junit.Test)

Example 5 with Label

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

the class NodeWrapper method getLabels.

@Override
public Iterable<Label> getLabels() {
    assertNotStale();
    final SessionTransaction tx = db.getCurrentTransaction();
    final Map<String, Object> map = new HashMap<>();
    final List<Label> result = new LinkedList<>();
    final String tenantIdentifier = db.getTenantIdentifier();
    map.put("id", id);
    // execute query
    for (final String label : tx.getStrings("MATCH (n" + (tenantIdentifier != null ? ":" + tenantIdentifier : "") + ") WHERE ID(n) = {id} RETURN LABELS(n)", map)) {
        result.add(db.forName(Label.class, label));
    }
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SessionTransaction(org.structr.bolt.SessionTransaction) Label(org.structr.api.graph.Label) LinkedList(java.util.LinkedList)

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