package com.lgx8.common.servlet;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.lgx8.common.util.ArrayUtil;
import com.lgx8.common.util.Constants;
import com.lgx8.management.dao.IDictionaryDao;
import com.lgx8.management.entities.Dictionary;
import com.lgx8.right.action.City;
import com.lgx8.right.action.Province;
import com.lgx8.right.dao.IModuleDao;
import com.lgx8.right.entities.Module;
@SuppressWarnings("serial")
public class InitContextThread extends HttpServlet{
/**
* 构造方法.
*/
public InitContextThread() {
super();
}
/**
* 销毁方法
*
*
*
*/
public void destroy() {
}
/**
* 初始化
*
* @throws ServletException
*/
public void init() throws ServletException {
//初始化主菜单
initModule();
initProvinces();
}
/**
* 初始化省份和城市
* */
public void initProvinces(){
ServletContext appcontext = getServletContext();
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IDictionaryDao dao = (IDictionaryDao) ac.getBean("dictionaryDao");
List<Dictionary> prodic = dao.listByKind("province");
List<Province> pros = new ArrayList<Province>();
for(int i=0;i<prodic.size();i++){
Dictionary dic = prodic.get(i);
Province pro = new Province();
pro.setCode(dic.getCODE());
pro.setName(dic.getDETAIL());
List<Dictionary> citydics = dao.listByKindAndCode(dic.getKIND(), dic.getCODE());
List<City> citys = initCitys(citydics);
pro.setCitys(citys);
pros.add(pro);
}
appcontext.setAttribute("provinces",pros);
}
private List<City> initCitys(List<Dictionary> dics){
if(null==dics)return new ArrayList<City>();
List<City> citys = new ArrayList<City>();
for(int i=0;i<dics.size();i++){
Dictionary dic = dics.get(i);
City city = new City();
city.setName(dic.getDETAIL());
city.setCode(dic.getCODE());
citys.add(city);
}
return citys;
}
/**
* @method initMenu: 初始化菜单
*
*
*/
private void initModule() {
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IModuleDao moduleDao = (IModuleDao) ac.getBean("moduleDao");
List<Module> modules = moduleDao.findModulesByLevel(1);//查询所有一级节点
Constants.setMenuList(modules);
initModuleUrl(modules);
}
private void initModuleUrl(List<Module> modules) {
for (Module module : modules) {
Constants.getModuleUrls().put(module.getId(), module);
if (!ArrayUtil.isEmptyCollection(module.getModules())) {
initModuleUrl(module.getModules());
}
}
}
}