Subversion Repositories bacoAlunos

Rev

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

package pt.estgp.estgweb.services.urlstat;

import pt.utl.ist.berserk.logic.serviceManager.IService;
import pt.estgp.estgweb.services.expceptions.ServiceException;
import pt.estgp.estgweb.domain.*;
import pt.estgp.estgweb.domain.dao.DaoFactory;
import pt.estgp.estgweb.web.filters.UrlStatFilter;
import org.apache.log4j.Logger;

import java.util.*;

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

/**
 * @author Jorge Machado
 * @date 15/Mar/2008
 * @time 6:55:38
 * @see pt.estgp.estgweb.services.urlstat
 */

public class UrlStatService 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(UrlStatService.class);

    /**
     * Saves a group of clicks in database
     * @param map with link clicks
     * @param year of group
     * @param month of group
     * @param day of group
     * @throws ServiceException on error
     */

    public void run(HashMap<String, UrlStatFilter.IntegerContainer> map, 1.5.0/docs/api/java/lang/Integer.html">Integer year, 1.5.0/docs/api/java/lang/Integer.html">Integer month, 1.5.0/docs/api/java/lang/Integer.html">Integer day) throws ServiceException
    {
        if(map.size() == 0)
            return;
        Set<Map.Entry<String,UrlStatFilter.IntegerContainer>> set = map.entrySet();
        for(1.5.0/docs/api/java/util/Map.html">Map.Entry<String,UrlStatFilter.IntegerContainer> entry: set)
        {
            1.5.0/docs/api/java/lang/String.html">String relativePath = entry.getKey();
            UrlStatFilter.IntegerContainer ic = entry.getValue();

            UrlStatImpl urlStat = (UrlStatImpl) DaoFactory.getUrlStatDaoImpl().loadOrNullLockUpgrade(relativePath);
            if(urlStat == null)
            {
                logger.info("url:" + relativePath + " does not exist... creating");
                urlStat = DomainObjectFactory.createUrlStatImpl();
                urlStat.setRelativePath(relativePath);
                urlStat.setStatus(false);
                DaoFactory.getUrlStatDaoImpl().save(urlStat);
            }

            UrlStatYearId urlStatYearId = new UrlStatYearId(year,urlStat);
            UrlStatYearImpl urlStatYear = (UrlStatYearImpl) DaoFactory.getUrlStatYearDaoImpl().loadOrNullLockUpgrade(urlStatYearId);
            if(urlStatYear == null)
            {
                logger.info("url:" + relativePath + " first year call does not exist... creating");
                urlStatYear = DomainObjectFactory.createUrlStatYearImpl();
                urlStatYear.setId(urlStatYearId);
                urlStat.addYear(urlStatYear);
                DaoFactory.getUrlStatYearDaoImpl().save(urlStatYear);
                DaoFactory.getUrlStatDaoImpl().reattach(urlStat);
            }

            UrlStatMonthId urlStatMonthId = new UrlStatMonthId(month,urlStatYear);
            UrlStatMonthImpl urlStatMonth = (UrlStatMonthImpl) DaoFactory.getUrlStatMonthDaoImpl().loadOrNullLockUpgrade(urlStatMonthId);
            if(urlStatMonth == null)
            {
                logger.info("url:" + relativePath + " first month call does not exist... creating");
                urlStatMonth = DomainObjectFactory.createUrlStatMonthImpl();
                urlStatMonth.setId(urlStatMonthId);
                urlStatMonth.setDate(new MyCalendar(year,month,1).getTime());
                urlStatYear.addMonth(urlStatMonth);
                DaoFactory.getUrlStatMonthDaoImpl().save(urlStatMonth);
                DaoFactory.getUrlStatYearDaoImpl().reattach(urlStatYear);
            }
            UrlStatDayImpl urlStatDay = (UrlStatDayImpl) DaoFactory.getUrlStatDayDaoImpl().loadOrNullLockUpgrade(new UrlStatDayId(day, urlStatMonth));
            if(urlStatDay == null)
            {
                logger.info("url:" + relativePath + " first day call does not exist... creating");
                urlStatDay = DomainObjectFactory.createUrlStatDayImpl();
                urlStatDay.setId(new UrlStatDayId(day,urlStatMonth));
                urlStatDay.setDate(new MyCalendar(year,month,day).getTime());
                urlStatMonth.addDay(urlStatDay);
                DaoFactory.getUrlStatDayDaoImpl().save(urlStatDay);
            }

            urlStat.setTotalClicks(urlStat.getTotalClicks() + ic.getCount());
            urlStatDay.setTotalClicks(urlStatDay.getTotalClicks() + ic.getCount());
            urlStatMonth.setTotalClicks(urlStatMonth.getTotalClicks() + ic.getCount());
            urlStatYear.setTotalClicks(urlStatYear.getTotalClicks() + ic.getCount());
        }
    }

    /**
     * Simple method to create links clicks from year 2003 until now
     * @param args --
     * @throws ServiceException --
     */

    public static void main(1.5.0/docs/api/java/lang/String.html">String[] args) throws ServiceException
    {
        AbstractDao.getCurrentSession().beginTransaction();
        HashMap<String,UrlStatFilter.IntegerContainer> map = new HashMap<String,UrlStatFilter.IntegerContainer>();
        UrlStatFilter.IntegerContainer ic = new UrlStatFilter.IntegerContainer(3);
        map.put("/teste2",ic);
        MyCalendar now = new MyCalendar();
        MyCalendar c = new MyCalendar(2003,1,1,11,11,11);
        1.5.0/docs/api/java/util/Random.html">Random r = new 1.5.0/docs/api/java/util/Random.html">Random(now.getTimeInMillis());
        UrlStatService service = new UrlStatService();
        int commit = 0;
        do
        {
            ic.setI(r.nextInt(200));
            service.run(map,c.getYear(),c.getMonth(),c.getDay());
            c.setTimeInMillis(c.getTimeInMillis() + (24 * 60 * 60 * 1000));

            if(commit++ > 100)
            {
                commit = 0;
                AbstractDao.getCurrentSession().getTransaction().commit();
                AbstractDao.getCurrentSession().beginTransaction();
            }
        }
        while(!(c.getYear() == now.getYear() && c.getMonth() == now.getMonth() && c.getDay() == now.getDay()));
        ic.setI(r.nextInt(200));
        service.run(map,c.getYear(),c.getMonth(),c.getDay());
        c.setTimeInMillis(c.getTimeInMillis() + (24 * 60 * 60 * 1000));
        AbstractDao.getCurrentSession().getTransaction().commit();
    }
}