Program: 64907
Project: 64907
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=getDataSet(model);
  if (targetParent instanceof TableHandle) {
    TableHandle tableHandle=(TableHandle)targetParent;
    if (tableHandle.isSummaryTable()) {
      setDataSet(tableHandle,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=getDataSet(model);
        String str=UIUtil.getAnalysisColumn(model);
        String type="String_Node_Str";
        ResultSetColumnHandle newResultColumn=null;
        if (str != null) {
          List<ColumnHintHandle> columnHints=DataUtil.getColumnHints(dataset);
          ColumnHintHandle temp=null;
          for (int i=0; i < columnHints.size(); i++) {
            ColumnHintHandle columnHintHandle=(ColumnHintHandle)columnHints.get(i);
            if (str.equals(columnHintHandle.getColumnName()) || str.equals(columnHintHandle.getAlias())) {
              temp=columnHintHandle;
              break;
            }
          }
          if (temp == null) {
            return dataHandle;
          }
          List columnList=DataUtil.getColumnList(dataset);
          for (int i=0; i < columnList.size(); i++) {
            ResultSetColumnHandle resultSetColumn=(ResultSetColumnHandle)columnList.get(i);
            if (temp != null && (temp.getAlias().equals(resultSetColumn.getColumnName()) || temp.getColumnName().equals((resultSetColumn.getColumnName())))) {
              newResultColumn=resultSetColumn;
              break;
            }
          }
          List<ColumnHintHandle> columnHints=DataUtil.getColumnHints(dataset);
          for (          ColumnHintHandle columnHint : columnHints) {
            if (str.equals(columnHint.getColumnName()) || str.equals(columnHint.getAlias())) {
              type=columnHint.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(temp.getColumnName()) || group.getName().equals(temp.getAlias()))               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);
        setDataSet(dataHandle,dataSet);
        dataHandle.addColumnBinding(bindingColumn,false);
      }
 else {
        ComputedColumn bindingColumn=createBindingColumn(target,container,model);
        setDataSet(container,dataSet);
        container.addColumnBinding(bindingColumn,false);
      }
    }
 else     if (root.getDataSet() == dataSet || (getAdapter() != null && root.getDataSet() != null && getAdapter().resolveExtendedData(root.getDataSet()).equals(getAdapter().resolveExtendedData(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);
        setDataSet(listingHandle,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);
    setDataSet(dataHandle,dataSet);
  }
  setDataItemAction(model,dataHandle);
  InsertInLayoutRule rule=new LabelAddRule(target);
  if (rule.canInsert()) {
    LabelHandle label=DesignElementFactory.getInstance().newLabel(null);
    label.setText(UIUtil.getHeadColumnDisplayName(model));
    String displayKey=UIUtil.getColumnHeaderDisplayNameKey(model);
    if (displayKey == null) {
      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=getDataSet(model);
  if (targetParent instanceof TableHandle) {
    TableHandle tableHandle=(TableHandle)targetParent;
    if (tableHandle.isSummaryTable()) {
      setDataSet(tableHandle,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=getDataSet(model);
        String str=UIUtil.getAnalysisColumn(model);
        String type="String_Node_Str";
        ResultSetColumnHandle newResultColumn=null;
        if (str != null) {
          List<ColumnHintHandle> columnHints=DataUtil.getColumnHints(dataset);
          ColumnHintHandle temp=null;
          for (int i=0; i < columnHints.size(); i++) {
            ColumnHintHandle columnHintHandle=(ColumnHintHandle)columnHints.get(i);
            if (str.equals(columnHintHandle.getColumnName()) || str.equals(columnHintHandle.getAlias())) {
              temp=columnHintHandle;
              break;
            }
          }
          if (temp == null) {
            return dataHandle;
          }
          List columnList=DataUtil.getColumnList(dataset);
          for (int i=0; i < columnList.size(); i++) {
            ResultSetColumnHandle resultSetColumn=(ResultSetColumnHandle)columnList.get(i);
            if (temp != null && (temp.getAlias().equals(resultSetColumn.getColumnName()) || temp.getColumnName().equals((resultSetColumn.getColumnName())))) {
              newResultColumn=resultSetColumn;
              break;
            }
          }
          columnHints=DataUtil.getColumnHints(dataset);
          for (          ColumnHintHandle columnHint : columnHints) {
            if (str.equals(columnHint.getColumnName()) || str.equals(columnHint.getAlias())) {
              type=columnHint.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(temp.getColumnName()) || group.getName().equals(temp.getAlias()))               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);
        setDataSet(dataHandle,dataSet);
        dataHandle.addColumnBinding(bindingColumn,false);
      }
 else {
        ComputedColumn bindingColumn=createBindingColumn(target,container,model);
        setDataSet(container,dataSet);
        container.addColumnBinding(bindingColumn,false);
      }
    }
 else     if (root.getDataSet() == dataSet || (getAdapter() != null && root.getDataSet() != null && getAdapter().resolveExtendedData(root.getDataSet()).equals(getAdapter().resolveExtendedData(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);
        setDataSet(listingHandle,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);
    setDataSet(dataHandle,dataSet);
  }
  setDataItemAction(model,dataHandle);
  InsertInLayoutRule rule=new LabelAddRule(target);
  if (rule.canInsert()) {
    LabelHandle label=DesignElementFactory.getInstance().newLabel(null);
    label.setText(UIUtil.getHeadColumnDisplayName(model));
    String displayKey=UIUtil.getColumnHeaderDisplayNameKey(model);
    if (displayKey == null) {
      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;
}