package basic;
import uk.ac.bbk.dcs.automed.qproc.iql.ASG;
import uk.ac.bbk.dcs.automed.qproc.iql.ParseException;
import uk.ac.ic.doc.automed.qproc.cache.DefaultQueryCache;
import uk.ac.ic.doc.automed.util.Debugger;
import uk.ac.ic.doc.automed.util.LogOutputHandler;
import uk.ac.ic.doc.automed.util.Logger;
import uk.ac.ic.doc.automed.util.cache.CacheIntegrityException;
import uk.ac.ic.doc.automed.util.persist.PersistentHandler;
import uk.ac.ic.doc.automed.wrappers.AutoMedWrapper;
import uk.ac.ic.doc.rodex.p2p.gui.CacheViewerFrame;
import uk.ac.ic.doc.rodex.qproc.QueryAnalyser;
import uk.ac.ic.doc.rodex.qproc.QueryIntegrityException;
import utils.AutoMedUtils;


/**
 * QueryCacheExample.java<br><br>
 *
 * Shows how to construct a serialisable query cache. You may run this class in parallel
 * to running {@link PersistentDBExample} which populates entries to the cacheDB.
 *
 * @author Duc M Le  <a href="mailto:dmle@doc.ic.ac.uk"><i>dmle@doc.ic.ac.uk</i></a>
 * <br>Created on August 7, 2006
 * <br>Department of Computing, Imperial College
 */

public class QueryCacheExample {

  private CacheViewerFrame _cacheViewer;
  private DefaultQueryCache _queryCache;
  
  /**
   * Creates a new instance of QueryCacheExample
   */
  public QueryCacheExample() throws Exception {
    // create cache object
    _queryCache = AutoMedWrapper.getQueryCache();
    
    // get serialised contents from persistent storage
    _queryCache.deserialise();
    
    // create cache window
    _cacheViewer = new CacheViewerFrame("Query cache viewer ", _queryCache);
    
    // show the cache frame
    _cacheViewer.display();
  }
  
  public void addEntry() throws CacheIntegrityException {
    long rand = System.currentTimeMillis();
    String q = "<<test,x"+rand+">>";
    String fq = "f:<<test,x"+rand+">>";
    String sq = "s:<<test,x"+rand+">>";
    String iq = "i:<<test,x"+rand+">>";
    String result = "[{"+rand+","+(rand+55555)+"},{"+(rand+11111)+","+(rand+22222)+"}]";
//    String id = _queryCache.addQuery(q, fq,sq, iq, null, null);
    String id = _queryCache.addQuery(q, null);
    
    // added to cache and serialised to disk
    _queryCache.addQueryResult(id, result);
  }
  
  public void addPersonData() throws CacheIntegrityException, QueryIntegrityException, ParseException {
    long rand = 1;
    String q = "[{r,d} | {r,d} <- <<person,dob>>; "
      + "d >= 1970 ; d < 2000]";
    
    ASG asg = new ASG(q);
    
    String fq = "null";
    String sq = "null";
    String iq = "null";
    
    QueryAnalyser qa = new QueryAnalyser(asg);  
    qa.analyse();
    
    ASG result = new ASG ("[{"+rand+","+1970+"}," +
      "{"+(++rand)+","+1972+"}," +
      "{"+(++rand)+","+1975+"}," +
      "{"+(++rand)+","+1980+"}," +
      "{"+(++rand)+","+1985+"}," +
      "{"+(++rand)+","+1990+"}]");
    
//    String id = _queryCache.addQuery(qa.getQueryString(), fq,sq, iq, qa, null);
    String id = _queryCache.addQuery(qa.getQueryString(), null);
    
    // added to cache and serialised to disk
    _queryCache.addQueryResult(id, result);
  }

  // not implemented yet
  public void removeEntry(String id) {
    //
    System.out.println("Not implemented yet!");
  }

  /**
   * Clear the cache
   */
  public void clear() {
    _queryCache.clear();
  }
  
  public void viewDB() {
    PersistentHandler ph = _queryCache.getPersistentHandler();
    
    PersistentDBExample pdbe = new PersistentDBExample(ph);
    pdbe.viewDB();
  }
  
  public void finalize() {
    _cacheViewer.dispose();
  }
  
  public static void main(String[] args) throws Exception {
    
    Debugger.setSectionDebugLevel(Debugger.DEBUG_CACHE, Debugger.DEBUG_LEVEL_ALL);
    Debugger.setSectionDebugLevel(Debugger.DEBUG_QUERY, Debugger.DEBUG_LEVEL_ALL);
    //Debugger.setSectionDebugLevel(Debugger.DEBUG_GENERAL, Debugger.DEBUG_LEVEL_ALL);
    Logger.setOutputHandler(new LogOutputHandler(LogOutputHandler.CONSOLE + 
      LogOutputHandler.FILE));
    
    QueryCacheExample qcex = new QueryCacheExample();
    
    do {          
      if (AutoMedUtils.dialogYesNo("Add a test entry to cache?", 1) == 1) {
        qcex.addEntry();
      }

      if (AutoMedUtils.dialogYesNo("View cache DB content?", 0) == 1) {
        qcex.viewDB();
      }
      
    } while (AutoMedUtils.dialogYesNo("Run again?", 0) == 1);
    
    AutoMedUtils.banner("~END~");    
    System.exit(0);
  }
}
