package com.lgx8.gateway.merchant.servlet;
import java.io.IOException;
import java.io.Writer;
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.gateway.service.IGatewayService;
import com.lgx8.gateway.util.GatewayConstants;
import com.lgx8.gateway.vo.ProductVO;
import com.lgx8.management.dao.IMerchantDao;
import com.lgx8.management.entities.Merchant;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
* Servlet implementation class MerchantMainServlet
*/
public class MerchantMainServlet extends MerchantBaseServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MerchantMainServlet() {
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 {
Template temp = getConfiguration().getTemplate("merchant/main.ftl");
Map<Object, Object> root = new HashMap<Object, Object>();
root.putAll(base);
String merchantId = request.getParameter("merchantId");
ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
IMerchantDao merchantDao = (IMerchantDao) ac.getBean("merchantDao");
if(merchantId == null || "".equals(merchantId))
{
merchantId = "0";
}
Merchant merchant = merchantDao.getMerchantById(Long.parseLong(merchantId));
root.put("merchant", merchant);
IGatewayService gatewayService = (IGatewayService) ac.getBean("gatewayService");
List<ProductVO> productVOs = gatewayService.initGatewayInfo(GatewayConstants.CST_SJYMGG);
if(productVOs != null && productVOs.size() != 0)
{
root.put("productVOs", productVOs);
}
/* 将模板和数据模型合并 */
Writer out = response.getWriter();
try {
temp.process(root, out);
} catch (TemplateException e) {
e.printStackTrace();
}
out.flush();
}
}