ludc
2025-01-16 986aa62ed00bee39363bab41b4eeb8259d446efd
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
package com.vci.server.portal.cache;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
 
import com.vci.server.cache.redis.RedisUtil;
import com.vci.server.portal.entity.PLUILayoutEntity;
import com.vci.server.portal.service.PLUILayoutEntityService;
import com.vci.server.portal.tools.ServerTool;
import com.alibaba.fastjson.JSONObject;
import com.vci.corba.common.VCIError;
import com.vci.corba.portal.data.PLUILayout;
 
public class UIContextCacheUtil extends UICacheBaseUtil<PLUILayout>{
 
    private static volatile UIContextCacheUtil instance = null;
 
 
    public static void initCache() {
        try {
            getInstance().initPLUILayout();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
    
    private UIContextCacheUtil() {
        super(UICacheNames.UICONTEXT, PLUILayout.class);
    }
    
    public static UIContextCacheUtil getInstance() {
        if (instance == null) {
            synchronized (UIContextCacheUtil.class) {
                if (instance == null) {
                    instance = new UIContextCacheUtil();
                }
            }
        }
        
        return instance;
    }
    
    @Override
    public void setObject(PLUILayout obj)    {
        if (obj == null)
            return;
        
        String jsonObj = JSONObject.toJSONString(obj);
        
        RedisUtil.getInstance().hset(getName(), obj.plOId, jsonObj);
    }
    
    @Override
    public PLUILayout[] getObjects() {
        List<PLUILayout> list = getObjectList();
        
        return list.toArray(new PLUILayout[0]);
    }
 
    
    @Override
    public void delObject(PLUILayout obj) {
        if (obj == null || StringUtils.isBlank(obj.plOId))
            return;
        
        RedisUtil.getInstance().hdel(getName(), obj.plOId);
    }
    
    private void initPLUILayout() throws Throwable{
//        lstUILayout.clear();
//        mapType2UIs.clear();
//        mapID2UI.clear();
//        System.gc();
//        
//        List<PLUILayoutEntity> list = PLUILayoutEntityService.getInstance().getAllPLUILayoutEntitys();
//        //List<PLUILayout> list_ = new ArrayList<PLUILayout>();
//        for(int i = 0; i < list.size(); i++){
//            PLUILayoutEntity obj = list.get(i);
//            PLUILayout uiLayout = ServerTool.getPLUILayout(obj);
//            
//            if(uiLayout != null){
//                lstUILayout.add(uiLayout);
//                mapID2UI.put(uiLayout.plOId, uiLayout);
//                
//                List<PLUILayout> lstTypeUI = null;
//
//                if (mapType2UIs.get(uiLayout.plRelatedType) == null) {
//                    lstTypeUI = new ArrayList<PLUILayout>(); 
//                    mapType2UIs.put(uiLayout.plRelatedType, lstTypeUI);
//                } else {
//                    lstTypeUI = mapType2UIs.get(uiLayout.plRelatedType); 
//                }
//                lstTypeUI.add(uiLayout);
//            }
//        }
        
        RedisUtil.getInstance().del(getName());
        
        List<PLUILayoutEntity> list = PLUILayoutEntityService.getInstance().getAllPLUILayoutEntitys();
 
        Map<String, List<String>> mapTemp = new HashMap<String, List<String>>();
 
        for(PLUILayoutEntity uiEnt : list){
            PLUILayout uiLayout = ServerTool.getPLUILayout(uiEnt);
 
            if(uiLayout != null){
                setObject(uiLayout);
            }
            List<String> lstTemp = mapTemp.get(uiLayout.plRelatedType);
            if (lstTemp == null) {
                lstTemp = new ArrayList<String>();
                mapTemp.put(uiLayout.plRelatedType, lstTemp);
            }
            lstTemp.add(uiLayout.plOId);
        }
        
        for (String key : mapTemp.keySet()) {
            List<String> lstTemp = mapTemp.get(key);
            RedisUtil.getInstance().hset(getName(), "MAP-" + key, String.join(";", lstTemp));
        }
    }
    
    
    /**
     * 获取业务类型下的上下文信息
     * @param btmType
     * @return
     * @throws VCIError 
     */
    public PLUILayout[] getContext(String objType, String code) throws VCIError {
//        if (!contextMap.containsKey(objType)) {
//            try {
//                initUILayoutDefination(objType);
//            } catch (Throwable e) {
//                e.printStackTrace();
//                return new ArrayList<PLUILayout>();
//            }
//            
//            return contextMap.get(objType);
//        }
            
        List<PLUILayout> lstObj = new ArrayList<PLUILayout>();
        String temp = RedisUtil.getInstance().hget(getName(), "MAP-" + objType);
        if (StringUtils.isBlank(temp))
            return new PLUILayout[0];
        
        String oids[] = temp.split(";");
        
        boolean eqCode = !StringUtils.isBlank(code);
        
        for (String oid : oids) {
            PLUILayout ui = getObject(oid);
            if (ui != null && (!eqCode || (eqCode && ui.plCode.equals(code))))
                lstObj.add(ui);
        }
        
        return lstObj.toArray(new PLUILayout[0]);
    }
    
 
}