//SQuirreL.java
// Thanks to my homie C-Dawg Charlie. Visit his kickin' home page:
//   http://cse.unl.edu/~cusack/
// Modified heavily by Brian Kell. http://upsilon.unl.edu/
//
// Following Big Chuck's example, this code is self-documenting.

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.*;
import javax.swing.*;

public class SQuirreL extends JApplet implements ActionListener {
  
  // Strings and crap
  String driver = "org.gjt.mm.mysql.Driver";
  String url = "jdbc:mysql://tacse/RecipesDB";
  String userName="jde156";
  String password="student";
  String query="select  EmpFirstName, EmpLastName," +
      "EmpStreetAddress, EmpCity, EmpState,"+
      "EmpZipCode from Employees";
  String text="";
  boolean ok = true;  // true as long as no errors have occurred
  
  // Graphical objects
  private JComboBox combo;
  private JLabel recipeLabel;

  public void init() {

    // String array for the combo box
    String[] recipeTitleArray;
    
    // Database objects
    Connection connect;
    Statement statement;
  
    ArrayList recipeTitleList = new ArrayList(50);

    try {
      Class.forName(driver);
    } catch(java.lang.ClassNotFoundException e) {
      text = "SQL driver not found.";
      ok = false;
    }

    if (ok)
      try {
        connect = DriverManager.getConnection(url, userName, password);
        statement = connect.createStatement();
        ResultSet rs = statement.executeQuery(
            "SELECT RecipeTitle FROM Recipes");
        while (rs.next()) // loop through the result set
          recipeTitleList.add(rs.getString("RecipeTitle"));
        statement.close();
        connect.close();
      } catch(SQLException e) {
        text = "An SQL exception was thrown. Get your baseball glove.\n" +
            e.getMessage();
        ok = false;
      }
    
    // Construct array for combo box
    recipeTitleArray = new String[recipeTitleList.size()];
    for (int i = 0; i < recipeTitleList.size(); i++)
      recipeTitleArray[i] = (String)recipeTitleList.get(i);

    // Do graphical hoo-hah
    Container cp = this.getContentPane();
    cp.setBackground(Color.white);
    cp.setLayout(new BorderLayout());
    JPanel topPanel = new JPanel(new BorderLayout());
    topPanel.setBackground(Color.white);
    cp.add(topPanel, BorderLayout.NORTH);
    topPanel.add(new JLabel("<html><h1>SQuirreL</h1></html>", new ImageIcon(
        this.getClass().getResource("squirrel.gif")), SwingConstants.CENTER),
        BorderLayout.CENTER);
    topPanel.add(combo = new JComboBox(recipeTitleArray), BorderLayout.SOUTH);
    if (!ok)
      combo.setEnabled(false);
    combo.addActionListener(this);
    cp.add(recipeLabel = new JLabel(text), BorderLayout.CENTER);
    recipeLabel.setVerticalAlignment(SwingConstants.TOP);
    this.validate();
    actionPerformed(new ActionEvent(combo, 0, "Initializing"));
  }
  
  public void actionPerformed(ActionEvent e) {
    
    if (e.getSource() == combo) { // combo box clicked
      try {
        
        Connection connect = DriverManager.getConnection(url, userName,
            password);
        Statement statement = connect.createStatement();
        
        // Get recipe info
        ResultSet rs = statement.executeQuery(
            "SELECT RecipeTitle, RecipeClassID, Preparation, Notes FROM " +
            "Recipes WHERE RecipeID = " + (combo.getSelectedIndex() + 1));
            
        rs.beforeFirst();
        rs.next(); // move to first result; there should only be one
        
        String recipeTitle = rs.getString("RecipeTitle"), recipeClassID = rs.
            getString("RecipeClassID"), preparation = rs.getString(
            "Preparation"), notes = rs.getString("Notes");
            
        // Get recipe class
        rs = statement.executeQuery(
            "SELECT RecipeClassDescription FROM Recipe_Classes WHERE " +
            "RecipeClassID = " + recipeClassID);
            
        rs.beforeFirst();
        rs.next(); // go to first result
        
        text = "<html><table width=100%><tr><td align=left><h1>" + recipeTitle +
            "</h1></td><td align=right><h2>" + rs.getString(
            "RecipeClassDescription") + "</h2></td></tr></table><ul>";
            
        // Get ingredients
        rs = statement.executeQuery(
            "SELECT r.Amount, i.IngredientName, m.MeasurementDescription " +
            "FROM (Recipe_Ingredients r INNER JOIN Ingredients AS i ON " +
            "r.IngredientID = i.IngredientID) INNER JOIN Measurements AS m " +
            "ON r.MeasureAmountID = m.MeasureAmountID WHERE r.RecipeID = " +
            (combo.getSelectedIndex() + 1));
        
        while (rs.next()) // loop through results
          text += "<li>" + rs.getString("r.Amount") + " " +
              rs.getString("m.MeasurementDescription") + " " +
              rs.getString("i.IngredientName") + "</li>";
        
        text += "</ul><h3>Preparation</h3><p>" + preparation;
        if (notes != null)
          text += "</p><h3>Notes</h3><p>" + notes;
        text += "</p></html>";
        
        statement.close();
        connect.close();
        
      } catch(SQLException exc) {
        text = "<html><p>An SQL exception was thrown. Get your baseball " +
            "glove.</p><p>" + exc.getMessage() + "</p></html>";
        ok = false;
        combo.setEnabled(false);
      }
      
      recipeLabel.setText(text);
      
    } // <end> if (e.getSource() == combo)

  }
  
}


