/*
 * Attribute.java
 * Attribute class defining methods for creating
 * and editing Attribute types, as well as 
 * retrieving them from the database. This
 * class extends the DBObject class
 */

package base;


import java.sql.ResultSet;


public class Attribute extends DBObject{
   private String Name;
   private String Type;
   
   //Creates an attribute instance for 

   //an attribute already in the Database

   public Attribute(int i){
      //Set ID from User Input

      id = i;
      getConn();
      ResultSet rs = query("SELECT * FROM attribute WHERE AttributeID = " + id + ";");
      try {
         rs.next();
         Name = rs.getString("AttributeName");
         Type = rs.getString("Attribute Type");
      } catch (Exception e){e.printStackTrace();}
   }
   
   //Creates a new attribute type from User Input

   public Attribute(String n, String t){
      if (checkType(t)){
         Type = t;
         Name = n;
         String query = "INSERT INTO attribute VALUES (NULL, '" +
                     Name +
                     "', '" +
                     Type +
                     "');"
                     ;

         //Connect to the Database

         getConn();
         
         //Execute the Query and set NodeID

         id = create(query);
      }
      else {
         System.out.println("Invalid Attribute Type, Attribute not Created");
      }
   }

   /* checkType(Attribute Type)
    * function to allow for easy checking
    * of an attribute type to ensure it's
    * validity.
    */
   public boolean checkType(String t){
      //Basic Data Types

      if (t.equals("Text")){return true;}
      if (t.equals("Int")){return true;}
      if (t.equals("Long")){return true;}
      if (t.equals("Date")){return true;}
      if (t.equals("URL")){return true; }
      // Blob Types

      if (t.equals("Audio")){return true;}
      if (t.equals("Video")){return true;}
      if (t.equals("Image")){return true;}
      if (t.equals("File")){return true;}
      
      return false;
      /*
       * This is pretty clumsy currently, at a
       * future point i'll sit down and hammer
       * out a more thought out list for data
       * types, but these seem to cover most
       * everything for now. the File type is
       * in there to catch any other blob objects
       * which aren't covered in those other types
       */
   }

   public String getAttributeName(){
      
      if (DBWrite){
      getConn();
      ResultSet rs = query("SELECT AttributeName from attribute WHERE AttributeID = " + id + ";");
      try {
         rs.next();
         Name = rs.getString("AttributeName");
      } catch (Exception e){
         e.printStackTrace();
         Name = "Unable to Retrieve Attribute Name";
      }
      return Name;
      }
      else { 
         return Name; 
         }
   }

   public String getAttributeType(){

      if (DBWrite){
      getConn();
      ResultSet rs = query("SELECT AttributeType FROM attribute WHERE AttributeID = " + id + ";");
      try{
         rs.next();
         Type = rs.getString("AttributeType");
      }catch(Exception e){
         e.printStackTrace();
         Type = "Unable to Retrieve Attribute Type";
      }
      return Type;
      }
      else { return Type; }
   }

   public void setAttributeName(String n){
      Name = n;
      if (DBWrite){
         String q = "UPDATE attribute SET AttributeName = '" + 
                  Name + 
                  "' WHERE AttributeID = " + 
                  id + 
                  ";";
         query(q);
      }
   }
   
   public void setAttributeType(String t){
      if (checkType(t)){
         Type = t;
         if (DBWrite){
            String q = "UPDATE attribute set AttributeType = '" + 
                     Type + 
                     "' WHERE AttributeID = " + 
                     id +
                     ";";
            query(q);
         }
      }
      else {System.out.println("Invalid Attribute Type Submitted");}
   }


}

syntax highlighted by Code2HTML, v. 0.93pm6