Program: 66422
Project: 66422
Buggy source:
/** 
 * INTERNAL: Update private owned part.
 */
protected void postUpdateWithChangeSetListOrder(WriteObjectQuery writeQuery) throws DatabaseException {
  ObjectChangeSet changeSet=writeQuery.getObjectChangeSet();
  DirectCollectionChangeRecord changeRecord=(DirectCollectionChangeRecord)changeSet.getChangesForAttributeNamed(this.getAttributeName());
  if (changeRecord == null) {
    return;
  }
  for (int index=0; index < getReferenceKeyFields().size(); index++) {
    DatabaseField referenceKey=getReferenceKeyFields().get(index);
    DatabaseField sourceKey=getSourceKeyFields().get(index);
    Object sourceKeyValue=writeQuery.getTranslationRow().get(sourceKey);
    writeQuery.getTranslationRow().put(referenceKey,sourceKeyValue);
  }
  boolean shouldRepairOrder=false;
  if ((List)changeRecord.getLatestCollection() instanceof IndirectList) {
    shouldRepairOrder=((IndirectList)changeRecord.getLatestCollection()).isListOrderBrokenInDb();
  }
  if (shouldRepairOrder) {
    DeleteObjectQuery deleteQuery=new DeleteObjectQuery();
    deleteQuery.setObject(writeQuery.getObject());
    deleteQuery.setSession(writeQuery.getSession());
    deleteQuery.setTranslationRow(writeQuery.getTranslationRow());
    Object[] eventDeleteAll=new Object[2];
    eventDeleteAll[0]=DeleteAll;
    eventDeleteAll[1]=deleteQuery;
    writeQuery.getSession().getCommitManager().addDataModificationEvent(this,eventDeleteAll);
    for (int i=0; i < ((List)changeRecord.getLatestCollection()).size(); i++) {
      Object value=((List)changeRecord.getLatestCollection()).get(i);
      value=getFieldValue(value,writeQuery.getSession());
      AbstractRecord insertRow=writeQuery.getTranslationRow().clone();
      insertRow.add(getDirectField(),value);
      insertRow.add(this.listOrderField,i);
      Object[] event=new Object[3];
      event[0]=Insert;
      event[1]=getInsertQuery();
      event[2]=insertRow;
      writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
    }
    ((IndirectList)changeRecord.getLatestCollection()).setIsListOrderBrokenInDb(false);
    changeRecord.setOrderHasBeenRepaired(true);
    return;
  }
  if (changeRecord.getChangedIndexes() == null) {
    compareListsForChange((List)changeRecord.getOriginalCollection(),(List)changeRecord.getLatestCollection(),changeRecord,writeQuery.getSession());
  }
  Iterator<Map.Entry<Object,Set[]>> it=changeRecord.getChangedIndexes().entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry<Object,Set[]> entry=it.next();
    Object value=entry.getKey();
    if (getValueConverter() != null) {
      value=getValueConverter().convertObjectValueToDataValue(value,writeQuery.getSession());
    }
    Set[] indexes=entry.getValue();
    Set indexesBefore=indexes[0];
    Set indexesAfter=indexes[1];
    if (indexesAfter == null) {
      AbstractRecord deleteRow=writeQuery.getTranslationRow().clone();
      deleteRow.add(getDirectField(),value);
      Object[] event=new Object[3];
      event[0]=Delete;
      event[1]=getDeleteQuery();
      event[2]=deleteRow;
      writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
    }
 else     if (indexesAfter.isEmpty()) {
      Iterator<Integer> itBefore=indexesBefore.iterator();
      while (itBefore.hasNext()) {
        AbstractRecord deleteAtIndexRow=writeQuery.getTranslationRow().clone();
        deleteAtIndexRow.add(getDirectField(),value);
        deleteAtIndexRow.add(this.listOrderField,itBefore.next());
        Object[] event=new Object[3];
        event[0]=DeleteAtIndex;
        event[1]=deleteAtIndexQuery;
        event[2]=deleteAtIndexRow;
        writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
      }
    }
 else {
      if (indexesBefore == null || indexesBefore.isEmpty()) {
        Iterator<Integer> itAfter=indexesAfter.iterator();
        while (itAfter.hasNext()) {
          AbstractRecord insertRow=writeQuery.getTranslationRow().clone();
          insertRow.add(getDirectField(),value);
          insertRow.add(this.listOrderField,itAfter.next());
          Object[] event=new Object[3];
          event[0]=Insert;
          event[1]=getInsertQuery();
          event[2]=insertRow;
          writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
        }
      }
 else {
        Iterator<Integer> itBefore=indexesBefore.iterator();
        Iterator<Integer> itAfter=indexesAfter.iterator();
        while (itBefore.hasNext() || itAfter.hasNext()) {
          if (itBefore.hasNext()) {
            if (itAfter.hasNext()) {
              AbstractRecord updateAtIndexRow=writeQuery.getTranslationRow().clone();
              updateAtIndexRow.add(getDirectField(),value);
              updateAtIndexRow.add(this.listOrderField,itBefore.next());
              Object[] event=new Object[4];
              event[0]=UpdateAtIndex;
              event[1]=updateAtIndexQuery;
              event[2]=updateAtIndexRow;
              DatabaseRecord modifyRow=new DatabaseRecord(1);
              modifyRow.add(this.listOrderField,itAfter.next());
              event[3]=modifyRow;
              writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
            }
 else {
              AbstractRecord deleteAtIndexRow=writeQuery.getTranslationRow().clone();
              deleteAtIndexRow.add(getDirectField(),value);
              deleteAtIndexRow.add(this.listOrderField,itBefore.next());
              Object[] event=new Object[3];
              event[0]=DeleteAtIndex;
              event[1]=deleteAtIndexQuery;
              event[2]=deleteAtIndexRow;
              writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
            }
          }
 else {
            AbstractRecord insertRow=writeQuery.getTranslationRow().clone();
            insertRow.add(getDirectField(),value);
            insertRow.add(this.listOrderField,itAfter.next());
            Object[] event=new Object[3];
            event[0]=Insert;
            event[1]=getInsertQuery();
            event[2]=insertRow;
            writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
          }
        }
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * INTERNAL: Update private owned part.
 */
protected void postUpdateWithChangeSetListOrder(WriteObjectQuery writeQuery) throws DatabaseException {
  ObjectChangeSet changeSet=writeQuery.getObjectChangeSet();
  DirectCollectionChangeRecord changeRecord=(DirectCollectionChangeRecord)changeSet.getChangesForAttributeNamed(this.getAttributeName());
  if (changeRecord == null) {
    return;
  }
  for (int index=0; index < getReferenceKeyFields().size(); index++) {
    DatabaseField referenceKey=getReferenceKeyFields().get(index);
    DatabaseField sourceKey=getSourceKeyFields().get(index);
    Object sourceKeyValue=writeQuery.getTranslationRow().get(sourceKey);
    writeQuery.getTranslationRow().put(referenceKey,sourceKeyValue);
  }
  boolean shouldRepairOrder=false;
  if ((List)changeRecord.getLatestCollection() instanceof IndirectList) {
    shouldRepairOrder=((IndirectList)changeRecord.getLatestCollection()).isListOrderBrokenInDb();
  }
  if (shouldRepairOrder) {
    DeleteObjectQuery deleteQuery=new DeleteObjectQuery();
    deleteQuery.setObject(writeQuery.getObject());
    deleteQuery.setSession(writeQuery.getSession());
    deleteQuery.setTranslationRow(writeQuery.getTranslationRow());
    Object[] eventDeleteAll=new Object[2];
    eventDeleteAll[0]=DeleteAll;
    eventDeleteAll[1]=deleteQuery;
    writeQuery.getSession().getCommitManager().addDataModificationEvent(this,eventDeleteAll);
    for (int i=0; i < ((List)changeRecord.getLatestCollection()).size(); i++) {
      Object value=((List)changeRecord.getLatestCollection()).get(i);
      value=getFieldValue(value,writeQuery.getSession());
      AbstractRecord insertRow=writeQuery.getTranslationRow().clone();
      insertRow.add(getDirectField(),value);
      insertRow.add(this.listOrderField,i);
      Object[] event=new Object[3];
      event[0]=Insert;
      event[1]=getInsertQuery();
      event[2]=insertRow;
      writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
    }
    ((IndirectList)changeRecord.getLatestCollection()).setIsListOrderBrokenInDb(false);
    changeRecord.setOrderHasBeenRepaired(true);
    return;
  }
  if (changeRecord.getChangedIndexes() == null) {
    compareListsForChange((List)changeRecord.getOriginalCollection(),(List)changeRecord.getLatestCollection(),changeRecord,writeQuery.getSession());
  }
  Iterator<Map.Entry<Object,Set[]>> it=changeRecord.getChangedIndexes().entrySet().iterator();
  while (it.hasNext()) {
    Map.Entry<Object,Set[]> entry=it.next();
    Object value=entry.getKey();
    if (getValueConverter() != null) {
      value=getValueConverter().convertObjectValueToDataValue(value,writeQuery.getSession());
    }
    Set[] indexes=entry.getValue();
    Set indexesBefore=indexes[0];
    Set indexesAfter=indexes[1];
    if (indexesAfter == null) {
      AbstractRecord deleteRow=writeQuery.getTranslationRow().clone();
      Object[] event=new Object[3];
      event[0]=Delete;
      if (value == null) {
        event[1]=getDeleteNullQuery();
      }
 else {
        deleteRow.add(getDirectField(),value);
        event[1]=getDeleteQuery();
      }
      event[2]=deleteRow;
      writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
    }
 else     if (indexesAfter.isEmpty()) {
      Iterator<Integer> itBefore=indexesBefore.iterator();
      while (itBefore.hasNext()) {
        AbstractRecord deleteAtIndexRow=writeQuery.getTranslationRow().clone();
        deleteAtIndexRow.add(getDirectField(),value);
        deleteAtIndexRow.add(this.listOrderField,itBefore.next());
        Object[] event=new Object[3];
        event[0]=DeleteAtIndex;
        event[1]=deleteAtIndexQuery;
        event[2]=deleteAtIndexRow;
        writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
      }
    }
 else {
      if (indexesBefore == null || indexesBefore.isEmpty()) {
        Iterator<Integer> itAfter=indexesAfter.iterator();
        while (itAfter.hasNext()) {
          AbstractRecord insertRow=writeQuery.getTranslationRow().clone();
          insertRow.add(getDirectField(),value);
          insertRow.add(this.listOrderField,itAfter.next());
          Object[] event=new Object[3];
          event[0]=Insert;
          event[1]=getInsertQuery();
          event[2]=insertRow;
          writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
        }
      }
 else {
        Iterator<Integer> itBefore=indexesBefore.iterator();
        Iterator<Integer> itAfter=indexesAfter.iterator();
        while (itBefore.hasNext() || itAfter.hasNext()) {
          if (itBefore.hasNext()) {
            if (itAfter.hasNext()) {
              AbstractRecord updateAtIndexRow=writeQuery.getTranslationRow().clone();
              updateAtIndexRow.add(getDirectField(),value);
              updateAtIndexRow.add(this.listOrderField,itBefore.next());
              Object[] event=new Object[4];
              event[0]=UpdateAtIndex;
              event[1]=updateAtIndexQuery;
              event[2]=updateAtIndexRow;
              DatabaseRecord modifyRow=new DatabaseRecord(1);
              modifyRow.add(this.listOrderField,itAfter.next());
              event[3]=modifyRow;
              writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
            }
 else {
              AbstractRecord deleteAtIndexRow=writeQuery.getTranslationRow().clone();
              deleteAtIndexRow.add(getDirectField(),value);
              deleteAtIndexRow.add(this.listOrderField,itBefore.next());
              Object[] event=new Object[3];
              event[0]=DeleteAtIndex;
              event[1]=deleteAtIndexQuery;
              event[2]=deleteAtIndexRow;
              writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
            }
          }
 else {
            AbstractRecord insertRow=writeQuery.getTranslationRow().clone();
            insertRow.add(getDirectField(),value);
            insertRow.add(this.listOrderField,itAfter.next());
            Object[] event=new Object[3];
            event[0]=Insert;
            event[1]=getInsertQuery();
            event[2]=insertRow;
            writeQuery.getSession().getCommitManager().addDataModificationEvent(this,event);
          }
        }
      }
    }
  }
}