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;

/**
 *  The <b><code>WriteSKOSannotations</code></b> class inserts several
 *  <code>SKOS</code>-defined annotation declarations into the result ontology. 
 *  These declarations are stored in the DB table SKOS_ANNOTATIONS. 
 *  <p>
 *  <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
 *  </ul>
 *  @version 1-001
 *  @since   2025/06/27
 *  @author  Edit Hlaszny (https://www.edithlaszny.eu/ edithlaszny@gmail.com)
 */ 

public class WriteSKOSannotations
{
    WriteSKOSannotations(String      OWLresultFile,  //  result file name 
                         Connection  DBconnection
                        )
    {
        int          countOfSKOSannotations = 0 ;
        DBFO_DButils dbu                    = new DBFO_DButils() ;
        FileWriter   fileWriter             = null ;
        SharedUtils  shu                    = new SharedUtils() ;
        
        
        ResultSet rs = dbu.establishResultSet(DBconnection, 
                       "SELECT * FROM SKOS_ANNOTATIONS ;") ;
        try 
        {
            fileWriter = new FileWriter(new File(OWLresultFile), true) ;

            fileWriter.write("\n    <!--  add SKOS declarations\n      -->\n") ;

            while (rs.next()) 
            {
                fileWriter.write(shu.addSKOSannotation(rs.getString("skos_annotation"))) ;
                
                countOfSKOSannotations++ ;
            }
            
            /**
             *  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  " + countOfSKOSannotations + " SKOS annotation(s)\n")
                      .toString()
                 ) ; 
    }   //  end of constructor()
        
}   //  end of class WriteIAOannotations
