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

package lmt;

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

public class NCRI {
  String ncri_id;
  String concept_id;
  String ncri_term_string;
  String ncri_pos;
  String ncri_concept_symbol;
  String ncri_type_phrase;
  String ncri_phrase_head;
  String active;
  Morphology	morph;
  SQLConnection con;
	
  /** constructor
   */
  public NCRI(SQLConnection con) {
    this.con = con;
    ncri_id = "";
    concept_id = "";		
    ncri_term_string = "";
    ncri_pos = "";
    ncri_concept_symbol = "*NCRI";
    ncri_type_phrase = "";
    ncri_phrase_head = "";
    active = "T";
    morph = null;
  }
	
  /** constructor
   * @param Term represeting term
   * @param concept represeting concept
   */	
  public NCRI(Term term, Concept concept, SQLConnection con) {
    this(con);
    concept_id = concept.getConceptID();		
    ncri_term_string = term.getTermString();
    ncri_pos = term.getPos();
    ncri_type_phrase = term.getTypePhrase();
    ncri_phrase_head = term.getPhraseHead();
  }	
	
  // setters and getters	
  public String getNcriID() {
    return ncri_id;
  }	
  public void setNcriID(String ncri_id) {
    if ( ncri_id == null) ncri_id = "";
    this.ncri_id = ncri_id;
  }	
  public String getConceptID() {
    return concept_id;
  }	
  public void setConceptID(String concept_id) {
    if ( concept_id == null) concept_id = "";
    this.concept_id = concept_id;
  }		
  public String getNcriTermString() {
    return ncri_term_string;
  }	
  public void setNcriTermString(String ncri_term_string) {
    if ( ncri_term_string == null) ncri_term_string = "";
    this.ncri_term_string = ncri_term_string;
  }	
  public String getNcriPos() {
    return ncri_pos;
  }	
  public void setNcriPos(String ncri_pos) {
    if ( ncri_pos == null) ncri_pos = "";
    this.ncri_pos = ncri_pos;
  }	
  public String getNcriConceptSymbol() {
    return ncri_concept_symbol;
  }	
  public void setNcriConceptSymbol(String ncri_concept_symbol) {
    if ( ncri_concept_symbol == null) ncri_concept_symbol = "";
    this.ncri_concept_symbol = ncri_concept_symbol;
  }	
  public String getNcriTypePhrase() {
    return ncri_type_phrase;
  }	
  public void setNcriTypePhrase(String ncri_type_phrase) {
    if ( ncri_type_phrase == null) ncri_type_phrase = "";
    this.ncri_type_phrase = ncri_type_phrase;
  }	
  public String getNcriPhraseHead() {
    return ncri_phrase_head;
  }	
  public void setNcriPhraseHead(String ncri_phrase_head) {
    if (ncri_phrase_head == null)
      this.ncri_phrase_head = "";	
    else
      this.ncri_phrase_head = ncri_phrase_head;
  }	
  public void setMorphology (Morphology morph) {
    this.morph = morph;
  }	
  public void setActive (String active) {
    this.active = active;
  }	

  /** get Morpholgy
   * @return Morpholgy this NCRI 
   **/	
  public Morphology getMorphology() {
    morph = Morphology.fetchMorph(con, ncri_pos, "-"+ncri_id);	
    if ( morph == null) {
      if ( ncri_pos.equals("ADJ") ||
	   ncri_pos.equals("ADVERB") ) 
	morph = new MorphAdj(con, "-"+ncri_id);
      else if (ncri_pos.equals("VERB") ||
	       ncri_pos.equals("AUX") ) 
	morph = new MorphVerb(con, "-"+ncri_id);
      else if (ncri_pos.equals("NOUN") ||
	       ncri_pos.equals("PROP") ||
	       ncri_pos.equals("UNIT") ) 
	morph = new MorphNoun(con, "-"+ncri_id);
    }
    return morph;
  }	

  /** Get all NCRIs which have the concept_id
   * @param concept_id a string representing concept_id
   * @return Vector all NCRIs fetched from the database.
   **/	
  public static Vector fetchNCRI(SQLConnection con, String concept_id) {
    Vector ncriVector = new Vector();
		
    String sql = "SELECT * FROM LMT_NCRI WHERE ";
    sql += " CONCEPT_ID = '" + concept_id + "'";
    sql += " ORDER BY NCRI_ID";

    try {
      SQLConnection.Result r = con.query(sql);
      while (r!=null &&r.hasData()){
	NCRI ncri = new NCRI(con);
	ncri.setNcriID(r.rs.getString(1));				 
	ncri.setConceptID(r.rs.getString(2));
	ncri.setNcriTermString(r.rs.getString(3));
	ncri.setNcriPos(Term.changePosForUI(r.rs.getString(4)));
	ncri.setNcriConceptSymbol(r.rs.getString(5));
	ncri.setNcriTypePhrase(r.rs.getString(6));
	ncri.setNcriPhraseHead(r.rs.getString(7));
	ncri.setActive(r.rs.getString(8));
	ncriVector.addElement(ncri);	
				
	ncri.getMorphology();
      }
      if (r!=null) r.close();
    } catch (SQLException E) {
      Debug.debug("SQLException: " + E.getMessage());
    }	
		
    Debug.debug("-- fetchNCRI: " + sql);		

    return ncriVector;
  }

  /** insert the new data into the table.
   * @return integer representing the result of operation
   **/		
  public int insert() {

    if ( isDuplicate() )
      return Constants.DUPLICATION;
		
    // get New Term ID				
    ncri_id = con.getNewID("NCRI_ID");				
		
    String sql = "INSERT INTO LMT_NCRI VALUES (";
    sql += "'" + ncri_id + "',";
    sql += "'" + concept_id + "',";
    sql += "'" + Util.replaceSingleQuote(ncri_term_string) + "',";
    sql += "'" + Term.changePosForDB(ncri_pos) + "',";
    sql += "'" + ncri_concept_symbol + "',";
    sql += "'" + ncri_type_phrase + "',";
    sql += "'" + ncri_phrase_head + "',";		
    sql += "'" + active + "')";

    Debug.debug("-- NCRI insertion : " + sql);		
		
    return con.insert(sql);
  }
	
  /** delete the data from the table.
   * @return integer representing the result of operation
   **/		
  public int delete() {
    // check where this exists
    /*		if (!isExistent(con))
      return Constants.DONOTEXIST;
		
      String sql = "DELETE FROM LMT_NCRI ";
      sql += " WHERE concept_id = '" + concept_id + "'";
      sql += " AND ncri_id = '" + ncri_id + "'";
		
      Debug.debug("-- NCRI delete : " + sql);		
		
      return con.delete(sql);
    */		
    // call stored procedure in the database
    boolean ret = con.callProcedure("DeleteNCRI", ncri_id);
    Debug.debug("-- delete ncri: " + ret);
		
    if (ret) 
      return Constants.SUCCESS;
    else
      return Constants.FAIL;		
  }
	
  /** update the data in the table.
   * @return integer representing the result of operation
   **/		
  public int update() {
    // check where this exists
    if (!isExistent())
      return Constants.DONOTEXIST;
		
    String sql = "UPDATE LMT_NCRI SET";
    sql += " ncri_term_string = '" + Util.replaceSingleQuote(ncri_term_string) + "',";
    sql += " ncri_pos = '" + Term.changePosForDB(ncri_pos) + "',";
    sql += " ncri_concept_symbol = '" + ncri_concept_symbol + "',";
    sql += " ncri_type_phrase = '" + ncri_type_phrase + "',";	
    sql += " ncri_phrase_head = '" + ncri_phrase_head + "'";
    sql += " WHERE concept_id = '" + concept_id + "'";
    sql += " AND ncri_id = '" + ncri_id + "'";
		
    Debug.debug("-- NCRI update : " + sql);		
		
    return con.update(sql);
  }	

  /** update the data in the table.
   * @return integer representing the result of operation
   **/	
  public static int update(SQLConnection con, Vector ncriVector) {
    // make query
    Vector queryVector = new Vector();
    NCRI.makeQuery(queryVector, ncriVector);		
		
    return con.update(queryVector);
  }
	
  /** check whether the concept exists or not
   * @return boolean representing the result of operation
   **/		
  public boolean isExistent() {
		
    String sql = "SELECT * FROM LMT_NCRI WHERE ";
    sql += " concept_id = '" + concept_id + "'";
    sql += " AND ncri_id = '" + ncri_id + "'";
		
    //Debug.debug("-- isExistent : " + sql);
    return con.isExistent(sql);
  }

  /** check whether the information exists or not in the database 
   * using ncri_term_string, ncri_pos and concept_id
   * @return boolean representing the result of operation
   **/	
  public boolean isDuplicate() {
		
    String sql = "SELECT * FROM LMT_NCRI WHERE ";
    sql += " ncri_term_string = '" + Util.replaceSingleQuote(ncri_term_string) + "'";
    sql += " AND ncri_pos = '" + Term.changePosForDB(ncri_pos) + "'";
    sql += " AND concept_id = '" + concept_id + "'";

    //Debug.debug("-- isDuplicated : " + sql);	
    return con.isExistent(sql);
  }
	
  /** make the query using input vector
   * @param comVector	vector which has objects of this class
   * @return String[] array including SQL
   **/	
  public static void makeQuery(Vector queryVector, Vector ncriVector) {
    if (ncriVector == null)
      return;
		
    String query;
    for ( int i=0; i<ncriVector.size(); i++) {
      NCRI n = (NCRI)ncriVector.elementAt(i);
      query = "UPDATE LMT_NCRI SET";
      query += " ncri_term_string = '" + Util.replaceSingleQuote(n.getNcriTermString()) + "',";
      query += " ncri_pos = '" + Term.changePosForDB(n.getNcriPos()) + "',";
      query += " ncri_concept_symbol = '" + n.getNcriConceptSymbol() + "',";
      query += " ncri_type_phrase = '" + n.getNcriTypePhrase() + "',";	
      query += " ncri_phrase_head = '" + n.getNcriPhraseHead() + "'";
      query += " WHERE concept_id = '" + n.getConceptID() + "'";
      query += " AND ncri_id = '" + n.getNcriID() + "'";
      //Debug.debug("-- update NCRI: " + query[i]);
      queryVector.addElement(query);				
			
      if (n.morph != null) {
	//Debug.debug("-- n.morph: " + n.morph);
	// The ncri_id is negative since NCRI uses the same tables of 
	// the normal morphology and needs to distinguish the keys
	n.morph.setConceptID("-" + n.getNcriID());
	n.morph.makeQuery(queryVector, n.getNcriPos());
      }
    }
  }			
}
