<commit_before>package com.noxlogic.joindin;

/*
 * Data helper for the SQLite database.
 *
 * All data is stored in a database so we do not have to call the joind.in api
 * to display data. Instead of filtering out all JSON data, we just store some
 * standard data (event_id, talk_id) and for the rest we just store the actual
 * received JSON data. This is because we not going to seek or query data anyway.
 *
 * The helper functions here take care of everything, so no need to query anything
 * outside this class.
 */

import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.ArrayAdapter;

import java.util.ArrayList;


public final class DataHelper {
    private static DataHelper DHinstance = null;

    private static final String DATABASE_NAME = "joindin.db";
    private static final int DATABASE_VERSION = 11;  // Increasing this version number will result in automatic call to onUpgrade()

    public static final int ORDER_DATE_ASC 		= 1;
    public static final int ORDER_DATE_DESC 	= 2;
    public static final int ORDER_TITLE_ASC 	= 3;
    public static final int ORDER_TITLE_DESC 	= 4;

    private SQLiteDatabase db = null;

    private DataHelper(Context context) {
        OpenHelper openHelper = new OpenHelper (context);
        this.db = openHelper.getWritableDatabase();
    }

    public static DataHelper createInstance (Context context) {
        if (DHinstance == null) DHinstance = new DataHelper (context);

        return DHinstance;
    }

    public static DataHelper getInstance () {
        if (DHinstance == null) return null;
        return DHinstance;
    }

    // Updates a event
    public long updateEvent(int eventRowID, JSONObject event) {
        ContentValues values = new ContentValues();
        values.put("json", event.toString());
        return db.update("events", values, "_rowid_=?", new String[] {Integer.toString(eventRowID)});
    }

    // insert a new event to specified event_type (hot, pending, past etc)
    public long insertEvent(String event_type, JSONObject event) {
        ContentValues values = new ContentValues();
        values.put("event_uri", event.optString("uri"));
        values.put("event_start", event.optInt("start_date"));
        values.put("event_title", event.optString("name"));
        values.put("json", event.toString());

        long eventID = getEventIDByURI(event.optString("event_uri"));
        if (eventID > 0) {
            db.delete("event_types", "event_id=? AND event_type=?", new String[]{String.valueOf(eventID), event_type});
        }
        else {
            eventID = db.insert("events", "", values);

        }

        // add event type
        values = new ContentValues();
        values.put("event_id", eventID);
        values.put("event_type", event_type);
        db.insert("event_types", "", values);

        return eventID;
    }

    // load event
    public JSONObject getEvent(int eventRowID) {
        Cursor c = this.db.rawQuery("SELECT json FROM events WHERE _rowid_ = " + eventRowID, null);
        JSONObject json;
        try {
            c.moveToFirst();
            json = new JSONObject (c.getString (0));
        } catch (Exception e) {
            json = new JSONObject();
        }
        return json;
    }

    public long getEventIDByURI(String eventURI) {
        if (eventURI.length() == 0) {<commit_msg>Fixed saving of events (was creating multiple copies).<commit_after>package com.noxlogic.joindin;

/*
 * Data helper for the SQLite database.
 *
 * All data is stored in a database so we do not have to call the joind.in api
 * to display data. Instead of filtering out all JSON data, we just store some
 * standard data (event_id, talk_id) and for the rest we just store the actual
 * received JSON data. This is because we not going to seek or query data anyway.
 *
 * The helper functions here take care of everything, so no need to query anything
 * outside this class.
 */

import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.ArrayAdapter;

import java.util.ArrayList;


public final class DataHelper {
    private static DataHelper DHinstance = null;

    private static final String DATABASE_NAME = "joindin.db";
    private static final int DATABASE_VERSION = 11;  // Increasing this version number will result in automatic call to onUpgrade()

    public static final int ORDER_DATE_ASC 		= 1;
    public static final int ORDER_DATE_DESC 	= 2;
    public static final int ORDER_TITLE_ASC 	= 3;
    public static final int ORDER_TITLE_DESC 	= 4;

    private SQLiteDatabase db = null;

    private DataHelper(Context context) {
        OpenHelper openHelper = new OpenHelper (context);
        this.db = openHelper.getWritableDatabase();
    }

    public static DataHelper createInstance (Context context) {
        if (DHinstance == null) DHinstance = new DataHelper (context);

        return DHinstance;
    }

    public static DataHelper getInstance () {
        if (DHinstance == null) return null;
        return DHinstance;
    }

    // Updates a event
    public long updateEvent(int eventRowID, JSONObject event) {
        ContentValues values = new ContentValues();
        values.put("json", event.toString());
        return db.update("events", values, "_rowid_=?", new String[] {Integer.toString(eventRowID)});
    }

    // insert a new event to specified event_type (hot, pending, past etc)
    public long insertEvent(String event_type, JSONObject event) {
        ContentValues values = new ContentValues();
        values.put("event_uri", event.optString("uri"));
        values.put("event_start", event.optInt("start_date"));
        values.put("event_title", event.optString("name"));
        values.put("json", event.toString());

        long eventID = getEventIDByURI(event.optString("uri"));
        if (eventID > 0) {
            db.delete("event_types", "event_id=? AND event_type=?", new String[]{String.valueOf(eventID), event_type});
        }
        else {
            eventID = db.insert("events", "", values);
        }

        // add event type
        values = new ContentValues();
        values.put("event_id", eventID);
        values.put("event_type", event_type);
        db.insert("event_types", "", values);

        return eventID;
    }

    // load event
    public JSONObject getEvent(int eventRowID) {
        Cursor c = this.db.rawQuery("SELECT json FROM events WHERE _rowid_ = " + eventRowID, null);
        JSONObject json;
        try {
            c.moveToFirst();
            json = new JSONObject (c.getString (0));
        } catch (Exception e) {
            json = new JSONObject();
        }
        return json;
    }

    public long getEventIDByURI(String eventURI) {
        if (eventURI.length() == 0) {
{'fraction_non_alphanumeric': 0.09906567738389667, 'fraction_numerical': 0.00247320692497939, 'mean_word_length': 2.9775956284153007, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2643509', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1977, 'n_words': 655}