package DBFOschemafy_V_3_001;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.sql.ResultSet;

/**
 *  <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 DButils
{
    private static final int columnType_BOOL                     = 1 ;
    private static final int columnType_INT                      = 2 ;
    private static final int columnType_STRING                   = 3 ;

    /**
     *  The OWL class individuals are with leading zeroes serial numbers suffixed.
     *  The <code>leadingZeroesInIndividualNameSuffix</code> defines the length the serial number.
     */
    private        final int leadingZeroesInIndividualNameSuffix = 5 ;  //  e.g.  actor_ID00012
    
    
    DButils()
    {
        ;
        
    }   //  end of constructor 
    
    public boolean DBupdate(Connection  DBconnection,
                            String      updateCommand
                           )
    {
        boolean returnValue = false ;
        
        try 
        {
            Statement stmt = DBconnection.createStatement() ;
            stmt.executeUpdate(updateCommand);

            stmt.close();
            
            returnValue = true ;
        }
        catch(SQLException SQLex)
        {
            System.out.println("Cannot update the database") ;
            System.out.println("SQLException: " + SQLex.getMessage());
            System.out.println("SQLState: "     + SQLex.getSQLState());
            System.out.println("VendorError: "  + SQLex.getErrorCode());

            SQLex.printStackTrace();
        }

        return returnValue ;
        
    }   //  end of method DBupdate

    public ResultSet establishResultSet(Connection conn,
                                        String     query
                                       )
    {
        ResultSet rs = null ;

        try 
        {
            Statement stmt = conn.createStatement() ;
            rs = (ResultSet)stmt.executeQuery(query) ;
        } 
        catch (SQLException ex) 
        {
            System.out.println("Cannot query the database") ;
            System.out.println("SQLException: " + ex.getMessage());
            System.out.println("SQLState: "     + ex.getSQLState());
            System.out.println("VendorError: "  + ex.getErrorCode());
            ex.printStackTrace();
        }

        return rs ;

    }   //  end of method establishResultSet()

    public String getObjectAnnotation(Connection  DBconnection,
                                      int         triplet_id
                                     )
    {
        String annotation = "" ; 

        String query =
            "SELECT * FROM CLASS_ANNOTATIONS " + 
            "WHERE (class_IRI = (SELECT object_IRI FROM TRIPLETS " + 
                    "WHERE (triplet_id = \"" + triplet_id + "\")) AND " +
                    "annotation_type_iri = \"obo:IAO_0000115\" AND " +
                    "language = \"en\")" ;
        
        ResultSet rs = establishResultSet(DBconnection, query) ;
        try
        {
            while (rs.next()) 
            {
                annotation = rs.getString("annotation") ;
            }
        
            rs.close() ;
        }
        catch(SQLException SQLex)
        {
            System.out.println("Cannot select the database") ;
            System.out.println("SQLException: " + SQLex.getMessage());
            System.out.println("SQLState: "     + SQLex.getSQLState());
            System.out.println("VendorError: "  + SQLex.getErrorCode());

            SQLex.printStackTrace();
        }
        
        return annotation ;
        
    }   //  end of method getObjectAnnotation()

    public String getSubjectAnnotation(Connection  DBconnection,
                                       int         triplet_id
                                      )
    {
        String annotation = "" ; 

        String query =
            "SELECT * FROM CLASS_ANNOTATIONS " + 
            "WHERE (class_IRI = (SELECT subject_IRI FROM TRIPLETS " + 
                   "WHERE (triplet_id = \"" + triplet_id + "\")) AND " +
                   "annotation_type_iri = \"obo:IAO_0000115\" AND " +
                   "language = \"en\")" ;
        
        ResultSet rs = establishResultSet(DBconnection, query) ;
        try
        {
            while (rs.next()) 
            {
                annotation = rs.getString("annotation") ;
            }
        
            /**
             *  closing the result set
             */
            rs.close() ;
        }
        catch(SQLException SQLex)
        {
            System.out.println("Cannot select the database") ;
            System.out.println("SQLException: " + SQLex.getMessage());
            System.out.println("SQLState: "     + SQLex.getSQLState());
            System.out.println("VendorError: "  + SQLex.getErrorCode());

            SQLex.printStackTrace();
        }
        
        return annotation ;
        
    }   //  end of method getSubjectAnnotation()

    public String getColumn(Connection  DBconnection,
                            String      tableName,
                            String      columnName,
                            int         columnType,
                            String      whereClause
                           )
    {
        String returnValue = "" ;
        
        ResultSet rs = establishResultSet(DBconnection, 
                       "SELECT " + columnName + " FROM " + tableName + 
                       " " + whereClause + " ;") ;
        try 
        {
            if (rs.next()) 
            {
                switch (columnType)
                {
                    case columnType_BOOL   : 
                        
                         if (rs.getBoolean(columnName) == true) 
                             returnValue = "true" ;
                         else
                             returnValue = "false" ;
                         break; 
                         
                    case columnType_INT    :
                         returnValue = Integer.toString(rs.getInt(columnName)) ;
                         break; 
                         
                    case columnType_STRING :
                         returnValue = rs.getString(columnName) ; 
                         break;
                         
                    default: throw new IllegalArgumentException() ;
                }
            }
            else
            {
                throw new IllegalArgumentException() ;
            }
        }
        catch(SQLException SQLex)
        {
            System.out.println("Cannot query the database") ;
            System.out.println("SQLException: " + SQLex.getMessage());
            System.out.println("SQLState: "     + SQLex.getSQLState());
            System.out.println("VendorError: "  + SQLex.getErrorCode());
            SQLex.printStackTrace();
        }
        catch(IllegalArgumentException ArgEx)
        {
            System.out.println("Field " + columnName + " does not exist in " + tableName + ".") ;
            ArgEx.printStackTrace();
        }

        return returnValue ;
        
    }   //  end of method getColumn()
    
    public String getInversePredicate(Connection  DBconnection,
                                      int         triplet_id
                                     )
    {
        String query = "WHERE (object_property_IRI = (SELECT predicate_IRI FROM TRIPLETS " +
                       "                              WHERE (triplet_id = " + 
                                                      triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OWL_INVERSE_OBJECT_PROPERTIES", //  table name
                         "inverse_object_property_IRI",   //  column name
                         columnType_STRING,               //  column type
                         query) ;
        
    }   //  end of method getInversePredicate()
    
    public String getInversePredicateAnnotation(Connection  DBconnection,
                                                int         triplet_id
                                               )
    {
        String invPredicate = getInversePredicate(DBconnection, triplet_id) ;
        
        if (invPredicate.length() == 0)
            return "" ;  //  there is no inverse predicate defined in the DB
        
        String query = "WHERE (object_property_IRI = \"" + invPredicate + "\") ;" ;
        return getColumn(DBconnection,
                         "OBJECT_PROPERTY_ANNOTATIONS", //  table name
                         "annotation",                  //  column name
                         columnType_STRING,             //  column type
                         query) ;
        
    }   //  end of method getInversePredicate()
    
    /**
     *  currently not used, however, can be useful in the future
     */
    public String getInverseSuperPredicate(Connection  DBconnection,
                                           int         triplet_id
                                          )
    {
        String query = "WHERE (inverse_object_property_IRI = (SELECT predicate_IRI FROM TRIPLETS " +
                       "                                      WHERE (triplet_id = " + 
                                                              triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OWL_SUPER_OBJECT_PROPERTIES", //  table name
                         "super_object_property_IRI",     //  column name
                         columnType_STRING,               //  column type
                         query) ;
        
    }   //  end of method getInverseSuperPredicate()

    public String getObjectIRI(Connection  DBconnection,
                               int         triplet_id
                              )
    {
        return getColumn(DBconnection,
                         "TRIPLETS",        //  table name
                         "object_iri",      //  column name
                         columnType_STRING, //  column type
                         "WHERE (triplet_id = " + triplet_id + ")") ;
        
    }   //  end of method getObjectIRI()

    /**
     *  currently not used, however, can be useful in the future
     */
    public String getPredAnnLanguage(Connection  DBconnection,
                                     int         triplet_id
                                    )
    {
        String query = "WHERE (object_property_IRI = (SELECT predicate_IRI FROM TRIPLETS " +
                       "                              WHERE (triplet_id = " + 
                                                      triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OBJECT_PROPERTY_ANNOTATIONS", //  table name
                         "language",         //  column name
                         columnType_STRING,             //  column type
                         query) ;
        
    }   //  end of method getPredAnnLanguage()
    
    public String getPredAnnotation(Connection  DBconnection,
                                    int         triplet_id
                                   )
    {
        String query = "WHERE (object_property_IRI = (SELECT predicate_IRI FROM TRIPLETS " +
                       "                              WHERE (triplet_id = " + 
                                                      triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OBJECT_PROPERTY_ANNOTATIONS", //  table name
                         "annotation",                  //  column name
                         columnType_STRING,             //  column type
                         query) ;
        
    }   //  end of method getPredAnnotation()
    
    /**
     *  currently not used, however, can be useful in the future
     */
    public String getPredAnnType(Connection  DBconnection,
                                 int         triplet_id
                                )
    {
        String query = "WHERE (object_property_IRI = (SELECT predicate_IRI FROM TRIPLETS " +
                       "                              WHERE (triplet_id = " + 
                                                      triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OBJECT_PROPERTY_ANNOTATIONS", //  table name
                         "annotation_type_IRI",         //  column name
                         columnType_STRING,             //  column type
                         query) ;
        
    }   //  end of method getPredAnnType()
    
    public String getPredicateIRI(Connection  DBconnection,
                                  int         triplet_id
                                 )
    {
        return getColumn(DBconnection,
                         "TRIPLETS",        //  table name
                         "predicate_iri",   //  column name
                         columnType_STRING, //  column type
                         "WHERE (triplet_id = " + triplet_id + ")") ;
        
    }   //  end of method getPredicateIRI()
    
    public String getSubjectIRI(Connection  DBconnection,
                                int         triplet_id
                               )
    {
        return getColumn(DBconnection,
                         "TRIPLETS",        //  table name
                         "subject_iri",     //  column name
                         columnType_STRING, //  column type
                         "WHERE (triplet_id = " + triplet_id + ")") ;
        
    }   //  end of method getSubjectIRI()

    /**
     *  currently not used, however, can be useful in the future
     */
    public String _getSuperObjectProperty(Connection  DBconnection,
                                          int         triplet_id
                                         )
    {
        String query = "WHERE (object_property_IRI = (SELECT predicate_IRI FROM TRIPLETS " +
                       "                              WHERE (triplet_id = " + 
                                                      triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OWL_SUPER_OBJECT_PROPERTIES", //  table name
                         "super_object_property_IRI",   //  column name
                         columnType_STRING,             //  column type
                         query) ;
        
    }   //  end of method getSuperObjectProperty()

    public void resetAllDBflags(Connection  DBconnection)
    {
        /*
         *  Set each individuals' counts to 0
         */
        String updateCommand = "UPDATE OWL_CLASSES SET class_individual_count = 0 ;" ;
        DBupdate(DBconnection, updateCommand ) ;
        
        /*
         *  Set each data property to unprocessed
         */
        updateCommand = "UPDATE OWL_DATA_PROPERTIES SET already_processed = 0 ;" ;
        DBupdate(DBconnection, updateCommand ) ;
        
    }   //  end of method resetAllDBflags()

    public int getInvidualCount(Connection  DBconnection,
                                String      class_IRI
                               )
    {
        return Integer.valueOf(getColumn(DBconnection,
                                         "OWL_CLASSES",              //  table name
                                         "class_individual_count",   //  column name
                                          columnType_INT,            //  column type
                                         "WHERE (class_IRI = \"" + class_IRI + "\")")
                              ) ;
        
    }   //  end of method getInvidualCount()

    public String produceSubjectIndividual(Connection  DBconnection,
                                           int         triplet_id
                                          )
    {
        String class_IRI = getSubjectIRI(DBconnection, triplet_id) ;
        
        int  individual_count = getInvidualCount(DBconnection, class_IRI) ;
        
        /**
         *  incrementing individual count 
         */
        individual_count++ ; 
        
        String updateCommand =         
        "UPDATE OWL_CLASSES SET class_individual_count = " + individual_count + " " +
        "WHERE (class_IRI = (SELECT subject_IRI FROM TRIPLETS " +
                                       "WHERE (triplet_id = " + triplet_id + ") ) )" ;
        /**
         *  incrementing individual count in the DB, as well
         */
        DBupdate(DBconnection, updateCommand ) ;
        
        /**
         *  re-read the individual count from the DB
         */
        individual_count = getInvidualCount(DBconnection, class_IRI) ;

        /**
         *  formatting the individual count from the DB
         */
        String individualName = class_IRI +
                                "_ID" + addLeadingZeros(individual_count,
                                                        this.leadingZeroesInIndividualNameSuffix) ;
        
        return  individualName.substring(0, 1).toLowerCase() + individualName.substring(1) ;
        
    }   //  end of method produceSubjectIndividual

    public String produceObjectIndividual(Connection  DBconnection,
                                          int         triplet_id
                                         )
    {
        String class_IRI = getObjectIRI(DBconnection, triplet_id) ;
        
        int  individual_count = getInvidualCount(DBconnection, class_IRI) ;
        
        /**
         *  incrementing individual count 
         */
        individual_count++ ; 
        
        String updateCommand =         
        "UPDATE OWL_CLASSES SET class_individual_count = " + individual_count + " " +
        "WHERE (class_IRI = (SELECT object_IRI FROM TRIPLETS " +
                                       "WHERE (triplet_id = " + triplet_id + ") ) )" ;
        /**
         *  incrementing individual count in the DB, as well
         */
        DBupdate(DBconnection, updateCommand ) ;
        
        /**
         *  re-read the individual count from the DB
         */
        individual_count = getInvidualCount(DBconnection, class_IRI) ;

        /**
         *  formatting the individual count from the DB
         */
        String individualName = class_IRI +
                                "_ID" + addLeadingZeros(individual_count,
                                                        this.leadingZeroesInIndividualNameSuffix) ;
        
        return  individualName.substring(0, 1).toLowerCase() + individualName.substring(1) ;
        
    }   //  end of method produceObjectIndividual

    private String addLeadingZeros(int num, int totalLengthOfString)
    {
        String numberString = Integer.toString(num);
        int    zerosToAdd   = totalLengthOfString - numberString.length();

        String zeros = Stream.generate(() -> "0").limit(zerosToAdd).collect(Collectors.joining());

        return zeros + numberString;

    }   //  end of method addLeadingZeros()
    
    public String produceTripletAnnotation(Connection  DBconnection,
                                           int         triplet_id
                                          )
    {
        String subject   = getSubjectIRI(DBconnection, triplet_id) ;
        String predicate = getPredicateIRI(DBconnection, triplet_id) ;
        String object    = getObjectIRI(DBconnection, triplet_id) ;
        
        StringBuilder tripletAnnotation = new StringBuilder()
            .append("Subject: "   + subject + 
                    " | "         + getSubjectAnnotation(DBconnection, triplet_id) + "\n")
                   
            .append("Object: "    + object + 
                    " | "         + getObjectAnnotation(DBconnection, triplet_id)  + "\n")

            .append("Predicate: " + predicate + 
                    " | "         + getPredAnnotation(DBconnection, triplet_id)    + "\n") ;

        String inversePredicate    = getInversePredicate(DBconnection, triplet_id) ;
        int    inversePredicateLen = getInversePredicate(DBconnection, triplet_id).length() ;
        if (inversePredicateLen > 0)
        {
            tripletAnnotation
            .append("Inverse predicate: " + inversePredicate + 
                    " | "       + getInversePredicateAnnotation(DBconnection, triplet_id) + "\n") ;
        }
        
        tripletAnnotation
            .append("Triplet-1 (S/P/O): " + subject + " - " + predicate        + " - " + object  + "\n") ;
        
        if (inversePredicateLen > 0)
        {
            tripletAnnotation
            .append("Triplet-2 (S/P/O): " + object  + " - " + inversePredicate + " - " + subject + "\n") ;        
        }
        
        return tripletAnnotation.toString() ;
        
    }   //  end of method produceTripletAnnotation()

    public String decodeBFOentityMeaning(Connection  DBconnection,
                                         String      bfo_entity_name
                                        ) 
    {
        String annotation = "unknown BFO entity name..." ;
     
        ResultSet rs = establishResultSet(DBconnection, 
                       "SELECT bfo_literal FROM BFO_ANNOTATIONS WHERE  " +
                       "bfo_entity_name = \"" + bfo_entity_name + "\" ;" ) ;
        try
        {
            if (rs.next()) 
            {
                annotation = rs.getString("bfo_literal") ;
            }
        
            rs.close() ;
        }
        catch(SQLException SQLex)
        {
            System.out.println("Cannot select the database") ;
            System.out.println("SQLException: " + SQLex.getMessage());
            System.out.println("SQLState: "     + SQLex.getSQLState());
            System.out.println("VendorError: "  + SQLex.getErrorCode());

            SQLex.printStackTrace();
        }

        return bfo_entity_name + " - " + annotation ;
    
    }   //  end of method decodeBFOentityMeaning()

    public String getInversePredicate(Connection  DBconnection,
                                      String      object_property_IRI
                                     )
    {
        String whereClause = "WHERE (object_property_IRI = \"" + object_property_IRI + "\") ;" ;
        
        return getColumn(DBconnection,                    //  active DB/connection
                         "OWL_INVERSE_OBJECT_PROPERTIES", //  table name
                         "inverse_object_property_IRI",   //  column name
                         columnType_STRING,               //  column type
                         whereClause) ;                   //  WHERE-clause
        
    }   //  end of method getInversePredicate()
    
    public String getSuperObjectProperty(Connection  DBconnection,
                                         String      object_property_IRI
                                        )
    {
        String whereClause = "WHERE (object_property_IRI = \"" + object_property_IRI + "\") ;" ;
        
        return getColumn(DBconnection,                    //  active DB/connection
                         "OWL_SUPER_OBJECT_PROPERTIES",   //  table name
                         "super_object_property_IRI",     //  column name
                         columnType_STRING,               //  column type
                         whereClause) ;                   //  WHERE-clause

    }   //  end of method getSuperObjectProperty()
    

    public String getPredAnnType(Connection  DBconnection,
                                 String      object_property_IRI
                                )
    {
        String whereClause = "WHERE (object_property_IRI = \"" + object_property_IRI + "\") ;" ;
        
        return getColumn(DBconnection,                    //  active DB/connection
                         "OBJECT_PROPERTY_ANNOTATIONS",   //  table name
                         "annotation_type_IRI",           //  column name
                         columnType_STRING,               //  column type
                         whereClause) ;                   //  WHERE-clause
    
    }   //  end of method object_property_IRI()                       
    
    public String getPredAnnLanguage(Connection  DBconnection,
                                     String      object_property_IRI
                                    )
    {
        String whereClause = "WHERE (object_property_IRI = \"" + object_property_IRI + "\") ;" ;
        
        return getColumn(DBconnection,                    //  active DB/connection
                         "OBJECT_PROPERTY_ANNOTATIONS",   //  table name
                         "language",                      //  column name
                         columnType_STRING,               //  column type
                         whereClause) ;                   //  WHERE-clause
    
    }   //  end of method object_property_IRI()                       
    
    public String getPredAnnotation(Connection  DBconnection,
                                    String      object_property_IRI
                                   )
    {
        String whereClause = "WHERE (object_property_IRI = \"" + object_property_IRI + "\") ;" ;
        
        return getColumn(DBconnection,                    //  active DB/connection
                         "OBJECT_PROPERTY_ANNOTATIONS",   //  table name
                         "annotation",                    //  column name
                         columnType_STRING,               //  column type
                         whereClause) ;                   //  WHERE-clause
    
    }   //  end of method object_property_IRI()                       

    public boolean isOWLclassAnnotated(Connection  DBconnection,
                                       String      dataProperty
                                      )
    {
        String query     = "WHERE (data_property_IRI = \"" + dataProperty + "\") ;" ;

        String annotated = getColumn(DBconnection,
                           "OWL_CLASSES",                //  table name
                           "annotated",                  //  column name
                           columnType_BOOL,              //  column type
                           query) ;

        if (annotated.equals("true"))
            return true ;
        else
            return false ;

    }   //  end of method isOWLclassAnnotated()

    public boolean isObjectPropertyAnnotated(Connection  DBconnection,
                                             String      dataProperty
                                            )
    {
        String query     = "WHERE (data_property_IRI = \"" + dataProperty + "\") ;" ;

        String annotated = getColumn(DBconnection,
                           "OWL_OBJECT_PROPERTIES",      //  table name
                           "annotated",                  //  column name
                           columnType_BOOL,              //  column type
                           query) ;

        if (annotated.equals("true"))
            return true ;
        else
            return false ;

    }   //  end of method isObjectPropertyAnnotated()

    public boolean isDataPropertyAnnotated(Connection  DBconnection,
                                           String      dataProperty
                                          )
    {
        String query     = "WHERE (data_property_IRI = \"" + dataProperty + "\") ;" ;

        String annotated = getColumn(DBconnection,
                           "OWL_DATA_PROPERTIES",        //  table name
                           "annotated",                  //  column name
                           columnType_BOOL,              //  column type
                           query) ;

        if (annotated.equals("true"))
            return true ;
        else
            return false ;

    }   //  end of method isDataPropertyAnnotated()

    public String getDataPropertyAnnotationType(Connection  DBconnection,
                                                String      dateProperty
                                               )
    {
        String query = "WHERE (data_property_IRI = \"" + dateProperty + "\") ;" ;
        return getColumn(DBconnection,
                         "DATA_PROPERTY_ANNOTATIONS",  //  table name
                         "annotation_type_IRI",        //  column name
                         columnType_STRING,            //  column type
                         query) ;

    }   //  end of method getDataPropertyAnnotationType()

    public String getDataPropertyAnnotationLanguage(Connection  DBconnection,
                                                    String      dateProperty
                                                   )
    {
        String query = "WHERE (data_property_IRI = \"" + dateProperty + "\") ;" ;
        return getColumn(DBconnection,
                         "DATA_PROPERTY_ANNOTATIONS",  //  table name
                         "language",                   //  column name
                         columnType_STRING,            //  column type
                         query) ;

    }   //  end of method getDataPropertyAnnotationLanguage()

    public String getDataPropertyAnnotation(Connection  DBconnection,
                                            String      dateProperty
                                           )
    {
        String query = "WHERE (data_property_IRI = \"" + dateProperty + "\") ;" ;
        return getColumn(DBconnection,
                         "DATA_PROPERTY_ANNOTATIONS",  //  table name
                         "annotation",                 //  column name
                         columnType_STRING,            //  column type
                         query) ;

    }   //  end of method getDataPropertyAnnotation()
    
    public String getSubjectDataProperty(Connection  DBconnection,
                                         int         triplet_id
                                        )
    {
        String query = "WHERE (triplet_id = " + triplet_id + ") ;" ;
        return getColumn(DBconnection,
                         "TRIPLETS",                      //  table name
                         "subject_data_property_IRI",     //  column name
                         columnType_STRING,               //  column type
                         query) ;

    }   //  end of method getSubjectDataProperty()
    
    public String getObjectDataProperty(Connection  DBconnection,
                                        int         triplet_id
                                       )
    {
        String query = "WHERE (triplet_id = " + triplet_id + ") ;" ;
        return getColumn(DBconnection,
                         "TRIPLETS",                    //  table name
                         "object_data_property_IRI",    //  column name
                         columnType_STRING,             //  column type
                         query) ;

    }   //  end of method getObjectDataProperty()

    public String getSubjectSuperDataProperty(Connection  DBconnection,
                                              int         triplet_id
                                             )
    {
        String query = "WHERE (data_property_IRI = (SELECT subject_data_property_IRI FROM TRIPLETS " +
                       "                            WHERE (triplet_id = " +
                                                           triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OWL_DATA_PROPERTIES",       //  table name
                         "super_data_property_IRI",   //  column name
                         columnType_STRING,           //  column type
                         query) ;

    }   //  end of method getSubjectSuperDataProperty()

    public String getObjectSuperDataProperty(Connection  DBconnection,
                                             int         triplet_id
                                             )
    {
        String query = "WHERE (data_property_IRI = (SELECT object_data_property_IRI FROM TRIPLETS " +
                       "                                 WHERE (triplet_id = " +
                                                         triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OWL_DATA_PROPERTIES",       //  table name
                         "super_data_property_IRI",   //  column name
                         columnType_STRING,           //  column type
                         query) ;

    }   //  end of method getObjectSuperDataProperty()

    public String getSubjectDataPropertyType(Connection  DBconnection,
                                             int         triplet_id
                                            )
    {
        String query = "WHERE (data_property_IRI = (SELECT subject_data_property_IRI FROM TRIPLETS " +
                       "                            WHERE (triplet_id = " +
                                                           triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OWL_DATA_PROPERTIES",    //  table name
                         "data_property_type_IRI", //  column name
                         columnType_STRING,        //  column type
                         query) ;

    }   //  end of method getSubjectDataPropertyType()

    public String getObjectDataPropertyType(Connection  DBconnection,
                                            int         triplet_id
                                            )
    {
        String query = "WHERE (data_property_IRI = (SELECT object_data_property_IRI FROM TRIPLETS " +
                       "                            WHERE (triplet_id = " +
                                                           triplet_id + ") ) ) ;" ;
        return getColumn(DBconnection,
                         "OWL_DATA_PROPERTIES",     //  table name
                         "data_property_type_IRI",  //  column name
                         columnType_STRING,         //  column type
                         query) ;

    }   //  end of method getObjectDataPropertyType()

    public String getSubjectDataPropertyValue(Connection  DBconnection,
                                              int         triplet_id
                                             )
    {
        String query = "WHERE (triplet_id = " + triplet_id + ") ;" ;
        return getColumn(DBconnection,
                         "TRIPLETS", //  table name
                         "subject_data_property_value",   //  column name
                         columnType_STRING,               //  column type
                         query) ;

    }   //  end of method getSubjectDataPropertyValue()

    public String getObjectDataPropertyValue(Connection  DBconnection,
                                             int         triplet_id
                                            )
    {
        String query = "WHERE (triplet_id = " + triplet_id + ") ;" ;
        return getColumn(DBconnection,
                         "TRIPLETS", //  table name
                         "object_data_property_value",   //  column name
                         columnType_STRING,              //  column type
                         query) ;

    }   //  end of method getObjectDataPropertyValue()
    
    public boolean isDatapropertyUnprocessed(Connection  DBconnection,
                                             String      data_property_IRI
                                            )
    {
        String query = "WHERE (data_property_IRI = \"" + data_property_IRI + "\") ;" ;
        String unprocessed = getColumn(DBconnection,
                                       "OWL_DATA_PROPERTIES", //  table name
                                       "already_processed",   //  column name
                                        columnType_BOOL,      //  column type
                                        query) ;
        if (unprocessed.equals("false"))
            return true ;
        else
            return false ;

    }   //  end of method getObjectDataPropertyValue()

    public void  DataPropertyUnprocessed(Connection  DBconnection,
                                         String      data_property_IRI
                                        )
    {
        /*
         *  Set each data property to unprocessed
         */
        String updateCommand = "UPDATE OWL_DATA_PROPERTIES SET already_processed = 0 " +
                               "WHERE (data_property_IRI = \"" + data_property_IRI + "\") ;" ;
        DBupdate(DBconnection, updateCommand ) ;
        
    }   //  end of method DataPropertyUnprocessed()


    public void  setDataPropertyProcessed(Connection  DBconnection,
                                          String      data_property_IRI
                                         )
    {
        /*
         *  Set each individuals' counts to 0
         */
        String updateCommand = "UPDATE OWL_DATA_PROPERTIES SET already_processed = 1 " +
                               "WHERE (data_property_IRI = \"" + data_property_IRI + "\") ;" ;
        DBupdate(DBconnection, updateCommand ) ;
        
    }   //  end of method setDataPropertyProcessed()
    
}   //  end of class DButils
