2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite ;
2017-11-23 18:16:47 +01:00
/ * Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-11-23 18:16:47 +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
2017-11-23 18:16:47 +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 ,
2017-11-23 18:16:47 +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 ;
2018-12-15 12:21:03 +01:00
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.Entities.TagTimeline ;
import app.fedilab.android.helper.Helper ;
2017-11-23 18:16:47 +01:00
/ * *
* Created by Thomas on 22 / 11 / 2017 .
* Manage search in DB
* /
public class SearchDAO {
private SQLiteDatabase db ;
public Context context ;
private String userId ;
public SearchDAO ( 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 ) ;
userId = sharedpreferences . getString ( Helper . PREF_KEY_ID , null ) ;
}
//------- INSERTIONS -------
/ * *
* Insert a keyword in database
* @param keyword String
* /
2018-12-27 18:40:18 +01:00
public void insertSearch ( String keyword ) {
2017-11-23 18:16:47 +01:00
ContentValues values = new ContentValues ( ) ;
2018-12-28 11:38:26 +01:00
if ( keyword ! = null & & keyword . trim ( ) . length ( ) > 0 ) {
values . put ( Sqlite . COL_KEYWORDS , keyword . trim ( ) ) ;
values . put ( Sqlite . COL_USER_ID , userId ) ;
values . put ( Sqlite . COL_DATE_CREATION , Helper . dateToString ( new Date ( ) ) ) ;
//Inserts search
try {
db . insert ( Sqlite . TABLE_SEARCH , null , values ) ;
} catch ( Exception ignored ) {
}
}
2017-11-23 18:16:47 +01:00
}
2018-12-15 17:13:06 +01:00
//------- UPDATES -------
/ * *
* update tag timeline info in database
* @param tagTimeline TagTimeline
* /
2019-04-26 19:05:09 +02:00
public void updateSearch ( TagTimeline tagTimeline ) {
2018-12-15 17:13:06 +01:00
ContentValues values = new ContentValues ( ) ;
values . put ( Sqlite . COL_IS_ART , tagTimeline . isART ( ) ? 1 : 0 ) ;
values . put ( Sqlite . COL_IS_NSFW , tagTimeline . isNSFW ( ) ? 1 : 0 ) ;
2019-04-26 19:05:09 +02:00
List < String > any = tagTimeline . getAny ( ) ;
List < String > all = tagTimeline . getAll ( ) ;
List < String > none = tagTimeline . getNone ( ) ;
String displayname = tagTimeline . getDisplayname ( ) ;
values . put ( Sqlite . COL_NAME , displayname ) ;
2018-12-27 17:49:50 +01:00
if ( any ! = null & & any . size ( ) > 0 )
values . put ( Sqlite . COL_ANY , Helper . arrayToStringStorage ( any ) ) ;
if ( all ! = null & & all . size ( ) > 0 )
values . put ( Sqlite . COL_ALL , Helper . arrayToStringStorage ( all ) ) ;
if ( none ! = null & & none . size ( ) > 0 )
values . put ( Sqlite . COL_NONE , Helper . arrayToStringStorage ( none ) ) ;
2018-12-15 17:13:06 +01:00
try {
2019-04-26 19:05:09 +02:00
db . update ( Sqlite . TABLE_SEARCH , values , Sqlite . COL_ID + " = ? " , new String [ ] { String . valueOf ( tagTimeline . getId ( ) ) } ) ;
2018-12-15 17:13:06 +01:00
} catch ( Exception ignored ) { }
}
2017-11-23 18:16:47 +01:00
//------- REMOVE -------
/ * * *
* Remove search by keyword
* @return int
* /
public int remove ( String keyword ) {
return db . delete ( Sqlite . TABLE_SEARCH , Sqlite . COL_KEYWORDS + " = \" " + keyword + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null ) ;
}
//------- GETTERS -------
/ * *
* Returns all search in db for a user
* @return search List < String >
* /
public List < String > getAllSearch ( ) {
try {
Cursor c = db . query ( Sqlite . TABLE_SEARCH , null , Sqlite . COL_USER_ID + " = ' " + userId + " ' " , null , null , null , Sqlite . COL_KEYWORDS + " ASC " , null ) ;
return cursorToListSearch ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
/ * *
* Returns search by its keyword in db
* @return keywords List < String >
* /
public List < String > getSearchStartingBy ( String keyword ) {
try {
Cursor c = db . query ( Sqlite . TABLE_SEARCH , null , Sqlite . COL_KEYWORDS + " LIKE \" % " + keyword + " % \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null , null , null , null , null ) ;
return cursorToListSearch ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
/ * *
* Returns search by its keyword in db
* @return keywords List < String >
* /
public List < String > getSearchByKeyword ( String keyword ) {
try {
Cursor c = db . query ( Sqlite . TABLE_SEARCH , null , Sqlite . COL_KEYWORDS + " = \" " + keyword + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null , null , null , null , null ) ;
return cursorToListSearch ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
/ * * *
* Method to hydrate stored search from database
* @param c Cursor
* @return List < String >
* /
private List < String > cursorToListSearch ( Cursor c ) {
//No element found
if ( c . getCount ( ) = = 0 )
return null ;
List < String > searches = new ArrayList < > ( ) ;
while ( c . moveToNext ( ) ) {
2018-12-28 10:41:40 +01:00
if ( c . getString ( c . getColumnIndex ( Sqlite . COL_NAME ) ) ! = null )
searches . add ( c . getString ( c . getColumnIndex ( Sqlite . COL_NAME ) ) ) ;
else
searches . add ( c . getString ( c . getColumnIndex ( Sqlite . COL_KEYWORDS ) ) ) ;
2017-11-23 18:16:47 +01:00
}
//Close the cursor
c . close ( ) ;
//Search list is returned
return searches ;
}
2018-12-15 12:21:03 +01:00
/ * *
* Returns TagTimeline information by its keyword in db
* @return info List < TagTimeline >
* /
public List < TagTimeline > getTimelineInfo ( String keyword ) {
try {
Cursor c = db . query ( Sqlite . TABLE_SEARCH , null , Sqlite . COL_KEYWORDS + " = \" " + keyword + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null , null , null , null , null ) ;
return cursorToTagTimelineSearch ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
2019-04-21 12:09:30 +02:00
/ * *
* Returns TagTimeline information by its keyword in db
* @return info List < TagTimeline >
* /
public List < TagTimeline > getAll ( ) {
try {
Cursor c = db . query ( Sqlite . TABLE_SEARCH , null , Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null , null , null , Sqlite . COL_KEYWORDS + " ASC " , null ) ;
return cursorToTagTimelineSearch ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
2018-12-28 10:41:40 +01:00
/ * *
* Returns TagTimeline information by its keyword in db
* @return info List < TagTimeline >
* /
2018-12-28 11:38:26 +01:00
public List < TagTimeline > getTabInfo ( String name ) {
2018-12-28 10:41:40 +01:00
try {
2018-12-28 11:38:26 +01:00
Cursor c = db . query ( Sqlite . TABLE_SEARCH , null , Sqlite . COL_NAME + " = \" " + name + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null , null , null , null , null ) ;
2018-12-28 10:41:40 +01:00
return cursorToTagTimelineSearch ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
2019-01-21 18:49:02 +01:00
/ * *
* Returns TagTimeline information by its keyword in db
* @return info List < TagTimeline >
* /
public List < TagTimeline > getTabInfoKeyword ( String name ) {
try {
Cursor c = db . query ( Sqlite . TABLE_SEARCH , null , Sqlite . COL_KEYWORDS + " = \" " + name + " \" AND " + Sqlite . COL_USER_ID + " = \" " + userId + " \" " , null , null , null , null , null ) ;
return cursorToTagTimelineSearch ( c ) ;
} catch ( Exception e ) {
return null ;
}
}
2018-12-15 12:21:03 +01:00
/ * * *
* Method to hydrate stored search from database
* @param c Cursor
* @return List < String >
* /
private List < TagTimeline > cursorToTagTimelineSearch ( Cursor c ) {
//No element found
if ( c . getCount ( ) = = 0 )
return null ;
List < TagTimeline > searches = new ArrayList < > ( ) ;
while ( c . moveToNext ( ) ) {
TagTimeline tagTimeline = new TagTimeline ( ) ;
2018-12-27 17:49:50 +01:00
try {
tagTimeline . setAny ( Helper . restoreArrayFromString ( c . getString ( c . getColumnIndex ( Sqlite . COL_ANY ) ) ) ) ;
} catch ( Exception ignored ) { }
try {
tagTimeline . setAll ( Helper . restoreArrayFromString ( c . getString ( c . getColumnIndex ( Sqlite . COL_ALL ) ) ) ) ;
} catch ( Exception ignored ) { }
try {
tagTimeline . setNone ( Helper . restoreArrayFromString ( c . getString ( c . getColumnIndex ( Sqlite . COL_NONE ) ) ) ) ;
} catch ( Exception ignored ) { }
2019-04-22 19:10:07 +02:00
tagTimeline . setId ( c . getInt ( c . getColumnIndex ( Sqlite . COL_ID ) ) ) ;
2018-12-15 12:21:03 +01:00
tagTimeline . setName ( c . getString ( c . getColumnIndex ( Sqlite . COL_KEYWORDS ) ) ) ;
2018-12-28 10:41:40 +01:00
tagTimeline . setDisplayname ( c . getString ( c . getColumnIndex ( Sqlite . COL_NAME ) ) ) ;
2018-12-15 12:21:03 +01:00
tagTimeline . setART ( c . getInt ( c . getColumnIndex ( Sqlite . COL_IS_ART ) ) = = 1 ) ;
tagTimeline . setNSFW ( c . getInt ( c . getColumnIndex ( Sqlite . COL_IS_NSFW ) ) = = 1 ) ;
searches . add ( tagTimeline ) ;
}
//Close the cursor
c . close ( ) ;
//Search list is returned
return searches ;
}
2017-11-23 18:16:47 +01:00
}