package zr.zrpower.collectionengine;

import javax.servlet.http.HttpServlet;
import zr.zrpower.collectionengine.service.CollectionService;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletOutputStream;


public class PhotoEngine extends HttpServlet {
  //Initialize global variables
  public void init() throws ServletException {

  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    try {
      String mId = request.getParameter("coll_id");
      String mPkid = request.getParameter("coll_pkid");
      String mImg = request.getParameter("coll_img");
	  CollectionService collMgr = new CollectionService();

      byte[] mImage = collMgr.getCollImage(mId,mPkid,mImg);
      if (mImage != null) {
        response.reset();
        response.setContentType("image/jpeg");
        ServletOutputStream op = response.getOutputStream();
        op.write(mImage);
        op.flush();
        op.close();
        op=null;
        collMgr = null;
        mImage = null;
      }
      else {
        response.sendRedirect("zrsysmanage/images/NoImage.gif");
      }
    }
    catch (Exception ex) {

    }
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }
}
