package OWL2generator;


import java.io.File;

import java.io.FileWriter;
import java.io.IOException;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WriteOWLHeaderAnnotations
{
    WriteOWLHeaderAnnotations(String      OWLresultFile,  //  result file name 
                              Connection  DBconnection

                             )
    {
        int            countOfHdrAnnotations = 0 ;
        DBFO_DButils        dbu                   = new DBFO_DButils() ;
        FileWriter     fileWriter            = null ;
        SharedUtils    shu                   = new SharedUtils() ;
        
        ResultSet rs = dbu.establishResultSet(DBconnection, 
                       "SELECT * FROM ONTOLOGY_HEADER_ANNOTATIONS ORDER BY hdrAnn_id ASC ;") ;
        try 
        {
            fileWriter = new FileWriter(new File(OWLresultFile), true) ;

            fileWriter.write("\n    <!--  add ontology header annotations\n      -->\n") ;

            while (rs.next()) 
            {
            	String iri     = rs.getString("iri") ;
            	String literal = rs.getString("literal") ;

            	if (iri.compareTo("dc:created") == 0)
            	{
            		String currDate = getCurrTime() ; 
            		String date     = currDate.substring(0,  10) ;
            		String time     = currDate.substring(11, 23) ;
                    
                    fileWriter.write(shu.addHdrAnnotation(iri,       date)) ;
                    fileWriter.write(shu.addHdrAnnotation("dc:time", time)) ;
            	}
            	else
            	{
                    fileWriter.write(shu.addHdrAnnotation(iri, literal)) ;
            	}

            	countOfHdrAnnotations++ ;
            }
            
            /**
             *  closing the result set
             */
            rs.close() ;
            
            /**
             *  closing the file writer
             */
            fileWriter.close() ;
        }
        catch(SQLException SQLex)
        {
            System.out.println("Cannot connect the database") ;
            System.out.println("SQLException: " + SQLex.getMessage());
            System.out.println("SQLState: "     + SQLex.getSQLState());
            System.out.println("VendorError: "  + SQLex.getErrorCode());

            SQLex.printStackTrace();
        }
        catch(IOException IOex)
        {
            IOex.printStackTrace() ;
        }

        /**
         *  Logging of the output volume
         */
        DBFO_main OWL2G = new DBFO_main() ;
        
        OWL2G.log(new StringBuilder()
                      .append("    asserted  " + countOfHdrAnnotations + " header annotation(s)\n")
                      .toString()
                 ) ; 
    }   //  end of constructor

    public String getCurrTime() 
    {
    	String format = "yyyy-MM-dd HH:mm:ss.SSS" ;
    	return new SimpleDateFormat(format).format(new Date()) ;
    	
    }   //  end of method getCurrTime()
    
}   //  end of class WriteOWLHeaderAnnotations
