Program: 68664
Project: 68664
Buggy source:
/** 
 * Inserts dataset column into the target. Add label or group key if possible
 * @param model column item
 * @param target insert target like cell or ListBandProxy
 * @param targetParent target container like table or list
 * @return to be inserted data item
 * @throws SemanticException
 */
protected static DesignElementHandle performInsertDataSetColumn(ResultSetColumnHandle model,Object target,Object targetParent) throws SemanticException {
  DataItemHandle dataHandle=DesignElementFactory.getInstance().newDataItem(null);
  DataSetHandle dataSet=(DataSetHandle)model.getElementHandle();
  if (targetParent instanceof TableHandle) {
    TableHandle tableHandle=(TableHandle)targetParent;
    if (tableHandle.isSummaryTable()) {
      tableHandle.setDataSet(dataSet);
      if (DesignChoiceConstants.ANALYSIS_TYPE_DIMENSION.equals(UIUtil.getColumnAnalysis(model))) {
        ComputedColumn bindingColumn=StructureFactory.newComputedColumn(dataHandle,model.getColumnName());
        bindingColumn.setDataType(model.getDataType());
        ExpressionUtility.setBindingColumnExpression(model,bindingColumn);
        bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(model));
        tableHandle.addColumnBinding(bindingColumn,false);
        dataHandle.setResultSetColumn(model.getColumnName());
        SlotHandle slotHandle=tableHandle.getGroups();
        for (        Object o : slotHandle.getContents()) {
          GroupHandle group=(GroupHandle)o;
          if (group.getName().equals(model.getColumnName()))           return null;
        }
        DesignElementFactory factory=DesignElementFactory.getInstance(tableHandle.getModuleHandle());
        GroupHandle groupHandle=factory.newTableGroup();
        int columnCount=tableHandle.getColumnCount();
        groupHandle.getHeader().add(factory.newTableRow(columnCount));
        groupHandle.getFooter().add(factory.newTableRow(columnCount));
        groupHandle.setName(model.getColumnName());
        Expression newKeyExpr=new Expression(UIUtil.convertToModelString(model.getColumnName(),true),"String_Node_Str");
        groupHandle.setExpressionProperty(IGroupElementModel.KEY_EXPR_PROP,newKeyExpr);
        TOC toc=StructureFactory.createTOC();
        toc.setExpression(ExpressionUtility.getColumnExpression(model.getColumnName(),ExpressionUtility.getExpressionConverter(ExpressionType.JAVASCRIPT)));
        groupHandle.addTOC(toc);
        slotHandle.add(groupHandle,slotHandle.getCount());
        RowHandle rowHandle=((RowHandle)groupHandle.getHeader().get(0));
        CellHandle cellHandle=(CellHandle)rowHandle.getCells().get(0);
        cellHandle.getContent().add(dataHandle);
        return groupHandle;
      }
 else       if (DesignChoiceConstants.ANALYSIS_TYPE_MEASURE.equals(UIUtil.getColumnAnalysis(model)) || DesignChoiceConstants.ANALYSIS_TYPE_ATTRIBUTE.equals(UIUtil.getColumnAnalysis(model))) {
        if (target instanceof CellHandle && ((CellHandle)target).getContainer().getContainer() instanceof GroupHandle) {
          CellHandle cellHandle=(CellHandle)target;
          GroupHandle group=(GroupHandle)cellHandle.getContainer().getContainer();
          ComputedColumn column=StructureFactory.newComputedColumn(tableHandle,model.getColumnName());
          ComputedColumnHandle binding=DEUtil.addColumn(tableHandle,column,true);
          binding.setAggregateOn(group.getName());
          if (DesignChoiceConstants.ANALYSIS_TYPE_MEASURE.equals(UIUtil.getColumnAnalysis(model)))           binding.setAggregateFunction(DesignChoiceConstants.MEASURE_FUNCTION_SUM);
 else           binding.setAggregateFunction(DesignChoiceConstants.MEASURE_FUNCTION_MAX);
          binding.setExpression(ExpressionUtil.createJSRowExpression(model.getColumnName()));
          dataHandle.setResultSetColumn(binding.getName());
          InsertInLayoutRule rule=new LabelAddRule(target);
          if (rule.canInsert()) {
            LabelHandle label=DesignElementFactory.getInstance().newLabel(null);
            label.setText(UIUtil.getColumnDisplayName(model));
            rule.insert(label);
          }
          rule=new GroupKeySetRule(target,model);
          if (rule.canInsert()) {
            rule.insert(model);
          }
          return dataHandle;
        }
      }
    }
  }
  dataHandle.setResultSetColumn(model.getColumnName());
  if (targetParent instanceof ReportItemHandle) {
    ReportItemHandle container=(ReportItemHandle)targetParent;
    ComputedColumn bindingColumn=StructureFactory.newComputedColumn(dataHandle,model.getColumnName());
    bindingColumn.setDataType(model.getDataType());
    ExpressionUtility.setBindingColumnExpression(model,bindingColumn);
    bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(model));
    if (target instanceof DesignElementHandle) {
      if (ExpressionUtil.hasAggregation(bindingColumn.getExpression())) {
        String groupType=DEUtil.getGroupControlType((DesignElementHandle)target);
        if (groupType.equals(DEUtil.TYPE_GROUP_GROUP))         bindingColumn.setAggregateOn(((GroupHandle)DEUtil.getGroups((DesignElementHandle)target).get(0)).getName());
 else         if (groupType.equals(DEUtil.TYPE_GROUP_LISTING))         bindingColumn.setAggregateOn(null);
      }
    }
    ReportItemHandle root=DEUtil.getBindingRoot(container);
    if (root == null) {
      container=DEUtil.getListingContainer(container);
      if (container == null) {
        dataHandle.setDataSet(dataSet);
        dataHandle.addColumnBinding(bindingColumn,false);
      }
 else {
        container.setDataSet(dataSet);
        container.addColumnBinding(bindingColumn,false);
      }
    }
 else     if (root.getDataSet() == dataSet) {
      DEUtil.getBindingHolder(container).addColumnBinding(bindingColumn,false);
    }
 else {
      ReportItemHandle listingHandle=DEUtil.getListingContainer(container);
      if (listingHandle != null && DEUtil.getBindingRoot(listingHandle) == root && DEUtil.getBindingHolder(listingHandle) != listingHandle) {
        listingHandle.setDataSet(dataSet);
        listingHandle.addColumnBinding(bindingColumn,false);
      }
    }
  }
 else {
    ComputedColumn bindingColumn=StructureFactory.newComputedColumn(dataHandle,model.getColumnName());
    bindingColumn.setDataType(model.getDataType());
    ExpressionUtility.setBindingColumnExpression(model,bindingColumn);
    bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(model));
    if (target instanceof DesignElementHandle) {
      if (ExpressionUtil.hasAggregation(bindingColumn.getExpression())) {
        String groupType=DEUtil.getGroupControlType((DesignElementHandle)target);
        if (groupType.equals(DEUtil.TYPE_GROUP_GROUP))         bindingColumn.setAggregateOn(((GroupHandle)DEUtil.getGroups((DesignElementHandle)target).get(0)).getName());
 else         if (groupType.equals(DEUtil.TYPE_GROUP_LISTING))         bindingColumn.setAggregateOn(null);
      }
    }
    dataHandle.addColumnBinding(bindingColumn,false);
    dataHandle.setDataSet(dataSet);
  }
  InsertInLayoutRule rule=new LabelAddRule(target);
  if (rule.canInsert()) {
    LabelHandle label=DesignElementFactory.getInstance().newLabel(null);
    label.setText(UIUtil.getColumnDisplayName(model));
    rule.insert(label);
  }
  rule=new GroupKeySetRule(target,model);
  if (rule.canInsert()) {
    rule.insert(model);
  }
  return dataHandle;
}

Fixed version (ground truth):
/** 
 * Inserts dataset column into the target. Add label or group key if possible
 * @param model column item
 * @param target insert target like cell or ListBandProxy
 * @param targetParent target container like table or list
 * @return to be inserted data item
 * @throws SemanticException
 */
protected static DesignElementHandle performInsertDataSetColumn(ResultSetColumnHandle model,Object target,Object targetParent) throws SemanticException {
  DataItemHandle dataHandle=DesignElementFactory.getInstance().newDataItem(null);
  DataSetHandle dataSet=(DataSetHandle)model.getElementHandle();
  if (targetParent instanceof TableHandle) {
    TableHandle tableHandle=(TableHandle)targetParent;
    if (tableHandle.isSummaryTable()) {
      tableHandle.setDataSet(dataSet);
      if (DesignChoiceConstants.ANALYSIS_TYPE_DIMENSION.equals(UIUtil.getColumnAnalysis(model))) {
        ComputedColumn bindingColumn=StructureFactory.newComputedColumn(dataHandle,model.getColumnName());
        bindingColumn.setDataType(model.getDataType());
        ExpressionUtility.setBindingColumnExpression(model,bindingColumn);
        bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(model));
        tableHandle.addColumnBinding(bindingColumn,false);
        dataHandle.setResultSetColumn(model.getColumnName());
        SlotHandle slotHandle=tableHandle.getGroups();
        for (        Object o : slotHandle.getContents()) {
          GroupHandle group=(GroupHandle)o;
          if (group.getName().equals(model.getColumnName()))           return null;
        }
        DesignElementFactory factory=DesignElementFactory.getInstance(tableHandle.getModuleHandle());
        GroupHandle groupHandle=factory.newTableGroup();
        int columnCount=tableHandle.getColumnCount();
        groupHandle.getHeader().add(factory.newTableRow(columnCount));
        groupHandle.getFooter().add(factory.newTableRow(columnCount));
        groupHandle.setName(model.getColumnName());
        Expression newKeyExpr=new Expression(ExpressionUtility.getColumnExpression(model.getColumnName(),ExpressionUtility.getExpressionConverter(ExpressionType.JAVASCRIPT)),ExpressionType.JAVASCRIPT);
        groupHandle.setExpressionProperty(IGroupElementModel.KEY_EXPR_PROP,newKeyExpr);
        TOC toc=StructureFactory.createTOC();
        toc.setExpression(ExpressionUtility.getColumnExpression(model.getColumnName(),ExpressionUtility.getExpressionConverter(ExpressionType.JAVASCRIPT)));
        groupHandle.addTOC(toc);
        slotHandle.add(groupHandle,slotHandle.getCount());
        RowHandle rowHandle=((RowHandle)groupHandle.getHeader().get(0));
        CellHandle cellHandle=(CellHandle)rowHandle.getCells().get(0);
        cellHandle.getContent().add(dataHandle);
        return groupHandle;
      }
 else       if (DesignChoiceConstants.ANALYSIS_TYPE_MEASURE.equals(UIUtil.getColumnAnalysis(model)) || DesignChoiceConstants.ANALYSIS_TYPE_ATTRIBUTE.equals(UIUtil.getColumnAnalysis(model))) {
        if (target instanceof CellHandle && ((CellHandle)target).getContainer().getContainer() instanceof GroupHandle) {
          CellHandle cellHandle=(CellHandle)target;
          GroupHandle group=(GroupHandle)cellHandle.getContainer().getContainer();
          ComputedColumn column=StructureFactory.newComputedColumn(tableHandle,model.getColumnName());
          ComputedColumnHandle binding=DEUtil.addColumn(tableHandle,column,true);
          binding.setAggregateOn(group.getName());
          if (DesignChoiceConstants.ANALYSIS_TYPE_MEASURE.equals(UIUtil.getColumnAnalysis(model)))           binding.setAggregateFunction(DesignChoiceConstants.MEASURE_FUNCTION_SUM);
 else           binding.setAggregateFunction(DesignChoiceConstants.MEASURE_FUNCTION_MAX);
          binding.setExpression(ExpressionUtil.createJSRowExpression(model.getColumnName()));
          dataHandle.setResultSetColumn(binding.getName());
          InsertInLayoutRule rule=new LabelAddRule(target);
          if (rule.canInsert()) {
            LabelHandle label=DesignElementFactory.getInstance().newLabel(null);
            label.setText(UIUtil.getColumnDisplayName(model));
            rule.insert(label);
          }
          rule=new GroupKeySetRule(target,model);
          if (rule.canInsert()) {
            rule.insert(model);
          }
          return dataHandle;
        }
      }
    }
  }
  dataHandle.setResultSetColumn(model.getColumnName());
  if (targetParent instanceof ReportItemHandle) {
    ReportItemHandle container=(ReportItemHandle)targetParent;
    ComputedColumn bindingColumn=StructureFactory.newComputedColumn(dataHandle,model.getColumnName());
    bindingColumn.setDataType(model.getDataType());
    ExpressionUtility.setBindingColumnExpression(model,bindingColumn);
    bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(model));
    if (target instanceof DesignElementHandle) {
      if (ExpressionUtil.hasAggregation(bindingColumn.getExpression())) {
        String groupType=DEUtil.getGroupControlType((DesignElementHandle)target);
        if (groupType.equals(DEUtil.TYPE_GROUP_GROUP))         bindingColumn.setAggregateOn(((GroupHandle)DEUtil.getGroups((DesignElementHandle)target).get(0)).getName());
 else         if (groupType.equals(DEUtil.TYPE_GROUP_LISTING))         bindingColumn.setAggregateOn(null);
      }
    }
    ReportItemHandle root=DEUtil.getBindingRoot(container);
    if (root == null) {
      container=DEUtil.getListingContainer(container);
      if (container == null) {
        dataHandle.setDataSet(dataSet);
        dataHandle.addColumnBinding(bindingColumn,false);
      }
 else {
        container.setDataSet(dataSet);
        container.addColumnBinding(bindingColumn,false);
      }
    }
 else     if (root.getDataSet() == dataSet) {
      DEUtil.getBindingHolder(container).addColumnBinding(bindingColumn,false);
    }
 else {
      ReportItemHandle listingHandle=DEUtil.getListingContainer(container);
      if (listingHandle != null && DEUtil.getBindingRoot(listingHandle) == root && DEUtil.getBindingHolder(listingHandle) != listingHandle) {
        listingHandle.setDataSet(dataSet);
        listingHandle.addColumnBinding(bindingColumn,false);
      }
    }
  }
 else {
    ComputedColumn bindingColumn=StructureFactory.newComputedColumn(dataHandle,model.getColumnName());
    bindingColumn.setDataType(model.getDataType());
    ExpressionUtility.setBindingColumnExpression(model,bindingColumn);
    bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(model));
    if (target instanceof DesignElementHandle) {
      if (ExpressionUtil.hasAggregation(bindingColumn.getExpression())) {
        String groupType=DEUtil.getGroupControlType((DesignElementHandle)target);
        if (groupType.equals(DEUtil.TYPE_GROUP_GROUP))         bindingColumn.setAggregateOn(((GroupHandle)DEUtil.getGroups((DesignElementHandle)target).get(0)).getName());
 else         if (groupType.equals(DEUtil.TYPE_GROUP_LISTING))         bindingColumn.setAggregateOn(null);
      }
    }
    dataHandle.addColumnBinding(bindingColumn,false);
    dataHandle.setDataSet(dataSet);
  }
  InsertInLayoutRule rule=new LabelAddRule(target);
  if (rule.canInsert()) {
    LabelHandle label=DesignElementFactory.getInstance().newLabel(null);
    label.setText(UIUtil.getColumnDisplayName(model));
    rule.insert(label);
  }
  rule=new GroupKeySetRule(target,model);
  if (rule.canInsert()) {
    rule.insert(model);
  }
  return dataHandle;
}