package DBFOschemafy_V_3_001;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 *  <b>Environment:</b> 
 *  <ul>
 *  <li>    IDE:              Eclipse IDE for Java Developers
 *  <li>    Version:          2021-12 (4.22.0)
 *  <li>    Build id:         20211202-1639
 *  <li>    HW Model Name:    iMac, MacOS Monterey, 12.5.1
 *  <li>    Processor Name:   Quad-Core Intel Core i5
 *  <li>    Processor Speed:  3.2 GHz
 *  <li>    Memory:           32 GB 1867 MHz DDR3
 *  <li>    Disk:             APPLE SSD SM0256G    
 *  <li>    Serial:           DGKRC080GG7V
 *  <li>    RDB:              MySQL Server (Version 8.0.30), 
 *  <li>    Workbench:        MySQL Workbench (Version 8.0.31 build 2235049 CE (64 bits) Community)
 *  <li>    JDBC connector:   mysql-connector-j-9.1.0.jar
 *  </ul>
 *  @version 1-001
 *  @since   2024/11/27
 *  @author  Edit Hlaszny PhD (https://www.edithlaszny.eu/) 
 */

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

                             )
    {
        int            countOfHdrAnnotations = 0 ;
        DButils        dbu                   = new 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) ;

            while (rs.next()) 
            {
                
                fileWriter.write(shu.addHdrAnnotation(rs.getString("iri")  ,
                                                      rs.getString("literal")
                                                     )
                                ) ;
                countOfHdrAnnotations++ ;
            }
            
            /**
             *  closing the result set and the file writer
             */
            rs.close() ;
            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
         */
        DBFOschemafyMain OWL2G = new DBFOschemafyMain() ;
        
        OWL2G.log(new StringBuilder()
                      .append("    asserted  " + countOfHdrAnnotations + " header annotation(s)\n")
                      .toString()
                 ) ; 
        
    }   //  end of constructor
    
}   //  end of class WriteOWLHeaderAnnotations
