fedilab-Android-App/app/src/main/java/app/fedilab/android/sqlite/InstancesDAO.java

184 lines
5.9 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite;
2018-08-20 19:00:20 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-08-20 19:00:20 +02: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-08-20 19:00:20 +02: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-08-20 19:00:20 +02: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.RemoteInstance;
import app.fedilab.android.helper.Helper;
2018-08-20 19:00:20 +02:00
/**
* Created by Thomas on 20/08/2018.
* Manage instance names in DB
*/
public class InstancesDAO {
public Context context;
2019-11-15 16:32:25 +01:00
private SQLiteDatabase db;
2018-08-20 19:00:20 +02:00
public InstancesDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
}
//------- INSERTIONS -------
/**
* Insert an instance name in database
2019-09-06 17:55:14 +02:00
*
2018-08-20 19:00:20 +02:00
* @param instanceName String
*/
2018-10-22 19:19:39 +02:00
public void insertInstance(String instanceName, String id, String type) {
2018-08-20 19:00:20 +02:00
ContentValues values = new ContentValues();
2018-08-21 14:34:20 +02:00
values.put(Sqlite.COL_INSTANCE, instanceName.trim());
2018-10-22 19:19:39 +02:00
values.put(Sqlite.COL_USER_ID, id);
values.put(Sqlite.COL_INSTANCE_TYPE, type);
2018-08-20 19:00:20 +02:00
values.put(Sqlite.COL_DATE_CREATION, Helper.dateToString(new Date()));
//Inserts search
2019-09-06 17:55:14 +02:00
try {
2018-08-20 19:00:20 +02:00
db.insert(Sqlite.TABLE_INSTANCES, null, values);
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
2018-08-20 19:00:20 +02:00
}
2019-05-25 11:29:58 +02:00
//------- UPDATES -------
/**
* update instance in database
2019-09-06 17:55:14 +02:00
*
2019-05-25 11:29:58 +02:00
* @param remoteInstance RemoteInstance
*/
public void updateInstance(RemoteInstance remoteInstance) {
ContentValues values = new ContentValues();
List<String> tags = remoteInstance.getTags();
values.put(Sqlite.COL_FILTERED_WITH, remoteInstance.getFilteredWith());
2019-09-06 17:55:14 +02:00
if (tags != null && tags.size() > 0) {
2019-05-25 11:29:58 +02:00
values.put(Sqlite.COL_TAGS, Helper.arrayToStringStorage(tags));
}
2019-09-06 17:55:14 +02:00
try {
db.update(Sqlite.TABLE_INSTANCES, values, Sqlite.COL_INSTANCE + " = ? ", new String[]{String.valueOf(remoteInstance.getHost())});
} catch (Exception ignored) {
ignored.printStackTrace();
}
2019-05-25 11:29:58 +02:00
}
2018-10-22 19:19:39 +02:00
public void insertInstance(String instanceName, String type) {
insertInstance(instanceName, "null", type);
}
2018-08-20 19:00:20 +02:00
//------- REMOVE -------
/***
* Remove instance by its name
* @return int
*/
2019-09-06 17:55:14 +02:00
public int remove(String host) {
return db.delete(Sqlite.TABLE_INSTANCES, Sqlite.COL_INSTANCE + " = \"" + host + "\"", null);
2018-08-20 19:00:20 +02:00
}
//------- REMOVE -------
/***
* Remove instance by its name
* @return int
*/
2019-09-06 17:55:14 +02:00
public void cleanDoublon() {
2018-10-22 19:19:39 +02:00
db.delete(Sqlite.TABLE_INSTANCES, Sqlite.COL_ID + " NOT IN (" +
" SELECT MIN(" + Sqlite.COL_ID + ")" +
2018-08-20 19:00:20 +02:00
" FROM " + Sqlite.TABLE_INSTANCES +
2018-10-22 19:19:39 +02:00
" GROUP BY " + Sqlite.COL_INSTANCE + "," + Sqlite.COL_USER_ID + ")", null);
2018-08-20 19:00:20 +02:00
}
//------- GETTERS -------
/**
* Returns all instances in db for a user
2019-09-06 17:55:14 +02:00
*
* @return instances List<RemoteInstance>
2018-08-20 19:00:20 +02:00
*/
2019-09-06 17:55:14 +02:00
public List<RemoteInstance> getAllInstances() {
2018-08-20 19:00:20 +02:00
try {
2018-10-22 19:19:39 +02:00
Cursor c = db.query(Sqlite.TABLE_INSTANCES, null, null, null, null, null, Sqlite.COL_INSTANCE + " ASC", null);
2018-08-20 19:00:20 +02:00
return cursorToListSearch(c);
} catch (Exception e) {
2019-05-25 11:29:58 +02:00
e.printStackTrace();
2018-08-20 19:00:20 +02:00
return null;
}
}
/**
* Returns instance by its nale in db
2019-09-06 17:55:14 +02:00
*
* @return instance List<RemoteInstance>
2018-08-20 19:00:20 +02:00
*/
2019-09-06 17:55:14 +02:00
public List<RemoteInstance> getInstanceByName(String keyword) {
2018-08-20 19:00:20 +02:00
try {
2018-10-22 19:19:39 +02:00
Cursor c = db.query(Sqlite.TABLE_INSTANCES, null, Sqlite.COL_INSTANCE + " = \"" + keyword + "\"", null, null, null, null, null);
2018-08-20 19:00:20 +02:00
return cursorToListSearch(c);
} catch (Exception e) {
return null;
}
}
/***
* Method to hydrate stored instances from database
* @param c Cursor
* @return List<RemoteInstance>
2018-08-20 19:00:20 +02:00
*/
2019-09-06 17:55:14 +02:00
private List<RemoteInstance> cursorToListSearch(Cursor c) {
2018-08-20 19:00:20 +02:00
//No element found
2019-08-17 11:10:31 +02:00
if (c.getCount() == 0) {
c.close();
2018-08-20 19:00:20 +02:00
return null;
2019-08-17 11:10:31 +02:00
}
List<RemoteInstance> remoteInstances = new ArrayList<>();
2019-09-06 17:55:14 +02:00
while (c.moveToNext()) {
RemoteInstance remoteInstance = new RemoteInstance();
2018-10-22 19:19:39 +02:00
remoteInstance.setDbID(c.getString(c.getColumnIndex(Sqlite.COL_ID)));
remoteInstance.setId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
2019-05-25 11:29:58 +02:00
try {
remoteInstance.setTags(Helper.restoreArrayFromString(c.getString(c.getColumnIndex(Sqlite.COL_TAGS))));
2019-09-06 17:55:14 +02:00
} catch (Exception ignored) {
}
remoteInstance.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
2019-05-25 11:29:58 +02:00
remoteInstance.setFilteredWith(c.getString(c.getColumnIndex(Sqlite.COL_FILTERED_WITH)));
2019-09-06 17:55:14 +02:00
remoteInstance.setType(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE_TYPE)) == null ? "MASTODON" : c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE_TYPE)));
2018-10-07 11:57:15 +02:00
remoteInstances.add(remoteInstance);
2018-08-20 19:00:20 +02:00
}
//Close the cursor
c.close();
return remoteInstances;
2018-08-20 19:00:20 +02:00
}
}