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

package lmt;
import java.sql.*;
import java.util.*;

public class SyntaxAdj extends Syntax{
  String full_form;
  String expl_subj;
  String form;
	
  /** constructor 
   * @param concept_id string representing concept_id
   **/	
  public SyntaxAdj(SQLConnection con, String concept_id) {
    super(con, concept_id);
    full_form = "";
    expl_subj = "";
    form = "";
  }
	
  // setters and getters
  public String getFullForm() {
    return full_form;
  }	
  public void setFullForm(String full_form) {
    if (full_form == null)
      full_form = "";
    this.full_form = full_form;
  }		
  public String getForm() {
    return form;
  }	
  public void setForm(String form) {
    if (form == null)
      form = "";
    this.form = form;
  }	
  public String getExplSubj() {
    return expl_subj;
  }	
  public void setExplSubj(String expl_subj) {
    if (expl_subj == null)
      expl_subj = "";		
    this.expl_subj = expl_subj;
  }	
	
  /** Get all syntaxs which have the concept_id. 
   * @param concept_id a string representing concept_id
   * @return Vector all syntax adjectives fetched from the database.
   **/	
  public static SyntaxAdj fetchSyntax(SQLConnection con, String concept_id) {
    SyntaxAdj syntax = new SyntaxAdj(con, concept_id);
		
    String sql = "SELECT * FROM LMT_ADJECTIVE WHERE ";
    sql += " CONCEPT_ID = '" + concept_id + "'";
    Debug.debug("-- fetchSyntaxAdv: " + sql);		
		
    try {
      SQLConnection.Result r = con.query(sql);
      if (r!=null &&r.hasData()){
	syntax.setConceptID(r.rs.getString(1));
	syntax.setAbbreviation(r.rs.getString(2));
	syntax.setAcronym(r.rs.getString(3));
	syntax.setFullForm(r.rs.getString(4));
	syntax.setComplementS(r.rs.getString(5));
	syntax.setComplementX(r.rs.getString(6));
	syntax.setExplSubj(r.rs.getString(7));
	syntax.setForm(r.rs.getString(8));
	syntax.setActive(r.rs.getString(9));
      }
      if (r!=null) r.close();
    } catch (SQLException E) {
      System.out.println("SQLException: " + E.getMessage());
    }	
		
    return syntax;
  }

  /** insert the new data into the table.
   * @return integer representing the result of operation
   **/		
  public int insert() {
    String sql = makeInsertQuery();
    Debug.debug("-- SyntaxAdj insertion : " + sql);		
    return con.insert(sql);
  }
		
  /** update this data in the table.
   * @return integer representing the result of operation
   **/
  public int update() {
    // check where this exists. If not, insert this 
    if (!isExistent()) {
      return insert();
    }
		
    Vector queryVector = new Vector();
    makeQuery(queryVector);
    return con.update(queryVector);
  }	
	
  /** check whether the information exists or not in the database
   * @return boolean representing the result of operation
   **/	
  public boolean isExistent() {
		
    String sql = "SELECT * FROM LMT_ADJECTIVE WHERE ";
    sql += " concept_id = '" + concept_id + "'";
		
    Debug.debug("-- SyntaxAdj isExistent : " + sql);	
    return con.isExistent(sql);
  }	
	
  /** make the query 
   * @return String[] array including SQL 
   **/	
  public void makeQuery(Vector queryVector) {
    String sql = ( isExistent() ? makeUpdateQuery() : makeInsertQuery() );
    queryVector.addElement(sql);
  }			
	
  String makeInsertQuery() {
    String sql = "INSERT INTO LMT_ADJECTIVE VALUES (";
    sql += "'" + concept_id + "',";
    sql += "'" + abbreviation + "',";
    sql += "'" + acronym + "',";
    sql += "'" + full_form + "',";
    sql += "'" + complement_s + "',";
    sql += "'" + complement_x + "',";
    sql += "'" + expl_subj + "',";
    sql += "'" + form + "',";
    sql += "'T')";		
    return sql;
  }
	
  String makeUpdateQuery() {
    String sql;		
    sql = "UPDATE LMT_ADJECTIVE SET";
    sql += " abbreviation = '" + abbreviation + "',";
    sql += " acronym = '" + acronym + "',";
    sql += " full_form = '" + full_form + "',";
    sql += " complement_s = '" + complement_s + "',";
    sql += " complement_x = '" + complement_x + "',";
    sql += " expl_subj = '" + expl_subj + "',";
    sql += " form = '" + form + "'";
    sql += " WHERE concept_id = '" + concept_id + "'";
    return sql;	
  }
}
