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; }  static String getDebugDataSql( DataQueryParams params ); static List<DimensionalItemObject> getByDataDimensionItemType( DataDimensionItemType itemType, List<DimensionalItemObject> dataDimensionOptions ); static Number getRoundedValue( DataQueryParams params, Integer decimals, Double value ); static Object getRoundedValueObject( DataQueryParams params, Object value ); static ColumnDataType getColumnType( ValueType valueType, boolean spatialSupport ); static Map<String, T> convertDxToOperand( Map<String, T> valueMap, TotalType totalType ); static Map<String, Double> getDoubleMap( Map<String, Object> map ); static Map<String, Object> getAggregatedDataValueMapping( Grid grid ); static DataValueSet getDataValueSetFromGrid( DataQueryParams params, Grid grid ); static void handleGridForDataValueSet( DataQueryParams params, Grid grid ); static Object getIntegerOrValue( Object value, DimensionalItemObject item ); static Map<String, DimensionalItemObject> getDimensionalItemObjectMap( DataQueryParams params ); static Map<String, String> getDimensionItemNameMap( DataQueryParams params ); static Map<String, MetadataItem> getDimensionMetadataItemMap( DataQueryParams params ); static Map<String, String> getCocNameMap( DataQueryParams params ); static Map<String, String> getUidDisplayPropertyMap( List<QueryItem> queryItems, DisplayProperty displayProperty ); static Map<String, String> getUidDisplayPropertyMap( List<DimensionalObject> dimensions, boolean hierarchyMeta, DisplayProperty displayProperty ); static boolean periodIsOutsideApprovalMaxYears( Integer year, Integer maxYears ); static int getLevelFromOrgUnitDimensionName( String dimensionName ); static boolean isTableLayout( List<String> columns, List<String> rows ); static Double calculateYearlyWeightedAverage( Double year1Value, Double year2Value, Double factor ); static Double getBaseMonth( PeriodType periodType ); static void throwIllegalQueryEx( ErrorCode errorCode, Object... args ); static boolean isPeriodInPeriods( String period, List<DimensionalItemObject> periods ); static List<DimensionalItemObject> findDimensionalItems( String dimensionIdentifier,
        List<DimensionalItemObject> items ); static boolean hasPeriod( List<Object> row, int periodIndex ); static final String ERR_MSG_TABLE_NOT_EXISTING; }
@Test public void testGetDataValueSetFromGridEmpty() { Grid grid = new ListGrid(); grid.addHeader( new GridHeader( DimensionalObject.DATA_X_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.ORGUNIT_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.PERIOD_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID ) ); grid.addHeader( new GridHeader( VALUE_ID, VALUE_HEADER_NAME, ValueType.NUMBER, Double.class.getName(), false, false ) ); DataValueSet dvs = AnalyticsUtils.getDataValueSetFromGrid( DataQueryParams.newBuilder().build(), grid ); assertNotNull( dvs ); assertNotNull( dvs.getDataValues() ); assertEquals( 0, dvs.getDataValues().size() ); }
@Test public void testGetDataValueSetFromGrid() { Grid grid = new ListGrid(); grid.addHeader( new GridHeader( DimensionalObject.DATA_X_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.ORGUNIT_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.PERIOD_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID ) ); grid.addHeader( new GridHeader( VALUE_ID, VALUE_HEADER_NAME, ValueType.NUMBER, Double.class.getName(), false, false ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxA", "ouA", "peA", "coA", "aoA", 1d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxA", "ouA", "peB", null, null, 2d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxA", "ouB", "peA", null, null, 3d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxA", "ouB", "peB", null, null, 4d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxB", "ouA", "peA", "coA", null, 5d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxB", "ouA", "peB", "coA", "aoB", 6d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxC", "ouA", "peA", null, "aoA", 7 ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxC", "ouA", "peB", null, null, 8d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxD", "ouA", "peA", "coB", null, 9d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxE", "ouA", "peB", null, null, 10 ) ); DataValueSet dvs = AnalyticsUtils.getDataValueSetFromGrid( DataQueryParams.newBuilder().build(), grid ); assertNotNull( dvs ); assertNotNull( dvs.getDataValues() ); assertEquals( 10, dvs.getDataValues().size() ); assertEquals( "dxA", dvs.getDataValues().get( 1 ).getDataElement() ); assertEquals( "ouA", dvs.getDataValues().get( 1 ).getOrgUnit() ); assertEquals( "peB", dvs.getDataValues().get( 1 ).getPeriod() ); assertNull( dvs.getDataValues().get( 1 ).getCategoryOptionCombo() ); assertNull( dvs.getDataValues().get( 1 ).getAttributeOptionCombo() ); assertEquals( "2.0", dvs.getDataValues().get( 1 ).getValue() ); assertEquals( "dxB", dvs.getDataValues().get( 4 ).getDataElement() ); assertEquals( "ouA", dvs.getDataValues().get( 4 ).getOrgUnit() ); assertEquals( "peA", dvs.getDataValues().get( 4 ).getPeriod() ); assertEquals( "coA", dvs.getDataValues().get( 4 ).getCategoryOptionCombo() ); assertNull( dvs.getDataValues().get( 4 ).getAttributeOptionCombo() ); assertEquals( "5.0", dvs.getDataValues().get( 4 ).getValue() ); assertEquals( "dxC", dvs.getDataValues().get( 6 ).getDataElement() ); assertEquals( "ouA", dvs.getDataValues().get( 6 ).getOrgUnit() ); assertEquals( "peA", dvs.getDataValues().get( 6 ).getPeriod() ); assertNull( dvs.getDataValues().get( 6 ).getCategoryOptionCombo() ); assertEquals( "aoA", dvs.getDataValues().get( 6 ).getAttributeOptionCombo() ); assertEquals( "7", dvs.getDataValues().get( 6 ).getValue() ); assertEquals( "dxD", dvs.getDataValues().get( 8 ).getDataElement() ); assertEquals( "ouA", dvs.getDataValues().get( 8 ).getOrgUnit() ); assertEquals( "peA", dvs.getDataValues().get( 8 ).getPeriod() ); assertEquals( "coB", dvs.getDataValues().get( 8 ).getCategoryOptionCombo() ); assertNull( dvs.getDataValues().get( 8 ).getAttributeOptionCombo() ); assertEquals( "9.0", dvs.getDataValues().get( 8 ).getValue() ); assertEquals( "dxE", dvs.getDataValues().get( 9 ).getDataElement() ); assertEquals( "ouA", dvs.getDataValues().get( 9 ).getOrgUnit() ); assertEquals( "peB", dvs.getDataValues().get( 9 ).getPeriod() ); assertNull( dvs.getDataValues().get( 9 ).getCategoryOptionCombo() ); assertNull( dvs.getDataValues().get( 9 ).getAttributeOptionCombo() ); assertEquals( "10", dvs.getDataValues().get( 9 ).getValue() ); }
@Test public void testGetDataValueSetFromGridWithDuplicates() { Grid grid = new ListGrid(); grid.addHeader( new GridHeader( DimensionalObject.DATA_X_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.ORGUNIT_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.PERIOD_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.CATEGORYOPTIONCOMBO_DIM_ID ) ); grid.addHeader( new GridHeader( DimensionalObject.ATTRIBUTEOPTIONCOMBO_DIM_ID ) ); grid.addHeader( new GridHeader( VALUE_ID, VALUE_HEADER_NAME, ValueType.NUMBER, Double.class.getName(), false, false ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxA", "ouA", "peA", null, null, 1d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxA", "ouA", "peB", null, null, 2d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxA", "ouA", "peB", null, null, 2d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxA", "ouB", "peA", null, null, 3d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxA", "ouB", "peB", null, null, 4d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxB", "ouA", "peA", null, null, 5d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxB", "ouA", "peA", null, null, 5d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxB", "ouA", "peB", null, null, 6d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxC", "ouA", "peA", null, null, 7d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxC", "ouA", "peA", null, null, 7d ) ); grid.addRow().addValuesAsList( Lists.newArrayList( "dxC", "ouA", "peB", null, null, 8d ) ); DataValueSet dvs = AnalyticsUtils.getDataValueSetFromGrid( DataQueryParams.newBuilder() .withDuplicatesOnly( true ).build(), grid ); assertNotNull( dvs ); assertNotNull( dvs.getDataValues() ); assertEquals( 3, dvs.getDataValues().size() ); }