package csis626;
//import java.io.*;
//import java.net.*;
import java.rmi.*;

/**
   This bean authenticates the user
*/
public class LoginBean
{
   private boolean isLoginSuccessful;
   private String stud_id;
   private String password;
   private String personalInfo;
   private String output_message;
   //private DBInterface database;

   /**
      Constructor
   */
   public LoginBean()
   {
      isLoginSuccessful = false;
      output_message = "Didn't do anything";
   }

   /**
      Read-only stud_id property
      @return the Student ID
   */
   public String getStud_id()
   {
      return stud_id;
   }

   /**
      Write-only stud_id property
      @param input_id the Student ID
   */
   public void setStud_id(String input_id)
   {
      stud_id = input_id;
   }

   /**
      Read-only password property
      @return the Passwprd
   */
   public String getPassword()
   {
      return password;
   }

   /**
      Write-only password property
      @param input_id the Student ID
   */
   public void setPassword(String input_password) throws Exception
   {
      password = input_password;

      /* Authenticate user */
      authenticate();

   } /* End method setPassword */

   /**
      Read-only output_message property
      @return output_message with regards to whether url link is valid
   */
   public String getOutput_message()
   {
      return output_message;
   }
   
   /**
      Write-only output_message property
      @param input_msg the output message to report
   */
   public void setOutput_message(String input_msg)
   {
      output_message = input_msg;
   }

   /**
      Read-only isLoginSuccessful property
      @return output_message with regards to whether url link is valid
   */
   public boolean getIsLoginSuccessful()
   {
      return isLoginSuccessful;
   }

   /**
      Read-only PersonalInfo property
      @return Student Personal Info in HTML string
   */
   public String getPersonalInfo()
   {
      return personalInfo;
   }


   /**
      Authenticates user
   */
   public void authenticate() throws Exception
   {
      try
      {
         /* Connect to Remote object */
         DBInterface database = (DBInterface) Naming.lookup("//www.pelennor.net/MyDatabase");
         
         /* Authenticate user */
         if ( database.isValidUser(stud_id, password) )
         {
            isLoginSuccessful = true;
            personalInfo = database.getPersonalInfo();
         }
         else
         {
            isLoginSuccessful = false;
         }
      }
      catch (Exception e)
      {
         output_message = "Error occurred: " + e;
      }

   } /* End method authenticate */


} /* End class LoginBean */

