package com.servlets;

import java.io.IOException;
import java.io.PrintWriter;

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

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.artifacts.FactoryProvider;
import com.entities.Seller;



public class SregisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    
    public SregisterServlet() {
        super();
        
    }

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	response.setContentType("text/html;charset=UTF-8");
    	try(PrintWriter out = response.getWriter()){
    		try {
    			String sellerName = request.getParameter("seller_name");
    			String sellerInds = request.getParameter("seller_inds");
                String sellerEmail = request.getParameter("seller_email");
                String sellerPassword = request.getParameter("seller_password");
                String sellerBtype = request.getParameter("seller_btype");
                String sellerAd = request.getParameter("seller_ad");
                String sellerAline = request.getParameter("seller_aline");
                String sellerPhone = request.getParameter("seller_phone");
                String sellerCity= request.getParameter("seller_city");
                String sellerPincode = request.getParameter("seller_pincode");
                String sellerCountry = request.getParameter("seller_country");
                String sellerState = request.getParameter("seller_state");
    			
    			Seller seller =new Seller(sellerName, sellerEmail, sellerPassword, sellerBtype, sellerInds,
    					sellerAd, sellerAline, sellerPhone, sellerPincode, sellerCity,
    					sellerState, sellerCountry);
    			Session hibernateSession = FactoryProvider.getFactory().openSession();
    			Transaction tx = hibernateSession.beginTransaction();
    			int sellerId = (int) hibernateSession.save(seller);
    			System.out.println(sellerId);
    			
    			
    			tx.commit();
    			
    			hibernateSession.close();
    			
    			response.sendRedirect("bmessage.jsp");
    			
    			
    		}catch(Exception e) {
    			
    		}
    		
    		
    		
    	}
    	
    	
    	
    	
    	
    }

	
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		processRequest(request,response);
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request,response);
	}

}
