Package com.lgx8.gateway.member.servlet

Source Code of com.lgx8.gateway.member.servlet.QueryAddressServlet

package com.lgx8.gateway.member.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.lgx8.common.PageList;
import com.lgx8.gateway.dao.IAddressDao;
import com.lgx8.gateway.entities.Address;
import com.lgx8.gateway.json.JSONObject;
import com.lgx8.right.entities.RunTimeUser;

/**
* Servlet implementation class QueryAddressServlet
*/
public class QueryAddressServlet extends HttpServlet {
  private static final long serialVersionUID = 1L;
      
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QueryAddressServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    //创造JSON所需的Map(此处代码可使用StringBuffer拼接JSON字符串代替)
    Map<Object,Object> pageInfo = new HashMap<Object,Object>();
   
    RunTimeUser currentUser = RunTimeUser.getRunTimeUser(request);
    if(null==currentUser) {
      response.setContentType("html/txt");
      response.setCharacterEncoding("utf-8");  
          response.setHeader("Pragma", "no-cache");  
          response.setHeader("Cache-Control", "no-cache, must-revalidate");  
          response.setHeader("Pragma", "no-cache");  
          try {  
              response.getWriter().write("");  
              response.getWriter().flush();  
              response.getWriter().close();
          } catch (IOException e) {  
              e.printStackTrace();  
          }
    }
   
    String hql = " from Address a where a.user.id = " + currentUser.getId();
   
    String hidden = request.getParameter("hidden");
    //得到当前页数
    String page = request.getParameter("page");
    //得到每页显示行数
    String maxResult = request.getParameter("rp");
    //获得查询的key
    String query = request.getParameter("query");
    //获得查询的条件
    String qtype = request.getParameter("qtype");
    //计算查询开始数据下标

    ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    IAddressDao addressDao = (IAddressDao) ac.getBean("addressDao");
   
    if("1".equals(hidden) || Integer.parseInt(hidden) == 1)
    {
      if(query!=null && !("").equals(query))
      {
        String[] q1 = qtype.split(",");
        String[] q2 = query.split(",");
        Map<String,Object> map = new HashMap<String, Object>();
        for(int i=0;i<q2.length;i++)
        {
          String[] q22 = q2[i].split("#");
          map.put(q22[0], q22.length > 1?q22[1]:"");
        }
       
        for(int i=0;i<q1.length;i++)
        {
          String q = q1[i].split(":")[0];
          String d = q1[i].split(":").length>1?q1[i].split(":")[1]:"";
          if("date".equals(d) && !"".equals(d))
          {
            String d1 = map.get(q).toString().split("&")[0];
            String d2 = map.get(q).toString().split("&").length >1?map.get(q).toString().split("&")[1]:"";
            hql += " and " + q + " >= str_to_date('"+d1+"','%Y-%m-%d %H:%i:%s')";
            hql += "".equals(d2)?"":" and " + q + " <= str_to_date('"+d2+"','%Y-%m-%d %H:%i:%s')";
          }else if(!"".equals(map.get(q)))
          {
            hql += " and " + q + " like '%"+map.get(q)+"%'";
          }
         
        }
      }
     
      PageList list = addressDao.findByPage4Report(hql, new Object[]{}, Integer.parseInt(page), Integer.parseInt(maxResult));
      //当前页数设置
      pageInfo.put("page", page);
      //当前页数设置
      pageInfo.put("size", maxResult);
      //总搜索数据量设置
      pageInfo.put("total", list.getTotalRecordNum());
     
      String jsonStr = creMessageJSON(list.getDataList(), pageInfo);
      //response相关处理
      response.setContentType("html/txt");
      response.setCharacterEncoding("utf-8");  
          response.setHeader("Pragma", "no-cache");  
          response.setHeader("Cache-Control", "no-cache, must-revalidate");  
          response.setHeader("Pragma", "no-cache");  
          try {  
              response.getWriter().write(jsonStr);  
              response.getWriter().flush();  
              response.getWriter().close();
          } catch (IOException e) {  
              e.printStackTrace();  
          }
     
    }
    else if("3".equals(hidden))
    {
      String idStr = request.getParameter("addressId");
     
      String[] ids = idStr.split(",");
     
      addressDao.batchDeleteAddress(ids);
      //跳转
      response.setContentType("text/html;charset=UTF-8");
      PrintWriter out = response.getWriter();
      out.print("<script type=\"text/javascript\">alert('删除成功');location.href='page/management/member/member_welcome.jsp?url=member_address.jsp'</script>");
    }
  }
 
  private String creMessageJSON(List<Address> list,Map<Object,Object> pageInfo){
    String jsonStr = "";
   
    int num = Integer.parseInt(pageInfo.get("page").toString());
    int size = Integer.parseInt(pageInfo.get("size").toString());
   
    List<Map<Object,Object>> mapList = new ArrayList<Map<Object,Object>>();
    for(int i=0;i<list.size();i++){
      Map<Object,Object> cellMap = new HashMap<Object,Object>();
      cellMap.put("id", list.get(i).getId());
      cellMap.put("cell", new Object[]{
          list.get(i).getId(),
          ((num-1)*size+(i+1)),
          list.get(i).getName(),
          list.get(i).getTel(),
          list.get(i).getProvince()+","+list.get(i).getCity()+","+list.get(i).getRegion()+","+list.get(i).getAddress() + " (" + list.get(i).getZipcode() +")",
          list.get(i).getIsdefault() == 1 ? "是":"否"
      });
      mapList.add(cellMap);
    }
    pageInfo.put("rows", mapList);
    JSONObject object = new JSONObject(pageInfo);
    jsonStr = object.toString();
    return jsonStr;
  }

}
TOP

Related Classes of com.lgx8.gateway.member.servlet.QueryAddressServlet

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.