Program: 65754
Project: 65754
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())) {
            if (target instanceof CellHandle) {
              CellHandle cellTarget=(CellHandle)target;
              if (cellTarget.getContent().getCount() == 0) {
                return dataHandle;
              }
            }
            return null;
          }
        }
        int index=-1;
        if (target instanceof CellHandle) {
          CellHandle cellTarget=(CellHandle)target;
          CellHandleAdapter cellAdapter=HandleAdapterFactory.getInstance().getCellHandleAdapter(cellTarget);
          index=cellAdapter.getColumnNumber();
        }
        return addGroupHandle(tableHandle,model,dataHandle,index - 1);
      }
 else       if (DesignChoiceConstants.ANALYSIS_TYPE_ATTRIBUTE.equals(UIUtil.getColumnAnalysis(model))) {
        DataSetHandle dataset=(DataSetHandle)model.getElementHandle();
        String str=UIUtil.getAnalysisColumn(model);
        String type="String_Node_Str";
        ResultSetColumnHandle newResultColumn=null;
        if (str != null) {
          List columnList=DataUtil.getColumnList(dataset);
          for (int i=0; i < columnList.size(); i++) {
            ResultSetColumnHandle resultSetColumn=(ResultSetColumnHandle)columnList.get(i);
            if (str.equals(resultSetColumn.getColumnName())) {
              newResultColumn=resultSetColumn;
              break;
            }
          }
          for (Iterator iter=dataset.getPropertyHandle(DataSetHandle.COLUMN_HINTS_PROP).iterator(); iter.hasNext(); ) {
            ColumnHintHandle element=(ColumnHintHandle)iter.next();
            if (element.getColumnName().equals(str) || str.equals(element.getAlias())) {
              type=element.getAnalysis();
              break;
            }
          }
          if (DesignChoiceConstants.ANALYSIS_TYPE_DIMENSION.equals(type)) {
            boolean hasGroup=false;
            SlotHandle slotHandle=tableHandle.getGroups();
            for (            Object o : slotHandle.getContents()) {
              GroupHandle group=(GroupHandle)o;
              if (group.getName().equals(str))               hasGroup=true;
            }
            if (!hasGroup) {
              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());
              bindingColumn=StructureFactory.newComputedColumn(tableHandle,newResultColumn.getColumnName());
              bindingColumn.setDataType(newResultColumn.getDataType());
              ExpressionUtility.setBindingColumnExpression(newResultColumn,bindingColumn);
              bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(newResultColumn));
              displayKey=UIUtil.getColumnDisplayNameKey(newResultColumn);
              if (displayKey != null)               bindingColumn.setDisplayNameID(displayKey);
              tableHandle.addColumnBinding(bindingColumn,false);
              int index=-1;
              if (target instanceof CellHandle) {
                CellHandle cellTarget=(CellHandle)target;
                CellHandleAdapter cellAdapter=HandleAdapterFactory.getInstance().getCellHandleAdapter(cellTarget);
                index=cellAdapter.getColumnNumber();
              }
              return addGroupHandle(tableHandle,newResultColumn,dataHandle,index - 1);
            }
          }
        }
        if (target instanceof CellHandle) {
          ComputedColumn column=StructureFactory.newComputedColumn(tableHandle,model.getColumnName());
          column.setDataType(model.getDataType());
          ExpressionUtility.setBindingColumnExpression(model,column);
          ComputedColumnHandle binding=DEUtil.addColumn(tableHandle,column,false);
          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;
        }
      }
 else       if (DesignChoiceConstants.ANALYSIS_TYPE_MEASURE.equals(UIUtil.getColumnAnalysis(model))) {
        CellHandle cellHandle=(CellHandle)target;
        ComputedColumn column=StructureFactory.newComputedColumn(tableHandle,model.getColumnName());
        ExpressionUtility.setBindingColumnExpression(model,column);
        column.setDataType(model.getDataType());
        ComputedColumnHandle binding=DEUtil.addColumn(tableHandle,column,false);
        DesignElementHandle group=cellHandle.getContainer().getContainer();
        if (group instanceof GroupHandle) {
          binding.setAggregateOn(((GroupHandle)group).getName());
        }
 else {
          binding.setAggregateOn(null);
        }
        if (DesignChoiceConstants.COLUMN_DATA_TYPE_INTEGER.equals(model.getDataType()) || DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT.equals(model.getDataType()) || DesignChoiceConstants.COLUMN_DATA_TYPE_DECIMAL.equals(model.getDataType())) {
          binding.setAggregateFunction(DesignChoiceConstants.MEASURE_FUNCTION_SUM);
        }
 else {
          binding.setAggregateFunction(DesignChoiceConstants.MEASURE_FUNCTION_MAX);
        }
        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);
      setDataItemAction(model,dataHandle);
      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())) {
            if (target instanceof CellHandle) {
              CellHandle cellTarget=(CellHandle)target;
              if (cellTarget.getContent().getCount() == 0) {
                return dataHandle;
              }
            }
            return null;
          }
        }
        int index=-1;
        if (target instanceof CellHandle) {
          CellHandle cellTarget=(CellHandle)target;
          CellHandleAdapter cellAdapter=HandleAdapterFactory.getInstance().getCellHandleAdapter(cellTarget);
          index=cellAdapter.getColumnNumber();
        }
        return addGroupHandle(tableHandle,model,dataHandle,index - 1);
      }
 else       if (DesignChoiceConstants.ANALYSIS_TYPE_ATTRIBUTE.equals(UIUtil.getColumnAnalysis(model))) {
        DataSetHandle dataset=(DataSetHandle)model.getElementHandle();
        String str=UIUtil.getAnalysisColumn(model);
        String type="String_Node_Str";
        ResultSetColumnHandle newResultColumn=null;
        if (str != null) {
          List columnList=DataUtil.getColumnList(dataset);
          for (int i=0; i < columnList.size(); i++) {
            ResultSetColumnHandle resultSetColumn=(ResultSetColumnHandle)columnList.get(i);
            if (str.equals(resultSetColumn.getColumnName())) {
              newResultColumn=resultSetColumn;
              break;
            }
          }
          for (Iterator iter=dataset.getPropertyHandle(DataSetHandle.COLUMN_HINTS_PROP).iterator(); iter.hasNext(); ) {
            ColumnHintHandle element=(ColumnHintHandle)iter.next();
            if (element.getColumnName().equals(str) || str.equals(element.getAlias())) {
              type=element.getAnalysis();
              break;
            }
          }
          if (DesignChoiceConstants.ANALYSIS_TYPE_DIMENSION.equals(type)) {
            boolean hasGroup=false;
            SlotHandle slotHandle=tableHandle.getGroups();
            for (            Object o : slotHandle.getContents()) {
              GroupHandle group=(GroupHandle)o;
              if (group.getName().equals(str))               hasGroup=true;
            }
            if (!hasGroup) {
              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());
              bindingColumn=StructureFactory.newComputedColumn(tableHandle,newResultColumn.getColumnName());
              bindingColumn.setDataType(newResultColumn.getDataType());
              ExpressionUtility.setBindingColumnExpression(newResultColumn,bindingColumn);
              bindingColumn.setDisplayName(UIUtil.getColumnDisplayName(newResultColumn));
              displayKey=UIUtil.getColumnDisplayNameKey(newResultColumn);
              if (displayKey != null)               bindingColumn.setDisplayNameID(displayKey);
              tableHandle.addColumnBinding(bindingColumn,false);
              int index=-1;
              if (target instanceof CellHandle) {
                CellHandle cellTarget=(CellHandle)target;
                CellHandleAdapter cellAdapter=HandleAdapterFactory.getInstance().getCellHandleAdapter(cellTarget);
                index=cellAdapter.getColumnNumber();
              }
              return addGroupHandle(tableHandle,newResultColumn,dataHandle,index - 1);
            }
          }
        }
        if (target instanceof CellHandle) {
          ComputedColumn column=StructureFactory.newComputedColumn(tableHandle,model.getColumnName());
          column.setDataType(model.getDataType());
          ExpressionUtility.setBindingColumnExpression(model,column);
          ComputedColumnHandle binding=DEUtil.addColumn(tableHandle,column,false);
          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;
        }
      }
 else       if (DesignChoiceConstants.ANALYSIS_TYPE_MEASURE.equals(UIUtil.getColumnAnalysis(model))) {
        CellHandle cellHandle=(CellHandle)target;
        ComputedColumn column=StructureFactory.newComputedColumn(tableHandle,model.getColumnName());
        ExpressionUtility.setBindingColumnExpression(model,column);
        column.setDataType(model.getDataType());
        ComputedColumnHandle binding=DEUtil.addColumn(tableHandle,column,false);
        DesignElementHandle group=cellHandle.getContainer().getContainer();
        if (group instanceof GroupHandle) {
          binding.setAggregateOn(((GroupHandle)group).getName());
        }
 else {
          binding.setAggregateOn(null);
        }
        if (DesignChoiceConstants.COLUMN_DATA_TYPE_INTEGER.equals(model.getDataType()) || DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT.equals(model.getDataType()) || DesignChoiceConstants.COLUMN_DATA_TYPE_DECIMAL.equals(model.getDataType())) {
          binding.setAggregateFunction(DesignChoiceConstants.MEASURE_FUNCTION_SUM);
        }
 else {
          binding.setAggregateFunction(DesignChoiceConstants.MEASURE_FUNCTION_MAX);
        }
        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);
  }
  setDataItemAction(model,dataHandle);
  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;
}