Program: 112282
Project: 112282
Buggy source:
private int pollForNewUndeliveredResources(SubscriptionTable theSubscriptionTable){
  Subscription subscription=toResource(Subscription.class,theSubscriptionTable.getSubscriptionResource(),false);
  RuntimeResourceDefinition resourceDef=validateCriteriaAndReturnResourceDefinition(subscription);
  SearchParameterMap criteriaUrl=translateMatchUrl(subscription.getCriteria(),resourceDef);
  criteriaUrl=new SearchParameterMap();
  long start=theSubscriptionTable.getMostRecentMatch().getTime();
  long end=System.currentTimeMillis() - getConfig().getSubscriptionPollDelay();
  if (end <= start) {
    ourLog.trace("String_Node_Str");
    return 0;
  }
  ourLog.debug("String_Node_Str",new Object[]{subscription.getId().getIdPart(),new InstantDt(new Date(start)),new InstantDt(new Date(end))});
  DateRangeParam range=new DateRangeParam();
  range.setLowerBound(new DateParam(QuantityCompararatorEnum.GREATERTHAN,start));
  range.setUpperBound(new DateParam(QuantityCompararatorEnum.LESSTHAN,end));
  criteriaUrl.setLastUpdated(range);
  criteriaUrl.setSort(new SortSpec(Constants.PARAM_LASTUPDATED,SortOrderEnum.ASC));
  IFhirResourceDao<? extends IBaseResource> dao=getDao(resourceDef.getImplementingClass());
  IBundleProvider results=dao.search(criteriaUrl);
  if (results.size() == 0) {
    return 0;
  }
  ourLog.info("String_Node_Str",results.size(),subscription.getId().getIdPart());
  List<SubscriptionFlaggedResource> flags=new ArrayList<SubscriptionFlaggedResource>();
  Date mostRecentMatch=null;
  for (  IBaseResource next : results.getResources(0,results.size())) {
    Date updated=ResourceMetadataKeyEnum.PUBLISHED.get((IResource)next).getValue();
    if (mostRecentMatch == null) {
      mostRecentMatch=updated;
    }
 else {
      long mostRecentMatchTime=mostRecentMatch.getTime();
      long updatedTime=updated.getTime();
      if (mostRecentMatchTime < updatedTime) {
        mostRecentMatch=updated;
      }
    }
    SubscriptionFlaggedResource nextFlag=new SubscriptionFlaggedResource();
    Long pid=IDao.RESOURCE_PID.get((IResource)next);
    nextFlag.setResource(myEntityManager.find(ResourceTable.class,pid));
    nextFlag.setSubscription(theSubscriptionTable);
    nextFlag.setVersion(next.getIdElement().getVersionIdPartAsLong());
    flags.add(nextFlag);
  }
  mySubscriptionFlaggedResourceDataDao.save(flags);
  ourLog.debug("String_Node_Str",subscription.getId().getIdPart(),new InstantDt(mostRecentMatch));
  theSubscriptionTable.setMostRecentMatch(mostRecentMatch);
  myEntityManager.merge(theSubscriptionTable);
  return results.size();
}

Fixed version (ground truth):
private int pollForNewUndeliveredResources(SubscriptionTable theSubscriptionTable){
  Subscription subscription=toResource(Subscription.class,theSubscriptionTable.getSubscriptionResource(),false);
  RuntimeResourceDefinition resourceDef=validateCriteriaAndReturnResourceDefinition(subscription);
  SearchParameterMap criteriaUrl=translateMatchUrl(subscription.getCriteria(),resourceDef);
  criteriaUrl=new SearchParameterMap();
  long start=theSubscriptionTable.getMostRecentMatch().getTime();
  long end=System.currentTimeMillis() - getConfig().getSubscriptionPollDelay();
  if (end <= start) {
    ourLog.trace("String_Node_Str");
    return 0;
  }
  ourLog.debug("String_Node_Str",new Object[]{subscription.getId().getIdPart(),new InstantDt(new Date(start)),new InstantDt(new Date(end))});
  DateRangeParam range=new DateRangeParam();
  range.setLowerBound(new DateParam(QuantityCompararatorEnum.GREATERTHAN,start));
  range.setUpperBound(new DateParam(QuantityCompararatorEnum.LESSTHAN,end));
  criteriaUrl.setLastUpdated(range);
  criteriaUrl.setSort(new SortSpec(Constants.PARAM_LASTUPDATED,SortOrderEnum.ASC));
  IFhirResourceDao<? extends IBaseResource> dao=getDao(resourceDef.getImplementingClass());
  IBundleProvider results=dao.search(criteriaUrl);
  if (results.size() == 0) {
    return 0;
  }
  ourLog.info("String_Node_Str",results.size(),subscription.getId().getIdPart());
  List<SubscriptionFlaggedResource> flags=new ArrayList<SubscriptionFlaggedResource>();
  Date mostRecentMatch=null;
  for (  IBaseResource next : results.getResources(0,results.size())) {
    Date updated=ResourceMetadataKeyEnum.UPDATED.get((IResource)next).getValue();
    if (mostRecentMatch == null) {
      mostRecentMatch=updated;
    }
 else {
      long mostRecentMatchTime=mostRecentMatch.getTime();
      long updatedTime=updated.getTime();
      if (mostRecentMatchTime < updatedTime) {
        mostRecentMatch=updated;
      }
    }
    SubscriptionFlaggedResource nextFlag=new SubscriptionFlaggedResource();
    Long pid=IDao.RESOURCE_PID.get((IResource)next);
    ourLog.info("String_Node_Str",pid);
    nextFlag.setResource(myEntityManager.find(ResourceTable.class,pid));
    nextFlag.setSubscription(theSubscriptionTable);
    nextFlag.setVersion(next.getIdElement().getVersionIdPartAsLong());
    flags.add(nextFlag);
  }
  mySubscriptionFlaggedResourceDataDao.save(flags);
  ourLog.debug("String_Node_Str",subscription.getId().getIdPart(),new InstantDt(mostRecentMatch));
  theSubscriptionTable.setMostRecentMatch(mostRecentMatch);
  mySubscriptionTableDao.save(theSubscriptionTable);
  return results.size();
}