// Source Code for RestoreDBDialog.class //
// Client Code for Desktop Calendar    //

// Code based on using Server #18     //
// Written by Khalid Mufti - Group 31 //

import java.io.*;
import java.awt.*;
import java.awt.event.*;

// This class handles the dialog which enables a user to restore a      //
// database from file into server memory.                               //
// Inherits from the Dialog class (modal) and requires the parent frame //
// and a socket connected to the server, as parameters                  //  
public class RestoreDBDialog extends Dialog implements ActionListener
{
   private GridBagLayout gbLayout;
   private GridBagConstraints gbConstraints; 
   private Button cancel, ok, help;
   private Label msg, msg2, msg3;
   private Label dum1;
   private ClientSocket clientsocket;
   private Frame frame;
   private MsgBox msgbox;
   private AdminHelpBox helpbox;
   private StringBuffer str;
 
   // The constructor for the class.  Requires a parent frame and socket connected //
   // to the server as parameters.                                                 //
   public RestoreDBDialog(Frame parent, ClientSocket cs)
   {
     // Call Dialog class constructor //
     super (parent, "  Warning! Possible Data Loss!", true);
    
     // Set window size and type and copy parameters to local variables //
     setSize(300,300);
     setResizable(false);  
     frame = parent;
     clientsocket = cs; 

     // Instantiate grid for layout purposes //
     gbLayout = new GridBagLayout();
     gbConstraints = new GridBagConstraints();
     setLayout (gbLayout); 
     
     // Allocate memory for all new items in Dialog, and add action listeners //
     dum1 = new Label (""); //used to provide spacing//
     cancel = new Button ("Cancel");
     cancel.addActionListener (this);
     ok = new Button ("OK");
     ok.addActionListener (this);
     help = new Button ("Help");
     help.addActionListener (this);
     msg = new Label("Restoring to an original database");
     msg2 = new Label("will lose any new data after a previous");
     msg3 = new Label("database save! Are you sure?");
 
     // Now place the components in the Dialog, based on the grid bag layout //
     gbConstraints.fill = GridBagConstraints.HORIZONTAL;
     addObj (msg,         0, 1, 1, 1);
     addObj (msg2,        1, 1, 1, 1);
     addObj (msg3,        2, 1, 1, 1);
     addObj (dum1,        3, 0, 1, 1);        
     gbConstraints.fill = GridBagConstraints.BOTH;
     addObj (help,        4, 0, 1, 2);
     addObj (ok,          4, 1, 1, 1);
     addObj (cancel,      5, 1, 1, 1);        
 
     // Add a listener for closing the window prematurely //
                                  		   
     addWindowListener (new CloseWindow());
   } 

   // This function handles any action events from the user //
   public void actionPerformed (ActionEvent e)
   {   
      // If user pressed the cancel button //
      if ((e.getSource())==cancel)
      {
         dispose();
      }
      
      // If user pressed the ok button //
      else if ((e.getSource())==ok)
      {
        try 
        {
           // Send request to server //
           clientsocket.Write("restoredb");
           String buf = new String (clientsocket.Read()); 

           // Parse numerical response from server // 
           if (buf.regionMatches(false, 0 , "226", 0,  3))
           {
             msgbox = new MsgBox (frame, "  SUCCESS", "Database Restored Successfully!", 250, 150);
             msgbox.setVisible(true);
             dispose();
           }     

           else if (buf.regionMatches(false, 0 , "813", 0,  3))
           {
             msgbox = new MsgBox (frame, "  DATABASE ERROR", "Database Not Found!", 250, 150);
             msgbox.setVisible(true);
           }     

           else 
           {
             msgbox = new MsgBox (frame, "  DATABASE ERROR", "Error Loading Database!", 250, 150);
             msgbox.setVisible(true);
           }     
         }
         
         catch (IOException io)  // Connection must have been lost //
         {
           msgbox = new MsgBox (frame, "  NETWORK ERROR", "Network Error Detected!", 250, 150);
           msgbox.setVisible(true);
           dispose();
         }  
      }      

      // If user pressed the help button //
      else if ((e.getSource())==help)
      {
         helper();
      }    
   }

   // This function simply adds an object (registers it) to the Dialog //
   // Further, it sets various parts of the grid bag layout            // 
   private void addObj (Component c, int row, int column, int width, int height)
   {
      gbConstraints.gridx=column;
      gbConstraints.gridy=row; 
      gbConstraints.gridwidth=width;
      gbConstraints.gridheight=height;
      gbLayout.setConstraints (c, gbConstraints);
      add (c);
   }
 
   // This function simply pops up a help dialog box that displays help on using //
   // this dialog screen.                                                        //
   private void helper()
   {
      // Set what the help box should say //
      str = new StringBuffer(); 
      str.append("This screen is used to Restore a database for\n");
      str.append("the desktop server - from a file to memory.\n");
      str.append("NOTE: all the new data since the last save\n");
      str.append("will be lost!\n");
      str.append("\n");
      str.append("Restore Database should be used to load an\n");
      str.append("old database when the server first loads up,\n");
      str.append("or if the current database becomes corrupt.\n");
      str.append("\n");
      str.append("To restore to an old database, cick on the\n");
      str.append("OK button; otherwise, click Cancel to\n");
      str.append("return to the Admin menu.\n");
     
      // Create and show modal help box //
      helpbox = new AdminHelpBox (frame, "  Help Screen", str.toString(), true, 300, 300);
      helpbox.setVisible(true); 
   }
}