Program: 189283
Project: 189283
Buggy source:
public Note update(Note pendingNote,Organization requestingOrganization) throws NoteNonOverrideableException, WorkflowStepNonOverrideableException {
  Note resultingNote=null;
  Note persistedNote=noteRepo.findOne(pendingNote.getId());
  boolean overridabilityOfPersistedNote=persistedNote.getOverrideable();
  boolean overridabilityOfOriginatingWorkflowStep=persistedNote.getOriginatingWorkflowStep().getOverrideable();
  WorkflowStep workflowStepWithNoteOnRequestingOrganization=null;
  boolean requestingOrganizationOriginatedWorkflowStep=false;
  boolean workflowStepWithNoteOnRequestingOrganizationOriginatedNote=false;
  for (  WorkflowStep workflowStep : requestingOrganization.getAggregateWorkflowSteps()) {
    if (workflowStep.getAggregateNotes().contains(persistedNote)) {
      workflowStepWithNoteOnRequestingOrganization=workflowStep;
      requestingOrganizationOriginatedWorkflowStep=workflowStepWithNoteOnRequestingOrganization.getOriginatingOrganization().getId().equals(requestingOrganization.getId());
    }
  }
  if (workflowStepWithNoteOnRequestingOrganization != null) {
    workflowStepWithNoteOnRequestingOrganizationOriginatedNote=persistedNote.getOriginatingWorkflowStep().getId().equals(workflowStepWithNoteOnRequestingOrganization.getId());
  }
  if (!overridabilityOfOriginatingWorkflowStep && !requestingOrganizationOriginatedWorkflowStep) {
    throw new WorkflowStepNonOverrideableException();
  }
  if (!overridabilityOfPersistedNote && !(workflowStepWithNoteOnRequestingOrganizationOriginatedNote && requestingOrganizationOriginatedWorkflowStep)) {
    throw new NoteNonOverrideableException();
  }
  if (requestingOrganizationOriginatedWorkflowStep) {
    resultingNote=noteRepo.save(pendingNote);
  }
 else {
    em.detach(pendingNote);
    pendingNote.setId(null);
    WorkflowStep persistedOriginatingWorkflowStep=persistedNote.getOriginatingWorkflowStep();
    if (!requestingOrganizationOriginatedWorkflowStep) {
      WorkflowStep existingOriginatingWorkflowStep=workflowStepRepo.findByNameAndOriginatingOrganization(persistedOriginatingWorkflowStep.getName(),requestingOrganization);
      if (existingOriginatingWorkflowStep == null) {
        WorkflowStep newOriginatingWorkflowStep=workflowStepRepo.update(persistedOriginatingWorkflowStep,requestingOrganization);
        pendingNote.setOriginatingWorkflowStep(newOriginatingWorkflowStep);
      }
 else {
        pendingNote.setOriginatingWorkflowStep(existingOriginatingWorkflowStep);
      }
    }
    pendingNote.setOriginatingNote(null);
    Note newNote=noteRepo.save(pendingNote);
    for (    WorkflowStep workflowStep : getContainingDescendantWorkflowStep(requestingOrganization,persistedNote)) {
      workflowStep.replaceAggregateNote(persistedNote,newNote);
      workflowStepRepo.save(workflowStep);
    }
    for (    WorkflowStep workflowStep : requestingOrganization.getAggregateWorkflowSteps()) {
      if (workflowStep.getAggregateNotes().contains(persistedNote)) {
        workflowStep.replaceAggregateNote(persistedNote,newNote);
        workflowStepRepo.save(workflowStep);
      }
    }
    if (workflowStepRepo.findByAggregateNotesId(persistedNote.getId()).size() == 0) {
      noteRepo.delete(persistedNote);
    }
 else {
      newNote.setOriginatingNote(persistedNote);
      newNote=noteRepo.save(newNote);
    }
    resultingNote=newNote;
  }
  return resultingNote;
}

Fixed version (ground truth):
public Note update(Note pendingNote,Organization requestingOrganization) throws NoteNonOverrideableException, WorkflowStepNonOverrideableException {
  Note resultingNote=null;
  Note persistedNote=noteRepo.findOne(pendingNote.getId());
  boolean overridabilityOfPersistedNote=persistedNote.getOverrideable();
  boolean overridabilityOfOriginatingWorkflowStep=persistedNote.getOriginatingWorkflowStep().getOverrideable();
  WorkflowStep workflowStepWithNoteOnRequestingOrganization=null;
  boolean requestingOrganizationOriginatedWorkflowStep=false;
  boolean workflowStepWithNoteOnRequestingOrganizationOriginatedNote=false;
  for (  WorkflowStep workflowStep : requestingOrganization.getAggregateWorkflowSteps()) {
    if (workflowStep.getAggregateNotes().contains(persistedNote)) {
      workflowStepWithNoteOnRequestingOrganization=workflowStep;
      requestingOrganizationOriginatedWorkflowStep=workflowStepWithNoteOnRequestingOrganization.getOriginatingOrganization().getId().equals(requestingOrganization.getId());
    }
  }
  if (workflowStepWithNoteOnRequestingOrganization != null) {
    workflowStepWithNoteOnRequestingOrganizationOriginatedNote=persistedNote.getOriginatingWorkflowStep().getId().equals(workflowStepWithNoteOnRequestingOrganization.getId());
  }
  if (!overridabilityOfOriginatingWorkflowStep && !requestingOrganizationOriginatedWorkflowStep) {
    throw new WorkflowStepNonOverrideableException();
  }
  if (!overridabilityOfPersistedNote && !(workflowStepWithNoteOnRequestingOrganizationOriginatedNote && requestingOrganizationOriginatedWorkflowStep)) {
    throw new NoteNonOverrideableException();
  }
  if (workflowStepWithNoteOnRequestingOrganizationOriginatedNote && requestingOrganizationOriginatedWorkflowStep) {
    resultingNote=noteRepo.save(pendingNote);
  }
 else {
    em.detach(pendingNote);
    pendingNote.setId(null);
    WorkflowStep persistedOriginatingWorkflowStep=persistedNote.getOriginatingWorkflowStep();
    if (!requestingOrganizationOriginatedWorkflowStep) {
      WorkflowStep existingOriginatingWorkflowStep=workflowStepRepo.findByNameAndOriginatingOrganization(persistedOriginatingWorkflowStep.getName(),requestingOrganization);
      if (existingOriginatingWorkflowStep == null) {
        WorkflowStep newOriginatingWorkflowStep=workflowStepRepo.update(persistedOriginatingWorkflowStep,requestingOrganization);
        pendingNote.setOriginatingWorkflowStep(newOriginatingWorkflowStep);
      }
 else {
        pendingNote.setOriginatingWorkflowStep(existingOriginatingWorkflowStep);
      }
    }
    pendingNote.setOriginatingNote(null);
    Note newNote=noteRepo.save(pendingNote);
    for (    WorkflowStep workflowStep : getContainingDescendantWorkflowStep(requestingOrganization,persistedNote)) {
      workflowStep.replaceAggregateNote(persistedNote,newNote);
      workflowStepRepo.save(workflowStep);
    }
    for (    WorkflowStep workflowStep : requestingOrganization.getAggregateWorkflowSteps()) {
      if (workflowStep.getAggregateNotes().contains(persistedNote)) {
        workflowStep.replaceAggregateNote(persistedNote,newNote);
        workflowStepRepo.save(workflowStep);
      }
    }
    if (workflowStepRepo.findByAggregateNotesId(persistedNote.getId()).size() == 0) {
      noteRepo.delete(persistedNote);
    }
 else {
      newNote.setOriginatingNote(persistedNote);
      newNote=noteRepo.save(newNote);
    }
    resultingNote=newNote;
  }
  return resultingNote;
}