package csis626;
import java.rmi.*;
import java.rmi.server.*;
import java.sql.*;

/**
   Remote Class for DB which contains methods to extract data
   regarding a student from the MySQL database GRAD_DEGREE
*/
public class DB extends UnicastRemoteObject implements DBInterface 
{
  private String stud_id;
  private String password;
  private String personal_info;
  private Connection conn;
  private Statement stat;
  private ResultSet result;
  private boolean valid_user;

   /**
      Constructs DB using an anonymous port
   */
   public DB() throws RemoteException
   {
      /* Implicit call to superclass constructor */
   }

   /**
      Constructs DB using the supplied port
   */
   public DB(int port) throws RemoteException
   {
      super(port);
   }
   
   /**
      Returns the string represention of StudentData
      @return the string representation of StudentData
   */
   public String getPersonalInfo() throws RemoteException
   {
      return personal_info;
   }

   /**
      Parses through the ResultSet object for Student's
      Personal Info and converts it into a String HTML
      representation
   */
   private void parsePersonalInfo(ResultSet rslt) throws SQLException
   {
      personal_info = "\n<P><B>Name: </B>" + 
                      rslt.getString("first_name") + " " +
                      rslt.getString("middle_name") + " " + 
                      rslt.getString("last_name") +
                      "\n<P><B>Student ID: </B>" + stud_id +
                      "\n<P><B>Password: </B>" + password + 
                      "\n<P><B>Address: </B>" + 
                      rslt.getString("address") + ", " + 
                      rslt.getString("city") + " " + 
                      rslt.getString("zip") +
                      "\n<P><B>Phone: </B>" + 
                      rslt.getString("phone") +
                      "\n<P><B>E-mail: </B>" + 
                      rslt.getString("email") +
                      "\n<P><B>Degree: </B>" +
                      rslt.getString("degree");

   } /* End method parsePersonalInfo */


   /**
      Authenticate the user based on the supplied id and password
      @return the boolean indicating if user is valid
   */
   public boolean isValidUser(String id, String pw) throws RemoteException
   {
      try
      {
         /* Initialize database driver, url, username, and password */
         SimpleDataSource.init("database.properties");

         /* Connect to search_db database */
         conn = SimpleDataSource.getConnection();
	 
         /* Create SQL statement object */
         stat = conn.createStatement();

         /* Retrieve password from database */
         result = stat.executeQuery("SELECT * FROM Students WHERE stud_id='" +
                                    id + "';");

         /* Student ID exists in database */
         if (result.next())
         {
            /* Authenticate user */
            if (pw.equals(result.getString("password")))
            {
               stud_id = id;
               password = pw;
               valid_user = true;

               /* Pull out Student's Personal Info */
               parsePersonalInfo(result);
            } 
            else
            {
               valid_user = false;
            }
         } /* End result.next() */

         stat.close();
         result.close();

	      if (conn != null) 
	         conn.close();
      }
      catch (Exception e)
      {
        System.out.println("Exception occurred:" + e);
      }

      return valid_user;

   } /* End method isValidUser */

} /* End class DB */

