package frame.object.data;
|
|
import chat.server.call.IJSONWriter;
|
import frame.object.meta.EntityMeta;
|
|
|
public class EntityTree extends EntitySet {
|
|
private EntitySet rootSet;
|
|
public EntityTree(EntityMeta tableMeta) {
|
super(tableMeta);
|
rootSet = new EntitySet(tableMeta);
|
}
|
|
public EntitySet getChildren(ID parentId) throws DataException {
|
Node parent = getNode(parentId);
|
|
if (parent == null) {
|
return null;
|
}
|
|
return parent.children;
|
}
|
|
public Node getNode(ID id) throws DataException {
|
Entity result = getEntity(id);
|
return (Node) result;
|
}
|
|
public EntitySet getRootSet() {
|
return rootSet;
|
}
|
|
public void initRelation() throws DataException {
|
initFamilyRelation();
|
initLevelRelation();
|
}
|
|
public void initFamilyRelation() throws DataException {
|
for (Entity entity: this) {
|
Node node = (Node) entity;
|
ID parentId = node.getParentId();
|
|
if (parentId.isEmpty()) {
|
node.level = 0;
|
rootSet.append(node);
|
continue;
|
}
|
|
Node parent = getNode(parentId);
|
|
if (parent == null) {
|
continue;
|
}
|
|
parent.addChild(node);
|
}
|
}
|
|
public void initLevelRelation() throws DataException {
|
for (Entity entity: rootSet) {
|
Node node = (Node) entity;
|
doInitLevelRelation(node);
|
}
|
}
|
|
private void doInitLevelRelation(Node parent) {
|
EntitySet children = parent.getChildren();
|
|
for (Entity entity: children) {
|
Node child = (Node) entity;
|
child.level = parent.level + 1;
|
|
EntitySet offsprings = child.getChildren();
|
|
for (Entity offspring: offsprings) {
|
doInitLevelRelation((Node)offspring);
|
}
|
}
|
}
|
|
@Override
|
public void writeJSONObject(IJSONWriter writer) {
|
writer.beginArray();
|
|
for (Entity entity: rootSet) {
|
Node node = (Node)entity;
|
node.writeJSONObject(writer);
|
}
|
|
writer.endArray();
|
}
|
|
}
|