Package org.uned.agonzalo16.bitacora.service.article

Source Code of org.uned.agonzalo16.bitacora.service.article.FeaturedArticlesService

package org.uned.agonzalo16.bitacora.service.article;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.cache.Cache;
import javax.cache.CacheException;
import javax.cache.CacheFactory;
import javax.cache.CacheManager;
import javax.servlet.jsp.JspException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.uned.agonzalo16.bitacora.dao.CommentDao;
import org.uned.agonzalo16.bitacora.domain.Comment;
import org.uned.agonzalo16.bitacora.web.tag.FeaturedArticle;

import com.google.appengine.api.memcache.stdimpl.GCacheFactory;

@Service("FeaturedArticlesService")
public class FeaturedArticlesService {

  private static final String FEATURED_ARTICLES_KEY = "FEATURED_ARTICLES_KEY";

  @Autowired
  private transient CommentDao commentDao;

  public List<FeaturedArticle> getFeaturedArticles() throws JspException {

    // consultar si el resultado est� en la cache
    try {
      Map<Integer, Integer> props = new HashMap<Integer, Integer>();
      props.put(GCacheFactory.EXPIRATION_DELTA, 1800);
      CacheFactory cacheFactory = CacheManager.getInstance().getCacheFactory();
      Cache cache = cacheFactory.createCache(Collections.emptyMap());

      List<FeaturedArticle> articles = (List<FeaturedArticle>) cache.get(FEATURED_ARTICLES_KEY);

      if (articles == null) {
        // si no esta, buscar los 5 art�culos con m�s comentarios
        articles = searchFeaturedArticles();
        // guardar el resultado en la cache
        cache.put(FEATURED_ARTICLES_KEY, articles);
      }
      return articles;
    } catch (CacheException e) {
      System.out.println(e);
      return Collections.EMPTY_LIST;
    }
  }

  private List<FeaturedArticle> searchFeaturedArticles() {
    Map<Long, ArticleCount> commentsCount = new HashMap<Long, ArticleCount>();

    // contar los comentarios
    for (Comment comment : commentDao.findAll()) {
      if (commentsCount.containsKey(comment.getArticle().getId())) {
        ArticleCount current = commentsCount.get(comment.getArticle().getId());
        current.addComment();
      } else {
        commentsCount.put(comment.getArticle().getId(), new ArticleCount(comment.getArticle()));
      }
    }

    List<ArticleCount> totalCount = new ArrayList<ArticleCount>(commentsCount.values());
    Collections.sort(totalCount);

    // coger los 5 con m�s comentarios
    List<FeaturedArticle> articles = new ArrayList<FeaturedArticle>(5);
    for (ArticleCount article : totalCount) {
      articles.add(new FeaturedArticle(article.getArticle(), commentDao.countByArticle(article.getArticle())));

      if (articles.size() == 5) {
        break;
      }
    }

    return articles;
  }
}
TOP

Related Classes of org.uned.agonzalo16.bitacora.service.article.FeaturedArticlesService

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.