package frame.object.data;
|
|
import chat.server.call.IJSONWriter;
|
import frame.object.meta.EntityMeta;
|
|
|
public class Node extends Entity {
|
|
protected Node parent;
|
protected EntitySet children;
|
protected int level;
|
|
|
public Node(EntityMeta entityMeta) {
|
super(entityMeta);
|
children = new EntitySet(entityMeta);
|
level = 0;
|
}
|
|
public Node getParent() {
|
return parent;
|
}
|
|
public EntitySet getChildren() {
|
return children;
|
}
|
|
public int getLevel() {
|
return level;
|
}
|
|
public void changeParent(Node newParent) throws Exception {
|
//1.
|
parent.deleteChild(this);
|
newParent.addChild(newParent);
|
|
//2.
|
String parentid = newParent.getString("id");
|
set("parentid", parentid);
|
}
|
|
private void deleteChild(Node child) {
|
// String id = child.getString("id");
|
//TODO
|
}
|
|
public void addChild(Node child) {
|
child.level = level + 1;
|
children.append(child);
|
}
|
|
public void deleteChildren() {
|
children.clear();
|
}
|
|
public ID getParentId() {
|
return new ID(getVarinatValue("parentId"));
|
}
|
|
public Node getRoot() {
|
Node current = this;
|
Node parent = current.parent;
|
|
while (parent != null) {
|
current = parent;
|
parent = current.parent;
|
}
|
|
return current;
|
}
|
|
@Override
|
public void writeJSONData(IJSONWriter writer) {
|
//1. node
|
writer.write("level", level);
|
|
//2. entity
|
super.writeJSONData(writer);
|
|
//3. children
|
if (!children.isEmpty()) {
|
writer.writeName("children");
|
children.writeJSONObject(writer);
|
}
|
}
|
|
}
|