田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package com.vci.server.omd.lifecycle.cache;
 
import org.apache.commons.lang3.StringUtils;
 
import com.alibaba.fastjson.JSONObject;
import com.vci.common.exception.VciExceptionTool;
import com.vci.server.base.exception.ExceptionLocalHandler;
import com.vci.server.cache.CacheNames;
import com.vci.server.cache.redis.RedisUtil;
import com.vci.server.omd.lifecycle.service.LifeCycleService;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.lcm.LifeCycle;
 
public class LifeCycleCacheUtil { //extends BaseCacheTimer{
 
    private static volatile LifeCycleCacheUtil instance = null;
//    private LifeCycle[] lifeCycles = null;
    private String[] eventKeys = null;
//    private Map<String, LifeCycle> lifecyleNameMap = null;
//    private Map<String, String> keyValueMap = null;
    
    public static String CACHENAME = "lifecycle";
 
    private static LifeCycleCacheUtil getInstance() {
        if (instance == null) {
            synchronized (LifeCycleCacheUtil.class) {
                if (instance == null) {
                    instance = new LifeCycleCacheUtil();
                }
            }
        }
 
        return instance;
    }
 
    private LifeCycleCacheUtil() {
//        super(CACHENAME);
//        try {
//            this.setCurrentTime(this.getDataBaseCurrtenttime());
//            lifecyleNameMap = new HashMap<String, LifeCycle>();
//            keyValueMap = new HashMap<String, String>();
//            initLifecyle();
//            initLCEvent();
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
    }
    
    public static void initCache() {
        try {
            clearCache();
            
            getInstance().initLifecyle();
            getInstance().initLCEvent();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void clearCache() {
        RedisUtil.getInstance().del(CacheNames.LIFECYCLES);
        RedisUtil.getInstance().del(CacheNames.LCEVENTS);
    }
    
    
    public static void setLifeCycle(LifeCycle item) {
        if (item == null)
            return;
        
        String jsonObj = JSONObject.toJSONString(item);
        
        RedisUtil.getInstance().hset(CacheNames.LIFECYCLES, item.name.toLowerCase(), jsonObj);
    }
    
    
    public static void setLCEvent(String name, String value) {
        if (StringUtils.isBlank(name) || StringUtils.isBlank(value))
            return;
        
        RedisUtil.getInstance().hset(CacheNames.LCEVENTS, name, value);
    }
    
    public static void delLifeCycle(String name) {
        if (StringUtils.isBlank(name))
            return;
        
        RedisUtil.getInstance().hdel(CacheNames.LIFECYCLES, name.toLowerCase());
    }
 
 
    /**
     * 初始化event事件
     * 
     * @throws Throwable
     * 
     */
    private void initLCEvent() throws VCIError {
        //String lceEventKeys[];
        try {
            String[] lceKeys = LifeCycleService.getInstance().getLCEventKeys();
            //eventKeys = lceEventKeys;
            //String[] eventValue = new String[eventKeys.length];
            for (int i = 0; i < lceKeys.length; i++) {
                String eventValue = LifeCycleService.getInstance().getLCEventValueByKey(lceKeys[i]);
                //keyValueMap.put(eventKeys[i], eventValue[i]);
                setLCEvent(lceKeys[i], eventValue);
            }
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw getLocalVciError("P0010LIFECYCLE-00015", e);
        }
        
    }
    
    /**
     * 初始化生命周期
     * 
     */
    private void initLifecyle() throws Exception {
        LifeCycle[] lcs = LifeCycleService.getInstance().getLifeCyles();
        //lifeCycles = lcs;
        for (int i = 0; i < lcs.length; i++) {
            //lifecyleNameMap.put(lcs[i].name, lcs[i]);
            
            setLifeCycle(lcs[i]);
        }
    }
 
    /**
     * 获取事件key
     * 
     * @throws Throwable
     */
//    public static String[] getLCEventKeys() throws Throwable {
//        return getInstance().eventKeys
////        String[] cEventKeys = eventKeys;
////        return cEventKeys;
//    }
 
    /**
     * 获取事件value
     * 
     * @throws Throwable
     */
 
//    public String getLCEventValueByKey(String key) throws Throwable {
//        String value = keyValueMap.get(key);
//        return value;
//    }
 
//    public LifeCycle[] getLifeCyles() throws VCIError {
//        // 在调用具体方法时,先判断数据库中数据是否发生变化,若变化,则将发生变化的数据同步缓存
//        checkCache();
//        LifeCycle[] cLifeCycles = lifeCycles;
//        return cLifeCycles;
//    }
//
//    public LifeCycle getLifeCycle(String name) throws VCIError {
//        // 在调用具体方法时,先判断数据库中数据是否发生变化,若变化,则将发生变化的数据同步缓存
//        checkCache();
//        LifeCycle lifeCycle = null;
//        if (lifecyleNameMap.containsKey(name)) {
//            lifeCycle = lifecyleNameMap.get(name);
//        } else {
//            lifeCycle = new LifeCycle();
//            lifeCycle.bounds = new Bound[0];
//            lifeCycle.routes = new TransitionVO[0];
//        }
//
//        return lifeCycle;
//    }
 
    /****
     * 检查缓存和数据库的一致性,若不一致,则刷新缓存
     * 
     * @param spList
     * @throws VCIError 
     */
//    private synchronized void checkCache() throws VCIError {
////        if (!CacheConfUtil.getEveryTimeRefresh()) {
////            return;
////        }
//        // 在调用具体方法时,先判断数据库中数据是否发生变化,若变化,则将发生变化的数据同步缓存
//        long cTime = this.getDataBaseCurrtenttime();
//        //List<PLCacheTempEntity> pto = getStateLogs("lifecycle");
//        PLCacheRecordEnt[] records = getUpdatedCacheData("lifecycle");
//        if (records == null || records.length == 0) {
//            if (records != null)
//                this.setCurrentTime(cTime);
//            return;
//        }
//        List<LifeCycle> spList = new ArrayList<LifeCycle>();
//        for (int i = 0; i < lifeCycles.length; i++) {
//            spList.add(lifeCycles[i]);
//        }
//
//        List<String> list = new ArrayList<String>();
//        List<String> idList = new ArrayList<String>();
//        for (int i = 0; i < records.length; i++) {
//            idList.add(records[i].getId());
//            list.add(records[i].getObjId());
//        }
//        list = getSimpleId(list); // 去除重复的id号
//        Map<String, LifeCycle> lifeCycleIdMap = getChangeLifeCycleById(list);// 获取statepool表中所有被修改的数据
//        for (String key : lifeCycleIdMap.keySet()) {
//            LifeCycle lc = lifeCycleIdMap.get(key);
//            boolean flag = true;
//            for (int j = 0; j < spList.size(); j++) {
//                if (spList.get(j).oid.equals(key)) {
//                    if (lc == null) {
//                        spList.remove(j);
//                        flag = false;
//                        break;
//                    } else {
//                        spList.remove(j);
//                        spList.add(lc);
//                        flag = false;
//                        break;
//                    }
//                }
//            }
//            //add by caill 解决增删改后左侧树的刷新问题
//            if(flag){
//                if (lc != null) {
//                    spList.add(lc);
//                } 
//            }
//        }
//        LifeCycle[] cLifeCycles = new LifeCycle[spList.size()];
//        sort(spList);// 将所有的状态按照状态名称的字母排序
//        for (int i = 0; i < spList.size(); i++) {
//            cLifeCycles[i] = spList.get(i);
//        }
//        // 刷新公共变量lifeCycles和lifecyleNameMap
//        refresh(cLifeCycles);
//        this.setCurrentTime(cTime);
//    }
//
//    // 将所有的状态按照状态名称的字母排序
//    private void sort(List<LifeCycle> spList) {
//        Collections.sort(spList, new Comparator<LifeCycle>() {
//            @Override
//            public int compare(LifeCycle s1, LifeCycle s2) {
//                return new String(s1.name).compareTo(new String(s2.name));
//            }
//        });
//    }
//
//    // 获取statepool表中所有被修改的数据
//    private Map<String, LifeCycle> getChangeLifeCycleById(List<String> list) {
//        Map<String, LifeCycle> lifeCycleIdMap = new HashMap<String, LifeCycle>();
//        LifeCycle[] changedLifeCyle = new LifeCycle[list.size()];
//        for (int i = 0; i < list.size(); i++) {
//            LifeCycle lc = null;
//            try {
//                lc = LifeCycleService.getInstance().getLifeCycleByOid(
//                        list.get(i));
//                changedLifeCyle[i] = lc;
//                lifeCycleIdMap.put(list.get(i), lc);
//            } catch (Exception e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
//        return lifeCycleIdMap;
//    }
//
//    // 获得中间表中与状态池相关的数据
////    private List<PLCacheTempEntity> getStateLogs(String type) throws VCIError {
////        List<PLCacheTempEntity> pto = null;
////        pto = VciCacheServerDelegate.getInstance()
////                .getPLCacheTempEntityByObjType(type, this.getCurrentTime());
////        return pto;
////    }
//
//    // 去除中间表中重复的id并装入list
//    private List<String> getSimpleId(List<String> list) {
//        HashSet<String> h = new HashSet<String>(list);
//        list.clear();
//        list.addAll(h);
//        return list;
//    }
//
//    // 刷新公共变量
//    private void refresh(LifeCycle[] cLifeCycles) {
//        lifeCycles = cLifeCycles;
//        lifecyleNameMap.clear();// 先将lifecyleNameMap清空
//        for (int i = 0; i < lifeCycles.length; i++) {
//            lifecyleNameMap.put(lifeCycles[i].name, lifeCycles[i]);
//        }
//    }
//
//    
//    // 重新加载缓存
//    
//    public void reLoad() throws VCIError {
//        try {
//            this.setCurrentTime(this.getDataBaseCurrtenttime());
//            lifecyleNameMap = new HashMap<String, LifeCycle>();
//            keyValueMap = new HashMap<String, String>();
//            initLifecyle();
//            initLCEvent();    
//        } catch (Exception e) {
//            // TODO Auto-generated catch block
//            e.printStackTrace();
//            throw getLocalVciError("P0010LIFECYCLE-00015", e);
//        }
//        
//    }
    protected VCIError getLocalVciError(String key, Throwable e) {
        VCIError error = new VCIError(key, new String[]{VciExceptionTool.getExceptionStr(e), VciExceptionTool.getExceptionDetail(e)});
        VCIError rsError = ExceptionLocalHandler.getInstance().getLocalString(error, "PLMLIFECYCLE");
        return rsError;
    }
}