Program: 73777
Project: 73777
Buggy source:
/** 
 * Generate grouping bindings and add into query definition.
 * @param query
 * @throws DataException
 */
protected void generateGroupBindings(BaseQueryDefinition query) throws DataException {
  SeriesDefinition baseSD=null;
  SeriesDefinition orthSD=null;
  Object[] orthAxisArray=null;
  if (fChartModel instanceof ChartWithAxes) {
    ChartWithAxes cwa=(ChartWithAxes)fChartModel;
    baseSD=(SeriesDefinition)cwa.getBaseAxes()[0].getSeriesDefinitions().get(0);
    orthAxisArray=cwa.getOrthogonalAxes(cwa.getBaseAxes()[0],true);
    orthSD=(SeriesDefinition)((Axis)orthAxisArray[0]).getSeriesDefinitions().get(0);
  }
 else   if (fChartModel instanceof ChartWithoutAxes) {
    ChartWithoutAxes cwoa=(ChartWithoutAxes)fChartModel;
    baseSD=(SeriesDefinition)cwoa.getSeriesDefinitions().get(0);
    orthSD=(SeriesDefinition)baseSD.getSeriesDefinitions().get(0);
  }
  GroupDefinition yGroupingDefinition=createOrthogonalGroupingDefinition(orthSD);
  if (yGroupingDefinition != null) {
    query.addGroup(yGroupingDefinition);
    if (ChartReportItemUtil.isBaseGroupingDefined(baseSD) && orthSD.isSetSorting() && orthSD.getSortKey() != null) {
      String sortKey=orthSD.getSortKey().getDefinition();
      String yGroupingExpr=orthSD.getQuery().getDefinition();
      if (sortKey != null && !yGroupingExpr.equals(sortKey)) {
        String name=generateUniqueBindingName(sortKey);
        Binding binding=new Binding(name);
        query.addBinding(binding);
        binding.setExpression(new ScriptExpression(sortKey));
        binding.setDataType(org.eclipse.birt.core.data.DataType.ANY_TYPE);
        binding.addAggregateOn(yGroupingDefinition.getName());
        String aggFunc=getAggFunExpr(sortKey,baseSD,orthAxisArray);
        binding.setAggrFunction(ChartReportItemUtil.convertToDtEAggFunction(aggFunc));
        SortDefinition sortDefinition=new SortDefinition();
        sortDefinition.setColumn(binding.getBindingName());
        sortDefinition.setExpression(ExpressionUtil.createRowExpression(binding.getBindingName()));
        sortDefinition.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(orthSD.getSorting()));
        yGroupingDefinition.addSort(sortDefinition);
      }
    }
  }
  GroupDefinition baseGroupDefinition=createBaseGroupingDefinition(baseSD);
  if (baseGroupDefinition != null) {
    query.addGroup(baseGroupDefinition);
  }
  GroupDefinition innerGroupDef=null;
  if (query.getGroups() != null && query.getGroups().size() > 0) {
    innerGroupDef=(GroupDefinition)query.getGroups().get(query.getGroups().size() - 1);
  }
  Map valueExprMap=new HashMap();
  if (fChartModel instanceof ChartWithAxes) {
    for (int i=0; i < orthAxisArray.length; i++) {
      addValueSeriesAggregateBindingForGrouping(query,((Axis)orthAxisArray[i]).getSeriesDefinitions(),baseGroupDefinition,valueExprMap,baseSD);
    }
  }
 else   if (fChartModel instanceof ChartWithoutAxes) {
    addValueSeriesAggregateBindingForGrouping(query,baseSD.getSeriesDefinitions(),baseGroupDefinition,valueExprMap,baseSD);
  }
  String baseSortExpr=getValidSortExpr(baseSD);
  if (baseSD.isSetSorting() && baseSortExpr != null) {
    if (ChartReportItemUtil.isBaseGroupingDefined(baseSD)) {
      String baseExpr=((Query)baseSD.getDesignTimeSeries().getDataDefinition().get(0)).getDefinition();
      if (baseExpr.equals(getValidSortExpr(baseSD))) {
        baseGroupDefinition.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(baseSD.getSorting()));
      }
 else {
        SortDefinition sd=new SortDefinition();
        sd.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(baseSD.getSorting()));
        String newValueSeriesExpr=(String)valueExprMap.get(baseSortExpr);
        if (newValueSeriesExpr != null) {
          baseSD.getSortKey().setDefinition(newValueSeriesExpr);
          sd.setExpression(newValueSeriesExpr);
        }
 else {
          sd.setExpression(baseSortExpr);
        }
        baseGroupDefinition.addSort(sd);
      }
    }
 else {
      SortDefinition sd=new SortDefinition();
      sd.setExpression(baseSortExpr);
      sd.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(baseSD.getSorting()));
      query.addSort(sd);
    }
  }
}

Fixed version (ground truth):
/** 
 * Generate grouping bindings and add into query definition.
 * @param query
 * @throws DataException
 */
protected void generateGroupBindings(BaseQueryDefinition query) throws DataException {
  SeriesDefinition baseSD=null;
  SeriesDefinition orthSD=null;
  Object[] orthAxisArray=null;
  if (fChartModel instanceof ChartWithAxes) {
    ChartWithAxes cwa=(ChartWithAxes)fChartModel;
    baseSD=(SeriesDefinition)cwa.getBaseAxes()[0].getSeriesDefinitions().get(0);
    orthAxisArray=cwa.getOrthogonalAxes(cwa.getBaseAxes()[0],true);
    orthSD=(SeriesDefinition)((Axis)orthAxisArray[0]).getSeriesDefinitions().get(0);
  }
 else   if (fChartModel instanceof ChartWithoutAxes) {
    ChartWithoutAxes cwoa=(ChartWithoutAxes)fChartModel;
    baseSD=(SeriesDefinition)cwoa.getSeriesDefinitions().get(0);
    orthSD=(SeriesDefinition)baseSD.getSeriesDefinitions().get(0);
  }
  GroupDefinition yGroupingDefinition=createOrthogonalGroupingDefinition(orthSD);
  if (yGroupingDefinition != null) {
    query.addGroup(yGroupingDefinition);
    if (ChartReportItemUtil.isBaseGroupingDefined(baseSD) && orthSD.isSetSorting() && orthSD.getSortKey() != null) {
      String sortKey=orthSD.getSortKey().getDefinition();
      String yGroupingExpr=orthSD.getQuery().getDefinition();
      if (sortKey != null && !yGroupingExpr.equals(sortKey)) {
        String name=generateUniqueBindingName(sortKey);
        Binding binding=new Binding(name);
        query.addBinding(binding);
        binding.setExpression(new ScriptExpression(sortKey));
        binding.setDataType(org.eclipse.birt.core.data.DataType.ANY_TYPE);
        binding.addAggregateOn(yGroupingDefinition.getName());
        String aggFunc=getAggFunExpr(sortKey,baseSD,orthAxisArray);
        binding.setAggrFunction(ChartReportItemUtil.convertToDtEAggFunction(aggFunc));
        SortDefinition sortDefinition=new SortDefinition();
        sortDefinition.setColumn(binding.getBindingName());
        sortDefinition.setExpression(ExpressionUtil.createRowExpression(binding.getBindingName()));
        sortDefinition.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(orthSD.getSorting()));
        yGroupingDefinition.addSort(sortDefinition);
      }
    }
  }
  GroupDefinition baseGroupDefinition=createBaseGroupingDefinition(baseSD);
  if (baseGroupDefinition != null) {
    query.addGroup(baseGroupDefinition);
  }
  GroupDefinition innerGroupDef=null;
  if (query.getGroups() != null && query.getGroups().size() > 0) {
    innerGroupDef=(GroupDefinition)query.getGroups().get(query.getGroups().size() - 1);
  }
  Map valueExprMap=new HashMap();
  if (fChartModel instanceof ChartWithAxes) {
    for (int i=0; i < orthAxisArray.length; i++) {
      addValueSeriesAggregateBindingForGrouping(query,((Axis)orthAxisArray[i]).getSeriesDefinitions(),innerGroupDef,valueExprMap,baseSD);
    }
  }
 else   if (fChartModel instanceof ChartWithoutAxes) {
    addValueSeriesAggregateBindingForGrouping(query,baseSD.getSeriesDefinitions(),innerGroupDef,valueExprMap,baseSD);
  }
  String baseSortExpr=getValidSortExpr(baseSD);
  if (baseSD.isSetSorting() && baseSortExpr != null) {
    if (ChartReportItemUtil.isBaseGroupingDefined(baseSD)) {
      String baseExpr=((Query)baseSD.getDesignTimeSeries().getDataDefinition().get(0)).getDefinition();
      if (baseExpr.equals(getValidSortExpr(baseSD))) {
        baseGroupDefinition.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(baseSD.getSorting()));
      }
 else {
        SortDefinition sd=new SortDefinition();
        sd.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(baseSD.getSorting()));
        String newValueSeriesExpr=(String)valueExprMap.get(baseSortExpr);
        if (newValueSeriesExpr != null) {
          baseSD.getSortKey().setDefinition(newValueSeriesExpr);
          sd.setExpression(newValueSeriesExpr);
        }
 else {
          sd.setExpression(baseSortExpr);
        }
        baseGroupDefinition.addSort(sd);
      }
    }
 else {
      SortDefinition sd=new SortDefinition();
      sd.setExpression(baseSortExpr);
      sd.setSortDirection(ChartReportItemUtil.convertToDtESortDirection(baseSD.getSorting()));
      query.addSort(sd);
    }
  }
}