/**
 * Project    : KANT (LMT)
 * Source Name: Example.java
 * Copyright  : Copyright (c) LTI, Carnegie Mellon University
 * Description: Class for usage Examples
 * @version     1.0 (created by jko - 10/01/01)
 **/

package lmt;

import java.sql.*;
import java.util.*;

public class Example {
  String example_id;
  String definition_id;
  String usage_example;
  String active;
  SQLConnection con;
	
  /** constructor 
   **/
  public Example(SQLConnection con, String def_id) {
    this.con = con;
    example_id = "";
    definition_id = def_id; 
    usage_example = "";
    active = "";
  }

  // setters & getters
  public String getExampleID() {
    return example_id;
  }	
  public void setExampleID(String example_id) {
    if ( example_id == null )
      example_id = "";
    this.example_id = example_id;
  }	
  public String getDefinitionID() {
    return definition_id;
  }	
  public void setDefinitionID(String definition_id) {
    if ( definition_id == null )
      definition_id = "";
    this.definition_id = definition_id;
  }	 
  public String getUsageExample() {
    return usage_example;
  }	
  public void setUsageExample(String usage_example) {
    if ( usage_example == null )
      usage_example = "";
    this.usage_example = usage_example;
  }	
  public void setActive (String active) {
    if ( active == null )
      active = "";
    this.active = active;
  }	

  /** Get all examples which are related with the definition
   * @param definition_id a string representing definition_id
   * @return Vector all examples fetched from the database.
   **/		
  public static Vector fetch (SQLConnection con, String definition_id) {

    Vector exampleVector = new Vector();
	
    String sql = "SELECT * FROM LMT_EXAMPLES WHERE";
    sql += " DEFINITION_ID = '" + definition_id + "'";
    sql += " ORDER BY EXAMPLE_ID";

    Debug.debug("-- fetchExamples: " + sql);		

    try {
      SQLConnection.Result r = con.query(sql);
      while (r!=null &&r.hasData()){
	Example example = new Example(con, definition_id);
	example.setExampleID(r.rs.getString(1));
	//example.setDefinitionID(r.rs.getString(2));
	example.setUsageExample(r.rs.getString(3));	
	example.setActive(r.rs.getString(4));
	exampleVector.addElement(example);
      }
      if (r!=null) r.close();
    } catch (SQLException E) {
      System.out.println("SQLException: " + E.getMessage());
    }		

    return exampleVector;
  }	

  /** insert the new data into the table.
   * @return integer representing the result of operation
   **/	
  public int insert() {

    // get New Term ID				
    example_id = con.getNewID("EXAMPLE_ID");				
		
    String sql = "INSERT INTO LMT_EXAMPLES VALUES (";
    sql += "'" + example_id + "',";
    sql += "'" + definition_id + "',";
    sql += "'" + Util.replaceSingleQuote(usage_example) + "',";
    sql += "'T')";

    Debug.debug("-- Examples insertion : " + sql);		
    return con.insert(sql);
  }
	
  /** delete the data from the table.
   * @return integer representing the result of operation
   **/	
  public int delete() {
    // check where this exists
    if (!isExistent())
      return Constants.DONOTEXIST;
		
    String sql = "DELETE FROM LMT_EXAMPLES ";
    sql += " WHERE definition_id = '" + definition_id + "'";
    sql += " AND example_id = '" + example_id + "'";
		
    Debug.debug("-- Examples delete : " + sql);		
		
    return con.delete(sql);
  }
	
  /** update the data in the table.
   * @return integer representing the result of operation
   **/	
  public static int update(SQLConnection con, Vector exVector) {
    if ( exVector == null || exVector.size() == 0)
      return Constants.SUCCESS;
				
    // make query
    Vector queryVector = new Vector();
    Example.makeQuery(queryVector, exVector);
		
    return con.update(queryVector);
  }
	
  /** check whether the concept exists or not
   * @return boolean representing the result of operation
   **/	
  public boolean isExistent() {
		
    String sql = "SELECT * FROM LMT_EXAMPLES WHERE ";
    sql += " definition_id = '" + definition_id + "'";
    sql += " AND example_id = '" + example_id + "'";
		
    return con.isExistent(sql);
  }

  /** make the query using input vector
   * @param comVector	vector which has objects of this class
   * @return String[] array including SQL
   **/	
  public static void makeQuery(Vector queryVector, Vector exVector) {
    if (exVector == null)
      return;
		
    String query;
    for ( int i=0; i<exVector.size(); i++) {
      Example e = (Example)exVector.elementAt(i);
      query  = "UPDATE LMT_EXAMPLES SET";
      query += " usage_example = '" + Util.replaceSingleQuote(e.getUsageExample()) + "'";
      query += " WHERE example_id = '" + e.getExampleID() + "'";
      query += " AND definition_id = '" + e.getDefinitionID() + "'";			
      //Debug.debug("-- update Examples: " + query);
      queryVector.addElement(query);
    }
  }		
	
  public String makeInsertQuery() {
    String sql = "INSERT INTO LMT_EXAMPLES VALUES (";
    sql += "'" + example_id + "',";
    sql += "'" + definition_id + "',";
    sql += "'" + usage_example + "',";
    sql += "'" + active + "')";
    return sql;
  }	
	
}	
