Search in sources :

Example 1 with App

use of org.structr.core.app.App in project structr by structr.

the class DeploymentTest method test21ExportGrants.

@Test
public void test21ExportGrants() {
    Principal user1 = null;
    Principal user2 = null;
    try (final Tx tx = app.tx()) {
        user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
        user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
        tx.success();
    } catch (FrameworkException ex) {
        fail("Unexpected exception.");
    }
    Assert.assertNotNull("User was not created, test cannot continue", user1);
    Assert.assertNotNull("User was not created, test cannot continue", user2);
    // setup
    final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
    final App app1 = StructrApp.getInstance(context1);
    try (final Tx tx = app1.tx()) {
        final Page page = Page.createNewPage(context1, "test21");
        final Html html = createElement(page, page, "html");
        final Head head = createElement(page, html, "head");
        createElement(page, head, "title", "test21");
        final Body body = createElement(page, html, "body");
        final Div div1 = createElement(page, body, "div");
        final Content content = createContent(page, div1, "<b>Test</b>");
        content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
        // create grants
        page.grant(Permission.read, user2);
        div1.grant(Permission.read, user2);
        content.grant(Permission.read, user2);
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // test
    compare(calculateHash(), true, false);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) NodeAttribute(org.structr.core.graph.NodeAttribute) Head(org.structr.web.entity.html.Head) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Html(org.structr.web.entity.html.Html) Page(org.structr.web.entity.dom.Page) Div(org.structr.web.entity.html.Div) Content(org.structr.web.entity.dom.Content) SecurityContext(org.structr.common.SecurityContext) Body(org.structr.web.entity.html.Body) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 2 with App

use of org.structr.core.app.App in project structr by structr.

the class DeploymentTest method test20ExportOwnership.

@Test
public void test20ExportOwnership() {
    Principal user1 = null;
    Principal user2 = null;
    try (final Tx tx = app.tx()) {
        user1 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user1"));
        user2 = createTestNode(User.class, new NodeAttribute<>(AbstractNode.name, "user2"));
        tx.success();
    } catch (FrameworkException ex) {
        fail("Unexpected exception.");
    }
    Assert.assertNotNull("User was not created, test cannot continue", user1);
    Assert.assertNotNull("User was not created, test cannot continue", user2);
    // setup
    final SecurityContext context1 = SecurityContext.getInstance(user1, AccessMode.Backend);
    final App app1 = StructrApp.getInstance(context1);
    try (final Tx tx = app1.tx()) {
        final Page page = Page.createNewPage(context1, "test20");
        final Html html = createElement(page, page, "html");
        final Head head = createElement(page, html, "head");
        createElement(page, head, "title", "test20");
        final Body body = createElement(page, html, "body");
        final Div div1 = createElement(page, body, "div");
        final Content content = createContent(page, div1, "<b>Test</b>");
        content.setProperty(StructrApp.key(Content.class, "contentType"), "text/html");
        // set owner to different user
        div1.setProperty(AbstractNode.owner, user2);
        content.setProperty(AbstractNode.owner, user2);
        tx.success();
    } catch (FrameworkException fex) {
        fail("Unexpected exception.");
    }
    // test
    compare(calculateHash(), true, false);
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) NodeAttribute(org.structr.core.graph.NodeAttribute) Head(org.structr.web.entity.html.Head) User(org.structr.web.entity.User) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) Html(org.structr.web.entity.html.Html) Page(org.structr.web.entity.dom.Page) Div(org.structr.web.entity.html.Div) Content(org.structr.web.entity.dom.Content) SecurityContext(org.structr.common.SecurityContext) Body(org.structr.web.entity.html.Body) Principal(org.structr.core.entity.Principal) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 3 with App

use of org.structr.core.app.App in project structr by structr.

the class PerformanceTest method testPerformanceOfNodeDeletion.

@Test
public void testPerformanceOfNodeDeletion() {
    final App app = StructrApp.getInstance(setup());
    final List<TestOne> nodes = new LinkedList<>();
    final int number = 1000;
    try {
        try (final Tx tx = app.tx()) {
            nodes.addAll(createNodes(app, TestOne.class, number));
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    // start measuring
    final long t0 = System.currentTimeMillis();
    try {
        final BulkDeleteCommand cmd = app.command(BulkDeleteCommand.class);
        try (final Tx tx = app.tx()) {
            final Iterator<GraphObject> iterator = (Iterator) nodes.iterator();
            cmd.bulkDelete(iterator);
            tx.success();
        }
    } catch (FrameworkException ex) {
        logger.error(ex.toString());
        fail("Unexpected exception");
    }
    final long t1 = System.currentTimeMillis();
    DecimalFormat decimalFormat = new DecimalFormat("0.000000000", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    Double time = (t1 - t0) / 1000.0;
    Double rate = number / ((t1 - t0) / 1000.0);
    logger.info("Deleted {} nodes in {} seconds ({} per s)", number, decimalFormat.format(time), decimalFormat.format(rate));
    assertTrue("Deletion rate of nodes too low, expected > 100, was " + rate, rate > 50);
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) Tx(org.structr.core.graph.Tx) FrameworkException(org.structr.common.error.FrameworkException) BulkDeleteCommand(org.structr.core.graph.BulkDeleteCommand) DecimalFormat(java.text.DecimalFormat) GraphObject(org.structr.core.GraphObject) LinkedList(java.util.LinkedList) Iterator(java.util.Iterator) TestOne(org.structr.web.entity.TestOne) Test(org.junit.Test) StructrUiTest(org.structr.web.StructrUiTest)

Example 4 with App

use of org.structr.core.app.App in project structr by structr.

the class NodeServiceCommand method bulkGraphOperation.

/**
 * Executes the given operation on all nodes in the given list.
 *
 * @param <T>
 * @param securityContext
 * @param iterator the iterator that provides the nodes to operate on
 * @param commitCount
 * @param description
 * @param operation the operation to execute
 * @param validation
 * @return the number of nodes processed
 */
public <T> long bulkGraphOperation(final SecurityContext securityContext, final Iterator<T> iterator, final long commitCount, String description, final BulkGraphOperation<T> operation, boolean validation) {
    final Predicate<Long> condition = operation.getCondition();
    final App app = StructrApp.getInstance(securityContext);
    final boolean doValidation = operation.doValidation();
    final boolean doCallbacks = operation.doCallbacks();
    final boolean doNotifications = operation.doNotifications();
    long objectCount = 0L;
    boolean active = true;
    while (active) {
        active = false;
        try (final Tx tx = app.tx(doValidation, doCallbacks, doNotifications)) {
            while (iterator.hasNext() && (condition == null || condition.accept(objectCount))) {
                T node = iterator.next();
                active = true;
                try {
                    operation.handleGraphObject(securityContext, node);
                } catch (Throwable t) {
                    operation.handleThrowable(securityContext, t, node);
                }
                // commit transaction after commitCount
                if ((++objectCount % commitCount) == 0) {
                    break;
                }
            }
            tx.success();
        } catch (Throwable t) {
            // bulk transaction failed, what to do?
            operation.handleTransactionFailure(securityContext, t);
        }
        if (description != null) {
            info("{}: {} objects processed", description, objectCount);
        }
    }
    return objectCount;
}
Also used : StructrApp(org.structr.core.app.StructrApp) App(org.structr.core.app.App) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 5 with App

use of org.structr.core.app.App in project structr by structr.

the class SyncCommand method exportToFile.

// ----- static methods -----
/**
 * Exports the whole structr database to a file with the given name.
 *
 * @param graphDb
 * @param fileName
 * @param includeFiles
 * @throws FrameworkException
 */
public static void exportToFile(final DatabaseService graphDb, final String fileName, final String query, final boolean includeFiles) throws FrameworkException {
    final App app = StructrApp.getInstance();
    try (final Tx tx = app.tx()) {
        final NodeFactory nodeFactory = new NodeFactory(SecurityContext.getSuperUserInstance());
        final RelationshipFactory relFactory = new RelationshipFactory(SecurityContext.getSuperUserInstance());
        final Set<AbstractNode> nodes = new HashSet<>();
        final Set<AbstractRelationship> rels = new HashSet<>();
        boolean conditionalIncludeFiles = includeFiles;
        if (query != null) {
            logger.info("Using Cypher query {} to determine export set, disabling export of files", query);
            conditionalIncludeFiles = false;
            final List<GraphObject> result = StructrApp.getInstance().cypher(query, null);
            for (final GraphObject obj : result) {
                if (obj.isNode()) {
                    nodes.add((AbstractNode) obj.getSyncNode());
                } else {
                    rels.add((AbstractRelationship) obj.getSyncRelationship());
                }
            }
            logger.info("Query returned {} nodes and {} relationships.", new Object[] { nodes.size(), rels.size() });
        } else {
            nodes.addAll(nodeFactory.bulkInstantiate(graphDb.getAllNodes()));
            rels.addAll(relFactory.bulkInstantiate(graphDb.getAllRelationships()));
        }
        try (final FileOutputStream fos = new FileOutputStream(fileName)) {
            exportToStream(fos, nodes, rels, null, conditionalIncludeFiles);
        }
        tx.success();
    } catch (Throwable t) {
        logger.warn("", t);
        throw new FrameworkException(500, t.getMessage());
    }
}
Also used : App(org.structr.core.app.App) StructrApp(org.structr.core.app.StructrApp) FrameworkException(org.structr.common.error.FrameworkException) AbstractNode(org.structr.core.entity.AbstractNode) AbstractRelationship(org.structr.core.entity.AbstractRelationship) GraphObject(org.structr.core.GraphObject) FileOutputStream(java.io.FileOutputStream) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Aggregations

App (org.structr.core.app.App)296 StructrApp (org.structr.core.app.StructrApp)294 Tx (org.structr.core.graph.Tx)201 FrameworkException (org.structr.common.error.FrameworkException)176 LinkedList (java.util.LinkedList)60 SecurityContext (org.structr.common.SecurityContext)56 PropertyMap (org.structr.core.property.PropertyMap)41 Folder (org.structr.web.entity.Folder)38 GraphObject (org.structr.core.GraphObject)35 Principal (org.structr.core.entity.Principal)31 IOException (java.io.IOException)30 AbstractFile (org.structr.web.entity.AbstractFile)27 AbstractNode (org.structr.core.entity.AbstractNode)26 Test (org.junit.Test)24 NodeAttribute (org.structr.core.graph.NodeAttribute)24 File (org.structr.web.entity.File)23 NodeInterface (org.structr.core.graph.NodeInterface)22 SchemaNode (org.structr.core.entity.SchemaNode)19 PropertyKey (org.structr.core.property.PropertyKey)17 Map (java.util.Map)16