Program: 67084
Project: 67084
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(tableHandle,model.getColumnName());
        bindingColumn.setDataType(model.getDataType());
        ExpressionUtility.setBindingColumnExpression(model,bindingColumn);
        bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(model));
        String displayKey=UIUtil.getColumnDisplayNameKey(model);
        if (displayKey != null)         bindingColumn.setDisplayNameID(displayKey);
        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());
  formatDataHandle(dataHandle,model);
  if (targetParent instanceof ReportItemHandle) {
    ReportItemHandle container=(ReportItemHandle)targetParent;
    ReportItemHandle root=DEUtil.getBindingRoot(container);
    if (root == null) {
      container=DEUtil.getListingContainer(container);
      if (container == null) {
        ComputedColumn bindingColumn=createBindingColumn(target,dataHandle,model);
        dataHandle.setDataSet(dataSet);
        dataHandle.addColumnBinding(bindingColumn,false);
      }
 else {
        ComputedColumn bindingColumn=createBindingColumn(target,container,model);
        container.setDataSet(dataSet);
        container.addColumnBinding(bindingColumn,false);
      }
    }
 else     if (root.getDataSet() == dataSet) {
      container=DEUtil.getBindingHolder(container);
      ComputedColumn bindingColumn=createBindingColumn(target,container,model);
      container.addColumnBinding(bindingColumn,false);
    }
 else {
      ReportItemHandle listingHandle=DEUtil.getListingContainer(container);
      if (listingHandle != null && DEUtil.getBindingRoot(listingHandle) == root && DEUtil.getBindingHolder(listingHandle) != listingHandle) {
        ComputedColumn bindingColumn=createBindingColumn(target,listingHandle,model);
        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));
    String displayKey=UIUtil.getColumnDisplayNameKey(model);
    if (displayKey != null)     bindingColumn.setDisplayNameID(displayKey);
    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);
  }
  ActionHandle actionHandle=UIUtil.getColumnAction(model);
  if (actionHandle != null) {
    List source=new ArrayList();
    source.add(actionHandle.getStructure());
    List newAction=ModelUtil.cloneStructList(source);
    dataHandle.setAction((Action)newAction.get(0));
  }
  InsertInLayoutRule rule=new LabelAddRule(target);
  if (rule.canInsert()) {
    LabelHandle label=DesignElementFactory.getInstance().newLabel(null);
    label.setText(UIUtil.getColumnDisplayName(model));
    String displayKey=UIUtil.getColumnDisplayNameKey(model);
    if (displayKey != null) {
      label.setTextKey(displayKey);
    }
    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(tableHandle,model.getColumnName());
        bindingColumn.setDataType(model.getDataType());
        ExpressionUtility.setBindingColumnExpression(model,bindingColumn);
        bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(model));
        String displayKey=UIUtil.getColumnDisplayNameKey(model);
        if (displayKey != null)         bindingColumn.setDisplayNameID(displayKey);
        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);
        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());
  formatDataHandle(dataHandle,model);
  if (targetParent instanceof ReportItemHandle) {
    ReportItemHandle container=(ReportItemHandle)targetParent;
    ReportItemHandle root=DEUtil.getBindingRoot(container);
    if (root == null) {
      container=DEUtil.getListingContainer(container);
      if (container == null) {
        ComputedColumn bindingColumn=createBindingColumn(target,dataHandle,model);
        dataHandle.setDataSet(dataSet);
        dataHandle.addColumnBinding(bindingColumn,false);
      }
 else {
        ComputedColumn bindingColumn=createBindingColumn(target,container,model);
        container.setDataSet(dataSet);
        container.addColumnBinding(bindingColumn,false);
      }
    }
 else     if (root.getDataSet() == dataSet) {
      container=DEUtil.getBindingHolder(container);
      ComputedColumn bindingColumn=createBindingColumn(target,container,model);
      container.addColumnBinding(bindingColumn,false);
    }
 else {
      ReportItemHandle listingHandle=DEUtil.getListingContainer(container);
      if (listingHandle != null && DEUtil.getBindingRoot(listingHandle) == root && DEUtil.getBindingHolder(listingHandle) != listingHandle) {
        ComputedColumn bindingColumn=createBindingColumn(target,listingHandle,model);
        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));
    String displayKey=UIUtil.getColumnDisplayNameKey(model);
    if (displayKey != null)     bindingColumn.setDisplayNameID(displayKey);
    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);
  }
  ActionHandle actionHandle=UIUtil.getColumnAction(model);
  if (actionHandle != null) {
    List source=new ArrayList();
    source.add(actionHandle.getStructure());
    List newAction=ModelUtil.cloneStructList(source);
    dataHandle.setAction((Action)newAction.get(0));
  }
  InsertInLayoutRule rule=new LabelAddRule(target);
  if (rule.canInsert()) {
    LabelHandle label=DesignElementFactory.getInstance().newLabel(null);
    label.setText(UIUtil.getColumnDisplayName(model));
    String displayKey=UIUtil.getColumnDisplayNameKey(model);
    if (displayKey != null) {
      label.setTextKey(displayKey);
    }
    rule.insert(label);
  }
  rule=new GroupKeySetRule(target,model);
  if (rule.canInsert()) {
    rule.insert(model);
  }
  return dataHandle;
}