2019-05-18 11:10:30 +02:00
|
|
|
package app.fedilab.android.sqlite;
|
2018-12-09 12:14:05 +01:00
|
|
|
/* Copyright 2018 Thomas Schneider
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* This file is a part of Fedilab
|
2018-12-09 12:14:05 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2018-12-09 12:14:05 +01:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2019-05-18 11:10:30 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
2018-12-09 12:14:05 +01:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
2019-05-18 11:10:30 +02:00
|
|
|
import app.fedilab.android.client.Entities.Status;
|
|
|
|
import app.fedilab.android.client.Entities.StoredStatus;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
2018-12-09 12:14:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 09/12/2018
|
|
|
|
* Manage Status storage in DB
|
|
|
|
*/
|
|
|
|
|
|
|
|
public class BoostScheduleDAO {
|
|
|
|
|
|
|
|
public Context context;
|
2019-11-15 16:32:25 +01:00
|
|
|
private SQLiteDatabase db;
|
2018-12-09 12:14:05 +01:00
|
|
|
|
|
|
|
public BoostScheduleDAO(Context context, SQLiteDatabase db) {
|
|
|
|
//Creation of the DB with tables
|
|
|
|
this.context = context;
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//------- INSERTIONS -------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a status in database
|
2019-09-06 17:55:14 +02:00
|
|
|
*
|
2018-12-09 12:14:05 +01:00
|
|
|
* @param status Status
|
2019-09-06 17:55:14 +02:00
|
|
|
* @param jobId int
|
2018-12-09 12:14:05 +01:00
|
|
|
* @return boolean
|
|
|
|
*/
|
2019-09-06 17:55:14 +02:00
|
|
|
public long insert(Status status, int jobId, Date date_scheduled) {
|
2018-12-09 15:47:26 +01:00
|
|
|
|
2018-12-09 12:14:05 +01:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
String serializedStatus = Helper.statusToStringStorage(status);
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
String instance = Helper.getLiveInstance(context);
|
2019-09-06 17:55:14 +02:00
|
|
|
if (userId == null || instance == null)
|
2018-12-09 12:14:05 +01:00
|
|
|
return -1;
|
|
|
|
values.put(Sqlite.COL_STATUS_SERIALIZED, serializedStatus);
|
|
|
|
values.put(Sqlite.COL_INSTANCE, instance);
|
|
|
|
values.put(Sqlite.COL_USER_ID, userId);
|
|
|
|
values.put(Sqlite.COL_SENT, 0);
|
|
|
|
values.put(Sqlite.COL_IS_SCHEDULED, jobId);
|
|
|
|
values.put(Sqlite.COL_DATE_SCHEDULED, Helper.dateToString(date_scheduled));
|
|
|
|
//Inserts stored status
|
|
|
|
long last_id;
|
2019-09-06 17:55:14 +02:00
|
|
|
try {
|
2018-12-09 12:14:05 +01:00
|
|
|
last_id = db.insert(Sqlite.TABLE_BOOST_SCHEDULE, null, values);
|
2019-09-06 17:55:14 +02:00
|
|
|
} catch (Exception e) {
|
2018-12-09 15:47:26 +01:00
|
|
|
e.printStackTrace();
|
2019-09-06 17:55:14 +02:00
|
|
|
last_id = -1;
|
2018-12-09 12:14:05 +01:00
|
|
|
}
|
|
|
|
return last_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a Boost schedule in database
|
2019-09-06 17:55:14 +02:00
|
|
|
*
|
|
|
|
* @param id long
|
2018-12-09 12:14:05 +01:00
|
|
|
* @param jobId int
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public int updateJobId(long id, int jobId) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(Sqlite.COL_IS_SCHEDULED, jobId);
|
|
|
|
return db.update(Sqlite.TABLE_BOOST_SCHEDULE,
|
|
|
|
values, Sqlite.COL_ID + " = ? ",
|
|
|
|
new String[]{String.valueOf(id)});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update scheduled date for a Status in database
|
2019-09-06 17:55:14 +02:00
|
|
|
*
|
2018-12-09 12:14:05 +01:00
|
|
|
* @param scheduled_date Date
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2018-12-09 15:47:26 +01:00
|
|
|
public int updateScheduledDate(int statusStoredId, int jobid, Date scheduled_date) {
|
2018-12-09 12:14:05 +01:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(Sqlite.COL_DATE_SCHEDULED, Helper.dateToString(scheduled_date));
|
|
|
|
return db.update(Sqlite.TABLE_BOOST_SCHEDULE,
|
2018-12-09 15:47:26 +01:00
|
|
|
values, Sqlite.COL_IS_SCHEDULED + " = ? AND " + Sqlite.COL_ID + " = ?",
|
|
|
|
new String[]{String.valueOf(jobid), String.valueOf(statusStoredId)});
|
2018-12-09 12:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update date when task is done for a scheduled Status in database
|
2019-09-06 17:55:14 +02:00
|
|
|
*
|
|
|
|
* @param jobid int
|
2018-12-09 12:14:05 +01:00
|
|
|
* @param date_sent Date
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public int updateScheduledDone(int jobid, Date date_sent) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(Sqlite.COL_DATE_SENT, Helper.dateToString(date_sent));
|
|
|
|
values.put(Sqlite.COL_SENT, 1);
|
|
|
|
return db.update(Sqlite.TABLE_BOOST_SCHEDULE,
|
|
|
|
values, Sqlite.COL_IS_SCHEDULED + " = ? ",
|
|
|
|
new String[]{String.valueOf(jobid)});
|
|
|
|
}
|
|
|
|
|
|
|
|
//------- REMOVE -------
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Remove stored status by id
|
|
|
|
* @return int
|
|
|
|
*/
|
2019-09-06 17:55:14 +02:00
|
|
|
public int remove(long id) {
|
|
|
|
return db.delete(Sqlite.TABLE_BOOST_SCHEDULE, Sqlite.COL_ID + " = \"" + id + "\"", null);
|
2018-12-09 12:14:05 +01:00
|
|
|
}
|
|
|
|
|
2019-09-06 17:55:14 +02:00
|
|
|
public int removeAllSent() {
|
|
|
|
return db.delete(Sqlite.TABLE_BOOST_SCHEDULE, Sqlite.COL_IS_SCHEDULED + " != 0 AND " + Sqlite.COL_SENT + " = 1", null);
|
2018-12-09 12:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//------- GETTERS -------
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all scheduled Statuses in db
|
2019-09-06 17:55:14 +02:00
|
|
|
*
|
2018-12-09 12:14:05 +01:00
|
|
|
* @return stored status List<StoredStatus>
|
|
|
|
*/
|
2019-09-06 17:55:14 +02:00
|
|
|
public List<StoredStatus> getAllScheduled() {
|
2018-12-09 12:14:05 +01:00
|
|
|
try {
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
String instance = Helper.getLiveInstance(context);
|
2019-09-06 17:55:14 +02:00
|
|
|
Cursor c = db.query(Sqlite.TABLE_BOOST_SCHEDULE, null, Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_IS_SCHEDULED + " != 0 AND " + Sqlite.COL_SENT + " = 0", null, null, null, Sqlite.COL_DATE_SCHEDULED + " ASC", null);
|
2018-12-09 12:14:05 +01:00
|
|
|
return cursorToListStatuses(c);
|
|
|
|
} catch (Exception e) {
|
2018-12-09 15:47:26 +01:00
|
|
|
e.printStackTrace();
|
2018-12-09 12:14:05 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2019-09-06 17:55:14 +02:00
|
|
|
|
2018-12-09 12:14:05 +01:00
|
|
|
/**
|
|
|
|
* Returns all not sent Statuses in db
|
2019-09-06 17:55:14 +02:00
|
|
|
*
|
2018-12-09 12:14:05 +01:00
|
|
|
* @return stored status List<StoredStatus>
|
|
|
|
*/
|
2019-09-06 17:55:14 +02:00
|
|
|
public List<StoredStatus> getAllNotSent() {
|
2018-12-09 12:14:05 +01:00
|
|
|
try {
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
String instance = Helper.getLiveInstance(context);
|
2019-09-06 17:55:14 +02:00
|
|
|
Cursor c = db.query(Sqlite.TABLE_BOOST_SCHEDULE, null, Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_IS_SCHEDULED + " != 0 AND " + Sqlite.COL_SENT + " = 0", null, null, null, Sqlite.COL_DATE_SCHEDULED + " DESC", null);
|
2018-12-09 12:14:05 +01:00
|
|
|
return cursorToListStatuses(c);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all sent Statuses in db
|
2019-09-06 17:55:14 +02:00
|
|
|
*
|
2018-12-09 12:14:05 +01:00
|
|
|
* @return stored status List<StoredStatus>
|
|
|
|
*/
|
2019-09-06 17:55:14 +02:00
|
|
|
public List<StoredStatus> getAllSent() {
|
2018-12-09 12:14:05 +01:00
|
|
|
try {
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
String instance = Helper.getLiveInstance(context);
|
2019-09-06 17:55:14 +02:00
|
|
|
Cursor c = db.query(Sqlite.TABLE_BOOST_SCHEDULE, null, Sqlite.COL_USER_ID + " = '" + userId + "' AND " + Sqlite.COL_INSTANCE + " = '" + instance + "' AND " + Sqlite.COL_IS_SCHEDULED + " != 0 AND " + Sqlite.COL_SENT + " = 1", null, null, null, Sqlite.COL_DATE_SCHEDULED + " DESC", null);
|
2018-12-09 12:14:05 +01:00
|
|
|
return cursorToListStatuses(c);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a stored status by id in db
|
2019-09-06 17:55:14 +02:00
|
|
|
*
|
2018-12-09 12:14:05 +01:00
|
|
|
* @return stored status StoredStatus
|
|
|
|
*/
|
2019-09-06 17:55:14 +02:00
|
|
|
public StoredStatus getStatus(long id) {
|
2018-12-09 12:14:05 +01:00
|
|
|
try {
|
|
|
|
Cursor c = db.query(Sqlite.TABLE_BOOST_SCHEDULE, null, Sqlite.COL_ID + " = '" + id + "'", null, null, null, null, null);
|
|
|
|
return cursorToStoredStatus(c);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a stored status by id of job in db
|
2019-09-06 17:55:14 +02:00
|
|
|
*
|
2018-12-09 12:14:05 +01:00
|
|
|
* @return stored status StoredStatus
|
|
|
|
*/
|
2019-09-06 17:55:14 +02:00
|
|
|
public StoredStatus getStatusScheduled(int jobid) {
|
2018-12-09 12:14:05 +01:00
|
|
|
try {
|
|
|
|
Cursor c = db.query(Sqlite.TABLE_BOOST_SCHEDULE, null, Sqlite.COL_IS_SCHEDULED + " = '" + jobid + "'", null, null, null, null, null);
|
|
|
|
return cursorToStoredStatus(c);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Method to hydrate Stored statuses from database
|
|
|
|
* @param c Cursor
|
|
|
|
* @return StoredStatus
|
|
|
|
*/
|
2019-09-06 17:55:14 +02:00
|
|
|
private StoredStatus cursorToStoredStatus(Cursor c) {
|
2018-12-09 12:14:05 +01:00
|
|
|
//No element found
|
2019-08-17 14:34:33 +02:00
|
|
|
if (c.getCount() == 0) {
|
|
|
|
c.close();
|
2018-12-09 12:14:05 +01:00
|
|
|
return null;
|
2019-08-17 14:34:33 +02:00
|
|
|
}
|
2018-12-09 12:14:05 +01:00
|
|
|
//Take the first element
|
|
|
|
c.moveToFirst();
|
|
|
|
//New user
|
|
|
|
StoredStatus storedStatus = new StoredStatus();
|
|
|
|
storedStatus.setId(c.getInt(c.getColumnIndex(Sqlite.COL_ID)));
|
|
|
|
Status status = Helper.restoreStatusFromString(c.getString(c.getColumnIndex(Sqlite.COL_STATUS_SERIALIZED)));
|
2019-09-06 17:55:14 +02:00
|
|
|
if (status == null) {
|
2018-12-09 12:14:05 +01:00
|
|
|
remove(c.getInt(c.getColumnIndex(Sqlite.COL_ID)));
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
storedStatus.setStatus(status);
|
|
|
|
storedStatus.setStatusReply(null);
|
|
|
|
storedStatus.setSent(c.getInt(c.getColumnIndex(Sqlite.COL_SENT)) == 1);
|
|
|
|
storedStatus.setJobId(c.getInt(c.getColumnIndex(Sqlite.COL_IS_SCHEDULED)));
|
|
|
|
storedStatus.setScheduled_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_SCHEDULED))));
|
|
|
|
storedStatus.setSent_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_SENT))));
|
|
|
|
storedStatus.setUserId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
|
|
|
|
storedStatus.setInstance(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
|
|
|
|
//Close the cursor
|
|
|
|
c.close();
|
|
|
|
//Stored status is returned
|
|
|
|
return storedStatus;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Method to hydrate stored statuses from database
|
|
|
|
* @param c Cursor
|
|
|
|
* @return List<StoredStatus>
|
|
|
|
*/
|
2019-09-06 17:55:14 +02:00
|
|
|
private List<StoredStatus> cursorToListStatuses(Cursor c) {
|
2018-12-09 12:14:05 +01:00
|
|
|
//No element found
|
2019-08-17 14:34:33 +02:00
|
|
|
if (c.getCount() == 0) {
|
|
|
|
c.close();
|
2018-12-09 12:14:05 +01:00
|
|
|
return null;
|
2019-08-17 14:34:33 +02:00
|
|
|
}
|
2018-12-09 12:14:05 +01:00
|
|
|
List<StoredStatus> storedStatuses = new ArrayList<>();
|
2019-09-06 17:55:14 +02:00
|
|
|
while (c.moveToNext()) {
|
2018-12-09 12:14:05 +01:00
|
|
|
//Restore the status
|
|
|
|
StoredStatus storedStatus = new StoredStatus();
|
|
|
|
storedStatus.setId(c.getInt(c.getColumnIndex(Sqlite.COL_ID)));
|
|
|
|
Status status = Helper.restoreStatusFromString(c.getString(c.getColumnIndex(Sqlite.COL_STATUS_SERIALIZED)));
|
2019-09-06 17:55:14 +02:00
|
|
|
if (status == null) {
|
2018-12-09 12:14:05 +01:00
|
|
|
remove(c.getInt(c.getColumnIndex(Sqlite.COL_ID)));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
storedStatus.setStatus(status);
|
2018-12-09 15:47:26 +01:00
|
|
|
storedStatus.setStatusReply(null);
|
2018-12-09 12:14:05 +01:00
|
|
|
storedStatus.setSent(c.getInt(c.getColumnIndex(Sqlite.COL_SENT)) == 1);
|
2019-09-06 17:55:14 +02:00
|
|
|
storedStatus.setJobId(c.getInt(c.getColumnIndex(Sqlite.COL_IS_SCHEDULED)));
|
2018-12-09 12:14:05 +01:00
|
|
|
storedStatus.setScheduled_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_SCHEDULED))));
|
|
|
|
storedStatus.setSent_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_SENT))));
|
|
|
|
storedStatus.setUserId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
|
|
|
|
storedStatus.setInstance(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
|
|
|
|
storedStatuses.add(storedStatus);
|
|
|
|
}
|
|
|
|
//Close the cursor
|
|
|
|
c.close();
|
|
|
|
//Statuses list is returned
|
|
|
|
return storedStatuses;
|
|
|
|
}
|
|
|
|
}
|