All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.evasion.plugin.geoloc.dataimport.Importer Maven / Gradle / Ivy

There is a newer version: 2.0.0.2
Show newest version
/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package com.evasion.plugin.geoloc.dataimport;

import com.evasion.plugin.geoloc.dataimport.gis.GISParser;
import com.evasion.plugin.geoloc.dataimport.geoname.GeonameParser;
import com.evasion.EntityJPA;
import com.evasion.dao.api.DefaultDAO;
import com.evasion.entity.geolocation.City;
import com.evasion.entity.geolocation.Country;
import com.evasion.entity.geolocation.AdminDivision;
import com.evasion.exception.EvasionException;
import com.evasion.plugin.geoloc.dao.CountryDAO;
import com.evasion.plugin.geoloc.dao.RegionAdmDAO;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author glon-56610
 */
public class Importer {

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

    private DefaultDAO defaultDAO;

    private CountryDAO countryDAO;

    private RegionAdmDAO regionAdmDAO;

    private EntityManager em;

    private File tempFile = null;

    private static final int BUFFER_SIZE = 1024;

    private static final int BATCH_SIZE = 100;
    // Injects EJB context 

    private UserTransaction utx;

    public Importer(EntityManager em, UserTransaction utx) {
        this.em = em;
        defaultDAO = new DefaultDAO();
        regionAdmDAO = new RegionAdmDAO();
        countryDAO = new CountryDAO();
        defaultDAO.setEntityManager(em);
        regionAdmDAO.setEntityManager(em);
        countryDAO.setEntityManager(em);
        this.utx = utx;
    }

    public void setFile(String fileName) {
        tempFile = new File(fileName);
    }

    @edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_CONVERT_CASE")
    public boolean downloadCountryData(String adress, Long lastUpdate, String ctCode) throws EvasionException {
        BufferedOutputStream bout = null;
        BufferedInputStream in = null;
        if (!adress.endsWith("/")) {
            adress+="/";
        }
        
        try {
            URL url = new URL(adress + ctCode.toLowerCase(Locale.FRENCH) + ".zip");
            URLConnection conn = url.openConnection();

            // @TODO revoir le téléchargement sur critère de date de dernière modif.
            // if (conn.getLastModified()==0 || conn.getLastModified() > lastUpdate) {
            String tempURL = System.getProperty("java.io.tmpdir") + "/" + ctCode.toLowerCase() + ".zip";
            LOGGER.debug("Download file {}", url);
            in = new BufferedInputStream(conn.getInputStream());
            tempFile = new File(tempURL);
            FileOutputStream fos = new FileOutputStream(tempFile);
            bout = new BufferedOutputStream(fos, BUFFER_SIZE);
            byte[] data = new byte[BUFFER_SIZE];
            int x;
            while ((x = in.read(data, 0, BUFFER_SIZE)) >= 0) {
                bout.write(data, 0, x);
            }
            bout.close();
            in.close();
            return true;
            //} else {
            //    return false;
            // }
        } catch (IOException ex) {
            LOGGER.error("Can not download Country data file", ex);
            throw new EvasionException("IO Exception sur un readline.", ex);
        } finally {
            if (bout != null) {
                try {
                    bout.close();
                } catch (IOException ex) {
                    LOGGER.error("", ex);
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException ex) {
                    LOGGER.error("", ex);
                }
            }
        }
    }

    public boolean importData(String format, Mode mode) throws EvasionException {
        if (tempFile == null) {
            throw new EvasionException("No tempFile found for import");
        }
        AbstractFileParser parser;
            if (GeonameParser.FORMAT.equalsIgnoreCase(format)) {
                parser = new GeonameParser(tempFile);
            } else if (GISParser.FORMAT.equalsIgnoreCase(format)) {
                parser = new GISParser(tempFile);
            } else {
                throw new ImportFormatException("Parser not found");
            }
        int count = 0;
        try {
            parser.initParser();
            
            if (mode==Mode.DELETE_INSERT) {
                throw new IllegalStateException("Not supported yet");
            }
            while (parser.hasMoreElement()) {

                if (utx.getStatus()==Status.STATUS_MARKED_ROLLBACK) {
                    LOGGER.error("Import marqué comme rollback");
                    break;
                }
                EntityJPA entity = parser.next();
                if (entity != null) {
                    LOGGER.debug("Entity parsed: {}", entity.toString());
                    completeLocation(entity);
                    
                    if (mode== Mode.DELETE_INSERT) {
                        defaultDAO.persist(entity);
                    } else {
                        // recuperation de l'entity en base
                        Query query = defaultDAO.getEntityManager().createQuery("select l.id from "+entity.ENTITY_NAME+" where l.name=?1 and l.country=?2");
                        Long entityId =(Long) query.getSingleResult();
                        
                        entity.setId(entityId);
                        defaultDAO.merge(entity);
                    }
                    count++;
                    if (count % BATCH_SIZE == 0) {
                        em.flush();
                    }
                }
            }
        } catch (Exception ex) {
            LOGGER.error("Exception on import; current line " + parser.getLineNumber(), ex);
        } finally {
            if (parser != null) {
                parser.close();
            }
        }
        return false;
    }

    private void completeLocation(EntityJPA entity) {
        if (entity instanceof AdminDivision) {
            Country ct = (Country) countryDAO.findByCtCode(((AdminDivision) entity).getCountry().getCode());
            if (ct == null) {
                LOGGER.error("Country by ct code {} not found", ((AdminDivision) entity).getCountry().getCode());
            } else {
                ((AdminDivision) entity).setCountry(ct);
            }
        } else if (entity instanceof City) {
            AdminDivision adm = null;
            City city = (City) entity;
            if (city.getAdmDivision() != null && city.getAdmDivision().getCode() != null) {
                adm = (AdminDivision) regionAdmDAO.findByAdmCode(((City) entity).getAdmDivision().getCode());
            }
            ((City) entity).setAdmDivision(adm);
            Country ct = (Country) countryDAO.findByCtCode(((City) entity).getCountry().getCode());
            if (ct == null) {
                LOGGER.error("Country by ct code {} not found", ((City) entity).getCountry().getCode());
            } else {
                ((City) entity).setCountry(ct);
            }

        }
    }

    public void clear() {
        if (tempFile != null) {
            tempFile.deleteOnExit();
        }
    }
    
    public enum Mode {
        DELETE_INSERT,
        UPDATE;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy