Program: 86790
Project: 86790
Buggy source:
/** 
 * Adds the column headers and column data to the DataSet
 * @param dataSet
 * @param encounters
 * @param patientIdentifierTypes
 * @param optionalColumns
 * @param columnDisplayFormat
 * @param maxColumnHeaderWidth
 * @param allColumns
 * @param fieldMap
 * @return
 */
public DataSet addData(SimpleDataSet dataSet,List<Encounter> encounters,List<PatientIdentifierType> patientIdentifierTypes,List<EncounterAndObsDataSetDefinition.ObsOptionalColumn> optionalColumns,List<EncounterAndObsDataSetDefinition.ColumnDisplayFormat> columnDisplayFormat,Integer maxColumnHeaderWidth,Set<ObsColumnDescriptor> allColumns,Map<Encounter,Map<ObsColumnDescriptor,Obs>> fieldMap){
  for (  Encounter encounter : encounters) {
    DataSetRow row=new DataSetRow();
    String providerName=getProviderNameFromEncounter(encounter);
    DataSetColumn c1=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),Integer.class);
    row.addColumnValue(c1,encounter.getEncounterId());
    DataSetColumn c2=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),String.class);
    row.addColumnValue(c2,encounter.getEncounterDatetime().toString());
    DataSetColumn c3=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),String.class);
    row.addColumnValue(c3,(encounter.getLocation() != null) ? encounter.getLocation().getName() : EMPTY);
    DataSetColumn c4=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),String.class);
    row.addColumnValue(c4,providerName);
    DataSetColumn c5=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),Integer.class);
    row.addColumnValue(c5,encounter.getPatient() != null ? encounter.getPatient().getPatientId() : EMPTY);
    if (patientIdentifierTypes != null) {
      for (      PatientIdentifierType pit : patientIdentifierTypes) {
        List<PatientIdentifier> patientIdentifiers=encounter.getPatient().getPatientIdentifiers(pit);
        StringBuffer sbPatientIdentifiers=new StringBuffer();
        int count=0;
        for (        PatientIdentifier patientIdentifier : patientIdentifiers) {
          if (count > 0) {
            sbPatientIdentifiers.append("String_Node_Str");
          }
          sbPatientIdentifiers.append(patientIdentifier.toString());
          count++;
        }
        DataSetColumn c6=new DataSetColumn(pit.getName(),ObjectUtil.trimStringIfNeeded(pit.getName(),maxColumnHeaderWidth),String.class);
        row.addColumnValue(c6,sbPatientIdentifiers.toString());
      }
    }
    Map<ObsColumnDescriptor,Obs> obsInEncounter=fieldMap.get(encounter);
    for (    ObsColumnDescriptor columnKey : allColumns) {
      Obs obs=obsInEncounter.get(columnKey);
      String columnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth);
      DataSetColumn obsDsc=new DataSetColumn(columnName,columnName,String.class);
      StringBuffer columnValue=new StringBuffer();
      if (obs != null && obs.getValueCoded() != null) {
        if (columnDisplayFormat.contains(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.ID)) {
          columnValue.append(obs.getValueCoded());
        }
        if (columnDisplayFormat.contains(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.ID) && columnDisplayFormat.contains(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.BEST_SHORT_NAME)) {
          columnValue.append("String_Node_Str");
        }
        if (columnDisplayFormat.contains(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.BEST_SHORT_NAME)) {
          String conceptName=obs.getValueAsString(Context.getLocale());
          columnValue.append(maxColumnHeaderWidth != null && conceptName.length() > maxColumnHeaderWidth - columnValue.length() ? conceptName.substring(0,maxColumnHeaderWidth - columnValue.length() - 1) : conceptName);
        }
        row.addColumnValue(obsDsc,(obs != null) ? columnValue.toString() : EMPTY);
      }
 else {
        row.addColumnValue(obsDsc,(obs != null) ? obs.getValueAsString(Context.getLocale()) : EMPTY);
      }
      String dateColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 5 : null);
      DataSetColumn obsDscDate=new DataSetColumn(dateColumnName + "String_Node_Str",dateColumnName + "String_Node_Str",String.class);
      row.addColumnValue(obsDscDate,(obs != null) ? Context.getDateFormat().format(obs.getObsDatetime()) : EMPTY);
      String parentColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 7 : null);
      DataSetColumn obsDscParent=new DataSetColumn(parentColumnName + "String_Node_Str",parentColumnName + "String_Node_Str",String.class);
      row.addColumnValue(obsDscParent,(obs != null && obs.getObsGroup() != null) ? obs.getObsGroup().getId() : EMPTY);
      if (optionalColumns != null) {
        if (optionalColumns.contains(EncounterAndObsDataSetDefinition.ObsOptionalColumn.VALUE_MODIFIER)) {
          String valModColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 10 : null);
          DataSetColumn obsDscValueModifier=new DataSetColumn(valModColumnName + "String_Node_Str",valModColumnName + "String_Node_Str",String.class);
          row.addColumnValue(obsDscValueModifier,(obs != null) ? obs.getValueModifier() : EMPTY);
        }
        if (optionalColumns.contains(EncounterAndObsDataSetDefinition.ObsOptionalColumn.ACCESSION_NUMBER)) {
          String accessionNumColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 14 : null);
          DataSetColumn obsDscAccessionNumber=new DataSetColumn(accessionNumColumnName + "String_Node_Str",accessionNumColumnName + "String_Node_Str",String.class);
          row.addColumnValue(obsDscAccessionNumber,(obs != null) ? obs.getAccessionNumber() : EMPTY);
        }
        if (optionalColumns.contains(EncounterAndObsDataSetDefinition.ObsOptionalColumn.COMMENT)) {
          String commentColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 8 : null);
          DataSetColumn obsDscComment=new DataSetColumn(commentColumnName + "String_Node_Str",commentColumnName + "String_Node_Str",String.class);
          row.addColumnValue(obsDscComment,(obs != null) ? obs.getComment() : EMPTY);
        }
      }
    }
    dataSet.addRow(row);
  }
  return dataSet;
}

Fixed version (ground truth):
/** 
 * Adds the column headers and column data to the DataSet
 * @param dataSet
 * @param encounters
 * @param patientIdentifierTypes
 * @param optionalColumns
 * @param columnDisplayFormat
 * @param maxColumnHeaderWidth
 * @param allColumns
 * @param fieldMap
 * @return
 */
public DataSet addData(SimpleDataSet dataSet,List<Encounter> encounters,List<PatientIdentifierType> patientIdentifierTypes,List<EncounterAndObsDataSetDefinition.ObsOptionalColumn> optionalColumns,List<EncounterAndObsDataSetDefinition.ColumnDisplayFormat> columnDisplayFormat,Integer maxColumnHeaderWidth,Set<ObsColumnDescriptor> allColumns,Map<Encounter,Map<ObsColumnDescriptor,Obs>> fieldMap){
  for (  Encounter encounter : encounters) {
    DataSetRow row=new DataSetRow();
    String providerName=getProviderNameFromEncounter(encounter);
    DataSetColumn c1=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),Integer.class);
    row.addColumnValue(c1,encounter.getEncounterId());
    DataSetColumn c2=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),String.class);
    row.addColumnValue(c2,encounter.getEncounterDatetime().toString());
    DataSetColumn c3=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),String.class);
    row.addColumnValue(c3,(encounter.getLocation() != null) ? encounter.getLocation().getName() : EMPTY);
    DataSetColumn c4=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),String.class);
    row.addColumnValue(c4,providerName);
    DataSetColumn c5=new DataSetColumn(ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),ObjectUtil.trimStringIfNeeded("String_Node_Str",maxColumnHeaderWidth),Integer.class);
    row.addColumnValue(c5,encounter.getPatient() != null ? encounter.getPatient().getPatientId() : EMPTY);
    if (patientIdentifierTypes != null) {
      for (      PatientIdentifierType pit : patientIdentifierTypes) {
        List<PatientIdentifier> patientIdentifiers=encounter.getPatient().getPatientIdentifiers(pit);
        StringBuffer sbPatientIdentifiers=new StringBuffer();
        int count=0;
        for (        PatientIdentifier patientIdentifier : patientIdentifiers) {
          if (count > 0) {
            sbPatientIdentifiers.append("String_Node_Str");
          }
          sbPatientIdentifiers.append(patientIdentifier.toString());
          count++;
        }
        DataSetColumn c6=new DataSetColumn(pit.getName(),ObjectUtil.trimStringIfNeeded(pit.getName(),maxColumnHeaderWidth),String.class);
        row.addColumnValue(c6,sbPatientIdentifiers.toString());
      }
    }
    Map<ObsColumnDescriptor,Obs> obsInEncounter=fieldMap.get(encounter);
    for (    ObsColumnDescriptor columnKey : allColumns) {
      Obs obs=obsInEncounter.get(columnKey);
      String columnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth);
      DataSetColumn obsDsc=new DataSetColumn(columnName,columnName,String.class);
      StringBuffer columnValue=new StringBuffer();
      if (obs != null && obs.getValueCoded() != null) {
        if (columnDisplayFormat.contains(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.ID)) {
          columnValue.append(obs.getValueCoded());
        }
        if (columnDisplayFormat.contains(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.ID) && columnDisplayFormat.contains(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.BEST_SHORT_NAME)) {
          columnValue.append("String_Node_Str");
        }
        if (columnDisplayFormat.contains(EncounterAndObsDataSetDefinition.ColumnDisplayFormat.BEST_SHORT_NAME)) {
          String conceptName=obs.getValueAsString(Context.getLocale());
          columnValue.append(maxColumnHeaderWidth != null && conceptName.length() > maxColumnHeaderWidth - columnValue.length() ? conceptName.substring(0,maxColumnHeaderWidth - columnValue.length() - 1) : conceptName);
        }
        row.addColumnValue(obsDsc,(obs != null) ? columnValue.toString() : EMPTY);
      }
 else {
        row.addColumnValue(obsDsc,(obs != null) ? obs.getValueAsString(Context.getLocale()) : EMPTY);
      }
      String dateColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 5 : null);
      DataSetColumn obsDscDate=new DataSetColumn(dateColumnName + "String_Node_Str",dateColumnName + "String_Node_Str",String.class);
      row.addColumnValue(obsDscDate,(obs != null) ? obs.getObsDatetime().toString() : EMPTY);
      String parentColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 7 : null);
      DataSetColumn obsDscParent=new DataSetColumn(parentColumnName + "String_Node_Str",parentColumnName + "String_Node_Str",String.class);
      row.addColumnValue(obsDscParent,(obs != null && obs.getObsGroup() != null) ? obs.getObsGroup().getId() : EMPTY);
      if (optionalColumns != null) {
        if (optionalColumns.contains(EncounterAndObsDataSetDefinition.ObsOptionalColumn.VALUE_MODIFIER)) {
          String valModColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 10 : null);
          DataSetColumn obsDscValueModifier=new DataSetColumn(valModColumnName + "String_Node_Str",valModColumnName + "String_Node_Str",String.class);
          row.addColumnValue(obsDscValueModifier,(obs != null) ? obs.getValueModifier() : EMPTY);
        }
        if (optionalColumns.contains(EncounterAndObsDataSetDefinition.ObsOptionalColumn.ACCESSION_NUMBER)) {
          String accessionNumColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 14 : null);
          DataSetColumn obsDscAccessionNumber=new DataSetColumn(accessionNumColumnName + "String_Node_Str",accessionNumColumnName + "String_Node_Str",String.class);
          row.addColumnValue(obsDscAccessionNumber,(obs != null) ? obs.getAccessionNumber() : EMPTY);
        }
        if (optionalColumns.contains(EncounterAndObsDataSetDefinition.ObsOptionalColumn.COMMENT)) {
          String commentColumnName=columnKey.format(columnDisplayFormat,maxColumnHeaderWidth != null ? maxColumnHeaderWidth - 8 : null);
          DataSetColumn obsDscComment=new DataSetColumn(commentColumnName + "String_Node_Str",commentColumnName + "String_Node_Str",String.class);
          row.addColumnValue(obsDscComment,(obs != null) ? obs.getComment() : EMPTY);
        }
      }
    }
    dataSet.addRow(row);
  }
  return dataSet;
}