wangting
2025-01-16 18c43123b51a1688ab4ae01fe3d171c7d92e619b
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
package com.vci.server.omd.versionrule.cache;
 
import java.io.IOException;
import java.sql.SQLException;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.DocumentException;
 
import com.alibaba.fastjson.JSONObject;
import com.vci.common.exception.VciExceptionTool;
import com.vci.corba.common.VCIError;
import com.vci.corba.omd.vrm.VersionRule;
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.versionrule.service.VRService;
 
public class VRServerCacheUtil { //extends BaseCacheTimer{
    
    private static volatile VRServerCacheUtil instance = null;
//    private VersionRule[] versionRules = null;
//    private Map<String, VersionRule> versionRuleMap = null;
//    
//    public static String CACHENAME = "versionrule";
    
    public static void initCache() {
        try {
            clearCache();
            getInstance().initVerRules();
        } catch (VCIError e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static void clearCache() {
        RedisUtil.getInstance().del(CacheNames.VERRULES);
    }
 
    
    public static void setVersionRule(VersionRule item) {
        if (item == null)
            return;
        
        String jsonObj = JSONObject.toJSONString(item);
        
        RedisUtil.getInstance().hset(CacheNames.VERRULES, item.name.toLowerCase(), jsonObj);
    }
    
    public static void delVersionRule(String name) {
        if (StringUtils.isBlank(name))
            return;
        
        RedisUtil.getInstance().hdel(CacheNames.VERRULES, name.toLowerCase());
    }
 
    private VRServerCacheUtil() {
//        super(CACHENAME);
//        try {
//            this.setCurrentTime(System.currentTimeMillis());
//            versionRuleMap = new HashMap<String, VersionRule>();
//            initAttribute();
//        } catch (Throwable e) {
//            e.printStackTrace();
//        }
    }
    
    private static VRServerCacheUtil getInstance() {
        if (instance == null) {
            instance = new VRServerCacheUtil();
        }
        
        return instance;
    }
    
    private void initVerRules() throws VCIError, SQLException, IOException, DocumentException {
        VersionRule[] vrs = VRService.getInstance().getVersionRules();
        //versionRules = vrs;
        for (int i = 0; i < vrs.length; i++) {
            //versionRuleMap.put(vrs[i].name, vrs[i]);
            setVersionRule(vrs[i]);
        }
    }
    
//    public void reloadCache() throws VCIError {
//        try {
//            this.setCurrentTime(System.currentTimeMillis());
//            versionRuleMap = new HashMap<String, VersionRule>();
//            initAttribute();
//        } catch (Throwable e) {
//            e.printStackTrace();
//            throw getLocalVciError("P0010VERSION-00008", e);
//        }
//    }
    
//    public List<PLCacheTempEntity> needUpdateCache(String objType) throws VCIError {
//        if (!CacheConfUtil.getEveryTimeRefresh()) {
//            return null;
//        }
//        List<PLCacheTempEntity> list = VciCacheServerDelegate
//                .getInstance().getPLCacheTempEntityByObjType(objType, this.getCurrentTime());
//        return list;
//    }
//    
//    public void arraySort() {
//        List<VersionRule> list = Arrays.asList(versionRules);
//        Collections.sort(list, new Comparator<VersionRule>(){
//            @Override
//            public int compare(VersionRule v1, VersionRule v2) {
//                return new String(v1.name).compareTo(new String(v2.name));
//            }
//        });
//        versionRules = list.toArray(new VersionRule[list.size()]);
//    }
//    
//    public VersionRule[] getVersionRules() throws VCIError {
//        refreshCache();
//        return versionRules;
//    }
    
//    private synchronized void refreshCache() throws VCIError {
//        String objType = "versionrule";
//        long cTime = System.currentTimeMillis();
//        //List<PLCacheTempEntity> list = needUpdateCache(objType);
//        PLCacheRecordEnt[] list = getUpdatedCacheData(objType);
//        if (list != null) {
//            if (list.length > 0) {
//                updateCache(objType, list, cTime);
//            }
//            this.setCurrentTime(cTime);
//        }
//    }
 
//    public VersionRule getVersionRule(String name) throws VCIError {
//        refreshCache();
//        if (versionRuleMap.containsKey(name)) {
//            return versionRuleMap.get(name);
//        }
//        return new VersionRule();
//    }
//    
//    public boolean updateCache(String objType, PLCacheRecordEnt[] cacheTempList, long cTime) throws VCIError {
//        boolean flag = false;
//        try {
//            List<VersionRule> list = new ArrayList<VersionRule>();
//            for (int i = 0 ; i < versionRules.length ; i++) {
//                list.add(versionRules[i]);
//            }
//            List<String> idList = new ArrayList<String>();
//            for (PLCacheRecordEnt cache : cacheTempList) {
//                idList.add(cache.getId());
//                VersionRule vr = VRService.getInstance().getVersionRuleByOid(cache.getObjId());
//                if ("creat".equals(cache.getOperateType())) {
//                    list.add(vr);
//                    versionRuleMap.put(vr.name, vr);
//                } else if ("edit".equals(cache.getOperateType())) {
//                    list = removeListItemByOid(cache.getObjId(), list);
//                    versionRuleMap = removeMapItemByOid(cache.getObjId(), versionRuleMap);
//                    list.add(vr);
//                    versionRuleMap.put(vr.name, vr);
//                } else if ("delete".equals(cache.getOperateType())) {
//                    versionRuleMap = removeMapItemByOid(cache.getObjId(), versionRuleMap);
//                    list = removeListItemByOid(cache.getObjId(), list);
//                }
//            }
//            versionRules = list.toArray(new VersionRule[list.size()]);
//            this.setCurrentTime(cTime);
//            arraySort();
//        } catch (Throwable e) {
//            e.printStackTrace();
//            throw getLocalVciError("P0010VERSION-00008", e);
//        }
//        flag = true;
//        return flag;
//    }
    
    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, "PLMVERSION");
        return rsError;
    }
    
//    private Map<String, VersionRule> removeMapItemByOid(String oid, Map<String, VersionRule> map) {
//        for (Iterator<String> it = map.keySet().iterator();it.hasNext();) {
//            VersionRule vr = map.get(it.next());
//            if (oid.equals(vr.oid)) {
//                map.remove(vr);
//            }
//        }
//        return map;
//    }
//    
//    private List<VersionRule> removeListItemByOid(String oid, List<VersionRule> list) {
//        Iterator<VersionRule> it = list.iterator();
//        for (;it.hasNext();) {
//            VersionRule vr = it.next();
//            if (oid.equals(vr.oid)) {
//                it.remove();
//            }
//        }
//        return list;
//    }
}