Program: 73013
Project: 73013
Buggy source:
public static ICubeQueryDefinition createCubeQuery(CrosstabReportItemHandle crosstabItem,IDataQueryDefinition parentQuery,boolean needMeasure,boolean needRowDimension,boolean needColumnDimension,boolean needBinding,boolean needSorting,boolean needFilter) throws BirtException {
  ICubeQueryDefinition cubeQuery=getCubeElementFactory().createCubeQuery(crosstabItem.getCubeName());
  List rowLevelNameList=new ArrayList();
  List columnLevelNameList=new ArrayList();
  List levelViewList=new ArrayList();
  Map levelMap=new HashMap();
  if (needMeasure) {
    for (int i=0; i < crosstabItem.getMeasureCount(); i++) {
      MeasureViewHandle mv=crosstabItem.getMeasure(i);
      if (mv.getCubeMeasure() == null) {
        throw new CrosstabException(Messages.getString("String_Node_Str",mv.getCubeMeasureName()));
      }
      IMeasureDefinition mDef=cubeQuery.createMeasure(mv.getCubeMeasure().getName());
      mDef.setAggrFunction(mv.getCubeMeasure().getFunction() == null ? null : DataAdapterUtil.getRollUpAggregationName(mv.getCubeMeasure().getFunction()));
      Iterator mfitr=mv.filtersIterator();
      if (mfitr != null) {
        while (mfitr.hasNext()) {
          FilterConditionElementHandle filterCon=(FilterConditionElementHandle)mfitr.next();
          ConditionalExpression filterCondExpr;
          if (ModuleUtil.isListFilterValue(filterCon)) {
            filterCondExpr=new ConditionalExpression(filterCon.getExpr(),DataAdapterUtil.adaptModelFilterOperator(filterCon.getOperator()),filterCon.getValue1List());
          }
 else {
            filterCondExpr=new ConditionalExpression(filterCon.getExpr(),DataAdapterUtil.adaptModelFilterOperator(filterCon.getOperator()),filterCon.getValue1(),filterCon.getValue2());
          }
          ICubeFilterDefinition filterDef=getCubeElementFactory().creatCubeFilterDefinition(filterCondExpr,null,null,null);
          cubeQuery.addFilter(filterDef);
        }
      }
    }
  }
  if (needRowDimension && crosstabItem.getDimensionCount(ROW_AXIS_TYPE) > 0) {
    IEdgeDefinition rowEdge=cubeQuery.createEdge(ICubeQueryDefinition.ROW_EDGE);
    LevelHandle mirrorLevel=crosstabItem.getCrosstabView(ROW_AXIS_TYPE).getMirroredStartingLevel();
    for (int i=0; i < crosstabItem.getDimensionCount(ROW_AXIS_TYPE); i++) {
      DimensionViewHandle dv=crosstabItem.getDimension(ROW_AXIS_TYPE,i);
      if (dv.getCubeDimension() == null) {
        throw new CrosstabException(Messages.getString("String_Node_Str",dv.getCubeDimensionName()));
      }
      IDimensionDefinition dimDef=rowEdge.createDimension(dv.getCubeDimension().getName());
      IHierarchyDefinition hieDef=dimDef.createHierarchy(dv.getCubeDimension().getDefaultHierarchy().getName());
      for (int j=0; j < dv.getLevelCount(); j++) {
        LevelViewHandle lv=dv.getLevel(j);
        if (lv.getCubeLevel() == null) {
          throw new CrosstabException(Messages.getString("String_Node_Str",lv.getCubeLevelName()));
        }
        ILevelDefinition levelDef=hieDef.createLevel(lv.getCubeLevel().getName());
        rowLevelNameList.add(lv.getCubeLevel().getFullName());
        if (mirrorLevel != null && mirrorLevel.getQualifiedName().equals(lv.getCubeLevelName())) {
          rowEdge.setMirrorStartingLevel(levelDef);
        }
        levelViewList.add(lv);
        levelMap.put(lv.getCubeLevel(),levelDef);
      }
    }
  }
  if (needColumnDimension && crosstabItem.getDimensionCount(COLUMN_AXIS_TYPE) > 0) {
    IEdgeDefinition columnEdge=cubeQuery.createEdge(ICubeQueryDefinition.COLUMN_EDGE);
    LevelHandle mirrorLevel=crosstabItem.getCrosstabView(COLUMN_AXIS_TYPE).getMirroredStartingLevel();
    for (int i=0; i < crosstabItem.getDimensionCount(COLUMN_AXIS_TYPE); i++) {
      DimensionViewHandle dv=crosstabItem.getDimension(COLUMN_AXIS_TYPE,i);
      if (dv.getCubeDimension() == null) {
        throw new CrosstabException(Messages.getString("String_Node_Str",dv.getCubeDimensionName()));
      }
      IDimensionDefinition dimDef=columnEdge.createDimension(dv.getCubeDimension().getName());
      IHierarchyDefinition hieDef=dimDef.createHierarchy(dv.getCubeDimension().getDefaultHierarchy().getName());
      for (int j=0; j < dv.getLevelCount(); j++) {
        LevelViewHandle lv=dv.getLevel(j);
        if (lv.getCubeLevel() == null) {
          throw new CrosstabException(Messages.getString("String_Node_Str",lv.getCubeLevelName()));
        }
        ILevelDefinition levelDef=hieDef.createLevel(lv.getCubeLevel().getName());
        columnLevelNameList.add(lv.getCubeLevel().getFullName());
        if (mirrorLevel != null && mirrorLevel.getQualifiedName().equals(lv.getCubeLevelName())) {
          columnEdge.setMirrorStartingLevel(levelDef);
        }
        levelViewList.add(lv);
        levelMap.put(lv.getCubeLevel(),levelDef);
      }
    }
  }
  if (needSorting) {
    addLevelSorting(levelViewList,levelMap,cubeQuery);
  }
  if (needFilter) {
    addLevelFilter(levelViewList,levelMap,cubeQuery);
  }
  if (needBinding) {
    Iterator bindingItr=((ExtendedItemHandle)crosstabItem.getModelHandle()).columnBindingsIterator();
    ModuleHandle module=((ExtendedItemHandle)crosstabItem.getModelHandle()).getModuleHandle();
    if (bindingItr != null) {
      Map cache=new HashMap();
      while (bindingItr.hasNext()) {
        ComputedColumnHandle column=(ComputedColumnHandle)bindingItr.next();
        Binding binding=new Binding(column.getName());
        binding.setAggrFunction(column.getAggregateFunction() == null ? null : DataAdapterUtil.adaptModelAggregationType(column.getAggregateFunction()));
        binding.setExpression(new ScriptExpression(column.getExpression()));
        binding.setDataType(DataAdapterUtil.adaptModelDataType(column.getDataType()));
        if (column.getFilterExpression() != null) {
          binding.setFilter(new ScriptExpression(column.getFilterExpression()));
        }
        for (Iterator argItr=column.argumentsIterator(); argItr.hasNext(); ) {
          AggregationArgumentHandle aah=(AggregationArgumentHandle)argItr.next();
          binding.addArgument(new ScriptExpression(aah.getValue()));
        }
        List aggrList=column.getAggregateOnList();
        if (aggrList != null) {
          for (Iterator aggrItr=aggrList.iterator(); aggrItr.hasNext(); ) {
            String baseLevel=(String)aggrItr.next();
            CrosstabUtil.addHierachyAggregateOn(module,binding,baseLevel,rowLevelNameList,columnLevelNameList,cache);
          }
        }
        cubeQuery.addBinding(binding);
      }
    }
  }
  return cubeQuery;
}

Fixed version (ground truth):
public static ICubeQueryDefinition createCubeQuery(CrosstabReportItemHandle crosstabItem,IDataQueryDefinition parentQuery,boolean needMeasure,boolean needRowDimension,boolean needColumnDimension,boolean needBinding,boolean needSorting,boolean needFilter) throws BirtException {
  ICubeQueryDefinition cubeQuery=getCubeElementFactory().createCubeQuery(crosstabItem.getCubeName());
  List rowLevelNameList=new ArrayList();
  List columnLevelNameList=new ArrayList();
  List levelViewList=new ArrayList();
  Map levelMap=new HashMap();
  if (needMeasure) {
    for (int i=0; i < crosstabItem.getMeasureCount(); i++) {
      MeasureViewHandle mv=crosstabItem.getMeasure(i);
      if (mv instanceof ComputedMeasureViewHandle) {
        continue;
      }
      if (mv.getCubeMeasure() == null) {
        throw new CrosstabException(Messages.getString("String_Node_Str",mv.getCubeMeasureName()));
      }
      IMeasureDefinition mDef=cubeQuery.createMeasure(mv.getCubeMeasure().getName());
      mDef.setAggrFunction(mv.getCubeMeasure().getFunction() == null ? null : DataAdapterUtil.getRollUpAggregationName(mv.getCubeMeasure().getFunction()));
      Iterator mfitr=mv.filtersIterator();
      if (mfitr != null) {
        while (mfitr.hasNext()) {
          FilterConditionElementHandle filterCon=(FilterConditionElementHandle)mfitr.next();
          ConditionalExpression filterCondExpr;
          if (ModuleUtil.isListFilterValue(filterCon)) {
            filterCondExpr=new ConditionalExpression(filterCon.getExpr(),DataAdapterUtil.adaptModelFilterOperator(filterCon.getOperator()),filterCon.getValue1List());
          }
 else {
            filterCondExpr=new ConditionalExpression(filterCon.getExpr(),DataAdapterUtil.adaptModelFilterOperator(filterCon.getOperator()),filterCon.getValue1(),filterCon.getValue2());
          }
          ICubeFilterDefinition filterDef=getCubeElementFactory().creatCubeFilterDefinition(filterCondExpr,null,null,null);
          cubeQuery.addFilter(filterDef);
        }
      }
    }
  }
  if (needRowDimension && crosstabItem.getDimensionCount(ROW_AXIS_TYPE) > 0) {
    IEdgeDefinition rowEdge=cubeQuery.createEdge(ICubeQueryDefinition.ROW_EDGE);
    LevelHandle mirrorLevel=crosstabItem.getCrosstabView(ROW_AXIS_TYPE).getMirroredStartingLevel();
    for (int i=0; i < crosstabItem.getDimensionCount(ROW_AXIS_TYPE); i++) {
      DimensionViewHandle dv=crosstabItem.getDimension(ROW_AXIS_TYPE,i);
      if (dv.getCubeDimension() == null) {
        throw new CrosstabException(Messages.getString("String_Node_Str",dv.getCubeDimensionName()));
      }
      IDimensionDefinition dimDef=rowEdge.createDimension(dv.getCubeDimension().getName());
      IHierarchyDefinition hieDef=dimDef.createHierarchy(dv.getCubeDimension().getDefaultHierarchy().getName());
      for (int j=0; j < dv.getLevelCount(); j++) {
        LevelViewHandle lv=dv.getLevel(j);
        if (lv.getCubeLevel() == null) {
          throw new CrosstabException(Messages.getString("String_Node_Str",lv.getCubeLevelName()));
        }
        ILevelDefinition levelDef=hieDef.createLevel(lv.getCubeLevel().getName());
        rowLevelNameList.add(lv.getCubeLevel().getFullName());
        if (mirrorLevel != null && mirrorLevel.getQualifiedName().equals(lv.getCubeLevelName())) {
          rowEdge.setMirrorStartingLevel(levelDef);
        }
        levelViewList.add(lv);
        levelMap.put(lv.getCubeLevel(),levelDef);
      }
    }
  }
  if (needColumnDimension && crosstabItem.getDimensionCount(COLUMN_AXIS_TYPE) > 0) {
    IEdgeDefinition columnEdge=cubeQuery.createEdge(ICubeQueryDefinition.COLUMN_EDGE);
    LevelHandle mirrorLevel=crosstabItem.getCrosstabView(COLUMN_AXIS_TYPE).getMirroredStartingLevel();
    for (int i=0; i < crosstabItem.getDimensionCount(COLUMN_AXIS_TYPE); i++) {
      DimensionViewHandle dv=crosstabItem.getDimension(COLUMN_AXIS_TYPE,i);
      if (dv.getCubeDimension() == null) {
        throw new CrosstabException(Messages.getString("String_Node_Str",dv.getCubeDimensionName()));
      }
      IDimensionDefinition dimDef=columnEdge.createDimension(dv.getCubeDimension().getName());
      IHierarchyDefinition hieDef=dimDef.createHierarchy(dv.getCubeDimension().getDefaultHierarchy().getName());
      for (int j=0; j < dv.getLevelCount(); j++) {
        LevelViewHandle lv=dv.getLevel(j);
        if (lv.getCubeLevel() == null) {
          throw new CrosstabException(Messages.getString("String_Node_Str",lv.getCubeLevelName()));
        }
        ILevelDefinition levelDef=hieDef.createLevel(lv.getCubeLevel().getName());
        columnLevelNameList.add(lv.getCubeLevel().getFullName());
        if (mirrorLevel != null && mirrorLevel.getQualifiedName().equals(lv.getCubeLevelName())) {
          columnEdge.setMirrorStartingLevel(levelDef);
        }
        levelViewList.add(lv);
        levelMap.put(lv.getCubeLevel(),levelDef);
      }
    }
  }
  if (needSorting) {
    addLevelSorting(levelViewList,levelMap,cubeQuery);
  }
  if (needFilter) {
    addLevelFilter(levelViewList,levelMap,cubeQuery);
  }
  if (needBinding) {
    Iterator bindingItr=((ExtendedItemHandle)crosstabItem.getModelHandle()).columnBindingsIterator();
    ModuleHandle module=((ExtendedItemHandle)crosstabItem.getModelHandle()).getModuleHandle();
    if (bindingItr != null) {
      Map cache=new HashMap();
      while (bindingItr.hasNext()) {
        ComputedColumnHandle column=(ComputedColumnHandle)bindingItr.next();
        Binding binding=new Binding(column.getName());
        binding.setAggrFunction(column.getAggregateFunction() == null ? null : DataAdapterUtil.adaptModelAggregationType(column.getAggregateFunction()));
        binding.setExpression(column.getExpression() == null ? null : new ScriptExpression(column.getExpression()));
        binding.setDataType(DataAdapterUtil.adaptModelDataType(column.getDataType()));
        if (column.getFilterExpression() != null) {
          binding.setFilter(new ScriptExpression(column.getFilterExpression()));
        }
        for (Iterator argItr=column.argumentsIterator(); argItr.hasNext(); ) {
          AggregationArgumentHandle aah=(AggregationArgumentHandle)argItr.next();
          binding.addArgument(new ScriptExpression(aah.getValue()));
        }
        List aggrList=column.getAggregateOnList();
        if (aggrList != null) {
          for (Iterator aggrItr=aggrList.iterator(); aggrItr.hasNext(); ) {
            String baseLevel=(String)aggrItr.next();
            CrosstabUtil.addHierachyAggregateOn(module,binding,baseLevel,rowLevelNameList,columnLevelNameList,cache);
          }
        }
        cubeQuery.addBinding(binding);
      }
    }
  }
  return cubeQuery;
}