package org.waterforpeople.mapping.analytics;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.waterforpeople.mapping.dao.AccessPointDao;
import org.waterforpeople.mapping.dao.CommunityDao;
import org.waterforpeople.mapping.domain.AccessPoint;
import org.waterforpeople.mapping.domain.Community;

import com.gallatinsystems.framework.analytics.summarization.DataSummarizer;
import com.gallatinsystems.gis.geography.domain.Country;
import com.gallatinsystems.gis.location.GeoLocationService;
import com.gallatinsystems.gis.location.GeoLocationServiceGeonamesImpl;
import com.gallatinsystems.gis.location.GeoPlace;

/**
 * This class will populate country/community lookup tables based on geo
 * information in the access point.
 * 
 * @author Christopher Fagiani
 * 
 */
public class CommunityLocationSummarizer implements DataSummarizer {
	Logger logger = Logger.getLogger(CommunityLocationSummarizer.class
			.getName());

	@Override
	public boolean performSummarization(String key, String type, Integer offset) {
		if (key != null) {
			AccessPointDao accessPointDao = new AccessPointDao();
			AccessPoint ap = accessPointDao.getByKey(new Long(key));
			if (ap != null) {
				CommunityDao commDao = new CommunityDao();
				Community community = commDao.findCommunityByCode(ap
						.getCommunityCode());
				if (community == null
						&& (ap.getLatitude() != 0.0 && ap.getLongitude() != 0.0)) {
					GeoLocationService gl = new GeoLocationServiceGeonamesImpl();
					GeoPlace gp = gl.findGeoPlace(ap.getLatitude().toString(),
							ap.getLongitude().toString());
					if(gp == null){
						String countryCode = gl.getCountryCodeForPoint(ap.getLatitude().toString(),
							ap.getLongitude().toString());
						if(countryCode != null){
							gp = new GeoPlace();
							gp.setCountryCode(countryCode);
							gp.setCountryName(countryCode);
						}
					}
					if (gp != null) {
						Country ourCountry = commDao.findCountryByCode(gp
								.getCountryCode());
						if (ourCountry == null) {
							ourCountry = new Country();
							ourCountry.setIsoAlpha2Code(gp.getCountryCode());
							ourCountry.setName(gp.getCountryName());
							ourCountry.setDisplayName(gp.getCountryName());
							commDao.save(ourCountry);
						}
						community = new Community();
						community.setCommunityCode(ap.getCommunityCode());
						community.setName(gp.getName());
						community.setLat(gp.getLat());
						community.setLon(gp.getLng());
						// have to do this so we can query by it due to GAE's
						// JDO limitations
						community.setCountryCode(ourCountry.getIsoAlpha2Code());
						// this save will cascade-save the country
						commDao.save(community);
					} else {
						logger.log(Level.SEVERE,
								"Did not find a country for lat/lon "
										+ ap.getLatitude() + ","
										+ ap.getLongitude()
										+ " using the geonames service");
					}
				}
				if (ap.getCountryCode() == null && community != null) {
					ap.setCountryCode(community.getCountryCode());
					accessPointDao.save(ap);
				}
			}
		}
		return true;
	}

}

@@ -1425,17 +1425,93 @@
  != 
-0.0 && ap
+null && ap.getLatitude() != 0.0%0D%0A%09%09%09%09%09%09%09%09&& ap.getLongitude() != null && ap%0D%0A%09%09%09%09%09%09%09%09
 .get
@@ -1713,16 +1713,17 @@
 %0A%09%09%09%09%09if
+ 
 (gp == n
@@ -1722,24 +1722,25 @@
 (gp == null)
+ 
 %7B%0D%0A%09%09%09%09%09%09Str
@@ -1785,16 +1785,26 @@
 Point(ap
+%0D%0A%09%09%09%09%09%09%09%09
 .getLati
@@ -1817,33 +1817,25 @@
 .toString(),
-%0D%0A%09%09%09%09%09%09%09
+ 
 ap.getLongit
@@ -1831,32 +1831,42 @@
 p.getLongitude()
+%0D%0A%09%09%09%09%09%09%09%09
 .toString());%0D%0A%09
@@ -1872,16 +1872,17 @@
 %09%09%09%09%09%09if
+ 
 (country
@@ -1894,16 +1894,17 @@
 != null)
+ 
 %7B%0D%0A%09%09%09%09%09

fixed NPE in event that lat/long is null