Bool_t gApacheExec=1; Bool_t gApacheNoCache=kTRUE;

// MetadataDisplay.C
// Uses the systest_id which is passed into the file to index into the 
// metadata table in the database which gives the values of the metadata.
// The labels for the metadata is got from the info labels by doing a join
// with the metadata table


#include <TApache.h>
#include <TSQLServer.h>
#include <TSQLRow.h>
#include <TSQLResult.h> 
#include <TCanvas.h>
#include <TFrame.h>
#include <TH1.h>
#include <string.h>
#include <iostream.h>

void MetadataDisplay(){

  TApache ap;  
  gApache->Puts("hello");

  char sql[4096];


  // These display the test data for a particular version
  char *sql1="select il.label, info_value from glast_data.metadata m, glast_data.info_labels il, glast_data.system_tests s where il.info = m.info and m.systest_id = s.systest_id and m.systest_id =";
  char *sql2 = " order by il.label";

 
  // This allows us to recognise which test it is
  char *sql5 = "select test_type, test_name, version from glast_data.system_tests where systest_id =";
  
  // This connects to the database
  char *connection="oracle://sage/SLAC_TCP";
  char *user="LAT";
  char *pass="LATREAD";
 
  // Get the name of package
  TString systest_id = gSystem->Getenv("systestid");
  // Assign it to a string variable
  char *id = systest_id.Data();
    
  // Generate the sql query
  std::strcpy(sql, sql1);
  std::strcat(sql, id);
  std::strcat(sql, sql2);
  
  // connect to oracle server
  TSQLServer *db=TSQLServer::Connect(connection, user, pass);
  
  
  // The HTML form
  gApache->Puts("<HTML><BODY><CENTER>");
  gApache->Puts("<BR><BR>");
  
  // The query to get the test info
  char testsql[200];
  std::strcpy(testsql, sql5);
  std::strcat(testsql, id);
  TSQLResult *res1=db->Query(testsql);   
  TSQLRow *row1=res1->Next();

  // Setting up the table for displaying metadata values
  gApache->Puts("<h2>The Metadata Values for GLEAM version ");
  gApache->Puts(row1->GetField(2));
  gApache->Puts("are:</h2>");
  gApache->Puts("Test Type:");
  gApache->Puts(row1->GetField(0));
  gApache->Puts("<BR>");
  gApache->Puts("Test Name:");
  gApache->Puts(row1->GetField(1));
  gApache->Puts("<BR>");
  gApache->Puts("<TABLE>");
  gApache->Puts("<CAPTION>Values</CAPTION>");
  gApache->Puts("<TR>");
  gApache->Puts("<TH>Info Label</TH>");
  gApache->Puts("<TH>Info Value</TH>");
  gApache->Puts("</TR>");

  // This is the query to get the metadata
  TSQLResult *res=db->Query(sql);   
  TSQLRow *row=res->Next();
   
  do{
          
    gApache->Puts("<TR>");
    for (int i=0; i<res->GetFieldCount();i++)
      { 
	
	gApache->Puts("<TD>");
	ap.Printf(row->GetField(i));
	gApache->Puts("</TD>");
	
      }// end for
    
    gApache->Puts("</TR>");
    char *linebreak = "<BR>";
    //gApache->Puts(linebreak);
  }// end while
  while (res->Next());

  gApache->Puts("<TABLE>");
  gApache->Puts("<BR></form>");
  gApache->Puts("</CENTER></BODY></HTML>");
  

}// end of meta_overplot
