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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package foundation.org;
 
import foundation.capacity.ActorTarget;
import foundation.capacity.TargetType;
import foundation.dao.Filter;
import foundation.dao.bizlogic.ICodeProvider;
import foundation.data.entity.Entity;
import foundation.data.entity.EntitySet;
import foundation.data.meta.field.Field;
import foundation.data.object.DataObject;
import foundation.json.IJSONProvider;
import foundation.json.IJSONWriter;
import foundation.util.ID;
import foundation.util.Util;
 
public class Position implements IJSONProvider {
 
    private String id;
    private String code;
    private String name;
    private String buId;
    private String comapnyId;
    private int level;
    private String levelField;
    private String duty;
    private Org org;
    private BU bu;
    
    public Position() {
 
    }
 
    public void load(Entity entity) throws Exception {
        id = entity.getString("id");
        code = entity.getString("code");
        name = entity.getString("name");
        buId = entity.getString("bu_id");
        comapnyId = entity.getString("comapny_id");
        level = entity.getInteger("level_no", 1);
        levelField = "level" + level;
        duty = entity.getString("duty");
    }
 
    public static Position loadPosition(String positionId) throws Exception {
        DataObject dataObject = DataObject.getInstance("md_position");
        Entity entity = dataObject.getTableEntity(positionId);
        
        if (entity == null) {
            return null;
        }
        Position position = new Position();
        position.load(entity);
        
        return position;
    }
 
    public static EntitySet createPostion(String companyId, String buId, String actorId, String actorName) throws Exception {
        DataObject dataObject = DataObject.getInstance("md_bu_org");
        Filter filter = new Filter();
        
        if (Util.isEmpty(companyId)) {
            filter.add("company_id", companyId);
        }
        
        if (Util.isEmpty(buId)) {
            filter.add("bu_id", buId);
        }
        
        EntitySet entitySet = dataObject.getBrowseEntitySet(filter);
        
        dataObject = DataObject.getInstance("md_position");
        EntitySet positionSet = dataObject.createTableEntitySet();
        Entity positionEntity = dataObject.createTableEmptyEntity();
        ICodeProvider codeProvider = ICodeProvider.getInstance(new Field("code"), "Code-Position");
        String positionId, positionCode, positionName;
 
        for (Entity entity : entitySet) {
            buId = entity.getString("bu_id");
            companyId = entity.getString("company_id");
            positionCode = codeProvider.nextval(buId + companyId);
            positionName = actorName + "(" + entity.getString("company_name") + entity.getString("bu_name") + ")"; 
            positionId = ID.newValue();
            positionEntity.set("id", positionId);
            positionEntity.set("bu_id", buId);
            positionEntity.set("company_id", companyId);
            positionEntity.set("parent_id", "0");
            positionEntity.set("category_code", TargetType.Position);
            positionEntity.set("code", positionCode);
            positionEntity.set("name", positionName);
            positionEntity.set("is_delete", "F");
            
            Position position = new Position();
            position.load(positionEntity);
            ActorTarget.createInstance(actorId, position);
            
            positionSet.append(positionEntity);
        }
        
        return positionSet;
    }
 
    public String getLevelField() {
        return levelField;
    }
 
    public String getId() {
        return id;
    }
 
    public String getCode() {
        return code;
    }
 
    public String getName() {
        return name;
    }
 
    public String getBUId() {
        return buId;
    }
    
    public String getDuty() {
        return duty;
    }
 
    public void setOrg(Org org) {
        this.org = org;
    }
    
    public Org getOrg() {
        return org;
    }
    
    public BU getBU() {
        return bu;
    }
 
    public void setBU(BU bu) {
        this.bu = bu;
    }
 
    public String getComapnyId() {
        return comapnyId;
    }
 
    public void setComapnyId(String comapnyId) {
        this.comapnyId = comapnyId;
    }
 
    @Override
    public void writeJSON(IJSONWriter writer) {
        writer.beginObject();
        writeJSONBody(writer);
        writer.endObject();
    }
    
    public void writeJSONBody(IJSONWriter writer) {
        writer.write("id", id);
        writer.write("code", code);
        writer.write("name", name);
        writer.write("duty", duty);
    }
 
}