package model.weight;

import model.ModelDatabase;

import javax.swing.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * Created by Thagus on 24/09/16.
 */
public abstract class Weight {
    private String weightMethodName;
    private PreparedStatement stCalculateIDF;
    protected PreparedStatement stCalculateWeight, stCalculateQueryWeight;

    public Weight(String weightMethodName, Connection connection) throws SQLException {
        this.weightMethodName = weightMethodName;
        stCalculateIDF = connection.prepareStatement("MERGE INTO SPATIA.TERM(term,idf) " +
                "SELECT i.term, log(?/count(*)) as idf FROM SPATIA.INVERTEDINDEX i GROUP BY i.term");
    }

    public String getWeightMethodName() {
        return weightMethodName;
    }

    public void calculateWeights(){
        try{
            //Calculate IDF
            int numberOfDocuments = ModelDatabase.instance().opDocuments.countDocuments();
            stCalculateIDF.clearParameters();
            stCalculateIDF.setInt(1, numberOfDocuments);
            stCalculateIDF.executeUpdate();

            //Calculate weights
            stCalculateWeight.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.toString(), "Error calculating weights", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void calculateQueryWeights(){
        try{
            //Calculate weights
            stCalculateQueryWeight.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, e.toString(), "Error calculating query weights", JOptionPane.ERROR_MESSAGE);
        }
    }
}

@@ -841,32 +841,118 @@
 ulateWeights()%7B%0A
+        System.out.println(%22Calculating weights using: %22 + weightMethodName + %22...%22);%0A
         try%7B%0A   
@@ -1531,32 +1531,124 @@
 QueryWeights()%7B%0A
+        System.out.println(%22Calculating query weights using: %22 + weightMethodName + %22...%22);%0A
         try%7B%0A   

Debug messages