wangting
2024-11-05 19d2c06b04aab6d21a36bd5b41c78cdea8ae58b7
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package com.vci.server.framework.delegate;
 
import java.sql.Timestamp;
import java.util.List;
 
import org.apache.commons.lang3.StringUtils;
 
 
import com.vci.server.base.utility.LogHelper;
import com.vci.server.base.utility.LogRecordUtil;
import com.vci.server.framework.systemConfig.security.MachSecurity;
import com.vci.server.framework.systemConfig.security.MachSecurityService;
import com.vci.common.utility.ObjectUtility;
import com.vci.corba.common.VCIError;
import com.vci.corba.framework.data.MachSecurityInfo;
import com.vci.corba.log.data.LogType;
import com.vci.corba.common.data.UserEntityInfo;
 
import com.vci.server.base.delegate.BaseDelegate;
 
 
public class MachSecurityDelegate extends BaseDelegate {
 
    private MachSecurityService service = null;
    
    public MachSecurityDelegate(UserEntityInfo userInfo) {
        super(userInfo);
        service = new MachSecurityService(userEntity);
    }
    
    public MachSecurityDelegate() {
        service = new MachSecurityService();
    }
    
    @Override
    public void setUserEntityInfo(UserEntityInfo userInfo) {
        super.setUserEntityInfo(userInfo);
        service.setUserEntity(userEntity);
    }
    
    public MachSecurityInfo[] getAllMachSecurity() throws VCIError {
        List<MachSecurity> list = null;
        try {
            list = service.getMachSecurityList();
        } catch (Exception e) {
            throw new VCIError("120311", new String[0]);
        }
        int size = list.size();
        MachSecurityInfo[] infos = new MachSecurityInfo[size];
        for (int i = 0; i < size; i++) {
            infos[i] = changeEntityToInfo(list.get(i));
        }
        return infos;
    }
 
 
    public int getMachSecurityTolal() throws VCIError {
        int total = 0;
        try {
            total = service.getMachSecurityTolal();
        } catch (Exception e) {
            throw new VCIError("120312", new String[0]);
        }
        return total;
    }
 
 
    public int getMachSecurityTolalByCondition(String name, String ipAddress, int security) throws VCIError {
        int total = 0;
        try {
            total = service.getMachSecurityTolal(name, ipAddress, security);
        } catch (Exception e) {
            throw new VCIError("120312", new String[0]);
        }
        return total;
    }
 
    public MachSecurityInfo[] fetchMachSecurityByPage(int pageNo, int pageSize) throws VCIError {
        List<MachSecurity> list = null;
        try {
            list = service.fetchMachSecurityByPage(pageNo, pageSize);
        } catch (Exception e) {
            throw new VCIError("120313", new String[0]);
        }
        int size = list.size();
        MachSecurityInfo[] infos = new MachSecurityInfo[size];
        for (int i = 0; i < size; i++) {
            infos[i] = changeEntityToInfo(list.get(i));
        }
        return infos;
    }
    
 
    public MachSecurityInfo[] fetchMachSecurityByConditionPage(String name, String ipAddress, int security, int pageNo,
            int pageSize) throws VCIError {
        List<MachSecurity> list = null;
        try {
            list = service.fetchMachSecurityByConditionPage(name, ipAddress, security, pageNo, pageSize);
        } catch (Exception e) {
            throw new VCIError("120314", new String[0]);
        }
        int size = list.size();
        MachSecurityInfo[] infos = new MachSecurityInfo[size];
        for (int i = 0; i < size; i++) {
            infos[i] = changeEntityToInfo(list.get(i));
        }
        return infos;
    }
 
    
    public String saveMachSecurity(MachSecurityInfo info, UserEntityInfo userInfo) throws VCIError {
        this.setUserEntityInfo(userInfo);
        
        MachSecurity security = changeInfoToEntity(info);
        
        //System.out.println("======saveMachSecurity:user=" + userEntity.getUserName() + "===============");
        
        security.setCreator(userEntity.getUserName());
        security.setModifier(userEntity.getUserName());
        Timestamp currentTime = new Timestamp(System.currentTimeMillis());
        security.setCreateTime(currentTime);
        security.setModifyTime(currentTime);
        
        String id = security.getId();
        if (StringUtils.isBlank(id)) {
            id = ObjectUtility.getNewObjectID36();
            security.setId(id);
        }
 
        try {
            service.saveMachSecurity(security);
 
            String log = String.format("添加机器密级:%s [%s]", security.getName(), LogHelper.toNewLogString(security));
            LogRecordUtil.writeLog(userInfo, "添加", "成功", log, LogType.General, security.getId());
        } catch (Exception e) {
            throw new VCIError("120315", new String[0]);
        }
        return id;
    }
 
 
    public boolean batchSaveMachSecurity(MachSecurityInfo[] infos, UserEntityInfo userInfo) throws VCIError {
        this.setUserEntityInfo(userInfo);
        
        MachSecurity[] securities = new MachSecurity[infos.length];
        String[] ids = new String[infos.length];
        
        String id = null;
        for (int i = 0; i < infos.length; i++) {
            securities[i] = changeInfoToEntity(infos[i]);
            id = securities[i].getId();
            if (StringUtils.isBlank(id)) {
                id = ObjectUtility.getNewObjectID36();
                securities[i].setId(id);
            }
            ids[i] = id;
        }
 
        try {
            MachSecurityService machService = new MachSecurityService(userEntity);
            machService.saveMachSecurities(securities);
 
            String log = String.format("添加机器密级对象:%s", String.join(";", ids));
            LogRecordUtil.writeLog(userInfo, "添加", "成功", log, LogType.General, id);
        } catch (Exception e) {
            throw new VCIError("120316", new String[0]);
        }
 
        return false;
    }
 
 
    public boolean updateMachSecurity(MachSecurityInfo info, UserEntityInfo userInfo) throws VCIError {
        this.setUserEntityInfo(userInfo);
        
        boolean rs = true;
        MachSecurity ent = changeInfoToEntity(info);
        try {
            MachSecurityService msService = new MachSecurityService();
 
            MachSecurity old = msService.selectMachSecurity(ent.getId());
            
            rs = msService.updateMachSecurity(ent);
 
            String log = "";
            log = String.format("更改机器密级信息:%s; %s", ent.getName(),
                    LogHelper.toUpdataLogString(old, ent));
 
            if (rs)
                LogRecordUtil.writeLog(userInfo, "更新", "成功", log, LogType.General, ent.getId());
            else
                LogRecordUtil.writeLog(userInfo, "更新", "失败", log, LogType.General, ent.getId());
            
            return rs;
 
        } catch (Exception e) {
            throw new VCIError("120117", new String[0]);
        }
    }
 
 
    public boolean batchUpdateMachSecurity(MachSecurityInfo[] infos, UserEntityInfo userInfo) throws VCIError {
        this.setUserEntityInfo(userInfo);
 
        MachSecurity[] securities = new MachSecurity[infos.length];
        String[] ids = new String[infos.length];
        
        for (int i = 0; i < infos.length; i++) {
            securities[i] = changeInfoToEntity(infos[i]);
            ids[i] = securities[i].getId();
        }
        
        String log = String.format("更新机器密级对象:%s", String.join(";", ids));
 
        try {
            MachSecurityService machService = new MachSecurityService(userEntity);
            machService.updateMachSecurities(securities);
 
            LogRecordUtil.writeLog(userInfo, "更新", "成功", log, LogType.General, ids[0]);
            
            return true;
        } catch (Exception e) {
            LogRecordUtil.writeLog(userInfo, "更新", "失败", log, LogType.General, ids[0]);
            throw new VCIError("120318", new String[0]);
        }
    }
 
 
    public boolean deletMachSecurity(String[] ids, UserEntityInfo userEnt) throws VCIError {
        this.setUserEntityInfo(userEnt);
        return service.deleteMachSecurityByMQL(ids);
    }
 
 
    public int getMachSecurity(String machAddress, int type, UserEntityInfo userInfo) throws VCIError {
        this.setUserEntityInfo(userInfo);
        
        MachSecurity ms = service.selectMachSecurityByIP(machAddress);
        if (ms != null) {
            long sgrade = ms.getSecretGrade();
            return (int)sgrade;
        }
 
        return 0;
    }
 
 
    public MachSecurityInfo getMachSecurityInfo(String machAddress, int type, UserEntityInfo userInfo) throws VCIError {
        this.setUserEntityInfo(userInfo);
        
        MachSecurityInfo info = new MachSecurityInfo();
 
        MachSecurity ms =  service.selectMachSecurityByIP(machAddress);
        if (ms != null) {
            info = changeEntityToInfo(ms);
        } else {
            info = new MachSecurityInfo();
        }
 
        return info;
    }
    
 
    private MachSecurityInfo changeEntityToInfo(MachSecurity ent) {
        MachSecurityInfo info = new  MachSecurityInfo();
        
        info.id = ent.getId();
        info.name = ent.getName() == null ? "" : ent.getName();
        info.ipAddress = ent.getIpAddress() == null ? "" : ent.getIpAddress();
        info.macAddress = ent.getMacAddress() == null ? "" : ent.getMacAddress();
        info.secretGrade = ent.getSecretGrade();
        info.createTime = ent.getCreateTime() == null ? 0 : ent.getCreateTime().getTime();
        info.creator = ent.getCreator() == null ? "" : ent.getCreator();
        info.modifyTime = ent.getModifyTime() == null ? 0 : ent.getModifyTime().getTime();
        info.modifier = ent.getModifier() == null ? "" : ent.getModifier();
        info.desc = ent.getDescription() == null ? "" : ent.getDescription();
 
        return info;
    }
 
 
    private MachSecurity changeInfoToEntity(MachSecurityInfo info) {
        MachSecurity ent = new  MachSecurity();
        
        ent.setId(info.id);
        ent.setName(info.name);
        ent.setIpAddress(info.ipAddress == "" ? null : info.ipAddress);
        ent.setMacAddress(info.macAddress == "" ? null : info.macAddress);
        ent.setSecretGrade(info.secretGrade);
        
        ent.setDescription(info.desc == "" ? null : info.desc);
        ent.setName(info.name == "" ? null : info.name);
        ent.setCreateTime(info.createTime == 0 ? new java.sql.Timestamp(System.currentTimeMillis()) : new java.sql.Timestamp(info.createTime));
        ent.setCreator(info.creator == "" ? null : info.creator);
        ent.setModifyTime(info.modifyTime == 0 ? new java.sql.Timestamp(System.currentTimeMillis()) : new java.sql.Timestamp(info.createTime));
        ent.setModifier(info.modifier == "" ? null : info.modifier);
 
        return ent;
    }
 
 
}