Subversion Repositories bacoAlunos

Rev

Rev 1306 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package pt.estgp.estgweb.services.blogs;

import pt.utl.ist.berserk.logic.serviceManager.IService;
import pt.estgp.estgweb.domain.views.BlogView;
import pt.estgp.estgweb.domain.views.BlogPostView;
import pt.estgp.estgweb.domain.views.CourseUnitView;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.ArrayList;

import jomm.utils.MyCalendar;
import jomm.dao.impl.AbstractDao;

/*
 * @author Goncalo Luiz gedl [AT] rnl [DOT] ist [DOT] utl [DOT] pt
 *
 *
 * Created at 17/Out/2003 , 23:45:24
 *
 */

/**
 * @author Jorge Machado
 *
 *
 * Created at 17/Out/2003 , 23:45:24
 *
 */

public class LoadBlogService implements IService
{

    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(LoadBlogService.class);

    private static LoadBlogPostsService loadBlogPostsService = new LoadBlogPostsService();

    public BlogView loadIndex(long id, UserSession userSession) throws ServiceException
    {

        Blog b = DaoFactory.getBlogDaoImpl().get(id);

        if(b == null)
            return null;
        BlogView bV = new BlogView(b);
       

        List<BlogView.MonthPosts> monthIds = getMonths(id);
        List<BlogPostView> posts = loadBlogPostsService.loadLastPosts(id,userSession);
        bV.setLastPosts(posts);
        bV.setMonths(monthIds);

        return bV;
    }

    public BlogView loadMonth(long id, int month, int year, UserSession userSession) throws ServiceException
    {
        Blog b = DaoFactory.getBlogDaoImpl().get(id);
        if(b == null)
            return null;
        BlogView bV = new BlogView(b);
        List<BlogView.MonthPosts> monthIds = getMonths(id, month,year);
        List<BlogPostView> posts = loadBlogPostsService.loadByDate(id,month,year,userSession);
        bV.setLastPosts(posts);
        bV.setMonths(monthIds);
        bV.setMonth(month);
        bV.setYear(year);
        return bV;
    }

    private List<BlogView.MonthPosts> getMonths(long id)
    {
        return getMonths(id,0,0);
    }
    private List<BlogView.MonthPosts> getMonths(long id, int monthRequest, int yearRequest)
    {
        MyCalendar now = new MyCalendar();
        int year = now.getYear();
        int month = now.getMonth();
        List<BlogView.MonthPosts> monthIds = new ArrayList<BlogView.MonthPosts>();
        List<Object[]> months = DaoFactory.getBlogPostDaoImpl().loadMonths(id);
        new ArrayList<BlogView.MonthPosts>();
        for(5+0%2Fdocs%2Fapi+Object">Object[] monthId: months)
        {
            1.5.0/docs/api/java/lang/Long.html">Long count = (1.5.0/docs/api/java/lang/Long.html">Long) monthId[1];
            BlogView.MonthPosts m = new BlogView.MonthPosts(count.intValue(),(1.5.0/docs/api/java/lang/String.html">String)monthId[0]);
            if(!(m.getYear() == year && m.getMonth() == month))
            {   if(!(m.getYear() == yearRequest && m.getMonth() == monthRequest))
                {
                    monthIds.add(m);
                }
            }
        }
        return monthIds;
    }

    public 1.5.0/docs/api/java/lang/Long.html">Long loadIdByName(1.5.0/docs/api/java/lang/String.html">String name) throws ServiceException
    {
        List<BlogImpl> blogs = DaoFactory.getBlogDaoImpl().findByName(name);
        if(blogs.size() == 0)
            return null;

        else
            return blogs.get(0).getId();
    }
}