How to get Dynatree's parent node details - dynatree

Is there any way to find dynatree's root node details? Supposed tree has 5 level and i selected fifth level's node . I want to get selected nodes and its parents details till root.
Thanks in advance.

The visitParents function on the node should do what you want:
node.visitParents(function (node) {
// do stuff with node
}, true);
Passing true as the second parameter includes the node as well as it's parents.

Try below method:
node.getKeyPath()
It should return string of keys from leaf to root node like below
/_3/_23/_26/_27
You should be then able to retrieve the any node details of that branch by parsing above string
var node = $("#tree").dynatree("getTree").getNodeByKey("3")
This might not be the exact solution but should give you the direction to get to your solution.

var parent = node.getParent();
var parentnode = parent.data.key;
In parentnode you will get the value of parent node.

Related

SQL query - fetch last nodes from graph/tree

I'm looking for help. I want to fetch the last node in the tree. Having a table and data in that table creates a tree structure like here:
Node 0
/ \
Node 1 Node 2
/ \ \
Node 3 Node 4 <-- Node 5 <--
/
Node 6 <--
Each node has an id and parentId. So Node1 has id: 1 and parentId: 0 that connects it with the upper node.
Node columns:
Id
parentId (upper node)
What I want:
So when I choose node 0, I want to "go down" and get back nodes 6, 4, and 5. And when I choose node 1 I want to fetch nodes 6 and 4. The last nodes of the structure. I don't want intermediate nodes.
I tried doing a common table expression, with .. as. But I get intermediate nodes and that's a mess in my case.
Jpa solution is also appreciated.
Thanks and good luck ;)
Considering the following JPA model :
#Entity
class Node {
#Id
long id;
#ManyToOne
Node parent;
}
The leaves of the tree are the nodes not being a parent node.
If all the nodes of your model belongs to the same unique tree, the following request can be done :
SELECT n FROM Node n WHERE n NOT IN (SELECT p FROM Node p WHERE p.parent = n)
If you store several trees in your db or if you want the leaves under a specific node there is no way to build a recursive JPA query.
However, if there is not too much data in your Db, you can load all leaves (whatever node they belong to) with the same JPA request, and recursively scan each node in memory keeping those whose ancestor is the specific node limiting your scope.
BTW, if your store severeal trees in your Db, modifying the initial JPA model helps loading all the nodes of a tree with one JPA request.
Then do all kind of search in memory.
The model must be extended by adding on each node a relation to the root node :
#Entity
class Node {
#Id
long id;
#ManyToOne
Node parent;
#ManyToOne
Node root;
}
With such a model, the complete set of nodes of a tree can be loaded with one request :
SELECT n FROM node WHERE p.root.id = :id_of_the_wanted_root

Cytoscape.js dynamically add nodes without moving the others

I am working on an application that fetches data from a database and I would like to show them as graph.
I managed the "tap" event on a node by showing their neighbors (nodes and connection links).
The problem is that, every time I want to show the neighbors, all the graph is re-rendered and if some nodes were moved before, they lose their previous position.
Is there a way to add only the neighbors without affecting the position of the node already present in the layout?
Important: the constraint is that all the nodes should be "movable": the number of nodes in the graph can, easily, increase and I would like to have the availability to move/organize them without losing the result when I add new ones (by clicking on a node)
I am using cola-layout in my project.
Here the way I managed to add neighbors:
function addNeighbour(node, link) {
cy.startBatch();
addNode(link.otherNode.type, link.otherNode.name, link.otherNode.properties);
cy.add([
{
group: 'edges',
data:
{
id: node + ":" + link.type + ":" + link.otherNode.type + ":" + link.otherNode.name,
source: source,
target: target,
type: link.type,
properties: linkproperties
}
}
]);
refreshLayout()
cy.endBatch();
}
}
var layoutOpts = {
name: 'cola',
refresh: 2,
edgeLength: 200,
fit: false
}
function refreshLayout() {
layout.stop();
layout = cy.elements().makeLayout(layoutOpts);
layout.run();
}
Thanks in advance
(1) You can lock a node to make its position immutable, via nodes.lock().
(2) You can run a layout on a subset of the graph to exclude certain elements, via eles.layout().
Either of these strategies can be used in general, or they can be used in tandem.
For your case, it sounds like you should use (1).
Lock the existing nodes.
Add the new nodes.
Run Cola on the entire graph.
When Cola is done, free the locked nodes.
Note, however, that this won't always give a good result. You could over-constrain the system. If you want a good layout result, it's probably best to just run the layout on everything without locking, as Stephan T. suggested.

create nodes programatically with gmf but without setting its properties

I want to create nodes programatically with its properties but using the folowing codes nodes can be created but its properties can not be set.
CreateUnspecifiedTypeRequest request_ch = new
CreateUnspecifiedTypeRequest(
Collections.singletonList(xxxElementTypes.yy),
diagramEditPart.getDiagramPreferencesHint());
Command command = diagramEditPart.getCommand(request);
command.execute();
then element.set("idof element") but the properties of the node still empty.
may someone help me .thanks
I am currently using this method in order to create nodes programatically. The node and the properties appear just fine and you can edit them. (note that there is also a way to edit the properties programmatically, with another type of command (EMF))
public void createAndExecuteShapeRequestCommand(IElementType type, EditPart parent) {
CreateViewRequest actionRequest = CreateViewRequestFactory
.getCreateShapeRequest(
type,
PreferencesHint.USE_DEFAULTS);
org.eclipse.gef.commands.Command command = parent.getCommand(actionRequest);
command.execute();
}
A sample caller of that method if the node is meant to be added in the main area of the diagram.
createAndExecuteShapeRequestCommand(xxx.diagram.providers.xxxElementTypes.ELEMENT_HERE, diagramEditPart);
A sample caller of that method if the node is meant to be added inside another node or compartment.
DiagramEditPart diagramEditPart = getDiagramEditPart(); //diagram.getDiagramEditPart();
"ParentElement" parentElement = (("Root_ELEMENT") diagramEditPart.resolveSemanticElement())."getTheElement"();
List list = getDiagramGraphicalViewer().findEditPartsForElement(EMFCoreUtil.
getProxyID(parentElement),
TheElementsEDITPART.class);
createAndExecuteShapeRequestCommand(xxx.diagram.providers.xxxElementTypes.ELEMENT_HERE, (EditPart)list.get(0));
Note that, if you wish to call this method from other class than the one of the xxxDiagramEditor.java you will need somehow to pass there the diagramEditPart.

Is there simple way how to replace (ordered) existing node in JCR 2.0?

Is there some simple way how to replace existing node with another node in JCR 2.0?
Due to the ordering of nodes, currently I am doing these steps:
step 1: Find sibling node which is right after existing node i want to replace:
if (preserveOrdering) {
NodeIterator iter = parent.getNodes();
boolean found = false;
while (iter.hasNext()) {
if (tempNode.equals(iter.nextNode())) {
found = true;
if (iter.hasNext()) {
tempNodeSibling = iter.nextNode();
break;
}
}
}
assert found;
}
step 2: delete existing node:
tempNode.remove();
step 3:
Create new node (I am doing clone, but probably node.addNode() method can be used,
new node is appended to the end of the child node list):
workspace.clone(workspace.getName(), existingNodePath, tempNodePath, false);
step 4:
Move new node before his old tempNode sibling (remebered in the first step)
parent.orderBefore(tempNodeName, tempNodeSibling.getName());
All these steps looks to me quite cumbersome. But I cannot find in JCR API better way. Mainly because there is only one method orderBefore() working with ordering.
Do you think there is some different/more simple approach for solving this problem?
Unfortunately, with JCR 2.0 new nodes are always added at the end and using javax.jcr.Node.reorder(...) is the only way to change the position of a child node within the parent's list of children. It is inconvenient to say the least, but I suspect adding such methods would have added too much complexity to an already complicated API.

Node id of tree isnt getting published

I am trying to publish a node id*(coming from my rest services)* as per the below approach. First I am setting the value for nodeID and then I am passing it through var message to publishing it on check-box click of my dijit tree. I am able to get value stored in node id but when I put an alert on the message variable I don't obtain any result. Only object[object] gets returned . Can someone suggest where am I going wrong here? Why am I not getting the node id from message which I am trying to publish?
onclick:function(item){
var nodeID = id,
var message =
{
sender: this,
group: 'default',
itemIds: nodeID
};
alert(message);
dojo.publish("/checkbox/select", [message]);}
This question is solved we need to access the individual items as message.itemids