// Path: src/main/java/com/upnext/blekit/BeaconEvent.java
// public enum BeaconEvent {
// 
//     /**
//      * A beacon has appeared in range
//      */
//     REGION_ENTER,
// 
//     /**
//      * A beacon has disappeared from range.
//      * This event is in reality delayed to prevent imemdiate enter-exit-enter events.
//      * Because of that LEAVE is delayed by {@link com.upnext.blekit.BLEKitService#LEAVE_MSG_DELAY_MILLIS} ms.
//      */
//     REGION_LEAVE,
// 
//     /**
//      * A beacon has just changed proximity to immediate
//      */
//     CAME_IMMEDIATE,
// 
//     /**
//      * A beacon has just changed proximity to near
//      */
//     CAME_NEAR,
// 
//     /**
//      * A beacon has just changed proximity to far
//      */
//     CAME_FAR;
// 
// 
//     /**
//      * Matches IBeacon proximity to BeaconEvent proximity change
//      *
//      * @param iBeacon ibeacon
//      * @return beacon event
//      */
//     public static BeaconEvent fromIBeaconProximity(IBeacon iBeacon) {
//         return fromIBeaconProximity(iBeacon.getProximity());
//     }
// 
//     /**
//      * Matches IBeacon proximity to BeaconEvent proximity change
//      *
//      * @param proximity proximity
//      * @return beacon event
//      */
//     public static BeaconEvent fromIBeaconProximity(int proximity) {
//         switch (proximity) {
//             case IBeacon.PROXIMITY_IMMEDIATE:
//                 return CAME_IMMEDIATE;
// 
//             case IBeacon.PROXIMITY_NEAR:
//                 return CAME_NEAR;
// 
//             case IBeacon.PROXIMITY_FAR:
//                 return CAME_FAR;
// 
//             default:
//                 return null;
//         }
//     }
// }
// 
// Path: src/main/java/com/upnext/blekit/EventOccurenceUnit.java
// public enum EventOccurenceUnit {
// 
//     /**
//      * hour time unit
//      */
//     HOUR,
// 
//     /**
//      * day time unit
//      */
//     DAY,
// 
//     /**
//      * month time unit
//      */
//     MONTH,
// 
//     /**
//      * year time unit
//      */
//     YEAR,
// 
//     /**
//      * total time unit - all events are counted
//      */
//     TOTAL;
// 
//     private static Map<String, EventOccurenceUnit> namesMap = new HashMap<String, EventOccurenceUnit>(5);
// 
//     static {
//         namesMap.put("hour", HOUR);
//         namesMap.put("day", DAY);
//         namesMap.put("month", MONTH);
//         namesMap.put("year", YEAR);
//         namesMap.put("total", TOTAL);
//     }
// 
//     @JsonCreator
//     public static EventOccurenceUnit forValue(String value) {
//         if( value==null ) return TOTAL;
//         EventOccurenceUnit event = namesMap.get(value.toLowerCase());
//         if( event==null ) return TOTAL;
//         return event;
//     }
// }


import com.upnext.blekit.BeaconEvent;
import com.upnext.blekit.EventOccurenceUnit;
import java.util.Date;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
    private static final String KEY_BEACON_ID = "beacon_id";
    private static final String KEY_DATE = "date";

    private static final String[] COLUMNS = {KEY_ID,KEY_EVENT,KEY_BEACON_ID,KEY_DATE};


    /**
     * Adds an event to the events database
     *
     * @param event beacon event
     * @param beaconId beacon identifier
     */
    public void addBeaconEvent( BeaconEvent event, String beaconId ) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_EVENT, event.name());
        values.put(KEY_BEACON_ID, beaconId);
        values.put(KEY_DATE, new Date().getTime());
        db.insert(TABLE_BEACON_EVENTS, null, values);
        db.close();
    }

    /**
     * Returns the number of occurences of event for given beacon id in unit of time.
     *
     * @param event beacon event
     * @param beaconId beacon id
     * @param occurenceUnit occurence unit
     * @return number of occurences
     */
// Path: src/main/java/com/upnext/blekit/BeaconEvent.java
// public enum BeaconEvent {
// 
//     /**
//      * A beacon has appeared in range
//      */
//     REGION_ENTER,
// 
//     /**
//      * A beacon has disappeared from range.
//      * This event is in reality delayed to prevent imemdiate enter-exit-enter events.
//      * Because of that LEAVE is delayed by {@link com.upnext.blekit.BLEKitService#LEAVE_MSG_DELAY_MILLIS} ms.
//      */
//     REGION_LEAVE,
// 
//     /**
//      * A beacon has just changed proximity to immediate
//      */
//     CAME_IMMEDIATE,
// 
//     /**
//      * A beacon has just changed proximity to near
//      */
//     CAME_NEAR,
// 
//     /**
//      * A beacon has just changed proximity to far
//      */
//     CAME_FAR;
// 
// 
//     /**
//      * Matches IBeacon proximity to BeaconEvent proximity change
//      *
//      * @param iBeacon ibeacon
//      * @return beacon event
//      */
//     public static BeaconEvent fromIBeaconProximity(IBeacon iBeacon) {
//         return fromIBeaconProximity(iBeacon.getProximity());
//     }
// 
//     /**
//      * Matches IBeacon proximity to BeaconEvent proximity change
//      *
//      * @param proximity proximity
//      * @return beacon event
//      */
//     public static BeaconEvent fromIBeaconProximity(int proximity) {
//         switch (proximity) {
//             case IBeacon.PROXIMITY_IMMEDIATE:
//                 return CAME_IMMEDIATE;
// 
//             case IBeacon.PROXIMITY_NEAR:
//                 return CAME_NEAR;
// 
//             case IBeacon.PROXIMITY_FAR:
//                 return CAME_FAR;
// 
//             default:
//                 return null;
//         }
//     }
// }
// 
// Path: src/main/java/com/upnext/blekit/EventOccurenceUnit.java
// public enum EventOccurenceUnit {
// 
//     /**
//      * hour time unit
//      */
//     HOUR,
// 
//     /**
//      * day time unit
//      */
//     DAY,
// 
//     /**
//      * month time unit
//      */
//     MONTH,
// 
//     /**
//      * year time unit
//      */
//     YEAR,
// 
//     /**
//      * total time unit - all events are counted
//      */
//     TOTAL;
// 
//     private static Map<String, EventOccurenceUnit> namesMap = new HashMap<String, EventOccurenceUnit>(5);
// 
//     static {
//         namesMap.put("hour", HOUR);
//         namesMap.put("day", DAY);
//         namesMap.put("month", MONTH);
//         namesMap.put("year", YEAR);
//         namesMap.put("total", TOTAL);
//     }
// 
//     @JsonCreator
//     public static EventOccurenceUnit forValue(String value) {
//         if( value==null ) return TOTAL;
//         EventOccurenceUnit event = namesMap.get(value.toLowerCase());
//         if( event==null ) return TOTAL;
//         return event;
//     }
// }


// Path: src/main/java/com/upnext/blekit/util/BeaconsDB.java
import com.upnext.blekit.BeaconEvent;
import com.upnext.blekit.EventOccurenceUnit;
import java.util.Date;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

    private static final String KEY_BEACON_ID = "beacon_id";
    private static final String KEY_DATE = "date";

    private static final String[] COLUMNS = {KEY_ID,KEY_EVENT,KEY_BEACON_ID,KEY_DATE};


    /**
     * Adds an event to the events database
     *
     * @param event beacon event
     * @param beaconId beacon identifier
     */
    public void addBeaconEvent( BeaconEvent event, String beaconId ) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(KEY_EVENT, event.name());
        values.put(KEY_BEACON_ID, beaconId);
        values.put(KEY_DATE, new Date().getTime());
        db.insert(TABLE_BEACON_EVENTS, null, values);
        db.close();
    }

    /**
     * Returns the number of occurences of event for given beacon id in unit of time.
     *
     * @param event beacon event
     * @param beaconId beacon id
     * @param occurenceUnit occurence unit
     * @return number of occurences
     */
    public int getNumOccurencesForBeaconInTime( BeaconEvent event, String beaconId, EventOccurenceUnit occurenceUnit ) {