package com.vci.server.omd.attribpool.cache; import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.dom4j.DocumentException; import com.alibaba.fastjson.JSONObject; import com.vci.common.log.ServerWithLog4j; import com.vci.corba.common.VCIError; import com.vci.corba.omd.atm.AttribItem; import com.vci.server.cache.CacheNames; import com.vci.server.cache.OMCacheProvider; import com.vci.server.cache.redis.RedisUtil; import com.vci.server.omd.attribpool.service.AttrPoolService; public class APServerCacheUtil {//extends BaseCacheTimer{ //private AttribItem[] attributes = null; //private Map attrNameMap = null; // private static volatile APServerCacheUtil instance = null; // private static String CACHENAME = "attributepool"; // private APServerCacheUtil() { // super(CACHENAME); // try { // attrNameMap = new HashMap(); // this.setCurrentTime(this.getDataBaseCurrtenttime()); // initAttribute(); // } catch (Throwable e) { // //e.printStackTrace(); // ServerWithLog4j.logger.error(e); // } // } // // public static APServerCacheUtil getInstance() { // if (instance == null) { // synchronized (APServerCacheUtil.class) { // if (instance == null) { // instance = new APServerCacheUtil(); // } // } // } // // return instance; // } public static void initCache() { try { clearCache(); initAttribute(); } catch (Exception e) { ServerWithLog4j.logger.error(e); } } public static void clearCache() { RedisUtil.getInstance().del(CacheNames.ATTRITEMS); } private static void initAttribute() throws VCIError, SQLException, IOException, DocumentException { RedisUtil.getInstance().del(CacheNames.ATTRITEMS); AttribItem[] attrs = AttrPoolService.getInstance().getAttribItems("", 1, 1); for (int i = 0; i < attrs.length; i++) { if (attrs[i] == null) return; String jsonObj = JSONObject.toJSONString(attrs[i]); RedisUtil.getInstance().hset(CacheNames.ATTRITEMS, attrs[i].name.toLowerCase(), jsonObj); } } public static void delAttribute(String name) { if (StringUtils.isBlank(name)) return; RedisUtil.getInstance().hdel(CacheNames.ATTRITEMS, name.toLowerCase()); } public static void setAttribute(AttribItem item) { if (item == null) return; String jsonObj = JSONObject.toJSONString(item); RedisUtil.getInstance().hset(CacheNames.ATTRITEMS, item.name.toLowerCase(), jsonObj); } // public static void setAttribute(AttribItem item) { // if (item == null) // return; // // String jsonObj = JSONObject.toJSONString(item); // // RedisUtil.getInstance().hset(CacheNames.ATTRITEMS, item.name.toLowerCase(), jsonObj); // } // // public void reloadCache() throws VCIError { // try { // initAttribute(); // }catch (Throwable e) { // e.printStackTrace(); // throw getLocalVciError("P0010PLMAP-00012", e); // } // } // // // public void arraySort() { // List list = Arrays.asList(attributes); // Collections.sort(list, new Comparator(){ // @Override // public int compare(AttribItem a1, AttribItem a2) { // return new String(a1.name).compareTo(new String(a2.name)); // } // }); // attributes = list.toArray(new AttribItem[list.size()]); // } /** * 根据条件获取符合条件的属性定义 * * @param filter,条件,只有name其左右 * @param start,暂不起作用 * @param rows,暂不起作用 * @return * @throws VCIError */ // public static AttribItem[] getAttribItems(String filter, int start, int rows) throws VCIError { // // List attrList = new ArrayList(); // AttribItem[] cAttributes = OMCacheProvider.getAttributes(); // if(filter != null && !filter.equals("")){ // for (AttribItem attr : cAttributes) { // if (attr.name.indexOf(filter) >= 0) { // attrList.add(attr); // } // } // cAttributes = attrList.toArray(new AttribItem[attrList.size()]); // } // // return cAttributes; // } // /** * 判断当前名称的属性是否存在 * * @param name * @return * @throws VCIError */ // public boolean checkRowIsExists(String name) throws VCIError { // refreshCache(); // if (attrNameMap.containsKey(name)) { // return true; // } // // return false; // } /** * 根据属性的名称集合获取详细的属性定义 * @param attNames * @return * @throws VCIError */ // public AttribItem[] getAttribItemsByNames(String[] attNames) // throws VCIError { // refreshCache(); // List attrList = new ArrayList(); // for (String attrName : attNames) { // if (attrNameMap.containsKey(attrName)) { // attrList.add(attrNameMap.get(attrName)); // } // } // // return attrList.toArray(new AttribItem[attrList.size()]); // } /** * 获取单一属性名对应的属性详细信息 * @param abName * @return * @throws VCIError */ // public AttribItem getAttribItemByName(String abName) throws VCIError { // refreshCache(); // if (attrNameMap.containsKey(abName)) { // return attrNameMap.get(abName); // } // // return new AttribItem(); // } /** * 获取属性的数据类型 * @param abName * @return * @throws VCIError */ // public String getAttribItemDataType(String abName) throws VCIError { // refreshCache(); // if (attrNameMap.containsKey(abName)) { // return attrNameMap.get(abName).vtDataType; // } // // return ""; // } /** * 获取引用枚举的所有属性名称 * @param emName * @return * @throws VCIError */ // public static String[] getAPNamesByEMName(String emName) throws VCIError { // List attrNameList = new ArrayList(); // // AttribItem[] items = OMCacheProvider.getAttributes(); // for (AttribItem attr : items) { // if (attr.other.indexOf(emName) >= 0) { // attrNameList.add(attr.name); // } // } // // return attrNameList.toArray(new String[attrNameList.size()]); // } /** * 获取名称包含text,名称不在abNameArray数组中的属性详细信息 * @param abNameArray * @param text * @return * @throws VCIError */ public static AttribItem[] getAttribItemsOutNames(String[] abNameArray, String text) throws VCIError { List abNameList = new ArrayList(); for (String ab : abNameArray) { abNameList.add(ab); } AttribItem[] items = OMCacheProvider.getAttributes(); List attrList = new ArrayList(); for (AttribItem attr : items) { if (attr.name.indexOf(text) >= 0 && !abNameList.contains(attr.name)) { attrList.add(attr); } } return attrList.toArray(new AttribItem[attrList.size()]); } // private synchronized void refreshCache() throws VCIError { // long cTime = this.getDataBaseCurrtenttime(); // //List list = needUpdateCache(objType); // PLCacheRecordEnt[] list = getUpdatedCacheData(CACHENAME); // if (list != null) { // if (list.length > 0){ // updateCache(CACHENAME, list); // } // this.setCurrentTime(cTime); // } // } // private boolean updateCache(String objType, PLCacheRecordEnt[] cacheTempList) throws VCIError { // boolean flag = false; // try { // List list = new ArrayList(); // for (int i = 0 ; i < attributes.length ; i++) { // list.add(attributes[i]); // } // List idList = new ArrayList(); // for (PLCacheRecordEnt cache : cacheTempList) { // idList.add(cache.getId()); // AttribItem ab = AttrPoolService.getInstance().getAttribItemByOid(cache.getObjId()); // if ("creat".equals(cache.getOperateType())) { // list.add(ab); // attrNameMap.put(ab.name, ab); // } else if ("edit".equals(cache.getOperateType())) { // list = removeListItemByOid(cache.getObjId(), list); // attrNameMap = removeMapItemByOid(cache.getObjId(), attrNameMap); // list.add(ab); // attrNameMap.put(ab.name, ab); // } else if ("delete".equals(cache.getOperateType())) { // attrNameMap = removeMapItemByOid(cache.getObjId(), attrNameMap); // list = removeListItemByOid(cache.getObjId(), list); // } // } // attributes = list.toArray(new AttribItem[list.size()]); // arraySort(); // } catch (Throwable e) { // e.printStackTrace(); // throw getLocalVciError("P0010PLMAP-00012", 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, "PLMAP"); // return rsError; // } // // private Map removeMapItemByOid(String oid, Map map) { // for (Iterator it = map.keySet().iterator();it.hasNext();) { // AttribItem ab = map.get(it.next()); // if (oid.equals(ab.oid)) { // map.remove(ab); // } // } // // resetCache(); // return map; // } // // private List removeListItemByOid(String oid, List list) { // Iterator it = list.iterator(); // for (;it.hasNext();) { // AttribItem ab = it.next(); // if (oid.equals(ab.oid)) { // it.remove(); // } // } // // resetCache(); // return list; // } // // private void resetCache() { // this.resetCache(CACHENAME); // } }