Package com.evasion.plugin.geoloc

Source Code of com.evasion.plugin.geoloc.ImportTimerEJB

/*
* To change this template, choose Tools | Templates and open the template in
* the editor.
*/
package com.evasion.plugin.geoloc;

import com.evasion.EntityJPA;
import com.evasion.common.TimerInterface;
import com.evasion.dao.api.DefaultDAO;
import com.evasion.ejb.local.ParametreManagerLocal;
import com.evasion.entity.geolocation.Country;
import com.evasion.exception.EvasionException;
import com.evasion.plugin.geoloc.dataimport.Importer;
import com.evasion.plugin.geoloc.dataimport.Importer.Mode;
import com.evasion.plugin.geoloc.dataimport.gis.GISFormat;
import com.evasion.plugin.geoloc.dataimport.gis.GISParser;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.*;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
*
* @author sebastien.glon
*/
@Singleton(name = "ImportTimerEJB", description = "EJBTimer d'import des tables de ref de geoloc")
@Remote(value = TimerInterface.class)
@TransactionManagement(TransactionManagementType.BEAN)
public class ImportTimerEJB implements TimerInterface {

    /**
     * LOGGER.
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(
            ImportTimerEJB.class);

    @Resource
    private TimerService timerService;

    @PersistenceContext(unitName = "EvasionPU")
    private transient EntityManager em;

    @EJB
    private ParametreManagerLocal parametreManager;

    @Resource
    UserTransaction utx;

    private DefaultDAO defaultDAO;

    public ImportTimerEJB() {
        defaultDAO = new DefaultDAO();
    }

    public ImportTimerEJB(EntityManager em) {
        this.em = em;
        defaultDAO.setEntityManager(this.em);
    }

    @PostConstruct
    public void init() {
        defaultDAO.setEntityManager(this.em);
        parametreManager.saveParametre(Constante.IMPORT_TIMER_ENABLED, Boolean.FALSE.toString(), false);
        parametreManager.saveParametre(Constante.IMPORT_TIMER_INTERVAL, "*|*|0|0", false);
        parametreManager.saveParametre(Constante.IMPORT_FILE_FORMAT, GISParser.FORMAT, false);
        //parametreManager.saveParametre(Constante.IMPORT_FTP_DOWNLOAD_URL, "", false);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void updateImportTimer() throws EvasionException {
        try {
            utx.begin();
            defaultDAO.setEntityManager(em);
            boolean enabled = Boolean.valueOf(parametreManager.getProperty(Constante.IMPORT_TIMER_ENABLED));

            String month, day, hour, min;
            String timerInterval = parametreManager.getProperty(Constante.IMPORT_TIMER_INTERVAL);
            if (timerInterval == null && enabled) {
                utx.rollback();
                throw new EvasionException("Timer interval not define.");
            }

            //stop possible timer précédent.
            Timer timer = getImportTimer();
            if (timer != null) {
                timer.cancel();
            }

            if (enabled) {
                String[] param = parametreManager.getProperty(Constante.IMPORT_TIMER_INTERVAL).split("\\|");
                month = param[0];
                day = param[1];
                hour = param[2];
                min = param[3];
                LOGGER.debug("Geoloc import Timer is : {} with timer  {}", enabled, param);
                try {
                    //start timer
                    timer = timerService.createCalendarTimer(
                            new ScheduleExpression().month(month).dayOfMonth(day).hour(hour).minute(min),
                            new TimerConfig(Constante.IMPORT_TIMER_NAME, false));
                    LOGGER.info("Last geoloc import: {}", timer.getNextTimeout());
                } catch (IllegalArgumentException ex) {
                    LOGGER.error("Illegal argument exception on " + Constante.IMPORT_TIMER_INTERVAL + " parameter", ex);
                    utx.rollback();
                }
            }
            utx.commit();
        } catch (Exception ex) {
            throw new EvasionException("Fail update import Geoloc Data.", ex);
        }
    }

    private Timer getImportTimer() {
        for (Object obj : timerService.getTimers()) {
            Timer timer = (Timer) obj;
            String scheduled = (String) timer.getInfo();
            if (scheduled.equals(Constante.IMPORT_TIMER_NAME)) {
                return timer;
            }
        }
        return null;
    }

    @Timeout()
    @Override
    public void timeout(Timer timer) {
        LOGGER.info("Import Geoloc timer step.");
        try {
            Importer geo = new Importer(em, utx);
            List<EntityJPA> countrys = defaultDAO.findAll(Country.class);
            for (EntityJPA object : countrys) {
                Country country = (Country) object;
                try {
                    utx.begin();  
                    if (geo.downloadCountryData(parametreManager.getProperty(Constante.IMPORT_FTP_DOWNLOAD_URL), country.getGeoname().getModDate().getTime(), country.getCode())) {
                        geo.importData(parametreManager.getProperty(Constante.IMPORT_FILE_FORMAT), Mode.DELTE_INSERT);
                        parametreManager.saveParametre(
                                Constante.IMPORT_LAST_UPDATE, String.valueOf(new Date().getTime()), Boolean.TRUE);
                    }
                    geo.clear();
                    utx.commit();

                    // @TODo revoir la gestion des exceptions
                } catch (Exception ex) {
                    throw  new EvasionException("Error import Geoloc data for country "+ country, ex);
                }
            }

        } catch (EvasionException ex) {
            LOGGER.error("Error import Geoloc;", ex);
        }
    }
}
TOP

Related Classes of com.evasion.plugin.geoloc.ImportTimerEJB

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.