/*
 * @(#)KSDBChangedSentences.java	0.1 2001/08/27
 *
 * Copyright 2001 by Carnegie Mellon, All rights reserved.
 */

package ksdb;
import java.awt.datatransfer.*;
import java.awt.event.*;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.*;
import javax.swing.*;
import javax.swing.tree.*;

import edu.cmu.lti.kantoo.network.SQLConnection;
import edu.cmu.lti.kantoo.shared.*;


/**
 * KSDBChangedSentences is a program that lists all sentences changed in
 * a testsuite between the current Analyzer/Generator and a base version
 * of the Analyzer. It takes two parameters, the name of the testsuite, and
 * the Analyzer version number.
 *
 * @author  David Svoboda
 * @version 0.1 2001/08/27
 */
public class KSDBChangedSentences {

  /**
   * Main procedure. Locks sentences before testing them
   * Takes two arguments: a suite-name and Analyzer version number
   * (version number is key from db, not version string)
   * Outputs sentences on stdout, with progress on stderr.
   */
  public static void main(String[] args) {
    System.setProperty("edu.cmu.cs.kantoo.shared.ReportError.windows", "no");
    PropertyManager.getArgs( args);
    
    try {
      Ksdb ksdb = new Ksdb(System.getProperty("user.name"));
      System.err.println("WARNING: This method is not lock-safe!");
      Tester tester = null;
      synchronized (Tester.InitLock) {
	tester = Tester.createFrame("Testing suite " + args[0], ksdb, 0, "Sentence.id, Sentence.text, Sentence.previous FROM Suite, Suite_Document, Document, Context, Sentence WHERE Context.document=Document.id AND Context.sentence=Sentence.id AND Suite_Document.suite=Suite.id AND Suite_Document.document=Document.id AND Suite.name=" + SQLConnection.quote( args[0]));
	tester.getFrame().setVisible( ReportError.Windows);
	tester.InitLock.wait();
	tester.baseAnalyzerVersion = Integer.parseInt( args[1]);
      }

      // Print out every sentence that is changed
      // First determine index of sentence to start with.
      // either one that selection is in, or first one
      TreeNode root = (TreeNode) (tester.jTree.getModel().getRoot());
      int current = -1;
      int limit = root.getChildCount();
      TreeNode currentNode;
      for (current++; current < limit; current++) {
	if (current % 10 == 0) {
	  System.err.print(".");
	  System.err.flush();
	}
	currentNode = root.getChildAt(current);
	TreeNode translation = (currentNode.getChildAt(0).getChildAt(0));
	// discard translation; we just want to color sentence
	String sentence = ((Tester.SentenceTreeNode) currentNode).toString();
	int place = sentence.indexOf( tester.changedMarkup);
	if (place >= 0)
	  System.out.println(sentence.substring( place + tester.changedMarkup.length()));
      }
    } catch (Exception x) {ReportError.warning(x);}
    System.err.println();
    System.exit(0);
  }
}
