use of org.structr.core.graph.NodeInterface in project structr by structr.
the class IncomingFunction method apply.
@Override
public Object apply(final ActionContext ctx, final Object caller, final Object[] sources) throws FrameworkException {
if (arrayHasMinLengthAndMaxLengthAndAllElementsNotNull(sources, 1, 2)) {
final RelationshipFactory factory = new RelationshipFactory(ctx.getSecurityContext());
final Object source = sources[0];
if (source instanceof NodeInterface) {
final NodeInterface node = (NodeInterface) source;
if (sources.length > 1) {
final Object relType = sources[1];
if (relType != null && relType instanceof String) {
final String relTypeName = (String) relType;
return factory.bulkInstantiate(node.getNode().getRelationships(Direction.INCOMING, RelationshipType.forName(relTypeName)));
}
} else {
return factory.bulkInstantiate(node.getNode().getRelationships(Direction.INCOMING));
}
} else {
logger.warn("Error: entity is not a node. Parameters: {}", getParametersAsString(sources));
return "Error: entity is not a node.";
}
} else {
logParameterError(caller, sources, ctx.isJavaScriptContext());
}
return "";
}
use of org.structr.core.graph.NodeInterface in project structr by structr.
the class AccessControlTest method test07ResultCount.
@Test
public void test07ResultCount() {
// remove auto-generated resource access objects
clearResourceAccess();
try {
final Class type = TestOne.class;
final List<NodeInterface> nodes = createTestNodes(type, 10);
try (final Tx tx = app.tx()) {
nodes.get(3).setProperty(AbstractNode.visibleToPublicUsers, true);
nodes.get(5).setProperty(AbstractNode.visibleToPublicUsers, true);
nodes.get(7).setProperty(AbstractNode.visibleToPublicUsers, true);
tx.success();
}
SecurityContext publicContext = SecurityContext.getInstance(null, AccessMode.Frontend);
try (final Tx tx = app.tx()) {
Result result = StructrApp.getInstance(publicContext).nodeQuery(type).sort(AbstractNode.createdDate).getResult();
assertEquals(3, result.size());
assertEquals(3, (int) result.getRawResultCount());
// do not test order of elements
// assertEquals(nodes.get(3).getUuid(), result.get(0).getUuid());
// assertEquals(nodes.get(5).getUuid(), result.get(1).getUuid());
// assertEquals(nodes.get(7).getUuid(), result.get(2).getUuid());
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.graph.NodeInterface in project structr by structr.
the class AccessControlTest method test07ResultCountWithPaging.
@Test
public void test07ResultCountWithPaging() {
// remove auto-generated resource access objects
clearResourceAccess();
try {
final Class type = TestOne.class;
final List<NodeInterface> nodes = createTestNodes(type, 10);
int count = 0;
try (final Tx tx = app.tx()) {
// add names to make sorting work...
for (final NodeInterface node : nodes) {
node.setProperty(AbstractNode.name, "node0" + count++);
}
nodes.get(3).setProperty(AbstractNode.visibleToPublicUsers, true);
nodes.get(5).setProperty(AbstractNode.visibleToPublicUsers, true);
nodes.get(7).setProperty(AbstractNode.visibleToPublicUsers, true);
nodes.get(9).setProperty(AbstractNode.visibleToPublicUsers, true);
tx.success();
}
SecurityContext publicContext = SecurityContext.getInstance(null, AccessMode.Frontend);
PropertyKey sortKey = AbstractNode.name;
boolean sortDesc = false;
int pageSize = 2;
int page = 1;
try (final Tx tx = app.tx()) {
Result result = StructrApp.getInstance(publicContext).nodeQuery(type).sort(sortKey).order(sortDesc).page(page).pageSize(pageSize).getResult();
assertEquals(2, result.size());
assertEquals(4, (int) result.getRawResultCount());
assertEquals(nodes.get(3).getUuid(), result.get(0).getUuid());
assertEquals(nodes.get(5).getUuid(), result.get(1).getUuid());
}
} catch (FrameworkException ex) {
logger.warn("", ex);
fail("Unexpected exception");
}
}
use of org.structr.core.graph.NodeInterface in project structr by structr.
the class BasicTest method test01DeleteNode.
/**
* Test successful deletion of a node.
*
* The node shouldn't be found afterwards.
* Creation and deletion are executed in two different transactions.
*/
@Test
public void test01DeleteNode() {
try {
final PropertyMap props = new PropertyMap();
final String type = "GenericNode";
final String name = "GenericNode-name";
NodeInterface node = null;
String uuid = null;
props.put(AbstractNode.type, type);
props.put(AbstractNode.name, name);
try (final Tx tx = app.tx()) {
node = app.create(GenericNode.class, props);
tx.success();
}
assertTrue(node != null);
try (final Tx tx = app.tx()) {
uuid = node.getUuid();
}
try (final Tx tx = app.tx()) {
app.delete(node);
tx.success();
}
try (final Tx tx = app.tx()) {
Result result = app.nodeQuery().uuid(uuid).getResult();
assertEquals("Node should have been deleted", 0, result.size());
} catch (FrameworkException fe) {
}
} catch (FrameworkException ex) {
logger.warn("", ex);
logger.error(ex.toString());
fail("Unexpected exception");
}
}
use of org.structr.core.graph.NodeInterface in project structr by structr.
the class BasicTest method test06CascadeDeleteBidirectional.
/**
* DELETE_INCOMING + DELETE_OUTGOING should trigger delete cascade from start to end node
* and from end node to start node
*/
@Test
public void test06CascadeDeleteBidirectional() {
try {
// Create a relationship with DELETE_INCOMING
AbstractRelationship rel = cascadeRel(TestOne.class, TestTwo.class, Relation.TARGET_TO_SOURCE | Relation.SOURCE_TO_TARGET);
NodeInterface sourceNode;
NodeInterface targetNode;
String startNodeId;
String endNodeId;
try (final Tx tx = app.tx()) {
startNodeId = rel.getSourceNode().getUuid();
endNodeId = rel.getTargetNode().getUuid();
sourceNode = rel.getSourceNode();
}
deleteCascade(sourceNode);
try (final Tx tx = app.tx()) {
// Start node should not be found after deletion
assertNodeNotFound(startNodeId);
// End node should not be found after deletion of start node
assertNodeNotFound(endNodeId);
}
// Create a relationship with DELETE_INCOMING
rel = cascadeRel(TestOne.class, TestTwo.class, Relation.TARGET_TO_SOURCE | Relation.SOURCE_TO_TARGET);
try (final Tx tx = app.tx()) {
startNodeId = rel.getSourceNode().getUuid();
endNodeId = rel.getTargetNode().getUuid();
targetNode = rel.getTargetNode();
}
deleteCascade(targetNode);
try (final Tx tx = app.tx()) {
// End node should not be found after deletion
assertNodeNotFound(endNodeId);
// Start node should not be found after deletion of end node
assertNodeNotFound(startNodeId);
}
} catch (FrameworkException ex) {
logger.warn("", ex);
logger.error(ex.toString());
fail("Unexpected exception");
}
}
Aggregations