//This servlet is for giving ratings to other users by a user with whom he does any transaction

import java.io.*;	
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.lang.*;
import java.util.*;
import java.util.Date;


public class UserFeedbackServlet extends HttpServlet
{ 
    public HttpSession session;
    public void doGet (HttpServletRequest request, 
		       HttpServletResponse response) 
	throws ServletException, IOException {
	PrintWriter out;
	response.setContentType("text/html");
	out=response.getWriter();
	//ResultSet rs=null;
	try{
	    session=request.getSession(false);
	    if (session!=null){
		String login=(String)session.getValue("userLogin");
		String productId= request.getParameter("productId");//productId
		String sellerLogin = request.getParameter("sellerLogin");//seller Login
		String winnerLogin = request.getParameter("winnerLogin");//winne Login
		String toDo = request.getParameter("toDo");//whether to update or insert
		System.out.println("userLogin="+login);
		System.out.println("productId="+productId);
		System.out.println("sellerLogin="+sellerLogin);
		System.out.println("winnerLogin="+winnerLogin);
		if (sellerLogin.equals(login) || winnerLogin.equals(login)){
		    //function of UserPageGenerator which generated feedback form with fixed attributes ProductId, UserLogin, SellerLogin and hidden attribute toDo. 
		    UserPageGenerator.feedbackForm(productId, sellerLogin, winnerLogin, login, toDo, out);
		}
		else{
		    System.out.println("error UserFeedBackForm line 36");
		}
	    }
	    else{
		System.out.println("null session");
	    }
	}catch(IllegalStateException e){System.out.println("not a valid session");}
    }
    
    public void doPost (HttpServletRequest request, 
			HttpServletResponse response) 
	throws ServletException, IOException {
	PrintWriter out;
	response.setContentType("text/html");
	System.out.println("inside doPost method of userFeedbackServlet");
	try{
	    session=request.getSession(false);
	    if (session!=null){
		String login=(String)session.getValue("userLogin");
		String productId= request.getParameter("productId");//productId
		String sellerLogin = request.getParameter("sellerLogin");//seller Login
		String winnerLogin = request.getParameter("winnerLogin");//winner Login
		String winnerRatedSellerAs = request.getParameter("winnerRatedSellerAs");//winner Login
		String sellerRatedWinnerAs = request.getParameter("sellerRatedWinnerAs");//winner Login
		String toDo = request.getParameter("toDo");//whether to update or insert

		if (toDo.equals("update") && sellerLogin.equals(login)){
		    System.out.println("UPDATE 1:inside doPost method of userFeedbackServlet");
		    (SharedObjects.qe).updateWinnerRatedAs(productId, sellerRatedWinnerAs);
		}
		else if ( toDo.equals("insert") && sellerLogin.equals(login)){
		    System.out.println("INSERT!: inside doPost method of userFeedbackServlet");
		    (SharedObjects.qe).insertWinnerRatedAs(productId, sellerRatedWinnerAs, winnerLogin, sellerLogin);
		    System.out.println("INSERT!after method call: inside doPost method of userFeedbackServlet");
		}
		else if ( toDo.equals("update") && winnerLogin.equals(login)){
		    (SharedObjects.qe).updateSellerRatedAs(productId, winnerRatedSellerAs);		
		}
		else if ( toDo.equals("insert") && winnerLogin.equals(login)){
		    (SharedObjects.qe).insertSellerRatedAs(productId, winnerRatedSellerAs, winnerLogin, sellerLogin);
		}	
		else {
		    System.out.println("error UserFeedBackFormServlet line 77");
		}
	    }
	}catch(IllegalStateException e){System.out.println("not a valid session");}
    }
}


