package foundation.data.entity;
|
|
import foundation.data.meta.field.FieldsRuntime;
|
import foundation.json.IJSONWriter;
|
import foundation.util.Util;
|
|
|
public class EntityTree extends EntitySet {
|
|
private EntitySet rootSet;
|
private boolean selectActive;
|
|
|
public EntityTree(FieldsRuntime fieldMetas) {
|
super(fieldMetas);
|
rootSet = new EntitySet(fieldMetas);
|
selectActive = false;
|
}
|
|
public EntitySet getChildren(String parentId) throws Exception {
|
EntityNode parent = getNode(parentId);
|
|
if (parent == null) {
|
return null;
|
}
|
|
return parent.children;
|
}
|
|
public EntityNode getNode(String id) throws Exception {
|
if (Util.isEmpty(id)) {
|
return null;
|
}
|
|
Entity result = getEntity("id", id);
|
return (EntityNode) result;
|
}
|
|
public EntitySet getRootSet() {
|
return rootSet;
|
}
|
|
public void initRelation() throws Exception {
|
initFamilyRelation();
|
initLevelRelation();
|
}
|
|
public void initFamilyRelation() throws Exception {
|
for (Entity entity: this) {
|
EntityNode node = (EntityNode) entity;
|
String parentId = node.getParentId();
|
|
EntityNode parent = getNode(parentId);
|
|
if (parent == null) {
|
node.level = 0;
|
rootSet.append(node);
|
continue;
|
}
|
|
parent.addChild(node);
|
}
|
}
|
|
public void initLevelRelation() throws Exception {
|
for (Entity entity: rootSet) {
|
EntityNode node = (EntityNode) entity;
|
doInitLevelRelation(node);
|
}
|
}
|
|
private void doInitLevelRelation(EntityNode parent) {
|
EntitySet children = parent.getChildren();
|
|
for (Entity entity: children) {
|
EntityNode child = (EntityNode) entity;
|
child.level = parent.level + 1;
|
|
doInitLevelRelation(child);
|
}
|
}
|
|
public void setSelectActive(boolean selectActive) {
|
this.selectActive = selectActive;
|
}
|
|
@Override
|
public void writeJSON(IJSONWriter writer) {
|
writer.beginArray();
|
if (selectActive) {
|
for (Entity entity: rootSet) {
|
if (SelectState.Unselect == entity.selectState) {
|
continue;
|
}
|
|
EntityNode node = (EntityNode)entity;
|
node.writeJSON(writer, node.selectState);
|
}
|
}
|
else {
|
for (Entity entity: rootSet) {
|
EntityNode node = (EntityNode)entity;
|
node.writeJSON(writer, SelectState.CurrentAndChildren);
|
}
|
}
|
|
writer.endArray();
|
}
|
|
@Override
|
public void writeJSONBody(IJSONWriter writer) {
|
if (selectActive) {
|
for (Entity entity: rootSet) {
|
if (SelectState.Unselect == entity.selectState) {
|
continue;
|
}
|
|
EntityNode node = (EntityNode)entity;
|
node.writeJSON(writer, node.selectState);
|
}
|
}
|
else {
|
for (Entity entity: rootSet) {
|
EntityNode node = (EntityNode)entity;
|
node.writeJSON(writer, SelectState.CurrentAndChildren);
|
}
|
}
|
}
|
}
|