package com.asnyc.test;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AsnycServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String pIdByPage = request.getParameter("id")==null?"0":request.getParameter("id");
String str="";
DemoData d = new DemoData();
List<WarehouseNodeInfo> list = (d.queryDate(pIdByPage));
if (list != null && list.size() != 0) {
WarehouseNodeInfo menu=null;
WarehouseNodeInfo menuNext=null;
int level=1;//节点层次
long nId=0;//节点id
long pId=0;//父节点id
String nodeName="库房结构信息";//节点名称
long nodeType=0l;//当前节点类型
int size=list.size();//菜单数量
String url="";
String target="";
for (int i=0;i<size;i++) {
menu=list.get(i);
nodeName=menu.getNodeName();
nId=menu.getWarehouseinfoNodeId();
pId=menu.getParentNodeId();
level=(int)menu.getNodeLevel();
nodeType=menu.getNodeType();//下一节点类型(欲加载的节点类型)
target=menu.getTarget();
url=menu.getUrl()+"?nodeId="+menu.getWarehouseinfoNodeId()+"&contentType="+menu.getContentType();
if(i<size-1){//非最后一个
menuNext=list.get(i+1);
/*
* 因为是用递归查询出来的集合,所以不用判断nodeId是否等于pid了
* 此处只有level显得重要
*
*/
if(level<(int)menuNext.getNodeLevel()){//表示为公共节点,即该节点下面还有子节点
str +="{id:"+nId+",pId:"+pId+",name:'"+nodeName+"',nodeType:'"+nodeType+"',isParent:true";
str += ",nodes:[";
}
if(level>(int)menuNext.getNodeLevel()){//标识此处为菜单从子级向它父级过渡,
//即已经到叶子节点
if(nodeType==1){
str+="{id:"+nId+",pId:"+pId+",name:'"+nodeName+"',nodeType:'"+nodeType+"'}";
}else{//父节点
str+="{id:"+nId+",pId:"+pId+",name:'"+nodeName+"',nodeType:'"+nodeType+"',isParent:true}";
}
int len = (int)(level - menuNext.getNodeLevel());//过渡多少级
for( int l = 0;l < len;l++){
str += "]}";
}
str += ",";
}
if(level==(int)menuNext.getNodeLevel()){//同级节点
if(nodeType==1){
str+="{id:"+nId+",pId:"+pId+",name:'"+nodeName+"',nodeType:'"+nodeType+"'},";
}else{//父节点
str+="{id:"+nId+",pId:"+pId+",name:'"+nodeName+"',nodeType:'"+nodeType+"',isParent:true},";
}
}
}else{//最后一个菜单
String endStr="";
if(level>1){
/*for(int k=0;k<level-1;k++){
endStr += "]}";
}*/
if(nodeType==1){
str+="{id:"+nId+",pId:"+pId+",name:'"+nodeName+"',nodeType:'"+nodeType+"'}"+endStr;
}else{//父节点
str+="{id:"+nId+",pId:"+pId+",name:'"+nodeName+"',nodeType:'"+nodeType+"',isParent:true}"+endStr;
}
}else{
if(nodeType==1){
str+="{id:"+nId+",pId:"+pId+",name:'"+nodeName+"',nodeType:'"+nodeType+"'}";
}else{
str+="{id:"+nId+",pId:"+pId+",name:'"+nodeName+"',nodeType:'"+nodeType+"',isParent:true}";
}
}
}
}
str= "["+str+"]";
response.setContentType("text/xml;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(str);
out.flush();
out.close();
}
System.out.println("str="+str);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
doGet(request,response);
}
}