/*This servlet is called when a user wants to change his profile. when the user will click on change my profile link, he will be directed to this servlet. first the changeUserProfile functin wil be called in the doGet method of this servlet. and then the UpdateUserProfile function willl be called in doPost method. Session would be continuing here.Also you need to check if the oldPassword matched with the NewPassword. In UserQueryEngine, a function has to be written which returens password checks the old password of the user. We can as well bind the password of the user with the session. just m,ake sure if this is a good practice(for the time being assume that the password has been bind to this session */


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

/**
 * This servlet is called when a user wants to change his profile. He will be directed to this servlet on clicking on the chnge profile link. 
 * The methods of this servlet generated form for changing various fields as well as password. It varifies all entries and changes the profile.
 * @author      Nilesh Agarwal
 * @version     1.00, 09/10/2001
 */ 

public class UserProfileChangeServlet extends HttpServlet
{ 


    /**
     * This method gets all user infomation using methods of QueryEngine class and generates a form for the user to change his profile.
     * On submitting the form the control passes on to doPost method. 
     * @param HttpServletRequest request
     * @param HttpServletResponse response
     */      

    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");
		// a function of UserQueryEngine CLASS which will return a user's data given his login
		(SharedObjects.qe).getUserInfo(out, login);
	    }
	    else{
		System.out.println("null session");
	    }
	}catch(IllegalStateException e){System.out.println("not a valid session");}
    }
    
    /**
     * This will varify all entries made by user in change profile form and would change his profile on all correct entries.
     * @param HttpServletRequest request
     * @param HttpServletResponse response
     */      

    public void doPost (HttpServletRequest request, 
			HttpServletResponse response) 
	throws ServletException, IOException {
	PrintWriter out;
	response.setContentType("text/html");
	session=request.getSession(false);
	String firstName= request.getParameter("firstName");//first name of user
	String lastName = request.getParameter("lastName");//first name of user
	String email = request.getParameter("email");//first name of user
	String gender = request.getParameter("gender");//first name of user
	String address = request.getParameter("address");//first name of user
	String city = request.getParameter("city");//first name of user
	String oldPassword =  request.getParameter("oldPassword");//old password of user
	String newPassword =  request.getParameter("newPassword1");//new password of user
	String actualOldPassword = (String)session.getValue("password");
	String userLogin = (String)session.getValue("userLogin");
	System.out.println("userLogin:"+userLogin ); // just printing to test
	System.out.println("actualOldPassword:"+actualOldPassword ); // ----do------
	System.out.println("oldPassword"+oldPassword ); // ----do------
	System.out.println("newPassword:"+newPassword ); // ----do------
	
	
	out=response.getWriter();
	if (!(newPassword.equals(""))){
	    System.out.println("newPassword not equal to empty string");
	    if (actualOldPassword.equals(oldPassword)){
		System.out.println("actualOldPassword=oldPassword");
		// NOTE UserQueryEngine CLASS FUNCTION
		(SharedObjects.qe).updateUserInfo(userLogin, oldPassword, newPassword, firstName, lastName, email, gender, address, city);
		session.removeValue("password");
		session.putValue("password", newPassword);
		UserPageGenerator.profileSuccessfullyChanged(out);
	    }
	    else{
		// NOTE UserPageGenerator CLASS FUNCTION ...to be done
		System.out.println("actualOldPassword!=oldPassword");
		UserPageGenerator.passwordDoNotMatch(out, userLogin);
	    }
	}
	else{
	    System.out.println("new password = empty string");
	    // NOTE UserQueryEngine CLASS FUNCTION
	    (SharedObjects.qe).updateUserInfo(userLogin, actualOldPassword, actualOldPassword, firstName, lastName, email, gender, address, city);
	    UserPageGenerator.profileSuccessfullyChanged(out);
	}
	
    }
}

