/**
 * Most of the code in the Qalingo project is copyrighted Hoteia and licensed
 * under the Apache License Version 2.0 (release version 0.7.0)
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *                   Copyright (c) Hoteia, 2012-2013
 * http://www.hoteia.com - http://twitter.com/hoteia - contact@hoteia.com
 *
 */
package org.hoteia.qalingo.core.service.impl;

import java.io.File;
import java.net.InetAddress;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.hoteia.qalingo.core.Constants;
import org.hoteia.qalingo.core.domain.EngineSetting;
import org.hoteia.qalingo.core.service.EmailService;
import org.hoteia.qalingo.core.service.EngineSettingService;
import org.hoteia.qalingo.core.service.GeolocService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fasterxml.jackson.databind.JsonNode;
import com.maxmind.db.Reader;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.AddressNotFoundException;
import com.maxmind.geoip2.model.CityResponse;
import com.maxmind.geoip2.model.CountryResponse;
import com.maxmind.geoip2.record.City;
import com.maxmind.geoip2.record.Country;

@Service("geolocService")
@Transactional
public class GeolocServiceImpl implements GeolocService {
    
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    
    @Autowired
    protected EmailService emailService;
    
    @Autowired
    protected EngineSettingService engineSettingService;
    
    /**
     * 
     */
    public String geolocAndGetCountryIsoCode(final String customerRemoteAddr) throws Exception {
        final Country country = geolocAndGetCountry(customerRemoteAddr);
        return country.getIsoCode();
    }
    
    /**
     * 
     */
    public Country geolocAndGetCountry(final String customerRemoteAddr) throws Exception {
        try {
            final Reader reader = new Reader(getCountryDataBase());
            final InetAddress address = InetAddress.getByName(customerRemoteAddr);
            final JsonNode jsonNode = reader.get(address);
            if(jsonNode != null){
                System.out.println("jsonNode!!!!!!!!!!: " + jsonNode.toString());
            }
            reader.close();
            
            final DatabaseReader databaseReader = new DatabaseReader.Builder(getCountryDataBase()).build();
            final CountryResponse countryResponse = databaseReader.country(address);
            if(countryResponse != null){
                System.out.println("Country !!!! " + countryResponse.getCountry().getIsoCode());
                return countryResponse.getCountry();
            }
        } catch (AddressNotFoundException e) {
            logger.warn("Geoloc can't find this address:" + customerRemoteAddr);
        } catch (Exception e) {
            logger.error("Geoloc can't find this address:" + customerRemoteAddr);
        }
        return null;
    }
    
    /**
     * 
     */
    public String geolocAndGetCityName(final String customerRemoteAddr) throws Exception {
        final City city = geolocAndGetCity(customerRemoteAddr);
        return city.getName();
    }
    
    /**
     * 
     */
    public City geolocAndGetCity(final String customerRemoteAddr) throws Exception {
        try {
            final Reader reader = new Reader(getCityDataBase());
            final InetAddress address = InetAddress.getByName(customerRemoteAddr);
            final JsonNode jsonNode = reader.get(address);
            if(jsonNode != null){
                System.out.println("jsonNode!!!!!!!!!!: " + jsonNode.toString());
            }
            reader.close();
            
            final DatabaseReader databaseReader = new DatabaseReader.Builder(getCityDataBase()).build();

            final CityResponse cityResponse = databaseReader.city(address);
            if(cityResponse != null){
                System.out.println("Country !!!! " + cityResponse.getCountry().getIsoCode());
                return cityResponse.getCity();
                
            }
        } catch (AddressNotFoundException e) {
            logger.warn("Geoloc can't find this address:" + customerRemoteAddr);
        } catch (Exception e) {
            logger.error("Geoloc can't find this address:" + customerRemoteAddr);
        }
        return null;
    }
    
    protected File getCityDataBase(){
        EngineSetting engineSetting = engineSettingService.getGeolocCityFilePath();
        final File database = new File(engineSetting.getDefaultValue());
        return database;
    }
    
    protected File getCountryDataBase(){
        EngineSetting engineSetting = engineSettingService.getGeolocCountryFilePath();
        final File database = new File(engineSetting.getDefaultValue());
        return database;
    }
    
    
}
@@ -432,141 +432,8 @@
 s;%0A%0A
-import javax.servlet.http.HttpServletRequest;%0A%0Aimport org.apache.commons.lang.StringUtils;%0Aimport org.hoteia.qalingo.core.Constants;%0A
 impo
@@ -886,86 +886,8 @@
 l;%0A%0A
-import com.fasterxml.jackson.databind.JsonNode;%0Aimport com.maxmind.db.Reader;%0A
 impo
@@ -1846,32 +1846,34 @@
 %7B%0A        try %7B%0A
+//
             fina
@@ -1928,91 +1928,10 @@
 ));%0A
-            final InetAddress address = InetAddress.getByName(customerRemoteAddr);%0A
+//
     
@@ -1977,32 +1977,34 @@
 r.get(address);%0A
+//
             if(j
@@ -2003,151 +2003,104 @@
     
-if(jsonNode != null)%7B%0A                System.out.println(%22jsonNode!!!!!!!!!!: %22 + jsonNode.toString());%0A            %7D%0A            reader.close(
+reader.close();%0A            final InetAddress address = InetAddress.getByName(customerRemoteAddr
 );%0A 
@@ -2349,105 +2349,8 @@
 l)%7B%0A
-                System.out.println(%22Country !!!! %22 + countryResponse.getCountry().getIsoCode());%0A
     
@@ -3040,16 +3040,18 @@
   try %7B%0A
+//
         
@@ -3111,91 +3111,10 @@
 ));%0A
-            final InetAddress address = InetAddress.getByName(customerRemoteAddr);%0A
+//
     
@@ -3160,32 +3160,34 @@
 r.get(address);%0A
+//
             if(j
@@ -3186,151 +3186,104 @@
     
-if(jsonNode != null)%7B%0A                System.out.println(%22jsonNode!!!!!!!!!!: %22 + jsonNode.toString());%0A            %7D%0A            reader.close(
+reader.close();%0A            final InetAddress address = InetAddress.getByName(customerRemoteAddr
 );%0A 
@@ -3518,102 +3518,8 @@
 l)%7B%0A
-                System.out.println(%22Country !!!! %22 + cityResponse.getCountry().getIsoCode());%0A
     
@@ -4344,14 +4344,9 @@
  %7D%0A    %0A
-    %0A
 %7D

fix Geoloc