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

package lmt;

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

public class MorphVerb extends Morphology{
  String present1sg;
  String present3sg;
  String presentpl;
  String prespart;
  String prespartpl;
  String past13sg;
  String pastpl;
  String past;
  String pastpart;
	
  String irr_present1sg;
  String irr_present3sg;
  String irr_presentpl;
  String irr_prespart;
  String irr_prespartpl;
  String irr_past13sg;
  String irr_pastpl;
  String irr_past;
  String irr_pastpart;
	
  SQLConnection con;
	
  /** constructor 
   **/	
  public MorphVerb(SQLConnection con, String concept_id) {
    super();
    this.con = con;
    this.concept_id = concept_id;
		
    present1sg = "";
    present3sg = "";
    presentpl = "";
    prespart = "";
    prespartpl = "";
    past13sg = "";		
    pastpl = "";
    past = "";
    pastpart = "";

    irr_present1sg = "";
    irr_present3sg = "";
    irr_presentpl = "";
    irr_prespart = "";
    irr_prespartpl = "";
    irr_past13sg = "";		
    irr_pastpl = "";
    irr_past = "";
    irr_pastpart = "";
  }
	
  // getters and setters	
  public String getPresent1sg() {
    return present1sg;
  }	
  public void setPresent1sg(String present1sg) {
    if ( present1sg == null )
      present1sg = "";
    this.present1sg = present1sg;
  }	
  public String getPresent3sg() {
    return present3sg;
  }	
  public void setPresent3sg(String present3sg) {
    if ( present3sg == null )
      present3sg = "";
    this.present3sg = present3sg;
  }	
  public String getPresentpl() {
    return presentpl;
  }	
  public void setPresentpl(String presentpl) {
    if ( presentpl == null )
      presentpl = "";
    this.presentpl = presentpl;
  }	
  public String getPrespart() {
    return prespart;
  }	
  public void setPrespart(String prespart) {
    if ( prespart == null )
      prespart = "";
    this.prespart = prespart;
  }
  public String getPrespartpl() {
    return prespartpl;
  }	
  public void setPrespartpl(String prespartpl) {
    if ( prespartpl == null )
      prespartpl = "";
    this.prespartpl = prespartpl;
  }	
  public String getPast13sg() {
    return past13sg;
  }	
  public void setPast13sg(String past13sg) {
    if ( past13sg == null )
      past13sg = "";
    this.past13sg = past13sg;
  }	
  public String getPastpl() {
    return pastpl;
  }	
  public void setPastpl(String pastpl) {
    if ( pastpl == null )
      pastpl = "";
    this.pastpl = pastpl;
  }	
  public String getPast() {
    return past;
  }	
  public void setPast(String past) {
    if ( past == null )
      past = "";
    this.past = past;
  }		
  public String getPastpart() {
    return pastpart;
  }	
  public void setPastpart(String pastpart) {
    if ( pastpart == null )
      pastpart = "";
    this.pastpart = pastpart;
  }
  // irregular morph
  public String getIrrPresent1sg() {
    return irr_present1sg;
  }	
  public void setIrrPresent1sg(String irr_present1sg) {
    if ( irr_present1sg == null )
      irr_present1sg = "";
    this.irr_present1sg = irr_present1sg;
  }	
  public String getIrrPresent3sg() {
    return irr_present3sg;
  }	
  public void setIrrPresent3sg(String irr_present3sg) {
    if ( irr_present3sg == null )
      irr_present3sg = "";
    this.irr_present3sg = irr_present3sg;
  }	
  public String getIrrPresentpl() {
    return irr_presentpl;
  }	
  public void setIrrPresentpl(String irr_presentpl) {
    if ( irr_presentpl == null )
      irr_presentpl = "";
    this.irr_presentpl = irr_presentpl;
  }	
  public String getIrrPast13sg() {
    return irr_past13sg;
  }	
  public void setIrrPast13sg(String irr_past13sg) {
    if ( irr_past13sg == null )
      irr_past13sg = "";
    this.irr_past13sg = irr_past13sg;
  }	
  public String getIrrPrespart() {
    return irr_prespart;
  }	
  public void setIrrPrespart(String irr_prespart) {
    if ( irr_prespart == null )
      irr_prespart = "";
    this.irr_prespart = irr_prespart;
  }
  public String getIrrPrespartpl() {
    return irr_prespartpl;
  }	
  public void setIrrPrespartpl(String irr_prespartpl) {
    if ( irr_prespartpl == null )
      irr_prespartpl = "";
    this.irr_prespartpl = irr_prespartpl;
  }	

  public String getIrrPastpl() {
    return irr_pastpl;
  }	
  public void setIrrPastpl(String irr_pastpl) {
    if ( irr_pastpl == null )
      irr_pastpl = "";
    this.irr_pastpl = irr_pastpl;
  }	
  public String getIrrPast() {
    return irr_past;
  }	
  public void setIrrPast(String irr_past) {
    if ( irr_past == null )
      irr_past = "";
    this.irr_past = irr_past;
  }		
  public String getIrrPastpart() {
    return irr_pastpart;
  }	
  public void setIrrPastpart(String irr_pastpart) {
    if ( irr_pastpart == null )
      irr_pastpart = "";
    this.irr_pastpart = irr_pastpart;
  }

  /** Get all morphology information which has the concept_id
   * @param concept_id a string representing concept_id
   * @return MorphVerb fetched from the database.
   **/	
  public static MorphVerb fetchMorph(SQLConnection con, String concept_id) {
    MorphVerb morph = new MorphVerb(con, concept_id);
		
    try {
      String sql = "SELECT * FROM LMT_MORPH_VERB WHERE ";
      sql += " CONCEPT_ID = '" + concept_id + "'";
				
      Debug.debug("-- fetchMorphVerb: " + sql);						
      SQLConnection.Result r = con.query(sql);
      if (r!=null &&r.hasData()){
	morph.setConceptID(r.rs.getString(1));
	morph.setConDouble(r.rs.getString(2));
	morph.setPresent1sg(r.rs.getString(3));
	morph.setPresent3sg(r.rs.getString(4));
	morph.setPresentpl(r.rs.getString(5));
	morph.setPrespart(r.rs.getString(6));
	morph.setPrespartpl(r.rs.getString(7));
	morph.setPast13sg(r.rs.getString(8));
	morph.setPastpl(r.rs.getString(9));
	morph.setPast(r.rs.getString(10));
	morph.setPastpart(r.rs.getString(11));
	morph.setActive(r.rs.getString(12));
      }
      if (r!=null) r.close();

      // get irregular expression
      sql = "SELECT * FROM LMT_IRR_MORPH_VERB WHERE";
      sql += " CONCEPT_ID = '" + concept_id + "'";
				
      Debug.debug("-- fetchIrrMorphVerb: " + sql);	
      r = con.query(sql);
      if (r!=null &&r.hasData()){
	morph.setIrrPresent1sg(r.rs.getString(2));
	morph.setIrrPresent3sg(r.rs.getString(3));
	morph.setIrrPresentpl(r.rs.getString(4));
	morph.setIrrPrespart(r.rs.getString(5));
	morph.setIrrPrespartpl(r.rs.getString(6));
	morph.setIrrPast13sg(r.rs.getString(7));
	morph.setIrrPastpl(r.rs.getString(8));
	morph.setIrrPast(r.rs.getString(9));
	morph.setIrrPastpart(r.rs.getString(10));
	morph.setIrrActive(r.rs.getString(11));				
      }
      if (r!=null) r.close();
    } catch (SQLException E) {
      System.out.println("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("-- MorphVerb insertion : " + sql[0]);					
    Debug.debug("-- MorphIrrVerb 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 has the concept_id
   * @return boolean representing the result of operation
   **/	
  public boolean isExistent() {
		
    String sql = "SELECT * FROM LMT_MORPH_VERB 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_VERB VALUES (";
    sql[0] += "'" + concept_id + "',";
    sql[0] += "'" + con_double + "',";			
    sql[0] += "'" + present1sg + "',";
    sql[0] += "'" + present3sg + "',";
    sql[0] += "'" + presentpl + "',";
    sql[0] += "'" + prespart + "',";
    sql[0] += "'" + prespartpl + "',";
    sql[0] += "'" + past13sg + "',";
    sql[0] += "'" + pastpl + "',";
    sql[0] += "'" + past + "',";
    sql[0] += "'" + pastpart + "',";			
    sql[0] += "'T')";
    // irr morph
    sql[1] = "INSERT INTO LMT_IRR_MORPH_VERB VALUES (";
    sql[1] += "'" + concept_id + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_present1sg) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_present3sg) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_presentpl) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_prespart) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_prespartpl) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_past13sg) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_pastpl) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_past) + "',";
    sql[1] += "'" + Util.replaceSingleQuote(irr_pastpart) + "',";	
    sql[1] += "'T')";		
    return sql;
  }
	
  String[] makeUpdateQuery() {
    String[] sql = new String[2];
		
    // morph
    sql[0] = "UPDATE LMT_MORPH_VERB SET";
    sql[0] += " con_double = '" + con_double + "',";
    sql[0] += " present1sg = '" + present1sg + "',";
    sql[0] += " present3sg = '" + present3sg + "',";
    sql[0] += " presentpl = '" + presentpl + "',";
    sql[0] += " prespart = '" + prespart + "',";
    sql[0] += " prespartpl = '" + prespartpl + "',";
    sql[0] += " past13sg = '" + past13sg + "',";
    sql[0] += " pastpl = '" + pastpl + "',";
    sql[0] += " past = '" + past + "',";
    sql[0] += " pastpart = '" + pastpart + "'";
    sql[0] += " WHERE concept_id = '" + concept_id + "'";
		
    // irr morph
    sql[1] = "UPDATE LMT_IRR_MORPH_VERB SET";
    sql[1] += " present1sg_string = '" + Util.replaceSingleQuote(irr_present1sg) + "',";
    sql[1] += " present3sg_string = '" + Util.replaceSingleQuote(irr_present3sg) + "',";
    sql[1] += " presentpl_string = '" + Util.replaceSingleQuote(irr_presentpl) + "',";
    sql[1] += " prespart_string = '" + Util.replaceSingleQuote(irr_prespart) + "',";
    sql[1] += " prespartpl_string = '" + Util.replaceSingleQuote(irr_prespartpl) + "',";
    sql[1] += " past13sg_string = '" + Util.replaceSingleQuote(irr_past13sg) + "',";
    sql[1] += " pastpl_string = '" + Util.replaceSingleQuote(irr_pastpl) + "',";
    sql[1] += " past_string = '" + Util.replaceSingleQuote(irr_past) + "',";
    sql[1] += " pastpart_string = '" + Util.replaceSingleQuote(irr_pastpart) + "'";
    sql[1] += " WHERE concept_id = '" + concept_id + "'";	
		
    return sql;
  }
}
