Program: 15644
Project: 15644
Buggy source:
/** 
 * Synchronize the feed to local database.
 * @param url RSS url of a feed or page containing a feed to synchronize
 * @throws Exception
 */
public Feed synchronize(String url) throws Exception {
  if (log.isInfoEnabled()) {
    log.info(MessageFormat.format("String_Node_Str",url));
  }
  long startTime=System.currentTimeMillis();
  RssReader rssReader=parseFeedOrPage(url,true);
  Feed newFeed=rssReader.getFeed();
  List<Article> articleList=rssReader.getArticleList();
  FeedDao feedDao=new FeedDao();
  String rssUrl=newFeed.getRssUrl();
  Feed feed=feedDao.getByRssUrl(rssUrl);
  if (feed == null) {
    feed=new Feed();
    feed.setUrl(newFeed.getUrl());
    feed.setBaseUri(newFeed.getBaseUri());
    feed.setRssUrl(rssUrl);
    feed.setTitle(StringUtils.abbreviate(newFeed.getTitle(),100));
    feed.setLanguage(newFeed.getLanguage() != null && newFeed.getLanguage().length() <= 10 ? newFeed.getLanguage() : null);
    feed.setDescription(StringUtils.abbreviate(newFeed.getDescription(),4000));
    feed.setLastFetchDate(new Date());
    feedDao.create(feed);
    EntityManagerUtil.flush();
    FaviconUpdateRequestedEvent faviconUpdateRequestedEvent=new FaviconUpdateRequestedEvent();
    faviconUpdateRequestedEvent.setFeed(feed);
    AppContext.getInstance().getAsyncEventBus().post(faviconUpdateRequestedEvent);
  }
 else {
    boolean newDay=feed.getLastFetchDate() == null || DateTime.now().getDayOfYear() != new DateTime(feed.getLastFetchDate()).getDayOfYear();
    int daysFromCreation=Days.daysBetween(Instant.now(),new Instant(feed.getCreateDate().getTime())).getDays();
    boolean updateFavicon=newDay && daysFromCreation % 7 == 0;
    feed.setUrl(newFeed.getUrl());
    feed.setBaseUri(newFeed.getBaseUri());
    feed.setTitle(StringUtils.abbreviate(newFeed.getTitle(),100));
    feed.setLanguage(newFeed.getLanguage() != null && newFeed.getLanguage().length() <= 10 ? newFeed.getLanguage() : null);
    feed.setDescription(StringUtils.abbreviate(newFeed.getDescription(),4000));
    feed.setLastFetchDate(new Date());
    feedDao.update(feed);
    if (updateFavicon) {
      FaviconUpdateRequestedEvent faviconUpdateRequestedEvent=new FaviconUpdateRequestedEvent();
      faviconUpdateRequestedEvent.setFeed(feed);
      AppContext.getInstance().getAsyncEventBus().post(faviconUpdateRequestedEvent);
    }
  }
  Map<String,Article> articleMap=new HashMap<String,Article>();
  for (  Article article : articleList) {
    articleMap.put(article.getGuid(),article);
  }
  List<String> guidIn=new ArrayList<String>();
  for (  Article article : articleList) {
    guidIn.add(article.getGuid());
  }
  ArticleSanitizer sanitizer=new ArticleSanitizer();
  ArticleDao articleDao=new ArticleDao();
  if (!guidIn.isEmpty()) {
    ArticleCriteria articleCriteria=new ArticleCriteria();
    articleCriteria.setFeedId(feed.getId());
    articleCriteria.setGuidIn(guidIn);
    List<ArticleDto> currentArticleDtoList=articleDao.findByCriteria(articleCriteria);
    List<Article> articleUpdatedList=new ArrayList<Article>();
    for (    ArticleDto currentArticle : currentArticleDtoList) {
      Article newArticle=articleMap.remove(currentArticle.getGuid());
      Article article=new Article();
      article.setPublicationDate(currentArticle.getPublicationDate());
      article.setId(currentArticle.getId());
      article.setFeedId(feed.getId());
      article.setUrl(newArticle.getUrl());
      article.setTitle(StringUtils.abbreviate(TextSanitizer.sanitize(newArticle.getTitle()),4000));
      article.setCreator(StringUtils.abbreviate(newArticle.getCreator(),200));
      String baseUri=UrlUtil.getBaseUri(feed,newArticle);
      article.setDescription(sanitizer.sanitize(baseUri,newArticle.getDescription()));
      article.setCommentUrl(newArticle.getCommentUrl());
      article.setCommentCount(newArticle.getCommentCount());
      article.setEnclosureUrl(newArticle.getEnclosureUrl());
      article.setEnclosureLength(newArticle.getEnclosureLength());
      article.setEnclosureType(newArticle.getEnclosureType());
      if (!currentArticle.getTitle().equals(article.getTitle()) || !currentArticle.getDescription().equals(article.getDescription())) {
        articleDao.update(article);
        articleUpdatedList.add(article);
      }
    }
    if (!articleUpdatedList.isEmpty()) {
      ArticleUpdatedAsyncEvent articleUpdatedAsyncEvent=new ArticleUpdatedAsyncEvent();
      articleUpdatedAsyncEvent.setArticleList(articleUpdatedList);
      AppContext.getInstance().getAsyncEventBus().post(articleUpdatedAsyncEvent);
    }
  }
  if (!articleMap.isEmpty()) {
    FeedSubscriptionCriteria feedSubscriptionCriteria=new FeedSubscriptionCriteria();
    feedSubscriptionCriteria.setFeedId(feed.getId());
    FeedSubscriptionDao feedSubscriptionDao=new FeedSubscriptionDao();
    List<FeedSubscriptionDto> feedSubscriptionList=feedSubscriptionDao.findByCriteria(feedSubscriptionCriteria);
    UserArticleDao userArticleDao=new UserArticleDao();
    for (    Article article : articleMap.values()) {
      article.setFeedId(feed.getId());
      article.setTitle(StringUtils.abbreviate(TextSanitizer.sanitize(article.getTitle()),4000));
      article.setCreator(StringUtils.abbreviate(article.getCreator(),200));
      String baseUri=UrlUtil.getBaseUri(feed,article);
      article.setDescription(sanitizer.sanitize(baseUri,article.getDescription()));
      if (article.getPublicationDate() == null) {
        article.setPublicationDate(new Date());
      }
      articleDao.create(article);
      for (      FeedSubscriptionDto feedSubscription : feedSubscriptionList) {
        UserArticle userArticle=new UserArticle();
        userArticle.setArticleId(article.getId());
        userArticle.setUserId(feedSubscription.getUserId());
        userArticleDao.create(userArticle);
        feedSubscription.setUnreadUserArticleCount(feedSubscription.getUnreadUserArticleCount() + 1);
        feedSubscriptionDao.updateUnreadCount(feedSubscription.getId(),feedSubscription.getUnreadUserArticleCount());
      }
    }
    ArticleCreatedAsyncEvent articleCreatedAsyncEvent=new ArticleCreatedAsyncEvent();
    articleCreatedAsyncEvent.setArticleList(Lists.newArrayList(articleMap.values()));
    AppContext.getInstance().getAsyncEventBus().post(articleCreatedAsyncEvent);
  }
  long endTime=System.currentTimeMillis();
  if (log.isInfoEnabled()) {
    log.info(MessageFormat.format("String_Node_Str",endTime - startTime));
  }
  return feed;
}

Fixed version (ground truth):
/** 
 * Synchronize the feed to local database.
 * @param url RSS url of a feed or page containing a feed to synchronize
 * @throws Exception
 */
public Feed synchronize(String url) throws Exception {
  if (log.isInfoEnabled()) {
    log.info(MessageFormat.format("String_Node_Str",url));
  }
  long startTime=System.currentTimeMillis();
  RssReader rssReader=parseFeedOrPage(url,true);
  Feed newFeed=rssReader.getFeed();
  List<Article> articleList=rssReader.getArticleList();
  FeedDao feedDao=new FeedDao();
  String rssUrl=newFeed.getRssUrl();
  Feed feed=feedDao.getByRssUrl(rssUrl);
  if (feed == null) {
    feed=new Feed();
    feed.setUrl(newFeed.getUrl());
    feed.setBaseUri(newFeed.getBaseUri());
    feed.setRssUrl(rssUrl);
    feed.setTitle(StringUtils.abbreviate(newFeed.getTitle(),100));
    feed.setLanguage(newFeed.getLanguage() != null && newFeed.getLanguage().length() <= 10 ? newFeed.getLanguage() : null);
    feed.setDescription(StringUtils.abbreviate(newFeed.getDescription(),4000));
    feed.setLastFetchDate(new Date());
    feedDao.create(feed);
    EntityManagerUtil.flush();
    FaviconUpdateRequestedEvent faviconUpdateRequestedEvent=new FaviconUpdateRequestedEvent();
    faviconUpdateRequestedEvent.setFeed(feed);
    AppContext.getInstance().getAsyncEventBus().post(faviconUpdateRequestedEvent);
  }
 else {
    boolean newDay=feed.getLastFetchDate() == null || DateTime.now().getDayOfYear() != new DateTime(feed.getLastFetchDate()).getDayOfYear();
    int daysFromCreation=Days.daysBetween(Instant.now(),new Instant(feed.getCreateDate().getTime())).getDays();
    boolean updateFavicon=newDay && daysFromCreation % 7 == 0;
    feed.setUrl(newFeed.getUrl());
    feed.setBaseUri(newFeed.getBaseUri());
    feed.setTitle(StringUtils.abbreviate(newFeed.getTitle(),100));
    feed.setLanguage(newFeed.getLanguage() != null && newFeed.getLanguage().length() <= 10 ? newFeed.getLanguage() : null);
    feed.setDescription(StringUtils.abbreviate(newFeed.getDescription(),4000));
    feed.setLastFetchDate(new Date());
    feedDao.update(feed);
    if (updateFavicon) {
      FaviconUpdateRequestedEvent faviconUpdateRequestedEvent=new FaviconUpdateRequestedEvent();
      faviconUpdateRequestedEvent.setFeed(feed);
      AppContext.getInstance().getAsyncEventBus().post(faviconUpdateRequestedEvent);
    }
  }
  Map<String,Article> articleMap=new HashMap<String,Article>();
  for (  Article article : articleList) {
    articleMap.put(article.getGuid(),article);
  }
  List<String> guidIn=new ArrayList<String>();
  for (  Article article : articleList) {
    guidIn.add(article.getGuid());
  }
  ArticleSanitizer sanitizer=new ArticleSanitizer();
  ArticleDao articleDao=new ArticleDao();
  if (!guidIn.isEmpty()) {
    ArticleCriteria articleCriteria=new ArticleCriteria();
    articleCriteria.setFeedId(feed.getId());
    articleCriteria.setGuidIn(guidIn);
    List<ArticleDto> currentArticleDtoList=articleDao.findByCriteria(articleCriteria);
    List<Article> articleUpdatedList=new ArrayList<Article>();
    for (    ArticleDto currentArticle : currentArticleDtoList) {
      Article newArticle=articleMap.remove(currentArticle.getGuid());
      Article article=new Article();
      article.setPublicationDate(currentArticle.getPublicationDate());
      article.setId(currentArticle.getId());
      article.setFeedId(feed.getId());
      article.setUrl(newArticle.getUrl());
      article.setTitle(StringUtils.abbreviate(TextSanitizer.sanitize(newArticle.getTitle()),4000));
      article.setCreator(StringUtils.abbreviate(newArticle.getCreator(),200));
      String baseUri=UrlUtil.getBaseUri(feed,newArticle);
      article.setDescription(sanitizer.sanitize(baseUri,newArticle.getDescription()));
      article.setCommentUrl(newArticle.getCommentUrl());
      article.setCommentCount(newArticle.getCommentCount());
      article.setEnclosureUrl(newArticle.getEnclosureUrl());
      article.setEnclosureLength(newArticle.getEnclosureLength());
      article.setEnclosureType(newArticle.getEnclosureType());
      if (!Strings.nullToEmpty(currentArticle.getTitle()).equals(Strings.nullToEmpty(article.getTitle())) || !Strings.nullToEmpty(currentArticle.getDescription()).equals(Strings.nullToEmpty(article.getDescription()))) {
        articleDao.update(article);
        articleUpdatedList.add(article);
      }
    }
    if (!articleUpdatedList.isEmpty()) {
      ArticleUpdatedAsyncEvent articleUpdatedAsyncEvent=new ArticleUpdatedAsyncEvent();
      articleUpdatedAsyncEvent.setArticleList(articleUpdatedList);
      AppContext.getInstance().getAsyncEventBus().post(articleUpdatedAsyncEvent);
    }
  }
  if (!articleMap.isEmpty()) {
    FeedSubscriptionCriteria feedSubscriptionCriteria=new FeedSubscriptionCriteria();
    feedSubscriptionCriteria.setFeedId(feed.getId());
    FeedSubscriptionDao feedSubscriptionDao=new FeedSubscriptionDao();
    List<FeedSubscriptionDto> feedSubscriptionList=feedSubscriptionDao.findByCriteria(feedSubscriptionCriteria);
    UserArticleDao userArticleDao=new UserArticleDao();
    for (    Article article : articleMap.values()) {
      article.setFeedId(feed.getId());
      article.setTitle(StringUtils.abbreviate(TextSanitizer.sanitize(article.getTitle()),4000));
      article.setCreator(StringUtils.abbreviate(article.getCreator(),200));
      String baseUri=UrlUtil.getBaseUri(feed,article);
      article.setDescription(sanitizer.sanitize(baseUri,article.getDescription()));
      if (article.getPublicationDate() == null) {
        article.setPublicationDate(new Date());
      }
      articleDao.create(article);
      for (      FeedSubscriptionDto feedSubscription : feedSubscriptionList) {
        UserArticle userArticle=new UserArticle();
        userArticle.setArticleId(article.getId());
        userArticle.setUserId(feedSubscription.getUserId());
        userArticleDao.create(userArticle);
        feedSubscription.setUnreadUserArticleCount(feedSubscription.getUnreadUserArticleCount() + 1);
        feedSubscriptionDao.updateUnreadCount(feedSubscription.getId(),feedSubscription.getUnreadUserArticleCount());
      }
    }
    ArticleCreatedAsyncEvent articleCreatedAsyncEvent=new ArticleCreatedAsyncEvent();
    articleCreatedAsyncEvent.setArticleList(Lists.newArrayList(articleMap.values()));
    AppContext.getInstance().getAsyncEventBus().post(articleCreatedAsyncEvent);
  }
  long endTime=System.currentTimeMillis();
  if (log.isInfoEnabled()) {
    log.info(MessageFormat.format("String_Node_Str",endTime - startTime));
  }
  return feed;
}