<commit_before>package pt.webdetails.cda.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import javax.swing.table.TableModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.reporting.engine.classic.core.util.TypedTableModel;
import pt.webdetails.cda.dataaccess.ColumnDefinition;
import pt.webdetails.cda.dataaccess.DataAccess;
import pt.webdetails.cda.dataaccess.DataAccessEnums;
import pt.webdetails.cda.dataaccess.Parameter;
import pt.webdetails.cda.query.QueryOptions;
import pt.webdetails.cda.utils.kettle.SortException;
import pt.webdetails.cda.utils.kettle.SortTableModel;

/**
 * Utility class to handle TableModel operations
 * <p/>
 * User: pedro
 * Date: Feb 4, 2010
 * Time: 12:31:54 PM
 */
public class TableModelUtils
{

  private static final Log logger = LogFactory.getLog(TableModelUtils.class);
  private static TableModelUtils _instance;


  public TableModelUtils()
  {
  }


  public TableModel postProcessTableModel(final DataAccess dataAccess,
          final QueryOptions queryOptions,
          final TableModel rawTableModel) throws SortException, InvalidOutputIndexException
  {
    
    if(rawTableModel == null){
      throw new IllegalArgumentException("Cannot process null table.");
    } else if (rawTableModel.getRowCount() == 0) {
      return rawTableModel;
    }

    // We will:
    //  1. Evaluate Calculated columns
    //  2. Show only the output columns we want;
    //  3. Sort
    //  3. Pagination

    // 1
    TableModel t;
    final ArrayList<ColumnDefinition> columnDefinitions = dataAccess.getCalculatedColumns();
    if (columnDefinitions.isEmpty())
    {
      t = rawTableModel;
    }
    else
    {
      t = new CalculatedTableModel(rawTableModel, columnDefinitions.toArray(new ColumnDefinition[columnDefinitions.size()]));
    }

    // First we need to check if there's nothing to do.
    ArrayList<Integer> outputIndexes = dataAccess.getOutputs(queryOptions.getOutputIndexId());
    if(outputIndexes == null) {
      throw new InvalidOutputIndexException("Invalid outputIndexId: " + queryOptions.getOutputIndexId(), null);
    }
    /*
    if (queryOptions.isPaginate() == false && outputIndexes.isEmpty() && queryOptions.getSortBy().isEmpty())
    {
    // No, the original one is good enough
    return t;
    }<commit_msg>format tablemodel also on empty result<commit_after>package pt.webdetails.cda.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import javax.swing.table.TableModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.pentaho.reporting.engine.classic.core.util.TypedTableModel;
import pt.webdetails.cda.dataaccess.ColumnDefinition;
import pt.webdetails.cda.dataaccess.DataAccess;
import pt.webdetails.cda.dataaccess.DataAccessEnums;
import pt.webdetails.cda.dataaccess.Parameter;
import pt.webdetails.cda.query.QueryOptions;
import pt.webdetails.cda.utils.kettle.SortException;
import pt.webdetails.cda.utils.kettle.SortTableModel;

/**
 * Utility class to handle TableModel operations
 * <p/>
 * User: pedro
 * Date: Feb 4, 2010
 * Time: 12:31:54 PM
 */
public class TableModelUtils
{

  private static final Log logger = LogFactory.getLog(TableModelUtils.class);
  private static TableModelUtils _instance;


  public TableModelUtils()
  {
  }


  public TableModel postProcessTableModel(final DataAccess dataAccess,
          final QueryOptions queryOptions,
          final TableModel rawTableModel) throws SortException, InvalidOutputIndexException
  {
    
    if(rawTableModel == null){
      throw new IllegalArgumentException("Cannot process null table.");
    } 
	
    // We will:
    //  1. Evaluate Calculated columns
    //  2. Show only the output columns we want;
    //  3. Sort
    //  3. Pagination

    // 1
    TableModel t;
    final ArrayList<ColumnDefinition> columnDefinitions = dataAccess.getCalculatedColumns();
    if (columnDefinitions.isEmpty())
    {
      t = rawTableModel;
    }
    else
    {
      t = new CalculatedTableModel(rawTableModel, columnDefinitions.toArray(new ColumnDefinition[columnDefinitions.size()]));
    }

    // First we need to check if there's nothing to do.
    ArrayList<Integer> outputIndexes = dataAccess.getOutputs(queryOptions.getOutputIndexId());
    if(outputIndexes == null) {
      throw new InvalidOutputIndexException("Invalid outputIndexId: " + queryOptions.getOutputIndexId(), null);
    }
    /*
    if (queryOptions.isPaginate() == false && outputIndexes.isEmpty() && queryOptions.getSortBy().isEmpty())
    {
    // No, the original one is good enough
    return t;
    }
{'fraction_non_alphanumeric': 0.10097581671616461, 'fraction_numerical': 0.007000424268137463, 'mean_word_length': 3.80142566191446, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2360973', 'n_tokens_mistral': 1411, 'n_tokens_neox': 1351, 'n_words': 317}