- package com.wodexiangce.web.servlet;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.io.UnsupportedEncodingException;
- import java.net.InetAddress;
- import java.net.UnknownHostException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Iterator;
- import java.util.List;
- import javax.faces.FactoryFinder;
- import javax.faces.context.FacesContext;
- import javax.faces.context.FacesContextFactory;
- import javax.faces.lifecycle.Lifecycle;
- import javax.faces.lifecycle.LifecycleFactory;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.output.XMLOutputter;
- import com.wodexiangce.persistence.model.WdxcAlbum;
- import com.wodexiangce.persistence.model.WdxcOrderItem;
- import com.wodexiangce.persistence.model.WdxcPhoto;
- import com.wodexiangce.persistence.model.WdxcProductRecord;
- import com.wodexiangce.services.AlbumService;
- import com.wodexiangce.services.OrderService;
- import com.wodexiangce.services.PhotoService;
- import com.wodexiangce.services.ProductService;
- import com.wodexiangce.servlet.WdxcWebAbstractServlet;
- import com.wodexiangce.web.product.CommonModel;
- import com.wodexiangce.web.product.ProductHandler;
- import com.wodexiangce.web.query.PhotoAdvanceQuery;
- public class PhotoBookServlet extends WdxcWebAbstractServlet {
- private static final long serialVersionUID = 1L;
- private CommonModel cm ;//=========加入购物车参数
- public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException {
- //=====得到处理类型
- String handlerType = request.getParameter("handlertype");
- if(handlerType==null){
- return ;
- }
- switch(new Integer(handlerType).intValue()){
- case 1: //=====相册列表
- this.createdAlbumXml(request,response);
- break ;
- case 2: //======相册的所有照片
- this.createdPhotosXML(request,response);
- break ;
- case 3: //======加入购物车
- this.addToshoppingCart(request,response) ;
- break ;
- case 4: //======获取订单信息中的产品内容
- getOrderInfo(request,response);
- break ;
- case 5: //======自动填充
- String albumid = request.getParameter("albumid") ;
- this.getAllphotoOfAlbum(request, response, new Long(albumid)) ;
- break ;
- case 6: //======产品保存
- this.saveProductHanlder(request, response);
- break ;
- case 7: //======得到产品信息
- this.getProductRecordInfo(request,response) ;
- break ;
- case 8: //======得到产品名称
- this.getProductName(request, response) ;
- break ;
- }
- }
- /************* 取得订单信息中的产品内容********************************/
- private void getOrderInfo(HttpServletRequest request, HttpServletResponse response)throws IOException{
- String itemID = request.getParameter("itemid") ;
- //wdxcOrderItem = orderService.getOrderItemByID(Long.parseLong(itemID));
- OrderService orderService = (OrderService)wac.getBean("orderService");
- WdxcOrderItem wdxcOrderItem = orderService.getOrderItemByID(Long.parseLong(itemID));
- String context = wdxcOrderItem.getProductcontent() ;
- response.setContentType("text/xml");
- response.setCharacterEncoding("UTF-8");
- PrintWriter pw = response.getWriter();
- pw.write(context);
- pw.flush();
- pw.close() ;
- }
- /**********获取已保存产品信息*********************************/
- public void getProductRecordInfo(HttpServletRequest request, HttpServletResponse response)throws IOException{
- String pid = request.getParameter("productrecordid");
- ProductService ps = (ProductService)wac.getBean("productService");
- WdxcProductRecord wpr = ps.getProductRecord(new Long(pid));
- String content = wpr.getContent() ;
- response.setContentType("text/xml");
- response.setCharacterEncoding("UTF-8");
- PrintWriter pw = response.getWriter();
- pw.write(content);
- pw.flush();
- pw.close() ;
- }
- /**********保存产品,并返回保存结果*********************************/
- public void saveProductHanlder(HttpServletRequest request, HttpServletResponse response) throws IOException{
- long pid = this.saveProduct(request, response) ;
- response.getOutputStream().print(pid);
- }
- /**********保存产品*********************************/
- public long saveProduct(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException{
- String content = request.getParameter("content") ;
- String productname = request.getParameter("productname") ;
- String productid = request.getParameter("productid") ;
- Long userID = this.getProductHandler(request, response).getPrinciple().getUserid() ;
- String pid = request.getParameter("pid") ;
- String previewURL = request.getParameter("previewURL") ;
- content = new String(content.getBytes("ISO-8859-1"),"UTF-8");
- ProductService p = (ProductService)wac.getBean("productService");
- WdxcProductRecord wpr = new WdxcProductRecord() ;
- wpr.setProductID(new Long(productid));
- wpr.setUserID(userID);
- wpr.setClassID(87l);
- wpr.setPreurl(previewURL);
- /*========保存产品,产品名称不为空,但如果没有保存产品,直接加入购物车,此时ProductName为空*/
- if(productname!=null){
- productname = new String(productname.getBytes("ISO-8859-1"),"UTF-8");
- wpr.setName(productname);
- }
- wpr.setCreatetime(new Date());
- wpr.setFlag(0);
- wpr.setContent(content);
- //============保存产品
- if(pid==null||pid==""){
- //======之前没有保存产品,直接加入购物车,产品名称为空,创建默认名称
- if(productname==null){
- SimpleDateFormat formatname = new SimpleDateFormat ("yyyyMMdd");
- productname = "我的照片书"+formatname.format(new Date());
- wpr.setName(productname) ;
- }
- return p.saveProductRecord(wpr);
- }
- //============更新产品
- else{
- wpr.setId(new Long(pid));
- p.updateProductRecord(wpr);
- return new Long(pid) ;
- }
- }
- /**********获取作品名称************************/
- private void getProductName(HttpServletRequest request, HttpServletResponse response)throws IOException{
- String pid = request.getParameter("pid") ;
- ProductService p = (ProductService)wac.getBean("productService");
- WdxcProductRecord wpr = p.getProductRecord(new Long(pid));
- response.setCharacterEncoding("UTF-8");
- response.getOutputStream().print(wpr.getName());
- }
- /**********获取某个相册的所有照片(自动填充)************************/
- public void getAllphotoOfAlbum(HttpServletRequest request,HttpServletResponse response,long albumid)throws IOException{
- PhotoService ps = (PhotoService)wac.getBean("photoService");
- PhotoAdvanceQuery paq = new PhotoAdvanceQuery();
- paq.setAlbumId(albumid);
- int photoCount = ps.getPhotoCountByAlbumId(albumid) ;
- Long userID = this.getProductHandler(request, response).getPrinciple().getUserid() ;
- List photos = ps.getPhotoAdvanceLimited(userID,paq,0,photoCount);
- Element root = new Element("wdxc");
- Document doc = new Document(root);
- //========总数
- Element count = new Element("count");
- count.addContent(new Element("num").setText(String.valueOf(photoCount)));
- root.addContent(count);
- for(Object obj:photos){
- WdxcPhoto wp = (WdxcPhoto)obj ;
- //=====创建节点
- Element elements = new Element("photos");
- elements.addContent(new Element("id").setText(String.valueOf(wp.getId())));
- elements.addContent(new Element("compressurl").setText(wp.getCompressurl()));
- //===== 给父节点wdxc添加album子节点;
- root.addContent(elements);
- }
- XMLOutputter XMLOut = new XMLOutputter();
- //=====发送xml数据到客户端浏览器
- response.setContentType("text/xml");
- response.setCharacterEncoding("UTF-8");
- XMLOut.output(doc, response.getOutputStream());
- }
- public void addToshoppingCart(HttpServletRequest request,HttpServletResponse response)throws IOException{
- String content = request.getParameter("content") ;
- String productID = request.getParameter("productID") ;
- String previewURL = request.getParameter("previewURL") ;
- String productInnerNum = request.getParameter("productInnerNum");
- //========购买数量
- String number = request.getParameter("number");
- /*加入购物车之前,先保存产品*/
- long pid =this.saveProduct(request, response) ;
- ProductHandler ph = this.getProductHandler(request, response) ;
- cm = new CommonModel () ;
- cm.setNumber(Integer.parseInt(number)); //购买数量
- content = new String(content.getBytes("ISO-8859-1"),"UTF-8");
- cm.setContent(content);//所有图片版式和背景图片
- cm.setProductID(new Long(productID));
- cm.setPreviewURL(previewURL);
- cm.setProductRecordId(pid);
- // 产品iD算出类型
- cm.setProductInnerNum(Integer.parseInt(productInnerNum));
- ph.setCommonModel(cm);
- ph.addShoppingProductOfEdit();
- response.getOutputStream().print("success");
- }
- /**
- * **************获取照片名称列表**************
- */
- public void createdPhotosXML(HttpServletRequest request,HttpServletResponse response)throws IOException{
- Long albumid = new Long(request.getParameter("albumid"));
- int pageNum = Integer.parseInt(request.getParameter("pageNum"));
- Long userID = this.getProductHandler(request, response).getPrinciple().getUserid() ;
- PhotoService ps = (PhotoService)wac.getBean("photoService");
- PhotoAdvanceQuery paq = new PhotoAdvanceQuery();
- paq.setAlbumId(albumid);
- int pageSize = 20 ;
- int photoCount = ps.getPhotoCountByAlbumId(albumid) ;
- //==========总页数
- photoCount = (photoCount%pageSize==0)?(photoCount/pageSize):(photoCount/pageSize+1) ;
- List photos = ps.getPhotoAdvanceLimited(userID,paq,pageNum*pageSize,pageSize);
- Element root = new Element("wdxc");
- Document doc = new Document(root);
- //========总数
- Element count = new Element("count");
- count.addContent(new Element("num").setText(String.valueOf(photoCount)));
- root.addContent(count);
- for(Object obj:photos){
- WdxcPhoto wp = (WdxcPhoto)obj ;
- //=====创建节点
- Element elements = new Element("photos");
- elements.addContent(new Element("id").setText(String.valueOf(wp.getId())));
- elements.addContent(new Element("abbrurl").setText(wp.getAbbrurl()));
- elements.addContent(new Element("compressurl").setText(wp.getCompressurl()));
- //===== 给父节点wdxc添加album子节点;
- root.addContent(elements);
- }
- XMLOutputter XMLOut = new XMLOutputter();
- //=====发送xml数据到客户端浏览器
- response.setContentType("text/xml");
- response.setCharacterEncoding("UTF-8");
- XMLOut.output(doc, response.getOutputStream());
- }
- /**
- * **************获取相册名称列表**************
- * @param response
- */
- public void createdAlbumXml(HttpServletRequest request,HttpServletResponse response) throws IOException{
- AlbumService as = (AlbumService) wac.getBean("albumService");
- System.out.println(request.getParameter("userID"));
- Long userID = this.getProductHandler(request, response).getPrinciple().getUserid() ;
- List list =as.getAlbum(userID);
- //=====创建根节点 list;
- Element root = new Element("wdxc");
- Document doc = new Document(root);
- Iterator it = list.iterator();
- while(it.hasNext()) {
- WdxcAlbum wa = (WdxcAlbum)it.next();
- //=====创建节点
- Element elements = new Element("album");
- elements.addContent(new Element("albumid").setText(String.valueOf(wa.getId())));
- //=====加入节点name
- elements.addContent(new Element("albumname").setText(wa.getName()));
- //===== 给父节点wdxc添加album子节点;
- root.addContent(elements);
- }
- System.out.print(doc.toString());
- XMLOutputter XMLOut = new XMLOutputter();
- //=====发送xml数据到客户端浏览器
- response.setContentType("text/xml");
- response.setCharacterEncoding("UTF-8");
- XMLOut.output(doc, response.getOutputStream());
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException {
- this.doGet(request, response);
- }
- /********************************得到上下文环境*********************************************/
- private ProductHandler getProductHandler(HttpServletRequest request, HttpServletResponse response){
- LifecycleFactory lFactory = (LifecycleFactory) FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
- Lifecycle lifecycle = lFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);
- FacesContextFactory fcFactory = (FacesContextFactory) FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
- FacesContext facesContext = fcFactory.getFacesContext(getServletContext(), request, response, lifecycle);
- ProductHandler ph = (ProductHandler) facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, "productHandler");
- return ph ;
- }
- }
照片书后台
最新推荐文章于 2019-08-29 10:14:49 发布