Program: 188294
Project: 188294
Buggy source:
/** 
 * Create a new panel to edit a user
 * @param user the user to edit
 */
public EditDescriptiveMetadata(final String aipId,final String representationId,final DescriptiveMetadataEditBundle bundleParam){
  this.aipId=aipId;
  this.representationId=representationId;
  this.bundle=bundleParam;
  Set<MetadataValue> newValues=null;
  if (bundle.getValues() != null) {
    newValues=new TreeSet<>();
    for (    MetadataValue mv : bundle.getValues())     newValues.add(mv.copy());
  }
  supportedBundle=new SupportedMetadataTypeBundle(bundle.getId(),bundle.getType(),bundle.getVersion(),bundle.getId(),bundle.getRawTemplate(),newValues);
  initWidget(uiBinder.createAndBindUi(this));
  metadataXML=new TextArea();
  metadataXML.addStyleName("String_Node_Str");
  metadataXML.setTitle("String_Node_Str");
  id.setText(bundle.getId());
  id.setEnabled(false);
  type.addChangeHandler(new ChangeHandler(){
    @Override public void onChange(    ChangeEvent changeEvent){
      String typeString=null;
      String version="String_Node_Str";
      String value=type.getSelectedValue();
      if (value.contains(RodaConstants.METADATA_VERSION_SEPARATOR) && bundle.getVersion() != null) {
        typeString=value.substring(0,value.lastIndexOf(RodaConstants.METADATA_VERSION_SEPARATOR));
        version=value.substring(value.lastIndexOf(RodaConstants.METADATA_VERSION_SEPARATOR) + 1,value.length());
      }
      if (typeString == null) {
        typeString=value;
      }
      BrowserService.Util.getInstance().retrieveDescriptiveMetadataEditBundle(aipId,representationId,bundle.getId(),typeString,version,LocaleInfo.getCurrentLocale().getLocaleName(),new AsyncCallback<DescriptiveMetadataEditBundle>(){
        @Override public void onFailure(        Throwable caught){
          AsyncCallbackUtils.defaultFailureTreatment(caught);
        }
        @Override public void onSuccess(        DescriptiveMetadataEditBundle editBundle){
          bundle=editBundle;
          Set<MetadataValue> newValues=null;
          if (bundle.getValues() != null) {
            newValues=new TreeSet<>();
            for (            MetadataValue mv : bundle.getValues())             newValues.add(mv.copy());
          }
          supportedBundle=new SupportedMetadataTypeBundle(bundle.getId(),bundle.getType(),bundle.getVersion(),bundle.getId(),bundle.getRawTemplate(),newValues);
          updateFormOrXML();
        }
      }
);
    }
  }
);
  BrowserService.Util.getInstance().retrieveSupportedMetadata(aipId,representationId,LocaleInfo.getCurrentLocale().getLocaleName(),new AsyncCallback<List<SupportedMetadataTypeBundle>>(){
    @Override public void onFailure(    Throwable caught){
      AsyncCallbackUtils.defaultFailureTreatment(caught);
    }
    @Override public void onSuccess(    List<SupportedMetadataTypeBundle> metadataTypes){
      int selected=-1;
      int index=0;
      Map<String,Integer> types=new HashMap<>();
      for (      SupportedMetadataTypeBundle b : metadataTypes) {
        if (b.getVersion() != null) {
          type.addItem(b.getLabel(),b.getType() + RodaConstants.METADATA_VERSION_SEPARATOR + b.getVersion());
        }
 else {
          type.addItem(b.getLabel(),b.getType());
        }
        String lowerCaseType=bundle.getType() != null ? bundle.getType().toLowerCase() : null;
        if (b.getType().equalsIgnoreCase(lowerCaseType)) {
          String lowerCaseVersion=bundle.getVersion() != null ? bundle.getVersion().toLowerCase() : null;
          if (b.getVersion() != null && lowerCaseVersion != null) {
            if (lowerCaseVersion != null && b.getVersion().equalsIgnoreCase(lowerCaseVersion)) {
              selected=index;
            }
          }
 else           if (b.getVersion() == null && lowerCaseVersion == null) {
            selected=index;
          }
        }
        types.put(b.getType(),index);
        index++;
      }
      updateFormOrXML();
      if (selected >= 0) {
        type.addItem(messages.otherItem(),"String_Node_Str");
        type.setSelectedIndex(selected);
      }
 else       if ("String_Node_Str".equals(bundle.getType())) {
        type.addItem(messages.otherItem(),"String_Node_Str");
        type.setSelectedIndex(type.getItemCount() - 1);
      }
 else {
        if (!types.keySet().contains(bundle.getType())) {
          type.addItem(messages.otherItem() + "String_Node_Str" + bundle.getType()+ "String_Node_Str",bundle.getType());
          type.addItem(messages.otherItem(),"String_Node_Str");
          type.setSelectedIndex(type.getItemCount() - 2);
        }
 else {
          type.addItem(messages.otherItem(),"String_Node_Str");
          type.setSelectedIndex(types.get(bundle.getType()));
        }
      }
    }
  }
);
  Element firstElement=showXml.getElement().getFirstChildElement();
  if ("String_Node_Str".equalsIgnoreCase(firstElement.getTagName())) {
    firstElement.setAttribute("String_Node_Str","String_Node_Str");
  }
}

Fixed version (ground truth):
/** 
 * Create a new panel to edit a user
 * @param user the user to edit
 */
public EditDescriptiveMetadata(final String aipId,final String representationId,final DescriptiveMetadataEditBundle bundleParam){
  this.aipId=aipId;
  this.representationId=representationId;
  this.bundle=bundleParam;
  Set<MetadataValue> newValues=null;
  if (bundle.getValues() != null) {
    newValues=new TreeSet<>();
    for (    MetadataValue mv : bundle.getValues()) {
      newValues.add(mv.copy());
    }
  }
  supportedBundle=new SupportedMetadataTypeBundle(bundle.getId(),bundle.getType(),bundle.getVersion(),bundle.getId(),bundle.getRawTemplate(),newValues);
  initWidget(uiBinder.createAndBindUi(this));
  metadataXML=new TextArea();
  metadataXML.addStyleName("String_Node_Str");
  metadataXML.setTitle("String_Node_Str");
  id.setText(bundle.getId());
  id.setEnabled(false);
  type.addChangeHandler(new ChangeHandler(){
    @Override public void onChange(    ChangeEvent changeEvent){
      String typeString=null;
      String version=null;
      String value=type.getSelectedValue();
      if (value.contains(RodaConstants.METADATA_VERSION_SEPARATOR) && bundle.getVersion() != null) {
        typeString=value.substring(0,value.lastIndexOf(RodaConstants.METADATA_VERSION_SEPARATOR));
        version=value.substring(value.lastIndexOf(RodaConstants.METADATA_VERSION_SEPARATOR) + 1,value.length());
      }
      if (typeString == null) {
        typeString=value;
      }
      BrowserService.Util.getInstance().retrieveDescriptiveMetadataEditBundle(aipId,representationId,bundle.getId(),typeString,version,LocaleInfo.getCurrentLocale().getLocaleName(),new AsyncCallback<DescriptiveMetadataEditBundle>(){
        @Override public void onFailure(        Throwable caught){
          AsyncCallbackUtils.defaultFailureTreatment(caught);
        }
        @Override public void onSuccess(        DescriptiveMetadataEditBundle editBundle){
          bundle=editBundle;
          Set<MetadataValue> newValues=null;
          if (bundle.getValues() != null) {
            newValues=new TreeSet<>();
            for (            MetadataValue mv : bundle.getValues())             newValues.add(mv.copy());
          }
          supportedBundle=new SupportedMetadataTypeBundle(bundle.getId(),bundle.getType(),bundle.getVersion(),bundle.getId(),bundle.getRawTemplate(),newValues);
          updateFormOrXML();
        }
      }
);
    }
  }
);
  BrowserService.Util.getInstance().retrieveSupportedMetadata(aipId,representationId,LocaleInfo.getCurrentLocale().getLocaleName(),new AsyncCallback<List<SupportedMetadataTypeBundle>>(){
    @Override public void onFailure(    Throwable caught){
      AsyncCallbackUtils.defaultFailureTreatment(caught);
    }
    @Override public void onSuccess(    List<SupportedMetadataTypeBundle> metadataTypes){
      int selected=-1;
      int index=0;
      Map<String,Integer> types=new HashMap<>();
      for (      SupportedMetadataTypeBundle b : metadataTypes) {
        if (b.getVersion() != null) {
          type.addItem(b.getLabel(),b.getType() + RodaConstants.METADATA_VERSION_SEPARATOR + b.getVersion());
        }
 else {
          type.addItem(b.getLabel(),b.getType());
        }
        String lowerCaseType=bundle.getType() != null ? bundle.getType().toLowerCase() : null;
        if (b.getType().equalsIgnoreCase(lowerCaseType)) {
          String lowerCaseVersion=bundle.getVersion() != null ? bundle.getVersion().toLowerCase() : null;
          if (b.getVersion() != null && lowerCaseVersion != null) {
            if (lowerCaseVersion != null && b.getVersion().equalsIgnoreCase(lowerCaseVersion)) {
              selected=index;
            }
          }
 else           if (b.getVersion() == null && lowerCaseVersion == null) {
            selected=index;
          }
        }
        types.put(b.getType(),index);
        index++;
      }
      updateFormOrXML();
      if (selected >= 0) {
        type.addItem(messages.otherItem(),"String_Node_Str");
        type.setSelectedIndex(selected);
      }
 else       if ("String_Node_Str".equals(bundle.getType())) {
        type.addItem(messages.otherItem(),"String_Node_Str");
        type.setSelectedIndex(type.getItemCount() - 1);
      }
 else {
        if (!types.keySet().contains(bundle.getType())) {
          type.addItem(messages.otherItem() + "String_Node_Str" + bundle.getType()+ "String_Node_Str",bundle.getType());
          type.addItem(messages.otherItem(),"String_Node_Str");
          type.setSelectedIndex(type.getItemCount() - 2);
        }
 else {
          type.addItem(messages.otherItem(),"String_Node_Str");
          type.setSelectedIndex(types.get(bundle.getType()));
        }
      }
    }
  }
);
  Element firstElement=showXml.getElement().getFirstChildElement();
  if ("String_Node_Str".equalsIgnoreCase(firstElement.getTagName())) {
    firstElement.setAttribute("String_Node_Str","String_Node_Str");
  }
}