2018-11-30 18:39:01 +01:00
|
|
|
package fr.gouv.etalab.mastodon.sqlite;
|
|
|
|
/* Copyright 2018 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of Mastalab
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Mastalab 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 General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Thomas on 30/11/2018.
|
|
|
|
* Manage cache tags in DB
|
|
|
|
*/
|
|
|
|
public class TagsCacheDAO {
|
|
|
|
|
|
|
|
private SQLiteDatabase db;
|
|
|
|
public Context context;
|
|
|
|
|
|
|
|
public TagsCacheDAO(Context context, SQLiteDatabase db) {
|
|
|
|
//Creation of the DB with tables
|
|
|
|
this.context = context;
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------- INSERTIONS -------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert a tag in database
|
|
|
|
* @param tag String
|
|
|
|
*/
|
|
|
|
public void insert(String tag) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(Sqlite.COL_TAGS, tag);
|
|
|
|
try{
|
|
|
|
db.insert(Sqlite.TABLE_CACHE_TAGS, null, values);
|
|
|
|
}catch (Exception ignored) {}
|
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:07 +01:00
|
|
|
/**
|
|
|
|
* update a tag in database
|
|
|
|
* @param oldTag String
|
|
|
|
* @param newTag String
|
|
|
|
*/
|
|
|
|
public void update(String oldTag, String newTag) {
|
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
values.put(Sqlite.COL_TAGS, newTag);
|
|
|
|
try{
|
|
|
|
db.update(Sqlite.TABLE_CACHE_TAGS, values, Sqlite.COL_TAGS + " = ?",new String[]{ oldTag});
|
|
|
|
}catch (Exception ignored) {}
|
|
|
|
}
|
|
|
|
|
2018-11-30 18:39:01 +01:00
|
|
|
/***
|
|
|
|
* Remove all tags
|
|
|
|
*/
|
|
|
|
public void removeAll(){
|
|
|
|
db.delete(Sqlite.TABLE_CACHE_TAGS, null, null);
|
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:07 +01:00
|
|
|
|
|
|
|
public void removeTag(String tag) {
|
|
|
|
db.delete(Sqlite.TABLE_CACHE_TAGS, Sqlite.COL_TAGS + " = ?", new String[]{tag});
|
|
|
|
}
|
|
|
|
|
2018-11-30 18:39:01 +01:00
|
|
|
/**
|
|
|
|
* Returns all tags in db
|
|
|
|
* @return string tags List<String>
|
|
|
|
*/
|
|
|
|
public List<String> getAll(){
|
|
|
|
try {
|
|
|
|
Cursor c = db.query(Sqlite.TABLE_CACHE_TAGS, null, null, null, null, null, Sqlite.COL_TAGS+ " ASC", null);
|
|
|
|
return cursorToTag(c);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-01 15:32:07 +01:00
|
|
|
/**
|
|
|
|
* Returns tags starting by "search"
|
|
|
|
* @return boolean present
|
|
|
|
*/
|
|
|
|
public boolean isPresent(String search){
|
|
|
|
Cursor c = db.query(Sqlite.TABLE_CACHE_TAGS, null, Sqlite.COL_TAGS + " = \"" + search + "\"", null, null, null, null, null);
|
|
|
|
boolean isPresent = (c!= null && c.getCount() > 0);
|
|
|
|
assert c != null;
|
|
|
|
c.close();
|
|
|
|
return isPresent;
|
|
|
|
}
|
|
|
|
|
2018-11-30 18:39:01 +01:00
|
|
|
/**
|
|
|
|
* Returns tags starting by "search"
|
|
|
|
* @return tags List<String>
|
|
|
|
*/
|
|
|
|
public List<String> getBy(String search){
|
|
|
|
Cursor c = db.query(Sqlite.TABLE_CACHE_TAGS, null, Sqlite.COL_TAGS + " LIKE \"%" + search + "%\"", null, null, null, null, null);
|
|
|
|
return cursorToTag(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Method to hydrate tag from database
|
|
|
|
* @param c Cursor
|
|
|
|
* @return List<String>
|
|
|
|
*/
|
|
|
|
private List<String> cursorToTag(Cursor c){
|
|
|
|
//No element found
|
|
|
|
if (c.getCount() == 0)
|
|
|
|
return null;
|
|
|
|
List<String> tags = new ArrayList<>();
|
|
|
|
while (c.moveToNext() ) {
|
|
|
|
tags.add(c.getString(c.getColumnIndex(Sqlite.COL_TAGS)));
|
|
|
|
}
|
|
|
|
//Close the cursor
|
|
|
|
c.close();
|
|
|
|
//Tag list is returned
|
|
|
|
return tags;
|
|
|
|
}
|
|
|
|
}
|