hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
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);
        }
    }
    
}