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


package lmt;

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

public class MorphAdj extends Morphology{
  String comparative;
  String superlative;
  String irr_comparative;
  String irr_superlative;
  SQLConnection con;
	
  /** constructor 
   **/		
  public MorphAdj(SQLConnection con, String concept_id) {
    super();
    this.con = con;
    this.concept_id = concept_id;
    comparative = "";
    superlative = "";
    irr_comparative = "";
    irr_superlative = "";
  }
	
  // getters and setters
  public String getComparative() {
    return comparative;
  }	
  public void setComparative(String comparative) {
    if ( comparative == null )
      comparative = "";
    this.comparative = comparative;
  }	
  public String getSuperlative() {
    return superlative;
  }	
  public void setSuperlative(String superlative) {
    if ( superlative == null )
      superlative = "";
    this.superlative = superlative;
  }	
  public String getIrrComparative() {
    return irr_comparative;
  }	
  public void setIrrComparative(String irr_comparative) {
    if ( irr_comparative == null )
      irr_comparative = "";
    this.irr_comparative = irr_comparative;
  }	
  public String getIrrSuperlative() {
    return irr_superlative;
  }	
  public void setIrrSuperlative(String irr_superlative) {
    if ( irr_superlative == null )
      irr_superlative = "";
    this.irr_superlative = irr_superlative;
  }	

  /** Get all morphology information which has the concept_id
   * @param concept_id a string representing concept_id
   * @return MorphAdj fetched from the database.
   **/		
  public static MorphAdj fetchMorph(SQLConnection con, String concept_id) {
    MorphAdj morph = new MorphAdj(con, concept_id);
	
    try {
      String sql = "SELECT * FROM LMT_MORPH_ADJADV WHERE ";
      sql += " CONCEPT_ID = '" + concept_id + "'";
      Debug.debug("-- fetchMorphAdj: " + sql);		
      SQLConnection.Result r = con.query(sql);
      if (r!=null &&r.hasData()){
	morph.setConDouble(r.rs.getString(2));
	morph.setComparative(r.rs.getString(3));
	morph.setSuperlative(r.rs.getString(4));
	morph.setActive(r.rs.getString(5));
      }
      if (r!=null) r.close();							
				
      // get irregular expression
      if ( morph.getComparative().equals("I") || morph.getSuperlative().equals("I")) {
	sql = "SELECT * FROM LMT_IRR_MORPH_ADJADV WHERE";
	sql += " CONCEPT_ID = '" + concept_id + "'";
	Debug.debug("-- fetchIrrMorphAdj: " + sql);	
					
	r = con.query(sql);
	if (r!=null &&r.hasData()){
	  morph.setIrrComparative(r.rs.getString(2));
	  morph.setIrrSuperlative(r.rs.getString(3));
	  morph.setIrrActive(r.rs.getString(4));
	}
	if (r!=null) r.close();
      }				
    } catch (SQLException E) {
      Debug.debug("SQLException: " + E.getMessage());
    }	
		
    return morph;
  }

  /** insert the new data into Morphology and Irregular morphology tables.
   * @return integer representing the result of operation
   **/			
  public int insert() {
    String sql[] = makeInsertQuery();
    //Debug.debug("-- MorphIrrAdj insertion : " + sql);					
    return con.insert(sql);
  }
	
  /** update Morphology and Irregular morphology tables with the new values.
   * @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 the Morphology table has the concept_id
   * @return boolean representing the result of operation
   **/		
  public boolean isExistent() {
		
    String sql = "SELECT * FROM LMT_MORPH_ADJADV WHERE ";
    sql += " concept_id = '" + concept_id + "'";
    Debug.debug("-- MorphAdj 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[0]);		// morph
    queryVector.addElement(sql[1]);		// irr morph
  }
	
  String[] makeInsertQuery() {
    String sql[] = new String[2];
    // morph
    sql[0] = "INSERT INTO LMT_MORPH_ADJADV VALUES (";
    sql[0] += "'" + concept_id + "',";
    sql[0] += "'" + con_double + "',";
    sql[0] += "'" + comparative + "',";
    sql[0] += "'" + superlative + "',";
    sql[0] += "'T')";
    // irr morph
    sql[1] = "INSERT INTO LMT_IRR_MORPH_ADJADV VALUES (";
    sql[1] += "'" + concept_id + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_comparative) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_superlative) + "',";
    sql[1] += "'T')";		
    return sql;
  }
	
  String[] makeUpdateQuery() {
    String[] sql = new String[2];
			
    // morph
    sql[0] = "UPDATE LMT_MORPH_ADJADV SET";
    sql[0] += " con_double = '" + con_double + "',";
    sql[0] += " comparative = '" + comparative + "',";
    sql[0] += " superlative = '" + superlative + "'";
    sql[0] += " WHERE concept_id = '" + concept_id + "'";
    // irr morph
    sql[1] = "UPDATE LMT_IRR_MORPH_ADJADV SET";
    sql[1] += " comparative_string = '" + Util.replaceSingleQuote(irr_comparative) + "',";
    sql[1] += " superlative_string = '" + Util.replaceSingleQuote(irr_superlative) + "'";
    sql[1] += " WHERE concept_id = '" + concept_id + "'";	
    return sql;
  }		
}
