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

package lmt;

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

public class MorphNoun extends Morphology {
  String plural;
  String gensg;
  String genpl;
  String irr_plural;
  String irr_gensg;
  String irr_genpl;
  SQLConnection con;
	
  /** constructor 
   **/	
  public MorphNoun(SQLConnection con, String concept_id) {
    super();
    this.con = con;
    this.concept_id = concept_id;
    plural = "";
    gensg = "";
    genpl = "";
    irr_plural = "";
    irr_gensg = "";
    irr_genpl = "";
  }

  /** Get plural feature.
   * @return a string representing plural.
   **/
  public String getPlural() {
    return plural;
  }	
  /** Set plural feature.
   * @param plural a string representing the plural
   **/		
  public void setPlural(String plural) {
    if ( plural == null )
      plural = "";
    this.plural = plural;
  }	
  /** Get gensg feature.
   * @return a string representing gensg.
   **/	
  public String getGensg() {
    return gensg;
  }	
  /** Set gensg feature.
   * @param gensg a string representing the gensg
   **/		
  public void setGensg(String gensg) {
    if ( gensg == null )
      gensg = "";
    this.gensg = gensg;
  }	
  /** Get genpl feature.
   * @return a string representing genpl.
   **/	
  public String getGenpl() {
    return genpl;
  }	
  /** Set genpl feature.
   * @param genpl a string representing the genpl
   **/		
  public void setGenpl(String genpl) {
    if ( genpl == null )
      genpl = "";
    this.genpl = genpl;
  }	
  /** Get irr_plural feature.
   * @return a string representing irr_plural.
   **/
  public String getIrrPlural() {
    return irr_plural;
  }	
  /** Set irr_plural feature.
   * @param irr_plural a string representing the irr_plural
   **/		
  public void setIrrPlural(String irr_plural) {
    if ( irr_plural == null )
      irr_plural = "";
    this.irr_plural = irr_plural;
  }	
  /** Get gensg feature.
   * @return a string representing gensg.
   **/
  public String getIrrGensg() {
    return irr_gensg;
  }	
  /** Set gensg feature.
   * @param gensg a string representing the gensg
   **/		
  public void setIrrGensg(String irr_gensg) {
    if ( irr_gensg == null )
      irr_gensg = "";
    this.irr_gensg = irr_gensg;
  }	
  /** Get irr_genpl feature.
   * @return a string representing irr_genpl.
   **/
  public String getIrrGenpl() {
    return irr_genpl;
  }	
  /** Set irr_genpl feature.
   * @param irr_genpl a string representing the irr_genpl
   **/		
  public void setIrrGenpl(String irr_genpl) {
    if ( irr_genpl == null )
      irr_genpl = "";
    this.irr_genpl = irr_genpl;
  }	
	
  /** Get all morphology information which has the concept_id
   * @param concept_id a string representing concept_id
   * @return MorphNoun fetched from the database.
   **/	
  public static MorphNoun fetchMorph(SQLConnection con, String concept_id) {
    MorphNoun morph = new MorphNoun(con, concept_id);
			
    try {
      String sql = "SELECT * FROM LMT_MORPH_NOUN_PU WHERE ";
      sql += " CONCEPT_ID = '" + concept_id + "'";
      Debug.debug("-- fetchMorphNoun : " + sql);		
      SQLConnection.Result r = con.query(sql);
      if (r!=null &&r.hasData()){
	morph.setPlural(r.rs.getString(2));
	morph.setGensg(r.rs.getString(3));
	morph.setGenpl(r.rs.getString(4));
	morph.setActive(r.rs.getString(5));
      }
      if (r!=null) r.close();
								
      // get irregular expression
      sql = "SELECT * FROM LMT_IRR_MORPH_NOUN_PU WHERE";
      sql += " CONCEPT_ID = '" + concept_id + "'";
      Debug.debug("-- fetchIrrMorphNoun : " + sql);					
      r = con.query(sql);
      if (r!=null &&r.hasData()){
	morph.setIrrPlural(r.rs.getString(2));
	morph.setIrrGensg(r.rs.getString(3));
	morph.setIrrGenpl(r.rs.getString(4));
	morph.setIrrActive(r.rs.getString(5));
      }
      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() {
    // morph
    String sql[] = makeInsertQuery();
    Debug.debug("-- MorphNoun insertion : " + sql[0]);					
    Debug.debug("-- MorphIrrNoun insertion : " + sql[1]);
    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 have the concept_id
   * @return boolean representing the result of operation
   **/		
  public boolean isExistent() {
		
    String sql = "SELECT * FROM LMT_MORPH_NOUN_PU 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[0]);
    queryVector.addElement(sql[1]);
  }			
	
  String[] makeInsertQuery() {
    String sql[] = new String [2];
    // morph
    sql[0] = "INSERT INTO LMT_MORPH_NOUN_PU VALUES (";
    sql[0] += "'" + concept_id + "',";
    sql[0] += "'" + plural + "',";
    sql[0] += "'" + gensg + "',";
    sql[0] += "'" + genpl + "',";
    sql[0] += "'T')";
    // irr morph
    sql[1] = "INSERT INTO LMT_IRR_MORPH_NOUN_PU VALUES (";
    sql[1] += "'" + concept_id + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_plural) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_gensg) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_genpl) + "',";
    sql[1] += "'T')";
    return sql;
  }
	
  String[] makeUpdateQuery() {
    String[] sql = new String[2];
    // morph
    sql[0] = "UPDATE LMT_MORPH_NOUN_PU SET";
    sql[0] += " plural = '" + plural + "',";
    sql[0] += " gensg = '" + gensg + "',";
    sql[0] += " genpl = '" + genpl + "'";
    sql[0] += " WHERE concept_id = '" + concept_id + "'";
    // irr morph
    sql[1] = "UPDATE LMT_IRR_MORPH_NOUN_PU SET";
    sql[1] += " plural_string = '" + Util.replaceSingleQuote(irr_plural) + "',";
    sql[1] += " gensg_string = '" + Util.replaceSingleQuote(irr_gensg) + "',";
    sql[1] += " genpl_string = '" + Util.replaceSingleQuote(irr_genpl) + "'";
    sql[1] += " WHERE concept_id = '" + concept_id + "'";				
    return sql;		
  }
}
