Search in sources :

Example 91 with Tx

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

the class StructrLDAPClientModuleTest method createTestNode.

protected <T extends AbstractNode> T createTestNode(final Class<T> type, final NodeAttribute... attributes) throws FrameworkException {
    try (final Tx tx = app.tx()) {
        final T newNode = app.create(type, attributes);
        tx.success();
        return newNode;
    }
}
Also used : Tx(org.structr.core.graph.Tx)

Example 92 with Tx

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

the class StructrLDAPClientModuleTest method createTestNode.

protected <T extends AbstractNode> T createTestNode(final Class<T> type, final PropertyMap props) throws FrameworkException {
    props.put(AbstractNode.type, type.getSimpleName());
    try (final Tx tx = app.tx()) {
        final T newNode = app.create(type, props);
        tx.success();
        return newNode;
    }
}
Also used : Tx(org.structr.core.graph.Tx)

Example 93 with Tx

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

the class StructrLDAPClientModuleTest method createTestRelationships.

protected <T extends Relation> List<T> createTestRelationships(final Class<T> relType, final int number) throws FrameworkException {
    List<GenericNode> nodes = createTestNodes(GenericNode.class, 2);
    final NodeInterface startNode = nodes.get(0);
    final NodeInterface endNode = nodes.get(1);
    try (final Tx tx = app.tx()) {
        List<T> rels = new LinkedList<>();
        for (int i = 0; i < number; i++) {
            rels.add((T) app.create(startNode, endNode, relType));
        }
        tx.success();
        return rels;
    }
}
Also used : Tx(org.structr.core.graph.Tx) GenericNode(org.structr.core.entity.GenericNode) NodeInterface(org.structr.core.graph.NodeInterface) LinkedList(java.util.LinkedList)

Example 94 with Tx

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

the class TestLDAPClient method testLDAPClient.

@Test
public void testLDAPClient() {
    final Class type = StructrApp.getConfiguration().getNodeEntityClass("LDAPUser");
    Assert.assertNotNull("Type LDAPUser should exist", type);
    try (final Tx tx = app.tx()) {
        app.create(User.class, new NodeAttribute<>(StructrApp.key(User.class, "name"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "password"), "admin"), new NodeAttribute<>(StructrApp.key(User.class, "isAdmin"), true));
        tx.success();
    } catch (Throwable t) {
        fail("Unexpected exception");
    }
    try (final Tx tx = app.tx()) {
        app.create(type, new NodeAttribute<>(StructrApp.key(LDAPUser.class, "name"), "test"), new NodeAttribute<>(StructrApp.key(LDAPUser.class, "distinguishedName"), "distinguishedName"), new NodeAttribute<>(StructrApp.key(LDAPUser.class, "description"), "description"), new NodeAttribute<>(StructrApp.key(LDAPUser.class, "commonName"), "commonName"), new NodeAttribute<>(StructrApp.key(LDAPUser.class, "entryUuid"), "entryUuid"));
        tx.success();
    } catch (Throwable t) {
        fail("Unexpected exception");
    }
    RestAssured.given().filter(ResponseLoggingFilter.logResponseTo(System.out)).contentType("application/json; charset=UTF-8").headers("X-User", "admin", "X-Password", "admin").expect().statusCode(200).when().get("/LDAPUser");
}
Also used : Tx(org.structr.core.graph.Tx) Test(org.junit.Test)

Example 95 with Tx

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

the class StructrLDAPWrapper method add.

public void add(final Entry entry) throws LdapException {
    try (final Tx tx = app().tx()) {
        // create while descending
        final Dn dn = entry.getDn();
        final LDAPNode parent = find(dn.getParent());
        if (parent != null) {
            final Attribute objectClasses = entry.get(schemaManager.getAttributeType(SchemaConstants.OBJECT_CLASS_AT_OID));
            final ObjectClassRegistry reg = schemaManager.getObjectClassRegistry();
            final Set<String> classes = new LinkedHashSet<>();
            final Rdn rdn = dn.getRdn();
            String mainClass = null;
            // make rdn schema aware
            if (!rdn.isSchemaAware()) {
                rdn.apply(schemaManager);
            }
            if (objectClasses != null) {
                for (final Value<?> value : objectClasses) {
                    final String cls = value.getString();
                    final String objectClassOid = reg.getOidByName(cls);
                    if (reg.get(objectClassOid).isStructural()) {
                        mainClass = cls;
                    } else {
                        classes.add(cls);
                    }
                }
                final LDAPNode newChild = parent.createChild(rdn.getNormName(), rdn.getName(), mainClass, classes);
                if (newChild != null) {
                    for (final Attribute attr : entry) {
                        AttributeType type = attr.getAttributeType();
                        String oid = null;
                        if (type != null) {
                            oid = type.getOid();
                        } else {
                            type = schemaManager.getAttributeType(attr.getUpId());
                            oid = type.getOid();
                        }
                        newChild.createAttribute(oid, attr.getUpId(), attr);
                    }
                } else {
                    logger.warn("Unable to add entry {}, could not create new instance", entry);
                }
            } else {
                logger.warn("Unable to add entry {}, could not determine object class(es)", entry);
            }
        } else {
            logger.warn("Unable to add entry {}, parent not found", entry);
        }
        tx.success();
    } catch (FrameworkException fex) {
        handleException(fex);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) LDAPNode(org.structr.ldap.entity.LDAPNode) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) DefaultAttribute(org.apache.directory.api.ldap.model.entry.DefaultAttribute) NodeAttribute(org.structr.core.graph.NodeAttribute) LDAPAttribute(org.structr.ldap.entity.LDAPAttribute) Attribute(org.apache.directory.api.ldap.model.entry.Attribute) AttributeType(org.apache.directory.api.ldap.model.schema.AttributeType) ObjectClassRegistry(org.apache.directory.api.ldap.model.schema.registries.ObjectClassRegistry) Dn(org.apache.directory.api.ldap.model.name.Dn) Rdn(org.apache.directory.api.ldap.model.name.Rdn)

Aggregations

Tx (org.structr.core.graph.Tx)892 FrameworkException (org.structr.common.error.FrameworkException)684 Test (org.junit.Test)461 App (org.structr.core.app.App)201 StructrApp (org.structr.core.app.StructrApp)201 StructrUiTest (org.structr.web.StructrUiTest)139 NodeInterface (org.structr.core.graph.NodeInterface)117 StructrTest (org.structr.common.StructrTest)108 IOException (java.io.IOException)105 PropertyMap (org.structr.core.property.PropertyMap)102 LinkedList (java.util.LinkedList)99 TestOne (org.structr.core.entity.TestOne)98 File (org.structr.web.entity.File)83 Page (org.structr.web.entity.dom.Page)71 Principal (org.structr.core.entity.Principal)65 Folder (org.structr.web.entity.Folder)65 PropertyKey (org.structr.core.property.PropertyKey)62 NodeAttribute (org.structr.core.graph.NodeAttribute)57 SchemaNode (org.structr.core.entity.SchemaNode)45 AbstractFile (org.structr.web.entity.AbstractFile)44