P15GEN2\59518
2024-05-29 d4210c7c4b04abde20037ea8aa0f54ef8a2649aa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
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();
        }
    }
}