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

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

public class SyntaxPronoun extends Syntax{
  String genitive;
  String num_singular;
  String num_plural;
  String referential;
  String gender;
  String person;
  String anaphor;
	
  /** constructor 
   **/	
  SyntaxPronoun(SQLConnection con, String concept_id) {
    super(con, concept_id);
    genitive = "";
    num_singular = "";
    num_plural = "";
    referential = "";
    gender = "";
    person = "";
    anaphor = "";
  }
	
  // setters and getters
  public String getGenitive() {
    return genitive;
  }	
  public void setGenitive(String genitive) {
    if ( genitive == null)
      genitive = "";
    this.genitive = genitive;
  }	
  public String getNumSingular() {
    return num_singular;
  }	
  public void setNumSingular(String num_singular) {
    if ( num_singular == null)
      num_singular = "";
    this.num_singular = num_singular;
  }	
  public String getNumPlural() {
    return num_plural;
  }	
  public void setNumPlural(String num_plural) {
    if ( num_plural == null)
      num_plural = "";
    this.num_plural = num_plural;
  }	
  public String getReferential() {
    return referential;
  }	
  public void setReferential(String referential) {
    if ( referential == null)
      referential = "";
    this.referential = referential;
  }	
  public String getGender() {
    return gender;
  }	
  public void setGender(String gender) {
    if ( gender == null)
      gender = "";
    this.gender = gender;
  }	
  public String getPerson() {
    return person;
  }	
  public void setPerson(String person) {
    if ( person == null)
      person = "";
    this.person = person;
  }	
  public String getAnaphor() {
    return anaphor;
  }	
  public void setAnaphor(String anaphor) {
    if ( anaphor == null)
      anaphor = "";
    this.anaphor = anaphor;
  }	
	
  /** Get all syntax pronouns which have the concept_id
   * @param concept_id a string representing concept_id
   * @return Vector all syntax pronouns fetched from the database.
   **/			
  public static SyntaxPronoun fetchSyntax(SQLConnection con, String concept_id) {
    SyntaxPronoun syntax = new SyntaxPronoun(con, concept_id);
		
    String sql = "SELECT * FROM LMT_PRONOUN WHERE";
    sql += " CONCEPT_ID = '" + concept_id + "'";
    Debug.debug("-- fetchSyntaxPronou: " + sql);		
		
    try {
      SQLConnection.Result r = con.query(sql);
      if (r!=null &&r.hasData()){
	syntax.setGenitive(r.rs.getString(2));
	syntax.setNumSingular(r.rs.getString(3));
	syntax.setNumPlural(r.rs.getString(4));
	syntax.setReferential(r.rs.getString(5));
	syntax.setGender(r.rs.getString(6));
	syntax.setPerson(r.rs.getString(7));
	syntax.setAnaphor(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("-- SyntaxPronoun insertion : " + sql);		
    return con.insert(sql);
  }
	
  public int update() {
    // check where this exists. If not, insert this 
    if (!isExistent()) {
      return insert();
    }					
    String sql = makeUpdateQuery();
    Debug.debug("-- SyntaxPronoun update : " + sql);		
    return con.update(sql);
  }	
	
  public boolean isExistent() {
		
    String sql = "SELECT * FROM LMT_PRONOUN WHERE ";
    sql += " concept_id = '" + concept_id + "'";
    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_PRONOUN VALUES (";
    sql += "'" + concept_id + "',";
    sql += "'" + genitive + "',";
    sql += "'" + num_singular + "',";
    sql += "'" + num_plural + "',";			
    sql += "'" + referential + "',";
    sql += "'" + gender + "',";
    sql += "'" + person + "',";
    sql += "'" + anaphor + "',";			
    sql += "'T')";
    return sql;
  }
	
  String makeUpdateQuery(){
    String sql = "UPDATE LMT_PRONOUN SET";
    sql += " genitive = '" + genitive + "',";
    sql += " num_singular = '" + num_singular + "',";
    sql += " num_plural = '" + num_plural + "',";
    sql += " referential = '" + referential + "',";
    sql += " gender = '" + gender + "',";
    sql += " person = '" + person + "',";
    sql += " anaphor = '" + anaphor + "'";
    sql += " WHERE concept_id = '" + concept_id + "'";
    return sql;		
  }
}
