package foundation.data.entity;
|
|
import foundation.data.meta.field.FieldsRuntime;
|
import foundation.json.IJSONWriter;
|
|
public class EntityNode extends Entity {
|
|
protected EntityNode parent;
|
protected EntitySet children;
|
protected int level;
|
|
|
public EntityNode(FieldsRuntime entityMeta) {
|
super(entityMeta);
|
children = new EntitySet(entityMeta);
|
level = 0;
|
}
|
|
public EntityNode getParent() {
|
return parent;
|
}
|
|
public EntitySet getChildren() {
|
return children;
|
}
|
|
public int getLevel() {
|
return level;
|
}
|
|
public void changeParent(EntityNode newParent) throws Exception {
|
//1.
|
parent.deleteChild(this);
|
newParent.addChild(newParent);
|
|
//2.
|
String parentid = newParent.getString("id");
|
set("parent_id", parentid);
|
}
|
|
private void deleteChild(EntityNode child) {
|
// String id = child.getString("id");
|
//TODO
|
}
|
|
public void addChild(EntityNode child) {
|
child.level = level + 1;
|
child.parent = this;
|
children.append(child);
|
}
|
|
public void deleteChildren() {
|
children.clear();
|
}
|
|
public EntityNode getRoot() {
|
EntityNode current = this;
|
EntityNode parent = current.parent;
|
|
while (parent != null) {
|
current = parent;
|
parent = current.parent;
|
}
|
|
return current;
|
}
|
|
public void setSelected() {
|
this.selectState = SelectState.CurrentAndChildren;
|
|
if (parent != null) {
|
parent.setSelected(SelectState.Current);
|
}
|
}
|
|
private void setSelected(SelectState state) {
|
this.selectState = state;
|
|
if (parent != null) {
|
parent.setSelected(state);
|
}
|
}
|
|
public void writeJSON(IJSONWriter writer, SelectState selectState) {
|
writer.beginObject();
|
|
//1. node
|
super.writeJSONBody(writer);
|
writer.write("level", level);
|
|
//2. children
|
if (children.isEmpty()) {
|
writer.writeNull("children");
|
writer.endObject();
|
return;
|
}
|
|
writer.beginArray("children");
|
for (Entity entity: children) {
|
EntityNode child = (EntityNode)entity;
|
|
if (SelectState.Current == selectState && (child.selectState == SelectState.Unselect)) {
|
continue;
|
}
|
|
child.writeJSON(writer, child.selectState);
|
|
}
|
writer.endArray();
|
|
writer.endObject();
|
}
|
|
@Override
|
public void writeJSONBody(IJSONWriter writer) {
|
super.writeJSONBody(writer);
|
|
//1. node
|
writer.write("level", level);
|
|
//2. children
|
if (!children.isEmpty()) {
|
writer.beginArray("children");
|
children.writeJSONBody(writer);
|
writer.endArray();
|
}
|
}
|
}
|