Program: 188275
Project: 188275
Buggy source:
public static void fixParents(IndexService index,ModelService model,Optional<String> jobId,Optional<String> computedSearchScope,String updatedBy) throws GenericException, RequestNotValidException, AuthorizationDeniedException, NotFoundException {
  Map<String,List<String>> aipIdToGhost=new HashMap<>();
  Map<String,List<String>> sipIdToGhost=new HashMap<>();
  Filter ghostsFilter=new Filter(new SimpleFilterParameter(RodaConstants.AIP_GHOST,Boolean.TRUE.toString()));
  jobId.ifPresent(id -> ghostsFilter.add(new SimpleFilterParameter(RodaConstants.INGEST_JOB_ID,id)));
  IterableIndexResult<IndexedAIP> ghosts=index.findAll(IndexedAIP.class,ghostsFilter,Arrays.asList(RodaConstants.INDEX_UUID,RodaConstants.INGEST_SIP_IDS));
  for (  IndexedAIP aip : ghosts) {
    if (aip.getIngestSIPIds() != null && !aip.getIngestSIPIds().isEmpty()) {
      List<String> temp=new ArrayList<>();
      String firstIngestSIPId=aip.getIngestSIPIds().get(0);
      if (sipIdToGhost.containsKey(firstIngestSIPId)) {
        temp=sipIdToGhost.get(firstIngestSIPId);
      }
      temp.add(aip.getId());
      sipIdToGhost.put(firstIngestSIPId,temp);
    }
 else {
      List<String> temp=new ArrayList<>();
      if (aipIdToGhost.containsKey(aip.getId())) {
        temp=aipIdToGhost.get(aip.getId());
      }
      temp.add(aip.getId());
      aipIdToGhost.put(aip.getId(),temp);
    }
  }
  for (  Map.Entry<String,List<String>> entry : sipIdToGhost.entrySet()) {
    Filter nonGhostsFilter=new Filter(new SimpleFilterParameter(RodaConstants.INGEST_SIP_IDS,entry.getKey()),new SimpleFilterParameter(RodaConstants.AIP_GHOST,Boolean.FALSE.toString()));
    computedSearchScope.ifPresent(id -> nonGhostsFilter.add(new SimpleFilterParameter(RodaConstants.AIP_ANCESTORS,id)));
    IndexResult<IndexedAIP> result=index.find(IndexedAIP.class,nonGhostsFilter,Sorter.NONE,new Sublist(0,1),Arrays.asList(RodaConstants.INDEX_UUID));
    if (result.getTotalCount() > 1) {
      LOGGER.debug("String_Node_Str",entry.getKey());
    }
 else     if (result.getTotalCount() == 1) {
      IndexedAIP newParentIAIP=result.getResults().get(0);
      for (      String id : entry.getValue()) {
        moveChildrenAIPsAndDelete(index,model,id,newParentIAIP.getId(),computedSearchScope,updatedBy);
      }
    }
 else     if (result.getTotalCount() == 0) {
      String ghostIdToKeep=entry.getValue().get(0);
      AIP ghostToKeep=model.retrieveAIP(ghostIdToKeep);
      ghostToKeep.setGhost(false);
      model.updateAIP(ghostToKeep,"String_Node_Str");
      if (entry.getValue().size() > 1) {
        for (int i=1; i < entry.getValue().size(); i++) {
          updateParent(index,model,entry.getValue().get(i),ghostIdToKeep,computedSearchScope);
        }
      }
    }
  }
}

Fixed version (ground truth):
public static void fixParents(IndexService index,ModelService model,Optional<String> jobId,Optional<String> computedSearchScope,String updatedBy) throws GenericException, RequestNotValidException, AuthorizationDeniedException, NotFoundException {
  Map<String,List<String>> aipIdToGhost=new HashMap<>();
  Map<String,List<String>> sipIdToGhost=new HashMap<>();
  Filter ghostsFilter=new Filter(new SimpleFilterParameter(RodaConstants.AIP_GHOST,Boolean.TRUE.toString()));
  jobId.ifPresent(id -> ghostsFilter.add(new SimpleFilterParameter(RodaConstants.INGEST_JOB_ID,id)));
  IterableIndexResult<IndexedAIP> ghosts=index.findAll(IndexedAIP.class,ghostsFilter,Arrays.asList(RodaConstants.INDEX_UUID,RodaConstants.INGEST_SIP_IDS,RodaConstants.AIP_GHOST));
  for (  IndexedAIP aip : ghosts) {
    List<String> temp=new ArrayList<>();
    if (aip.getIngestSIPIds() != null && !aip.getIngestSIPIds().isEmpty()) {
      String firstIngestSIPId=aip.getIngestSIPIds().get(0);
      if (sipIdToGhost.containsKey(firstIngestSIPId)) {
        temp=sipIdToGhost.get(firstIngestSIPId);
      }
      temp.add(aip.getId());
      sipIdToGhost.put(firstIngestSIPId,temp);
    }
 else {
      if (aipIdToGhost.containsKey(aip.getId())) {
        temp=aipIdToGhost.get(aip.getId());
      }
      temp.add(aip.getId());
      aipIdToGhost.put(aip.getId(),temp);
    }
  }
  for (  Map.Entry<String,List<String>> entry : sipIdToGhost.entrySet()) {
    Filter nonGhostsFilter=new Filter(new SimpleFilterParameter(RodaConstants.INGEST_SIP_IDS,entry.getKey()),new SimpleFilterParameter(RodaConstants.AIP_GHOST,Boolean.FALSE.toString()));
    computedSearchScope.ifPresent(id -> nonGhostsFilter.add(new SimpleFilterParameter(RodaConstants.AIP_ANCESTORS,id)));
    IndexResult<IndexedAIP> result=index.find(IndexedAIP.class,nonGhostsFilter,Sorter.NONE,new Sublist(0,1),Arrays.asList(RodaConstants.INDEX_UUID));
    if (result.getTotalCount() > 1) {
      LOGGER.debug("String_Node_Str",entry.getKey());
    }
 else     if (result.getTotalCount() == 1) {
      IndexedAIP newParentIAIP=result.getResults().get(0);
      for (      String id : entry.getValue()) {
        moveChildrenAIPsAndDelete(index,model,id,newParentIAIP.getId(),computedSearchScope,updatedBy);
      }
    }
 else     if (result.getTotalCount() == 0) {
      String ghostIdToKeep=entry.getValue().get(0);
      if (entry.getValue().size() > 1) {
        for (int i=1; i < entry.getValue().size(); i++) {
          updateParent(index,model,entry.getValue().get(i),ghostIdToKeep,computedSearchScope,updatedBy);
        }
      }
    }
  }
}