fedilab-Android-App/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/AccountDAO.java

297 lines
12 KiB
Java
Raw Normal View History

2017-05-05 16:36:04 +02:00
package fr.gouv.etalab.mastodon.sqlite;
/* Copyright 2017 Thomas Schneider
*
2017-07-10 10:33:24 +02:00
* This file is a part of Mastalab
2017-05-05 16:36:04 +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.
*
2017-07-10 10:33:24 +02:00
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-05-05 16:36:04 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2017-08-04 11:11:27 +02:00
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
2017-05-05 16:36:04 +02:00
* 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;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.helper.Helper;
/**
* Created by Thomas on 24/04/2017.
* Manage Account in DB
*/
public class AccountDAO {
private SQLiteDatabase db;
public Context context;
public AccountDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
}
/**
* Insert an Account in database
* @param account Account
* @return boolean
*/
public boolean insertAccount(Account account)
{
ContentValues values = new ContentValues();
values.put(Sqlite.COL_USER_ID, account.getId());
values.put(Sqlite.COL_USERNAME, account.getUsername());
values.put(Sqlite.COL_ACCT, account.getAcct());
values.put(Sqlite.COL_DISPLAYED_NAME, account.getDisplay_name());
values.put(Sqlite.COL_LOCKED,account.isLocked());
values.put(Sqlite.COL_FOLLOWERS_COUNT,account.getFollowers_count());
values.put(Sqlite.COL_FOLLOWING_COUNT,account.getFollowing_count());
values.put(Sqlite.COL_STATUSES_COUNT,account.getStatuses_count());
values.put(Sqlite.COL_NOTE,account.getNote());
values.put(Sqlite.COL_URL,account.getUrl());
values.put(Sqlite.COL_AVATAR,account.getAvatar());
values.put(Sqlite.COL_AVATAR_STATIC,account.getAvatar_static());
values.put(Sqlite.COL_HEADER,account.getHeader());
values.put(Sqlite.COL_HEADER_STATIC,account.getHeader_static());
2018-04-28 16:54:06 +02:00
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(account.getCreated_at()));
2017-05-05 16:36:04 +02:00
values.put(Sqlite.COL_INSTANCE, account.getInstance());
2018-09-16 15:05:33 +02:00
values.put(Sqlite.COL_EMOJIS, Helper.emojisToStringStorage(account.getEmojis()));
2019-01-02 19:39:40 +01:00
values.put(Sqlite.COL_SOCIAL, account.getSocial());
2017-05-05 16:36:04 +02:00
if( account.getToken() != null)
values.put(Sqlite.COL_OAUTHTOKEN, account.getToken());
//Inserts account
try{
db.insert(Sqlite.TABLE_USER_ACCOUNT, null, values);
}catch (Exception e) {
return false;
}
return true;
}
/**
* Update an Account in database
* @param account Account
* @return boolean
*/
public int updateAccount(Account account)
{
ContentValues values = new ContentValues();
values.put(Sqlite.COL_ACCT, account.getAcct());
values.put(Sqlite.COL_DISPLAYED_NAME, account.getDisplay_name());
values.put(Sqlite.COL_LOCKED,account.isLocked());
values.put(Sqlite.COL_FOLLOWERS_COUNT,account.getFollowers_count());
values.put(Sqlite.COL_FOLLOWING_COUNT,account.getFollowing_count());
values.put(Sqlite.COL_STATUSES_COUNT,account.getStatuses_count());
values.put(Sqlite.COL_NOTE,account.getNote());
values.put(Sqlite.COL_URL,account.getUrl());
values.put(Sqlite.COL_AVATAR,account.getAvatar());
values.put(Sqlite.COL_AVATAR_STATIC,account.getAvatar_static());
values.put(Sqlite.COL_HEADER,account.getHeader());
values.put(Sqlite.COL_HEADER_STATIC,account.getHeader_static());
2018-04-28 16:54:06 +02:00
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(account.getCreated_at()));
2017-05-05 16:36:04 +02:00
values.put(Sqlite.COL_INSTANCE, account.getInstance());
2018-09-16 15:05:33 +02:00
values.put(Sqlite.COL_EMOJIS, Helper.emojisToStringStorage(account.getEmojis()));
2017-05-05 16:36:04 +02:00
if( account.getToken() != null)
values.put(Sqlite.COL_OAUTHTOKEN, account.getToken());
return db.update(Sqlite.TABLE_USER_ACCOUNT,
values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_USERNAME + " =?",
new String[]{account.getId(), account.getUsername()});
}
public int removeUser(Account account){
return db.delete(Sqlite.TABLE_USER_ACCOUNT, Sqlite.COL_USER_ID + " = '" +account.getId() +
"' AND " + Sqlite.COL_USERNAME + " = '" + account.getUsername()+ "'", null);
}
/**
* Returns an Account by id
* @param accountId String
* @return Account
*/
public Account getAccountByID(String accountId){
try {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USER_ID + " = '" + accountId + "'", null, null, null, null, "1");
return cursorToUser(c);
} catch (Exception e) {
return null;
}
}
2017-09-09 11:38:26 +02:00
/**
* Returns an Account by its id and acct
* @param accountId String
* @param accountAcct String
* @return Account
*/
public Account getAccountByIDAcct(String accountId, String accountAcct){
try {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USER_ID + " = '" + accountId + "' AND " + Sqlite.COL_ACCT + " = '" + accountAcct + "'", null, null, null, null, "1");
return cursorToUser(c);
} catch (Exception e) {
return null;
}
}
2017-07-17 15:22:59 +02:00
/**
* Returns an Account by id and instance
* @param accountId String
* @param instance String
* @return Account
*/
public Account getAccountByUserIDInstance(String accountId, String instance){
try {
2017-07-17 18:53:12 +02:00
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USER_ID + " = '" + accountId + "' AND " + Sqlite.COL_INSTANCE + "= '"+ instance +"'", null, null, null, null, "1");
2017-07-17 15:22:59 +02:00
return cursorToUser(c);
} catch (Exception e) {
return null;
}
}
2017-05-05 16:36:04 +02:00
/**
* Returns all Account in db
* @return Account List<Account>
*/
public List<Account> getAllAccount(){
try {
2017-08-22 09:02:40 +02:00
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, null, null, null, null, Sqlite.COL_INSTANCE + " ASC", null);
2017-05-05 16:36:04 +02:00
return cursorToListUser(c);
} catch (Exception e) {
return null;
}
}
/**
* Returns an Account by token
* @param token String
* @return Account
*/
public Account getAccountByToken(String token){
try {
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " = \"" + token + "\"", null, null, null, null, "1");
return cursorToUser(c);
} catch (Exception e) {
return null;
}
}
/**
* Test if the current user is already stored in data base
* @param account Account
* @return boolean
*/
public boolean userExist(Account account)
{
Cursor mCount= db.rawQuery("select count(*) from " + Sqlite.TABLE_USER_ACCOUNT
+ " where " + Sqlite.COL_ACCT + " = '" + account.getAcct() + "' AND " + Sqlite.COL_INSTANCE + " = '" + account.getInstance()+ "'", null);
2017-05-05 16:36:04 +02:00
mCount.moveToFirst();
int count = mCount.getInt(0);
mCount.close();
return (count > 0);
}
/***
* Method to hydrate an Account from database
* @param c Cursor
* @return Account
*/
private Account cursorToUser(Cursor c){
//No element found
if (c.getCount() == 0)
return null;
//Take the first element
c.moveToFirst();
//New user
Account account = new Account();
account.setId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
account.setUsername(c.getString(c.getColumnIndex(Sqlite.COL_USERNAME)));
account.setAcct(c.getString(c.getColumnIndex(Sqlite.COL_ACCT)));
account.setDisplay_name(c.getString(c.getColumnIndex(Sqlite.COL_DISPLAYED_NAME)));
account.setLocked(c.getInt(c.getColumnIndex(Sqlite.COL_LOCKED)) == 1);
account.setFollowers_count(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWERS_COUNT)));
account.setFollowing_count(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWING_COUNT)));
account.setStatuses_count(c.getInt(c.getColumnIndex(Sqlite.COL_STATUSES_COUNT)));
account.setNote(c.getString(c.getColumnIndex(Sqlite.COL_NOTE)));
account.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL)));
account.setAvatar(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR)));
account.setAvatar_static(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR_STATIC)));
account.setHeader(c.getString(c.getColumnIndex(Sqlite.COL_HEADER)));
account.setHeader_static(c.getString(c.getColumnIndex(Sqlite.COL_HEADER_STATIC)));
account.setCreated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
account.setInstance(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
2018-09-16 15:05:33 +02:00
account.setEmojis(Helper.restoreEmojisFromString(c.getString(c.getColumnIndex(Sqlite.COL_EMOJIS))));
2017-05-05 16:36:04 +02:00
account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN)));
2019-01-02 19:39:40 +01:00
account.setSocial(c.getString(c.getColumnIndex(Sqlite.COL_SOCIAL))!=null?c.getString(c.getColumnIndex(Sqlite.COL_SOCIAL)):"MASTODON");
2017-05-05 16:36:04 +02:00
//Close the cursor
c.close();
//User is returned
return account;
}
/***
* Method to hydrate an Accounts from database
* @param c Cursor
* @return List<Account>
*/
private List<Account> cursorToListUser(Cursor c){
//No element found
if (c.getCount() == 0)
return null;
List<Account> accounts = new ArrayList<>();
while (c.moveToNext() ) {
//New user
Account account = new Account();
account.setId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
account.setUsername(c.getString(c.getColumnIndex(Sqlite.COL_USERNAME)));
account.setAcct(c.getString(c.getColumnIndex(Sqlite.COL_ACCT)));
account.setDisplay_name(c.getString(c.getColumnIndex(Sqlite.COL_DISPLAYED_NAME)));
account.setLocked(c.getInt(c.getColumnIndex(Sqlite.COL_LOCKED)) == 1);
account.setFollowers_count(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWERS_COUNT)));
account.setFollowing_count(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWING_COUNT)));
account.setStatuses_count(c.getInt(c.getColumnIndex(Sqlite.COL_STATUSES_COUNT)));
account.setNote(c.getString(c.getColumnIndex(Sqlite.COL_NOTE)));
account.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL)));
account.setAvatar(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR)));
account.setAvatar_static(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR_STATIC)));
account.setHeader(c.getString(c.getColumnIndex(Sqlite.COL_HEADER)));
account.setHeader_static(c.getString(c.getColumnIndex(Sqlite.COL_HEADER_STATIC)));
account.setCreated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
account.setInstance(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN)));
2019-01-02 19:39:40 +01:00
account.setSocial(c.getString(c.getColumnIndex(Sqlite.COL_SOCIAL))!=null?c.getString(c.getColumnIndex(Sqlite.COL_SOCIAL)):"MASTODON");
2017-05-05 16:36:04 +02:00
accounts.add(account);
}
//Close the cursor
c.close();
//Users list is returned
return accounts;
}
}