package com.vci.server.omd.enumtype.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.log.ServerWithLog4j;
|
import com.vci.server.cache.CacheNames;
|
import com.vci.server.cache.redis.RedisUtil;
|
import com.vci.server.omd.enumtype.service.EnumService;
|
import com.vci.corba.common.VCIError;
|
import com.vci.corba.omd.etm.EnumItem;
|
|
public class EnumServerCacheUtil {//extends BaseCacheTimer{
|
|
private static volatile EnumServerCacheUtil instance = null;
|
// private EnumItem[] enumItems = null;
|
// private Map<String, EnumItem> enumMap = null;
|
// public static String CACHENAME = "enum";
|
|
// private EnumServerCacheUtil() {
|
// super(CACHENAME);
|
// try {
|
// this.setCurrentTime(this.getDataBaseCurrtenttime());
|
// enumMap = new HashMap<String, EnumItem>();
|
// initAttribute();
|
// } catch (Throwable e) {
|
// e.printStackTrace();
|
// }
|
// }
|
|
private static EnumServerCacheUtil getInstance() {
|
if (instance == null) {
|
synchronized (EnumServerCacheUtil.class) {
|
if (instance == null) {
|
instance = new EnumServerCacheUtil();
|
}
|
}
|
}
|
|
return instance;
|
}
|
|
public static void initCache() {
|
try {
|
clearCache();
|
getInstance().initAttribute();
|
} catch (Exception e) {
|
ServerWithLog4j.logger.error(e);
|
}
|
}
|
|
public static void clearCache() {
|
RedisUtil.getInstance().del(CacheNames.ATTRITEMS);
|
}
|
|
private void initAttribute() throws VCIError, SQLException, IOException, DocumentException {
|
EnumItem[] enumTypes = EnumService.getInstance().getEmItems("", 1, 1);
|
for (int i = 0; i < enumTypes.length; i++) {
|
setEnumType(enumTypes[i]);
|
}
|
}
|
|
|
public static void setEnumType(EnumItem item) {
|
if (item == null)
|
return;
|
|
String jsonObj = JSONObject.toJSONString(item);
|
|
RedisUtil.getInstance().hset(CacheNames.ENUMTYPES, item.name.toLowerCase(), jsonObj);
|
}
|
|
|
public static void delEnumType(String name) {
|
if (StringUtils.isBlank(name))
|
return;
|
|
RedisUtil.getInstance().hdel(CacheNames.ENUMTYPES, name.toLowerCase());
|
}
|
|
// public void reloadCache() throws VCIError {
|
// try {
|
// this.setCurrentTime(this.getDataBaseCurrtenttime());
|
// enumMap = new HashMap<String, EnumItem>();
|
// initAttribute();
|
// } catch (Throwable e) {
|
// e.printStackTrace();
|
// throw getLocalVciError("P0010PLMENUM-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<EnumItem> list = Arrays.asList(enumItems);
|
// Collections.sort(list, new Comparator<EnumItem>(){
|
// @Override
|
// public int compare(EnumItem e1, EnumItem e2) {
|
// return new String(e1.name).compareTo(new String(e2.name));
|
// }
|
// });
|
// enumItems = list.toArray(new EnumItem[list.size()]);
|
// }
|
|
/**
|
* 查询枚举
|
* @param filter,条件,只有name其左右
|
* @param start,暂不起作用
|
* @param rows,暂不起作用
|
*/
|
// public EnumItem[] getEmItems(String filter, int start, int rows)
|
// throws VCIError {
|
// refreshCache();
|
// List<EnumItem> enumList = new ArrayList<EnumItem>();
|
// EnumItem[] cEnumItems = enumItems;
|
// if(filter != null && !filter.equals("")){
|
// for (EnumItem em : enumItems) {
|
// if (em.name.indexOf(filter) >= 0) {
|
// enumList.add(em);
|
// }
|
// }
|
// cEnumItems = enumList.toArray(new EnumItem[enumList.size()]);
|
// }
|
//
|
// return cEnumItems;
|
// }
|
|
// private synchronized void refreshCache() throws VCIError {
|
// //String objType = "enum";
|
// long cTime = this.getDataBaseCurrtenttime();
|
// //List<PLCacheTempEntity> list = needUpdateCache(objType);
|
// PLCacheRecordEnt[] list = getUpdatedCacheData(CACHENAME);
|
// if (list != null) {
|
// if (list.length > 0)
|
// updateCache(CACHENAME, list);
|
//
|
// this.setCurrentTime(cTime);
|
// }
|
// }
|
|
/**
|
* 检查枚举名是否存在
|
*/
|
// public boolean checkRowIsExists(String name) throws VCIError {
|
// refreshCache();
|
// if (enumMap.containsKey(name)) {
|
// return true;
|
// }
|
// return false;
|
// }
|
//
|
/**
|
* 根据枚举类型查询枚举
|
*
|
*/
|
// public EnumItem[] getEmItemsByType(String type) throws VCIError {
|
// refreshCache();
|
// List<EnumItem> emItemList = new ArrayList<EnumItem>();
|
// try{
|
// for (EnumItem em : enumItems) {
|
// if (type.equals(em.type)) {
|
// EnumItem eiRes = es.FilterSecretGrade(em);
|
// emItemList.add(eiRes);
|
// }
|
// }
|
// }catch(Throwable e){
|
// e.printStackTrace();
|
// throw getLocalVciError("P0010PLMENUM-00006", e);
|
// }
|
// return emItemList.toArray(new EnumItem[emItemList.size()]);
|
// }
|
|
|
|
// private EnumService es = new EnumService();
|
|
/**
|
* 根据枚举名字查询枚举
|
*/
|
// public EnumItem getEmItemByName(String name) throws VCIError {
|
// refreshCache();
|
// try{
|
// if (enumMap.containsKey(name)) {
|
// EnumItem ei = enumMap.get(name);
|
// EnumItem eiRes = es.FilterSecretGrade(ei);
|
// return eiRes;
|
// }
|
// }catch(Throwable e){
|
// e.printStackTrace();
|
// throw getLocalVciError("P0010PLMENUM-00007", e);
|
// }
|
// EnumItem em = new EnumItem();
|
// em.length = 0;
|
// em.children = new EnumChild[0];
|
// return em;
|
// }
|
//
|
// public boolean updateCache(String objType, PLCacheRecordEnt[] records) throws VCIError {
|
// boolean flag = false;
|
// try {
|
// List<EnumItem> list = new ArrayList<EnumItem>();
|
// for (int i = 0 ; i < enumItems.length ; i++) {
|
// list.add(enumItems[i]);
|
// }
|
// List<String> idList = new ArrayList<String>();
|
// for (PLCacheRecordEnt record : records) {
|
// idList.add(record.getId());
|
// EnumItem em = EnumService.getInstance().getEnumItemByOid(record.getObjId());
|
// if ("creat".equals(record.getOperateType())) {
|
// list.add(em);
|
// enumMap.put(em.name, em);
|
// } else if ("edit".equals(record.getOperateType())) {
|
// list = removeListItemByOid(record.getObjId(), list);
|
// enumMap = removeMapItemByOid(record.getObjId(), enumMap);
|
// list.add(em);
|
// enumMap.put(em.name, em);
|
// } else if ("delete".equals(record.getOperateType())) {
|
// enumMap = removeMapItemByOid(record.getObjId(), enumMap);
|
// list = removeListItemByOid(record.getObjId(), list);
|
// }
|
// }
|
// enumItems = list.toArray(new EnumItem[list.size()]);
|
// arraySort();
|
// } catch (Throwable e) {
|
// e.printStackTrace();
|
// throw getLocalVciError("P0010PLMENUM-00008", e);
|
// }
|
// flag = true;
|
// return flag;
|
// }
|
//
|
// protected VCIError getLocalVciError(String key, Throwable e) {
|
// VCIError error = null;
|
// if (e == null) {
|
// error = new VCIError(key, new String[0]);
|
// } else {
|
// error = new VCIError(key, new String[]{VciExceptionTool.getExceptionStr(e), VciExceptionTool.getExceptionDetail(e)});
|
// }
|
// VCIError rsError = ExceptionLocalHandler.getInstance().getLocalString(error, "PLMENUM");
|
// return rsError;
|
// }
|
//
|
// private Map<String, EnumItem> removeMapItemByOid(String oid, Map<String, EnumItem> map) {
|
// for (Iterator<String> it = map.keySet().iterator();it.hasNext();) {
|
// EnumItem em = map.get(it.next());
|
// if (oid.equals(em.oid)) {
|
// map.remove(em);
|
// }
|
// }
|
// return map;
|
// }
|
//
|
// private List<EnumItem> removeListItemByOid(String oid, List<EnumItem> list) {
|
// Iterator<EnumItem> it = list.iterator();
|
// for (;it.hasNext();) {
|
// EnumItem em = it.next();
|
// if (oid.equals(em.oid)) {
|
// it.remove();
|
// }
|
// }
|
// return list;
|
// }
|
}
|