package net.aws.gps;

import java.io.IOException;
import java.sql.SQLException;
import java.text.DecimalFormat;

import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.application.FacesMessage;

import net.aws.conn.AwsConnection;

@ManagedBean
public class Coordinates {
	
	private String msg = "No message";
	AwsConnection awsConn = null;
	
	public void getCoord() {
		String latitude = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("latitude");
		String longitude = (String) FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("longitude");

		// Reduce the number to 4 digits because devices usually are precise
		// only to that particular decimal => 11m offset
		DecimalFormat coordFormater = new DecimalFormat("###.####");
		latitude = coordFormater.format(Double.parseDouble(latitude));
		longitude = coordFormater.format(Double.parseDouble(longitude));
		
		int hitID;
		if( (hitID = coordinatesExist(latitude,longitude)) != -1) {
			increaseHitCount(hitID);
		} else {
			addLocationToDB(latitude,longitude);
		}

		FacesContext.getCurrentInstance().addMessage(null, 
				new FacesMessage(FacesMessage.SEVERITY_INFO, "Response", msg));
	}

	private int coordinatesExist(String latitude, String longitude) {
		awsConn = new AwsConnection();
		int loc_id = -1;
		try {
			loc_id = awsConn.getHitCount(latitude, longitude);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return loc_id;
	}

	private void increaseHitCount(int loc_id) {
		awsConn = new AwsConnection();
		try {
			awsConn.incrementHitCount(loc_id);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void addLocationToDB(String latitude, String longitude) {
		int result;
		
		awsConn = new AwsConnection();
		try {
			result = awsConn.insertCoordinates(latitude, longitude);
			if(result == 1)
				msg = "Coordinates inserted";
			else
				msg = "Coordinates failed to inser";
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				awsConn.closeConn();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}		
	}
}







@@ -1080,16 +1080,20 @@
 (hitID);
+%09%09%09%09
 %0A%09%09%7D els
@@ -1746,16 +1746,20 @@
 ry %7B%0A%09%09%09
+if( 
 awsConn.
@@ -1783,16 +1783,153 @@
 (loc_id)
+ != 0)%0A%09%09%09%09msg = %22Coordinates existing. Hit count increased%22;%0A%09%09%09else%0A%09%09%09%09msg = %22Coordinates existing. Hit count could not be increased!%22
 ;%0A%09%09%7D ca
@@ -2107,19 +2107,16 @@
 ();%0A%09%09%7D%0A
-%09%09%0A
 %09%7D%0A%0A%09pri
@@ -2349,32 +2349,32 @@
 erted%22;%0A%09%09%09else%0A
-
 %09%09%09%09msg = %22Coord
@@ -2395,16 +2395,17 @@
 to inser
+t
 %22;%0A%09%09%7D c

Test if updating the hit count was a succes and display messages accordingly