class AnalyticsUtils {

    public static DataValueSet getDataValueSetFromGrid( DataQueryParams params, Grid grid )
    {
        int dxInx = grid.getIndexOfHeader( DATA_X_DIM_ID );
        int peInx = grid.getIndexOfHeader( PERIOD_DIM_ID );
        int ouInx = grid.getIndexOfHeader( ORGUNIT_DIM_ID );
        int coInx = grid.getIndexOfHeader( CATEGORYOPTIONCOMBO_DIM_ID );
        int aoInx = grid.getIndexOfHeader( ATTRIBUTEOPTIONCOMBO_DIM_ID );
        int vlInx = grid.getHeaderWidth() - 1;

        Assert.isTrue( dxInx >= 0, "Data dimension index must be greater than or equal to zero" );
        Assert.isTrue( peInx >= 0, "Period dimension index must be greater than or equal to zero" );
        Assert.isTrue( ouInx >= 0, "Org unit dimension index must be greater than or equal to zero" );
        Assert.isTrue( coInx >= 0, "Category option combo dimension index must be greater than or equal to zero" );
        Assert.isTrue( aoInx >= 0, "Attribute option combo dimension index must be greater than or equal to zero" );
        Assert.isTrue( vlInx >= 0, "Value index must be greater than or equal to zero" );

        String created = DateUtils.getMediumDateString();

        DataValueSet dvs = new DataValueSet();

        Set<String> primaryKeys = Sets.newHashSet();

        for ( List<Object> row : grid.getRows() )
        {
            DataValue dv = new DataValue();

            Object coc = row.get( coInx );
            Object aoc = row.get( aoInx );

            dv.setDataElement( String.valueOf( row.get( dxInx ) ) );
            dv.setPeriod( String.valueOf( row.get( peInx ) ) );
            dv.setOrgUnit( String.valueOf( row.get( ouInx ) ) );
            dv.setCategoryOptionCombo( coc != null ? String.valueOf( coc ) : null );
            dv.setAttributeOptionCombo( aoc != null ? String.valueOf( aoc ) : null );
            dv.setValue( String.valueOf( row.get( vlInx ) ) );
            dv.setComment( KEY_AGG_VALUE );
            dv.setStoredBy( KEY_AGG_VALUE );
            dv.setCreated( created );
            dv.setLastUpdated( created );

            if ( !params.isDuplicatesOnly() || !primaryKeys.add( dv.getPrimaryKey() ) )
            {
                dvs.getDataValues().add( dv );
            }
        }

        return dvs;
    }

}