<commit_before>package org.dashbuilder.client.uftable;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Style;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import org.dashbuilder.client.dataset.DataSetReadyCallback;
import org.dashbuilder.client.displayer.AbstractDataViewer;
import org.dashbuilder.model.dataset.ColumnType;
import org.dashbuilder.model.dataset.DataColumn;
import org.dashbuilder.model.dataset.DataSet;

import org.dashbuilder.model.dataset.sort.SortOrder;
import org.dashbuilder.model.displayer.DataDisplayerColumn;
import org.uberfire.client.tables.PagedTable;

import static com.google.gwt.dom.client.BrowserEvents.CLICK;

public class UFTableViewer extends AbstractDataViewer<org.dashbuilder.model.displayer.TableDisplayer> {

    private static  final String[] COLOR_ARRAY = new String[] {
            "red", "blue", "yellow", "green", "orange", "brown", "magenta", "lime", "gold",
            "indigo", "pink", "purple", "aqua", "salmon", "coral", "fuchsia", "silver"
    };

    private int colorIndex = 0;
    private Map<String, String> columnColorMap = new HashMap<String, String>( 5 );

    private Map< String, List<KeyValue<String, Integer>> > columnCellSelections =
            new HashMap< String, List<KeyValue<String, Integer>> >(5);

    protected int pageSize = 20;
    protected int numberOfRows = 0;
    protected int dataSetLowerLimit = 0;
    protected int dataSetUpperLimit = 0;
    protected String lastOrderedColumn;
    protected SortOrder lastSortOrder;

    protected boolean drawn = false;

    protected FlowPanel panel = new FlowPanel();
    protected Label label = new Label();
    protected PagedTable<UFTableRow> table;

    protected DataSet dataSet;

    protected UFTableDataProvider dataProvider = new UFTableDataProvider( this );

    public UFTableViewer() {
        initWidget( panel );
    }

    public void draw() {
        if ( !drawn ) {
            drawn = true;

            if ( dataDisplayer == null ) {
                displayMessage( "ERROR: DataDisplayer property not set" );
            } else if ( dataSetHandler == null ) {
                displayMessage( "ERROR: DataSetHandler property not set" );
            } else {
                try {
                    displayMessage( "Initializing '" + dataDisplayer.getTitle() + "'..." );

                    lookupDataSet(
                            0,
                            pageSize,
                            new DataSetReadyCallback() {
                                public void callback( DataSet result ) {
                                    Widget w = createWidget();
                                    panel.clear();
                                    panel.add( w );
                                }

                                public void notFound() {
                                    displayMessage( "ERROR: Data set not found." );
                                }
                            }
                    );
                } catch ( Exception e ) {
                    displayMessage( "ERROR: " + e.getMessage() );
                }
            }
        }
    }

    /**
     * Just reload the data set and make the current google Viewer to redraw.
     */
    public void redraw() {
        doLookupDataSet( 0, pageSize, lastOrderedColumn, lastSortOrder, new DataSetReadyCallback() {
            @Override
            public void callback( DataSet dataSet ) {
                UFTableViewer.this.dataSet = dataSet;
                numberOfRows = dataSet.getRowCountNonTrimmed();
                table.setRowCount( numberOfRows, true );
                table.redraw();
            }

            @Override
            public void notFound() {
                displayMessage( "ERROR: Data set not found." );
            }
        } );
    }

    /**
     * Clear the current display and show a notification message.
     */
    public void displayMessage( String msg ) {
        panel.clear();
        panel.add( label );
        label.setText( msg );
    }

    protected Widget createWidget() {
        pageSize = dataDisplayer.getPageSize();
        numberOfRows = dataSet.getRowCountNonTrimmed();

        final PagedTable<UFTableRow> table = createUFTable();

        final String defaultSortColumn = dataDisplayer.getDefaultSortColumnId();

        // TODO get the table to show the order icon programatically
        if ( defaultSortColumn != null && !"".equals( defaultSortColumn ) ) {
            lookupDataSet(
                    defaultSortColumn,
                    dataDisplayer.getDefaultSortOrder(),
                    new DataSetReadyCallback() {
                        @Override
                        public void callback( DataSet dataSet ) {
                            lastOrderedColumn = defaultSortColumn;
                            lastSortOrder = dataDisplayer.getDefaultSortOrder();
                            table.redraw();
                        }

                        @Override
                        public void notFound() {
                            displayMessage( "ERROR in data lookup." );
                        }
                    }
            );
        }

        this.table = table;
        dataProvider.addDataDisplay( table );

        HTML titleHtml = new HTML();
        if ( dataDisplayer.isTitleVisible() ) {
            titleHtml.setText( dataDisplayer.getTitle() );
        }

        VerticalPanel verticalPanel = new VerticalPanel();
        verticalPanel.add( titleHtml );
        verticalPanel.add( table );
        return verticalPanel;
    }

    protected PagedTable<UFTableRow> createUFTable() {

        final PagedTable<UFTableRow> ufPagedTable = new PagedTable<UFTableRow>( pageSize );<commit_msg>Change to avoid nested dataset lookups.<commit_after>package org.dashbuilder.client.uftable;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.google.gwt.cell.client.DateCell;
import com.google.gwt.cell.client.NumberCell;
import com.google.gwt.cell.client.TextCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.dom.client.Style;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import org.dashbuilder.client.dataset.DataSetReadyCallback;
import org.dashbuilder.client.displayer.AbstractDataViewer;
import org.dashbuilder.model.dataset.ColumnType;
import org.dashbuilder.model.dataset.DataColumn;
import org.dashbuilder.model.dataset.DataSet;

import org.dashbuilder.model.dataset.sort.SortOrder;
import org.dashbuilder.model.displayer.DataDisplayerColumn;
import org.uberfire.client.tables.PagedTable;

import static com.google.gwt.dom.client.BrowserEvents.CLICK;

public class UFTableViewer extends AbstractDataViewer<org.dashbuilder.model.displayer.TableDisplayer> {

    private static  final String[] COLOR_ARRAY = new String[] {
            "red", "blue", "yellow", "green", "orange", "brown", "magenta", "lime", "gold",
            "indigo", "pink", "purple", "aqua", "salmon", "coral", "fuchsia", "silver"
    };

    private int colorIndex = 0;
    private Map<String, String> columnColorMap = new HashMap<String, String>( 5 );

    private Map< String, List<KeyValue<String, Integer>> > columnCellSelections =
            new HashMap< String, List<KeyValue<String, Integer>> >(5);

    protected int pageSize = 10;
    protected int numberOfRows = 0;
    protected int dataSetLowerLimit = 0;
    protected int dataSetUpperLimit = 0;
    protected String lastOrderedColumn;
    protected SortOrder lastSortOrder;

    protected boolean drawn = false;

    protected FlowPanel panel = new FlowPanel();
    protected Label label = new Label();
    protected PagedTable<UFTableRow> table;

    protected DataSet dataSet;

    protected UFTableDataProvider dataProvider = new UFTableDataProvider( this );

    public UFTableViewer() {
        initWidget( panel );
    }

    public void draw() {
        if ( !drawn ) {
            drawn = true;

            if ( dataDisplayer == null ) {
                displayMessage( "ERROR: DataDisplayer property not set" );
            } else if ( dataSetHandler == null ) {
                displayMessage( "ERROR: DataSetHandler property not set" );
            } else {
                try {
                    displayMessage( "Initializing '" + dataDisplayer.getTitle() + "'..." );

                    dataSetLowerLimit = 0;
                    dataSetUpperLimit = pageSize;

                    DataSetReadyCallback callback =
                            new DataSetReadyCallback() {
                                @Override
                                public void callback( DataSet dataSet ) {
                                    Widget w = createWidget();
                                    panel.clear();
                                    panel.add( w );
                                }

                                @Override
                                public void notFound() {
                                    displayMessage( "ERROR: Data set not found." );
                                }
                            };

                    final String defaultSortColumn = dataDisplayer.getDefaultSortColumnId();
                    if ( defaultSortColumn != null && !"".equals( defaultSortColumn ) ) {
                        lastOrderedColumn = defaultSortColumn;
                        lastSortOrder = dataDisplayer.getDefaultSortOrder();
                        doLookupDataSet(
                                dataSetLowerLimit,
                                dataSetUpperLimit,
                                defaultSortColumn,
                                dataDisplayer.getDefaultSortOrder(),
                                callback );
                    } else {
                        lookupDataSet( dataSetLowerLimit, dataSetUpperLimit, callback );
                    }
                } catch ( Exception e ) {
                    displayMessage( "ERROR: " + e.getMessage() );
                }
            }
        }
    }

    /**
     * Just reload the data set and make the current google Viewer to redraw.
     */
    public void redraw() {
        doLookupDataSet( 0, pageSize, lastOrderedColumn, lastSortOrder, new DataSetReadyCallback() {
            @Override
            public void callback( DataSet dataSet ) {
                UFTableViewer.this.dataSet = dataSet;
                numberOfRows = dataSet.getRowCountNonTrimmed();
                table.setRowCount( numberOfRows, true );
                table.redraw();
            }

            @Override
            public void notFound() {
                displayMessage( "ERROR: Data set not found." );
            }
        } );
    }

    /**
     * Clear the current display and show a notification message.
     */
    public void displayMessage( String msg ) {
        panel.clear();
        panel.add( label );
        label.setText( msg );
    }

    protected Widget createWidget() {
        pageSize = dataDisplayer.getPageSize();
        numberOfRows = dataSet.getRowCountNonTrimmed();

        this.table = createUFTable();
        dataProvider.addDataDisplay( table );

        HTML titleHtml = new HTML();
        if ( dataDisplayer.isTitleVisible() ) {
            titleHtml.setText( dataDisplayer.getTitle() );
        }

        VerticalPanel verticalPanel = new VerticalPanel();
        verticalPanel.add( titleHtml );
        verticalPanel.add( table );
        return verticalPanel;
    }

    protected PagedTable<UFTableRow> createUFTable() {

        final PagedTable<UFTableRow> ufPagedTable = new PagedTable<UFTableRow>( pageSize );
{'fraction_non_alphanumeric': 0.09577614317130508, 'fraction_numerical': 0.002123303253203913, 'mean_word_length': 1.964261631827377, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7666695', 'n_tokens_mistral': 3551, 'n_tokens_neox': 3409, 'n_words': 766}