use of org.structr.api.graph.Node in project structr by structr.
the class LinkedListNodeImpl method listRemove.
/**
* Removes the current element from the list defined by this
* LinkedListManager.
*
* @param currentElement the element to be removed
*/
@Override
public void listRemove(final T currentElement) throws FrameworkException {
final T previousElement = listGetPrevious(currentElement);
final T nextElement = listGetNext(currentElement);
if (currentElement != null) {
if (previousElement != null) {
unlinkNodes(getSiblingLinkType(), previousElement, currentElement);
}
if (nextElement != null) {
unlinkNodes(getSiblingLinkType(), currentElement, nextElement);
}
}
if (previousElement != null && nextElement != null) {
Node previousNode = previousElement.getNode();
Node nextNode = nextElement.getNode();
if (previousNode != null && nextNode != null) {
linkNodes(getSiblingLinkType(), previousElement, nextElement);
}
}
}
use of org.structr.api.graph.Node in project structr by structr.
the class NodeWrapperTest method testBasicTransactions.
@Test
public void testBasicTransactions() {
try {
Settings.DatabasePath.setValue(Files.createTempDirectory("structr-test").toFile().getAbsolutePath());
Settings.ConnectionUrl.setValue(Settings.TestingConnectionUrl.getValue());
} catch (IOException ioex) {
logger.warn("", ioex);
}
final BoltDatabaseService s = new BoltDatabaseService();
s.initialize();
long id = 0L;
// create new node
try (final Transaction tx = s.beginTx()) {
final Node node = s.createNode(Collections.EMPTY_SET, Collections.EMPTY_MAP);
id = node.getId();
tx.success();
}
// set property
try (final Transaction tx = s.beginTx()) {
final Node node = s.getNodeById(id);
Assert.assertNotNull(node);
node.setProperty("name", "Test");
tx.success();
}
// set property and don't commit transaction
try (final Transaction tx = s.beginTx()) {
final Node node = s.getNodeById(id);
Assert.assertNotNull(node);
node.setProperty("name", "Fail");
}
// check property value
try (final Transaction tx = s.beginTx()) {
final Node node = s.getNodeById(id);
Assert.assertNotNull(node);
Assert.assertEquals("Invalid setProperty result", "Test", node.getProperty("name"));
tx.success();
}
// remove property
try (final Transaction tx = s.beginTx()) {
final Node node = s.getNodeById(id);
Assert.assertNotNull(node);
node.removeProperty("name");
tx.success();
}
// check property value
try (final Transaction tx = s.beginTx()) {
final Node node = s.getNodeById(id);
Assert.assertNotNull(node);
Assert.assertNull("Invalid removeProperty result", node.getProperty("name"));
node.setProperty("key1", "value1");
node.setProperty("key2", 2);
final Iterable<String> keys = node.getPropertyKeys();
final List<String> list = Iterables.toList(keys);
Assert.assertEquals("Invalid getPropertyKeys result", 2, list.size());
Assert.assertEquals("Invalid getPropertyKeys result", "key1", list.get(0));
Assert.assertEquals("Invalid getPropertyKeys result", "key2", list.get(1));
tx.success();
}
s.shutdown();
}
use of org.structr.api.graph.Node in project structr by structr.
the class NodeRelationshipStatisticsCommand method execute.
public Map<String, Long> execute(AbstractNode sNode, Direction dir) throws FrameworkException {
final Map<String, Long> statistics = new LinkedHashMap<>();
final Node node = sNode.getNode();
Iterable<Relationship> rels = null;
if (dir != null) {
rels = node.getRelationships(dir);
} else {
rels = node.getRelationships();
}
try {
// use temporary map to avoid frequent construction of Long values when increasing..
Map<String, LongValueHolder> values = new LinkedHashMap<>();
for (Relationship r : rels) {
final String relType = r.getType().name();
LongValueHolder count = values.get(relType);
if (count == null) {
count = new LongValueHolder();
values.put(relType, count);
}
count.inc();
}
// create results from temporary map
for (Entry<String, LongValueHolder> entry : values.entrySet()) {
final String key = entry.getKey();
LongValueHolder value = entry.getValue();
statistics.put(key, value.getValue());
}
} catch (RuntimeException e) {
logger.warn("Exception occured.", e);
}
return statistics;
}
use of org.structr.api.graph.Node in project structr by structr.
the class SyncCommand method checkAndMerge.
private static boolean checkAndMerge(final NodeInterface node, final Set<Long> deletedNodes, final Set<Long> deletedRels) throws FrameworkException {
final Class type = node.getClass();
final String name = node.getName();
final List<NodeInterface> existingNodes = StructrApp.getInstance().nodeQuery(type).andName(name).getAsList();
for (NodeInterface existingNode : existingNodes) {
final Node sourceNode = node.getNode();
final Node targetNode = existingNode.getNode();
// skip newly created node
if (sourceNode.getId() == targetNode.getId()) {
continue;
}
logger.info("Found existing schema node {}, merging!", name);
copyProperties(sourceNode, targetNode);
// handle outgoing rels
for (final Relationship outRel : sourceNode.getRelationships(Direction.OUTGOING)) {
final Node otherNode = outRel.getEndNode();
final Relationship newRel = targetNode.createRelationshipTo(otherNode, outRel.getType());
copyProperties(outRel, newRel);
// report deletion
deletedRels.add(outRel.getId());
// remove previous relationship
outRel.delete();
System.out.println("############################################ Deleting relationship " + outRel.getId());
}
// handle incoming rels
for (final Relationship inRel : sourceNode.getRelationships(Direction.INCOMING)) {
final Node otherNode = inRel.getStartNode();
final Relationship newRel = otherNode.createRelationshipTo(targetNode, inRel.getType());
copyProperties(inRel, newRel);
// report deletion
deletedRels.add(inRel.getId());
// remove previous relationship
inRel.delete();
System.out.println("############################################ Deleting relationship " + inRel.getId());
}
// merge properties, views and methods
final Map<String, List<Node>> groupedNodes = groupByTypeAndName(Iterables.toList(Iterables.map(new EndNodes(), targetNode.getRelationships(Direction.OUTGOING))));
for (final List<Node> nodes : groupedNodes.values()) {
final int size = nodes.size();
if (size > 1) {
final Node groupTargetNode = nodes.get(0);
for (final Node groupSourceNode : nodes.subList(1, size)) {
copyProperties(groupSourceNode, groupTargetNode);
// delete relationships of merged node
for (final Relationship groupRel : groupSourceNode.getRelationships()) {
deletedRels.add(groupRel.getId());
groupRel.delete();
}
// delete merged node
deletedNodes.add(groupSourceNode.getId());
groupSourceNode.delete();
System.out.println("############################################ Deleting node " + groupSourceNode.getId());
}
}
}
// report deletion
deletedNodes.add(sourceNode.getId());
// delete
sourceNode.delete();
System.out.println("############################################ Deleting node " + sourceNode.getId());
return true;
}
return false;
}
use of org.structr.api.graph.Node in project structr by structr.
the class SyncCommand method exportDatabase.
private static void exportDatabase(final ZipOutputStream zos, final OutputStream outputStream, final Iterable<? extends NodeInterface> nodes, final Iterable<? extends RelationshipInterface> relationships) throws IOException, FrameworkException {
// start database zip entry
final ZipEntry dbEntry = new ZipEntry(STRUCTR_ZIP_DB_NAME);
final DataOutputStream dos = new DataOutputStream(outputStream);
final String uuidPropertyName = GraphObject.id.dbName();
int nodeCount = 0;
int relCount = 0;
zos.putNextEntry(dbEntry);
for (NodeInterface nodeObject : nodes) {
// skip schema
if (nodeObject instanceof AbstractSchemaNode) {
continue;
}
final Node node = nodeObject.getNode();
// ignore non-structr nodes
if (node.hasProperty(GraphObject.id.dbName())) {
outputStream.write('N');
for (String key : node.getPropertyKeys()) {
serialize(dos, key);
serialize(dos, node.getProperty(key));
}
// do not use platform-specific line ending here!
dos.write('\n');
nodeCount++;
}
}
dos.flush();
for (RelationshipInterface relObject : relationships) {
final Relationship rel = relObject.getRelationship();
// ignore non-structr nodes
if (rel.hasProperty(GraphObject.id.dbName())) {
final Node startNode = rel.getStartNode();
final Node endNode = rel.getEndNode();
if (startNode.hasProperty(uuidPropertyName) && endNode.hasProperty(uuidPropertyName)) {
String startId = (String) startNode.getProperty(uuidPropertyName);
String endId = (String) endNode.getProperty(uuidPropertyName);
outputStream.write('R');
serialize(dos, startId);
serialize(dos, endId);
serialize(dos, rel.getType().name());
for (String key : rel.getPropertyKeys()) {
serialize(dos, key);
serialize(dos, rel.getProperty(key));
}
// do not use platform-specific line ending here!
dos.write('\n');
relCount++;
}
}
}
dos.flush();
// finish db entry
zos.closeEntry();
logger.info("Exported {} nodes and {} rels", new Object[] { nodeCount, relCount });
}
Aggregations