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 AnnotateClasses
{
    private DButils dbu = new DButils() ;
    
    AnnotateClasses(String      OWLresultFile,  //  result file name 
                    Connection  DBconnection    //  active DB server connection
                   )
    {
        int         countOfannotatedClasses = 0 ;
        SharedUtils shu                     = new SharedUtils() ;

        ResultSet rs = this.dbu.establishResultSet(DBconnection, 
                       "SELECT * FROM CLASS_ANNOTATIONS ORDER BY class_IRI ;") ;
        try
        {
            String     annotation = "" ;
            FileWriter fileWriter = new FileWriter(new File(OWLresultFile), true) ;
            
            while (rs.next()) 
            {
                if (rs.getString("annotation_type_IRI").equals("#BFO_URI"))
                {
                    annotation = dbu.decodeBFOentityMeaning(DBconnection,
                                                            rs.getString("annotation")) ;
                }
                else
                {
                    annotation = rs.getString("annotation") ;
                }
                
                fileWriter.write(shu.assertClassAnnotation(rs.getString("class_IRI")           ,
                                                           rs.getString("annotation_type_IRI") ,
                                                           rs.getString("language")            ,
                                                           annotation
                                                          )
                                ) ;
                
                countOfannotatedClasses++ ;
            }
        
            /**
             *  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  " + countOfannotatedClasses + " class annotation(s)\n")
                      .toString()
                 ) ; 
        
     }   //  end of constructor()

    
}   //  end of class AnnotateClasses
