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

package lmt;

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

public class SyntaxAdv extends Syntax{
  String form;
  String direction;
  String degree;
  String frequency;
	
  /** constructor 
   * @param concept_id string representing concept_id
   **/		
  SyntaxAdv(SQLConnection con, String concept_id) {
    super(con, concept_id);
    form = "";
    direction = "";
    degree = "";
    frequency = "";
  }
	
  // setters and getters
  public String getForm() {
    return form;
  }	
  public void setForm(String form) {
    if (form == null)
      form = "";
    this.form = form;
  }	
  public String getDirection() {
    return direction;
  }	
  public void setDirection(String direction) {
    if (direction == null)
      direction = "";
    this.direction = direction;
  }	
  public String getDegree() {
    return degree;
  }	
  public void setDegree(String degree) {
    if (degree == null)
      degree = "";
    this.degree = degree;
  }	
  public String getFrequency() {
    return frequency;
  }	
  public void setFrequency(String frequency) {
    if (frequency == null)
      frequency = "";
    this.frequency = frequency;
  }	
	
  /** Get all syntaxs which have the concept_id. 
   * @param concept_id a string representing concept_id
   * @return Vector all syntax adverbs fetched from the database.
   **/	
  public static SyntaxAdv fetchSyntax(SQLConnection con, String concept_id) {
    SyntaxAdv syntax = new SyntaxAdv(con, concept_id);
		
    String sql = "SELECT * FROM LMT_ADVERB WHERE ";
    sql += " CONCEPT_ID = '" + concept_id + "'";
    Debug.debug("-- fetchSyntaxAdv: " + sql);		
		
    try {
      SQLConnection.Result r = con.query(sql);
      if (r!=null &&r.hasData()){
	syntax.setAbbreviation(r.rs.getString(2));
	syntax.setAcronym(r.rs.getString(3));
	syntax.setComplementS(r.rs.getString(4));
	syntax.setComplementX(r.rs.getString(5));
	syntax.setForm(r.rs.getString(6));
	syntax.setDirection(r.rs.getString(7));
	syntax.setDegree(r.rs.getString(8));
	syntax.setFrequency(r.rs.getString(9));
	syntax.setActive(r.rs.getString(10));
      }
      if (r!=null) r.close();
    } catch (SQLException E) {
      System.out.println("SQLException: " + E.getMessage());
    }	
		
    return syntax;
  }
	
  public int insert() {
    String sql = makeInsertQuery();
    Debug.debug("-- SyntaxAdv 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("-- SyntaxAdv update : " + sql);		
    return con.update(sql);
  }	
	
  public boolean isExistent() {
		
    String sql = "SELECT * FROM LMT_ADVERB 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_ADVERB VALUES (";
    sql += "'" + concept_id + "',";
    sql += "'" + abbreviation + "',";
    sql += "'" + acronym + "',";
    sql += "'" + complement_s + "',";
    sql += "'" + complement_x + "',";
    sql += "'" + form + "',";
    sql += "'" + direction + "',";
    sql += "'" + degree + "',";
    sql += "'" + frequency + "',";			
    sql += "'T')";
    return sql;
  }
	
  String makeUpdateQuery() {
    String sql;		
    sql = "UPDATE LMT_ADVERB SET";
    sql += " abbreviation = '" + abbreviation + "',";
    sql += " acronym = '" + acronym + "',";
    sql += " complement_s = '" + complement_s + "',";
    sql += " complement_x = '" + complement_x + "',";
    sql += " form = '" + form + "',";
    sql += " direction = '" + direction + "',";
    sql += " degree = '" + degree + "',";
    sql += " frequency = '" + frequency + "'";
    sql += " WHERE concept_id = '" + concept_id + "'";
    return sql;		
  }
	
}
