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);
}
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);
}
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);
}
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;
}
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());
}
}
Aggregations