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

package lmt;

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

public class Comment {
  String comment_id;
  String comments;
  String datetime_stamp;
  String concept_id;
  String active;
  SQLConnection con;
	
  /** constructor 
   **/	
  public Comment(SQLConnection con) {
    this.con = con;
    comment_id = "";
    comments = "";
    datetime_stamp = "";
    concept_id = "";
    active = "T";
  }
	
  // setters and getters
  public String getCommentID() {
    return comment_id;
  }	
  public void setCommentID(String comment_id) {
    if ( comment_id == null )
      comment_id = "";
    this.comment_id = comment_id;
  }	
  public String getConceptID() {
    return concept_id;
  }	
  public void setConceptID(String concept_id) {
    if ( concept_id == null )
      concept_id = "";
    this.concept_id = concept_id;
  }	
  public String getComments() {
    return comments;
  }	
  public void setComments(String comments) {
    if ( comments == null )
      comments = "";
    this.comments = comments;
  }	
  public String getDatetimeStamp() {
    return datetime_stamp;
  }	
  public void setDatetimeStamp(String datetime_stamp) {
    if ( datetime_stamp == null )
      datetime_stamp = "";
    this.datetime_stamp = datetime_stamp;
  }	
  public void setActive (String active) {
    if ( active == null )
      active = "";
    this.active = active;
  }	
	
  /** Get all comments which are related with the concept_id
   * @param concept_id a string representing concept_id
   * @return Vector all comments fetched from the database.
   **/	
  public static Vector fetchComments(SQLConnection con, String concept_id) {
    Vector commentVector = new Vector();
		
    String sql = "SELECT * FROM LMT_COMMENTS WHERE ";
    sql += " CONCEPT_ID = '" + concept_id + "'";
    sql += " ORDER BY comment_id";

    try {
      SQLConnection.Result r = con.query(sql);
      while (r!=null && r.hasData()){
	Comment comment = new Comment(con);
	comment.setCommentID(r.rs.getString(1));				 
	comment.setConceptID(r.rs.getString(2));
	comment.setComments(r.rs.getString(3));
	comment.setDatetimeStamp(r.rs.getString(4));
	comment.setActive(r.rs.getString(5));
	commentVector.addElement(comment);	
      }
      if (r!=null) r.close();
    } catch (SQLException E) {
      Debug.debug("SQLException: " + E.getMessage());
    }	
    Debug.debug("-- fetchComments: " + sql);		
    //Debug.debug("   size of result: " + commentVector.size());
		
    return commentVector;
  }
	
  /** Get all comments which match with the keyword
   * @param keyword a string representing concept symbol. It can have wildcards.
   * @return Vector all comments fetched from the database.
   **/		
  public static Vector searchComments(SQLConnection con, String keyword) {
    Vector commentVector = new Vector();
		
    String sql = "SELECT CONCEPT_SYMBOL, COMMENTS FROM LMT_COMMENTS C1, LMT_CONCEPTS C2";
    sql += " WHERE C1.CONCEPT_ID=C2.CONCEPT_ID AND ";
	
    // replace single quote for Oracle representation
    keyword = Util.replaceSingleQuote(keyword);
		
    if (keyword.indexOf("%") >= 0 || keyword.indexOf("_") >= 0 ) {
      sql += "COMMENTS LIKE '" + keyword + "'";
    } else {
      sql += "COMMENTS = '" + keyword + "'";
    }
		
    sql += " ORDER BY COMMENTS";

    Debug.debug("-- searchComments: " + sql);		
    try {
      SQLConnection.Result r = con.query(sql);
      while (r!=null &&r.hasData()){
	Vector row = new Vector();
	row.addElement(r.rs.getString(1));
	row.addElement(r.rs.getString(2));				 
	commentVector.addElement(row);
      }
      if (r!=null) r.close();
    } catch (SQLException E) {
      Debug.debug("SQLException: " + E.getMessage());
    }		
    return commentVector;
  }	
	
  /** insert the data into the table.
   * @return integer representing the result of operation
   **/		
  public int insert() {

    // get New Term ID				
    comment_id = con.getNewID("COMMENT_ID");				
		
    String sql = "INSERT INTO LMT_COMMENTS VALUES (";
    sql += "'" + comment_id + "',";
    sql += "'" + concept_id + "',";
    sql += "'" + Util.replaceSingleQuote(comments) + "',";
    sql += "'" + datetime_stamp + "',";
    sql += "'" + active + "')";

    Debug.debug("-- Comment 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_COMMENTS ";
    sql += " WHERE concept_id = '" + concept_id + "'";
    sql += " AND comment_id = '" + comment_id + "'";
		
    Debug.debug("-- Comment 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 comVector) {
    if ( comVector == null || comVector.size() == 0)
      return Constants.SUCCESS;
		
    Vector queryVector = new Vector();		
    Comment.makeQuery(queryVector, comVector);
		
    for ( int i=0; i<queryVector.size(); i++)
      Debug.debug("-- Comment update : " + queryVector.elementAt(i));	
		
    // run the update transaction
    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_COMMENTS WHERE ";
    sql += " concept_id = '" + concept_id + "'";
    sql += " AND comment_id = '" + comment_id + "'";
		
    //Debug.debug("-- isExistent : " + sql);	
    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 comVector) {
    if (comVector == null)
      return;
		
    String query;
    for ( int i=0; i<comVector.size(); i++) {
      Comment c = (Comment)comVector.elementAt(i);
      query = "UPDATE LMT_COMMENTS SET";
      query += " comments = '" + Util.replaceSingleQuote(c.getComments()) + "',";
      query += " datetime_stamp = '" + c.getDatetimeStamp() + "'";
      query += " WHERE concept_id = '" + c.getConceptID() + "'";
      query += " AND comment_id = '" + c.getCommentID() + "'";
      queryVector.addElement(query);
    }
  }
}
