Program: 189282
Project: 189282
Buggy source:
public FieldProfile update(FieldProfile pendingFieldProfile,Organization requestingOrganization) throws FieldProfileNonOverrideableException, WorkflowStepNonOverrideableException {
  FieldProfile resultingFieldProfile=null;
  FieldProfile persistedFieldProfile=fieldProfileRepo.findOne(pendingFieldProfile.getId());
  boolean overridabilityOfPersistedFieldProfile=persistedFieldProfile.getOverrideable();
  boolean overridabilityOfOriginatingWorkflowStep=persistedFieldProfile.getOriginatingWorkflowStep().getOverrideable();
  WorkflowStep workflowStepWithFieldProfileOnRequestingOrganization=null;
  boolean requestingOrganizationOriginatedWorkflowStep=false;
  boolean workflowStepWithFieldProfileOnRequestingOrganizationOriginatedFieldProfile=false;
  for (  WorkflowStep workflowStep : requestingOrganization.getAggregateWorkflowSteps()) {
    if (workflowStep.getAggregateFieldProfiles().contains(persistedFieldProfile)) {
      workflowStepWithFieldProfileOnRequestingOrganization=workflowStep;
      requestingOrganizationOriginatedWorkflowStep=workflowStepWithFieldProfileOnRequestingOrganization.getOriginatingOrganization().getId().equals(requestingOrganization.getId());
    }
  }
  if (workflowStepWithFieldProfileOnRequestingOrganization != null) {
    workflowStepWithFieldProfileOnRequestingOrganizationOriginatedFieldProfile=persistedFieldProfile.getOriginatingWorkflowStep().getId().equals(workflowStepWithFieldProfileOnRequestingOrganization.getId());
  }
  if (!overridabilityOfOriginatingWorkflowStep && !requestingOrganizationOriginatedWorkflowStep) {
    throw new WorkflowStepNonOverrideableException();
  }
  if (!overridabilityOfPersistedFieldProfile && !(workflowStepWithFieldProfileOnRequestingOrganizationOriginatedFieldProfile && requestingOrganizationOriginatedWorkflowStep)) {
    throw new FieldProfileNonOverrideableException();
  }
  if (requestingOrganizationOriginatedWorkflowStep) {
    resultingFieldProfile=fieldProfileRepo.save(pendingFieldProfile);
  }
 else {
    List<FieldGloss> fieldGlosses=new ArrayList<FieldGloss>();
    List<ControlledVocabulary> controlledVocabularies=new ArrayList<ControlledVocabulary>();
    for (    FieldGloss fg : pendingFieldProfile.getFieldGlosses()) {
      fieldGlosses.add(fg);
    }
    for (    ControlledVocabulary cv : pendingFieldProfile.getControlledVocabularies()) {
      controlledVocabularies.add(cv);
    }
    pendingFieldProfile.setFieldGlosses(new ArrayList<FieldGloss>());
    pendingFieldProfile.setControlledVocabularies(new ArrayList<ControlledVocabulary>());
    em.detach(pendingFieldProfile);
    pendingFieldProfile.setId(null);
    WorkflowStep persistedOriginatingWorkflowStep=persistedFieldProfile.getOriginatingWorkflowStep();
    if (!requestingOrganizationOriginatedWorkflowStep) {
      WorkflowStep existingOriginatingWorkflowStep=workflowStepRepo.findByNameAndOriginatingOrganization(persistedOriginatingWorkflowStep.getName(),requestingOrganization);
      if (existingOriginatingWorkflowStep == null) {
        WorkflowStep newOriginatingWorkflowStep=workflowStepRepo.update(persistedOriginatingWorkflowStep,requestingOrganization);
        pendingFieldProfile.setOriginatingWorkflowStep(newOriginatingWorkflowStep);
      }
 else {
        pendingFieldProfile.setOriginatingWorkflowStep(existingOriginatingWorkflowStep);
      }
    }
    pendingFieldProfile.setOriginatingFieldProfile(null);
    pendingFieldProfile.setFieldGlosses(fieldGlosses);
    pendingFieldProfile.setControlledVocabularies(controlledVocabularies);
    FieldProfile newFieldProfile=fieldProfileRepo.save(pendingFieldProfile);
    for (    WorkflowStep workflowStep : getContainingDescendantWorkflowStep(requestingOrganization,persistedFieldProfile)) {
      workflowStep.replaceAggregateFieldProfile(persistedFieldProfile,newFieldProfile);
      workflowStepRepo.save(workflowStep);
    }
    for (    WorkflowStep workflowStep : requestingOrganization.getAggregateWorkflowSteps()) {
      if (workflowStep.getAggregateFieldProfiles().contains(persistedFieldProfile)) {
        workflowStep.replaceAggregateFieldProfile(persistedFieldProfile,newFieldProfile);
        workflowStepRepo.save(workflowStep);
      }
    }
    if (workflowStepRepo.findByAggregateFieldProfilesId(persistedFieldProfile.getId()).size() == 0) {
      fieldProfileRepo.delete(persistedFieldProfile);
    }
 else {
      newFieldProfile.setOriginatingFieldProfile(persistedFieldProfile);
      newFieldProfile=fieldProfileRepo.save(newFieldProfile);
    }
    resultingFieldProfile=newFieldProfile;
  }
  return resultingFieldProfile;
}

Fixed version (ground truth):
public FieldProfile update(FieldProfile pendingFieldProfile,Organization requestingOrganization) throws FieldProfileNonOverrideableException, WorkflowStepNonOverrideableException {
  FieldProfile resultingFieldProfile=null;
  FieldProfile persistedFieldProfile=fieldProfileRepo.findOne(pendingFieldProfile.getId());
  boolean overridabilityOfPersistedFieldProfile=persistedFieldProfile.getOverrideable();
  boolean overridabilityOfOriginatingWorkflowStep=persistedFieldProfile.getOriginatingWorkflowStep().getOverrideable();
  WorkflowStep workflowStepWithFieldProfileOnRequestingOrganization=null;
  boolean requestingOrganizationOriginatedWorkflowStep=false;
  boolean workflowStepWithFieldProfileOnRequestingOrganizationOriginatedFieldProfile=false;
  for (  WorkflowStep workflowStep : requestingOrganization.getAggregateWorkflowSteps()) {
    if (workflowStep.getAggregateFieldProfiles().contains(persistedFieldProfile)) {
      workflowStepWithFieldProfileOnRequestingOrganization=workflowStep;
      requestingOrganizationOriginatedWorkflowStep=workflowStepWithFieldProfileOnRequestingOrganization.getOriginatingOrganization().getId().equals(requestingOrganization.getId());
    }
  }
  if (workflowStepWithFieldProfileOnRequestingOrganization != null) {
    workflowStepWithFieldProfileOnRequestingOrganizationOriginatedFieldProfile=persistedFieldProfile.getOriginatingWorkflowStep().getId().equals(workflowStepWithFieldProfileOnRequestingOrganization.getId());
  }
  if (!overridabilityOfOriginatingWorkflowStep && !requestingOrganizationOriginatedWorkflowStep) {
    throw new WorkflowStepNonOverrideableException();
  }
  if (!overridabilityOfPersistedFieldProfile && !(workflowStepWithFieldProfileOnRequestingOrganizationOriginatedFieldProfile && requestingOrganizationOriginatedWorkflowStep)) {
    throw new FieldProfileNonOverrideableException();
  }
  if (workflowStepWithFieldProfileOnRequestingOrganizationOriginatedFieldProfile && requestingOrganizationOriginatedWorkflowStep) {
    resultingFieldProfile=fieldProfileRepo.save(pendingFieldProfile);
  }
 else {
    List<FieldGloss> fieldGlosses=new ArrayList<FieldGloss>();
    List<ControlledVocabulary> controlledVocabularies=new ArrayList<ControlledVocabulary>();
    for (    FieldGloss fg : pendingFieldProfile.getFieldGlosses()) {
      fieldGlosses.add(fg);
    }
    for (    ControlledVocabulary cv : pendingFieldProfile.getControlledVocabularies()) {
      controlledVocabularies.add(cv);
    }
    pendingFieldProfile.setFieldGlosses(new ArrayList<FieldGloss>());
    pendingFieldProfile.setControlledVocabularies(new ArrayList<ControlledVocabulary>());
    em.detach(pendingFieldProfile);
    pendingFieldProfile.setId(null);
    WorkflowStep persistedOriginatingWorkflowStep=persistedFieldProfile.getOriginatingWorkflowStep();
    if (!requestingOrganizationOriginatedWorkflowStep) {
      WorkflowStep existingOriginatingWorkflowStep=workflowStepRepo.findByNameAndOriginatingOrganization(persistedOriginatingWorkflowStep.getName(),requestingOrganization);
      if (existingOriginatingWorkflowStep == null) {
        WorkflowStep newOriginatingWorkflowStep=workflowStepRepo.update(persistedOriginatingWorkflowStep,requestingOrganization);
        pendingFieldProfile.setOriginatingWorkflowStep(newOriginatingWorkflowStep);
      }
 else {
        pendingFieldProfile.setOriginatingWorkflowStep(existingOriginatingWorkflowStep);
      }
    }
    pendingFieldProfile.setOriginatingFieldProfile(null);
    pendingFieldProfile.setFieldGlosses(fieldGlosses);
    pendingFieldProfile.setControlledVocabularies(controlledVocabularies);
    FieldProfile newFieldProfile=fieldProfileRepo.save(pendingFieldProfile);
    for (    WorkflowStep workflowStep : getContainingDescendantWorkflowStep(requestingOrganization,persistedFieldProfile)) {
      workflowStep.replaceAggregateFieldProfile(persistedFieldProfile,newFieldProfile);
      workflowStepRepo.save(workflowStep);
    }
    for (    WorkflowStep workflowStep : requestingOrganization.getAggregateWorkflowSteps()) {
      if (workflowStep.getAggregateFieldProfiles().contains(persistedFieldProfile)) {
        workflowStep.replaceAggregateFieldProfile(persistedFieldProfile,newFieldProfile);
        workflowStepRepo.save(workflowStep);
      }
    }
    if (workflowStepRepo.findByAggregateFieldProfilesId(persistedFieldProfile.getId()).size() == 0) {
      fieldProfileRepo.delete(persistedFieldProfile);
    }
 else {
      newFieldProfile.setOriginatingFieldProfile(persistedFieldProfile);
      newFieldProfile=fieldProfileRepo.save(newFieldProfile);
    }
    resultingFieldProfile=newFieldProfile;
  }
  return resultingFieldProfile;
}