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{ 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 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 list = PLUILayoutEntityService.getInstance().getAllPLUILayoutEntitys(); // //List list_ = new ArrayList(); // 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 lstTypeUI = null; // // if (mapType2UIs.get(uiLayout.plRelatedType) == null) { // lstTypeUI = new ArrayList(); // mapType2UIs.put(uiLayout.plRelatedType, lstTypeUI); // } else { // lstTypeUI = mapType2UIs.get(uiLayout.plRelatedType); // } // lstTypeUI.add(uiLayout); // } // } RedisUtil.getInstance().del(getName()); List list = PLUILayoutEntityService.getInstance().getAllPLUILayoutEntitys(); Map> mapTemp = new HashMap>(); for(PLUILayoutEntity uiEnt : list){ PLUILayout uiLayout = ServerTool.getPLUILayout(uiEnt); if(uiLayout != null){ setObject(uiLayout); } List lstTemp = mapTemp.get(uiLayout.plRelatedType); if (lstTemp == null) { lstTemp = new ArrayList(); mapTemp.put(uiLayout.plRelatedType, lstTemp); } lstTemp.add(uiLayout.plOId); } for (String key : mapTemp.keySet()) { List 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(); // } // // return contextMap.get(objType); // } List lstObj = new ArrayList(); 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]); } }