package com.lgx8.right.action;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import com.lgx8.common.servlet.BaseController;
import com.lgx8.common.util.Constants;
import com.lgx8.common.util.tag.authority.AuthorityControl;
import com.lgx8.right.dao.IModuleDao;
import com.lgx8.right.dao.IRoleDao;
import com.lgx8.right.entities.Module;
import com.lgx8.right.entities.RunTimeUser;
@Controller
public class ModuleController extends BaseController {
@Resource(name="moduleDao")
IModuleDao moduleDao;
@Resource(name="roleDao")
IRoleDao roleDao;
@RequestMapping(value="/authorize.do")
public void authorize(HttpServletRequest request,HttpServletResponse response){
RunTimeUser user = RunTimeUser.getRunTimeUser(request);
response.setContentType("text/html; charset=UTF-8");
response.setCharacterEncoding("UTF-8");
//获取树形结构的字符串
String xmlStr = this.loadXML(user);
this.printXML(response, xmlStr);
}
//获取左菜单树XMLStr
public String loadXML(RunTimeUser user) {
List<Module> modules = Constants.getMenuList();
StringBuffer xml = new StringBuffer(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
xml.append("<tree id=\"0\">");
getXML(user,modules,xml);
xml.append("</tree>");
return xml.toString();
}
private void getXML(RunTimeUser user,List<Module>modules,StringBuffer xml) {
sortRes(modules);
for(Module m : modules) {
//验证用户权限
if (!AuthorityControl.isAuthorized(user, m.getId())) {
continue;
}
//左树不展现按钮权限
if ("button".equals(m.getType())){
continue;
}
xml.append("<item text=\""
+ m.getName()
+ "\" id=\""
+ m.getId());
xml.append("\" im0=\"tombs.gif\" im1=\"tombs.gif\" im2=\"folderClosed.gif\" ");
xml.append(">");
xml.append("<userdata name=\"level\">" + m.getLevel()
+ "</userdata>");
xml.append("<userdata name=\"url\"><![CDATA[" + m.getUrl()
+ "]]></userdata>");
getXML(user, m.getModules(),xml);
xml.append("</item>");
}
}
@RequestMapping(value="/redirct.do")
public ModelAndView redirct(HttpServletRequest request,HttpServletResponse response){
String moduleId = (String) request.getParameter("moduleId");
String url = Constants.getModuleUrls().get(moduleId).getUrl();
if("#".equals(url) || "".equals(url) || url == null) return null;
if(url.indexOf(".do")!=-1){
try {
response.setContentType("charset=UTF-8");
response.sendRedirect(request.getContextPath()+url);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
if (Constants.getModuleUrls().get(moduleId).getUrl().indexOf("page") > -1) {
System.out.println(Constants.getModuleUrls().get(moduleId).getUrl());
return new ModelAndView(new RedirectView(Constants.getModuleUrls().get(moduleId).getUrl()));
}
return new ModelAndView(Constants.getModuleUrls().get(moduleId).getUrl());
}
//数据集合根据UID排序
private void sortRes(List<Module> modules) {
Collections.sort(modules, new Comparator<Module>(){
public int compare(Module o1, Module o2) {
int n = o1.getId().compareTo(o2.getId());
if (n == 0) {
return 0;
} else {
if (n > 0) {
return 1;
} else {
return -1;
}
}
}
});
}
}