数据字典 加载到 web 项目的 application 全局

package com.ibm.scm.common.system;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.support.WebApplicationObjectSupport;

import com.ibm.scm.common.system.dict.biz.IDictItemBiz;
import com.ibm.scm.pojo.common.DictItem;
import com.tjmc.eipv.framework.Globals;
import com.tjmc.eipv.framework.security.INumgClient;
import com.tjmc.eipv.framework.security.IUserManager;


public class ScmsGlobalBean extends WebApplicationObjectSupport implements InitializingBean{
	private static final Logger log = Logger.getLogger(ScmsGlobalBean.class);

	private IDictItemBiz dictItemBiz = null;
	private INumgClient numgClient;
	private IUserManager userManager = null;
	private Map<String,Map<String,String>>pubDictMap = null;
	
	/**
	 * 与具体业务相对松耦合的配置,或者在应用中多次用到的配置项,存放在appConfig中
	 */
	private Map<String, String> appConfig;
	
	/**
	 * 加载初始化方法
	 */
	public void afterPropertiesSet() throws Exception {
		this.initDictItems();
	}

	/**
	 * 初始化字典表
	 */
	private void initDictItems() {
		log.info("加载数据字典开始");

		List<DictItem> dicts = dictItemBiz.listAll();
		//所有字典数据
		Map<String, Map<String, String>> dictMap = new LinkedHashMap<String, Map<String, String>>();
		//有效的字典数据
		Map<String, Map<String, String>> validDictMap = new LinkedHashMap<String, Map<String, String>>();

		for (DictItem di : dicts) {
			if (dictMap.get(di.getDictName()) == null) {
				dictMap.put(di.getDictName(), new LinkedHashMap<String, String>());
			}
			dictMap.get(di.getDictName()).put(di.getItemValue(), di.getItemText());

			if (ScmsGlobals.IS_VALID_TRUE.equalsIgnoreCase(di.getIsValid())) {
				if (validDictMap.get(di.getDictName()) == null) {
					validDictMap.put(di.getDictName(), new LinkedHashMap<String, String>());
				}
				validDictMap.get(di.getDictName()).put(di.getItemValue(), di.getItemText());
			}
		}

		for (String s : dictMap.keySet()) {
			Map<String, String> m = dictMap.get(s);
			for (String s1 : m.keySet()) {
				String ddTemp = m.get(s1);
				log.info(String.format("name=%s, label=%s, value=%s", s, ddTemp, s1));
			}
		}
		log.info("加载数据字典结束");

		log.info("系统配置参数开始");
		if (appConfig != null) {
			for (String k : appConfig.keySet()) {
				log.info(String.format("%s=%s", k, appConfig.get(k)));
			}
		}
		log.info("系统配置参数结束");
		pubDictMap = dictMap;
		this.getWebApplicationContext().getServletContext().setAttribute(Globals.DICT_MAP_KEY, dictMap);
		this.getWebApplicationContext().getServletContext().setAttribute(Globals.VALID_DICT_MAP_KEY, validDictMap);
		this.getWebApplicationContext().getServletContext().setAttribute(Globals.APP_CONFIG_KEY, this.appConfig);
	}

	/**
	 * @return the appConfig
	 */
	public Map<String, String> getAppConfig() {
		return appConfig;
	}

	/**
	 * @param appConfig the appConfig to set
	 */
	public void setAppConfig(Map<String, String> appConfig) {
		this.appConfig = appConfig;
	}


	/**
	 * @return the numgClient
	 */
	public INumgClient getNumgClient() {
		return numgClient;
	}


	/**
	 * @param numgClient the numgClient to set
	 */
	public void setNumgClient(INumgClient numgClient) {
		this.numgClient = numgClient;
	}


	/**
	 * @return the userManager
	 */
	public IUserManager getUserManager() {
		return userManager;
	}


	/**
	 * @param userManager the userManager to set
	 */
	public void setUserManager(IUserManager userManager) {
		this.userManager = userManager;
	}


	/**
	 * @return the pubDictMap
	 */
	public Map<String, Map<String, String>> getPubDictMap() {
		return pubDictMap;
	}


	/**
	 * @param pubDictMap the pubDictMap to set
	 */
	public void setPubDictMap(Map<String, Map<String, String>> pubDictMap) {
		this.pubDictMap = pubDictMap;
	}

	/**
	 * @return the dictItemBiz
	 */
	public IDictItemBiz getDictItemBiz() {
		return dictItemBiz;
	}

	/**
	 * @param dictItemBiz the dictItemBiz to set
	 */
	public void setDictItemBiz(IDictItemBiz dictItemBiz) {
		this.dictItemBiz = dictItemBiz;
	}
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券