PeriodOffsetUtils { public static List<Object> getPeriodOffsetRow( Grid grid, DimensionalItemObject dimItem, String isoPeriod, int offset ) { if ( grid == null || dimItem == null ) { return null; } BiFunction<Integer, Integer, Integer> replaceIndexIfMissing = (Integer index, Integer defaultIndex ) -> index == -1 ? defaultIndex : index; final int dataIndex = replaceIndexIfMissing.apply( grid.getIndexOfHeader( DATA_X_DIM_ID ), 0 ); final int periodIndex = replaceIndexIfMissing.apply( grid.getIndexOfHeader( PERIOD_DIM_ID ), 1 ); Period shifted = offset != 0 ? shiftPeriod( PeriodType.getPeriodFromIsoString( isoPeriod ), offset ) : PeriodType.getPeriodFromIsoString( isoPeriod ); for ( List<Object> row : grid.getRows() ) { final String rowUid = (String) row.get( dataIndex ); final String rowPeriod = (String) row.get( periodIndex ); if ( rowUid.equals( dimItem.getUid() ) && rowPeriod.equals( shifted.getIsoDate() ) ) { return row; } } return null; }  static ListMap<String, DimensionalItemObject> getPeriodTypePeriodMap( DataQueryParams params ); static Period shiftPeriod( Period period, int periodOffset ); static DataQueryParams removeOffsetPeriodsIfNotNeeded( DataQueryParams params ); static List<Object> getPeriodOffsetRow( Grid grid, DimensionalItemObject dimItem, String isoPeriod,
        int offset );  }
@Test public void verifyGetPeriodOffsetRow() { 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.addRow(); grid.addValue( "de1" ); grid.addValue( "ou2" ); grid.addValue( "202001" ); grid.addValue( 3 ); grid.addRow(); grid.addValue( "de1" ); grid.addValue( "ou3" ); grid.addValue( "202002" ); grid.addValue( 5 ); DataElement dataElement = createDataElement( 0 ); dataElement.setUid( "de1" ); final List<Object> row = PeriodOffsetUtils.getPeriodOffsetRow( grid, dataElement, "202001", 1 ); assertThat( row, is( notNullValue() ) ); assertThat( row, hasSize( 4 ) ); assertThat( row.get( 0 ), is( "de1" ) ); }
@Test public void verifyGetPeriodOffsetRowNoMatch() { 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.addRow(); grid.addValue( "de1" ); grid.addValue( "ou2" ); grid.addValue( "202001" ); grid.addValue( 3 ); grid.addRow(); grid.addValue( "de1" ); grid.addValue( "ou3" ); grid.addValue( "202002" ); grid.addValue( 5 ); DataElement dataElement = createDataElement( 0 ); dataElement.setUid( "de1" ); final List<Object> row = PeriodOffsetUtils.getPeriodOffsetRow( grid, dataElement, "202003", 1 ); assertThat( row, is( nullValue() ) ); }