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 WT_ProcessPredicates
{
    private Connection  DBconnection  = null ;
    private DButils     dbu           = null ; 
    private FileWriter  fileWriter    = null ;
    private SharedUtils shu           = null ;

    WT_ProcessPredicates(String      OWLresultFile,  //  result file name 
                         Connection  DBconnection    //  active DB connection
                        )
    {
        this.DBconnection = DBconnection ;
        
        int  countOfObjectProperties = 0 ;
        this.dbu                     = new DButils() ;
        this.shu                     = new SharedUtils() ;
        
        ResultSet rs = dbu.establishResultSet(DBconnection, 
                       "SELECT object_property_IRI FROM OWL_OBJECT_PROPERTIES " +
                       "ORDER BY object_property_IRI ASC ;") ;
        try 
        {
            this.fileWriter = new FileWriter(new File(OWLresultFile), true) ;

            while (rs.next()) 
            {
                this.fileWriter.write(processObjectproperty(rs.getString("object_property_IRI")) + "\n") ;
                countOfObjectProperties++ ;
            }
            
            /**
             *  closing the result set and the file writer
             */
            rs.close() ;
            this.fileWriter.close() ;
        }
        catch(SQLException SQLex)
        {
            System.out.println("Cannot SELECT from 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
         */
        new DBFOschemafyMain().log(new StringBuilder()
                                 .append("    declared  " + countOfObjectProperties + 
                                         " object properties\n")
                                 .toString()
                                ) ; 
        
    }   //  end of constructor()

    private String  processObjectproperty(String objectProperty)
    {
        StringBuilder objectPropDeclaration = new StringBuilder() ;

        /**
         *  check and generate OWL snippet to inverse properties
         */
        String inverseObjPropSnippet = "" ;
        
        if (dbu.getInversePredicate(this.DBconnection, objectProperty).length() > 0)
        {
            inverseObjPropSnippet = shu.setInverseObjectProperties(objectProperty,
                                        dbu.getInversePredicate(this.DBconnection, 
                                                                objectProperty
                                                               )
                                                                  ) ;
        }
        
        /**
         *   object property is not yet declared so its flag
         *   have also to be set to 'already_declared
         */
        objectPropDeclaration
            .append(shu.declareObjectproperty(objectProperty)) 
            .append(shu.setSubObjectPropertyOf(objectProperty, 
                    dbu.getSuperObjectProperty(this.DBconnection, objectProperty))) 
            .append(annotateObjectProperty(objectProperty))
            .append(inverseObjPropSnippet)
        ;
        
        return objectPropDeclaration.toString() ;
    
    }   //  end of method processObjectproperty()
    
    private String annotateObjectProperty(String objectProperty)
    {
        String annotation_type = dbu.getPredAnnType    (this.DBconnection, objectProperty) ;
        String language        = dbu.getPredAnnLanguage(this.DBconnection, objectProperty) ;
        String annotation      = dbu.getPredAnnotation (this.DBconnection, objectProperty) ;
        
        return shu.annotateObjectProperty(objectProperty ,
                                          annotation_type,
                                          language       ,
                                          annotation
                                         ) ; 
        
   }   //  end of method annotateObjectProperty() 
    
}   //  end of class WT_ProcessPredicates
