/*
 *  Copyright (C) 2010-2012 Stichting Akvo (Akvo Foundation)
 *
 *  This file is part of Akvo FLOW.
 *
 *  Akvo FLOW is free software: you can redistribute it and modify it under the terms of
 *  the GNU Affero General Public License (AGPL) as published by the Free Software Foundation,
 *  either version 3 of the License or any later version.
 *
 *  Akvo FLOW is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the GNU Affero General Public License included below for more details.
 *
 *  The full license text can also be seen at <http://www.gnu.org/licenses/agpl.html>.
 */

package com.gallatinsystems.device.dao;

import java.util.Date;
import java.util.logging.Logger;

import com.gallatinsystems.device.domain.Device;
import com.gallatinsystems.device.domain.Device.DeviceType;
import com.gallatinsystems.framework.dao.BaseDAO;

/**
 * data access object for devices.
 * 
 * @author Christopher Fagiani
 * 
 */
public class DeviceDAO extends BaseDAO<Device> {
	@SuppressWarnings("unused")
	private static final Logger log = Logger.getLogger(DeviceDAO.class
			.getName());

	public DeviceDAO() {
		super(Device.class);
	}

	/**
	 * gets a single device by phoneNumber. If phone number is not unique (this
	 * shouldn't happen), it returns the first instance found.
	 * 
	 * @param phoneNumber
	 * @return
	 */
	public Device get(String phoneNumber) {
		return super.findByProperty("phoneNumber", phoneNumber, STRING_TYPE);
	}


	/**
	 * gets a single device by imei/esn. If phone number is not unique (this
	 * shouldn't happen), it returns the first instance found.
	 * 
	 * @param imei
	 * @return
	 */
	public Device getByImei(String imei) {
		return super.findByProperty("esn", imei, STRING_TYPE);
	}

	/**
	 * updates the device's last known position
	 * 
	 * @param phoneNumber
	 * @param lat
	 * @param lon
	 * @param accuracy
	 * @param version
	 * @param deviceIdentifier
	 * @param imei
	 */
	public void updateDeviceLocation(String phoneNumber,
			Double lat,	Double lon, Double accuracy,
			String version,
			String deviceIdentifier,
			String imei) {
		Device d;
		if (imei != null) //New Apps from 1.10.0 and on provide IMEI/ESN
			d = getByImei(imei);
		else
			d = get(phoneNumber); //Fall back to less-stable ID
		if (d == null) {
			// if device is null, we have to create it
			d = new Device();
			d.setCreatedDateTime(new Date());
			d.setDeviceType(DeviceType.CELL_PHONE_ANDROID);
			d.setPhoneNumber(phoneNumber);
		}
		if (lat != null && lon != null) {
			d.setLastKnownLat(lat);
			d.setLastKnownLon(lon);
			d.setLastKnownAccuracy(accuracy);
		}
		if (deviceIdentifier != null) {
			d.setDeviceIdentifier(deviceIdentifier);
		}
		if (imei != null) {
				d.setEsn(imei);
		}
		d.setLastLocationBeaconTime(new Date());
		d.setGallatinSoftwareManifest(version);
		// only save if d isn't already a persistent object
		if (d.getKey() == null) {
			save(d);
		}
	}
}

@@ -1559,17 +1559,16 @@
 E);%0A%09%7D%0A%0A
-%0A
 %09/**%0A%09 *
@@ -2086,20 +2086,17 @@
 eNumber,
-%0A%09%09%09
+ 
 Double l
@@ -2098,16 +2098,19 @@
 ble lat,
+%0A%09%09
 %09Double 
@@ -2130,20 +2130,17 @@
 ccuracy,
-%0A%09%09%09
+ 
 String v
@@ -2174,20 +2174,17 @@
 ntifier,
-%0A%09%09%09
+ 
 String i
@@ -2200,16 +2200,23 @@
 Device d
+ = null
 ;%0A%09%09if (
@@ -2228,19 +2228,22 @@
 != null)
+ %7B
  //
+ 
 New Apps
@@ -2309,12 +2309,29 @@
 ;%0A%09%09
-else
+%7D%0A%0A%09%09if (d == null) %7B
 %0A%09%09%09
@@ -2354,16 +2354,17 @@
 ber); //
+ 
 Fall bac
@@ -2383,16 +2383,20 @@
 able ID%0A
+%09%09%7D%0A
 %09%09if (d 
@@ -2837,17 +2837,16 @@
 l) %7B%0A%09%09%09
-%09
 d.setEsn
@@ -2946,91 +2946,8 @@
 n);%0A
-%09%09// only save if d isn't already a persistent object%0A%09%09if (d.getKey() == null) %7B%0A%09
 %09%09sa
@@ -2953,17 +2953,13 @@
 ave(d);%0A
-%09%09%7D%0A
 %09%7D%0A%7D%0A

fix saving device info in deviceDao