2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * Mobile-Ledger is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mobile-Ledger. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.model;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
23 import net.ktnx.mobileledger.utils.MLDB;
25 import java.util.ArrayList;
26 import java.util.List;
28 public final class MobileLedgerProfile {
32 private boolean useAuthentication;
33 private String authUserName;
34 private String authPassword;
35 public MobileLedgerProfile(String uuid, String name, String url, boolean useAuthentication,
36 String authUserName, String authPassword) {
40 this.useAuthentication = useAuthentication;
41 this.authUserName = authUserName;
42 this.authPassword = authPassword;
44 public static List<MobileLedgerProfile> loadAllFromDB() {
45 List<MobileLedgerProfile> result = new ArrayList<>();
46 SQLiteDatabase db = MLDB.getReadableDatabase();
47 try (Cursor cursor = db.rawQuery("SELECT uuid, name, url, use_authentication, auth_user, " +
48 "auth_password FROM profiles", null))
50 while (cursor.moveToNext()) {
51 result.add(new MobileLedgerProfile(cursor.getString(0), cursor.getString(1),
52 cursor.getString(2), cursor.getInt(3) == 1, cursor.getString(4),
53 cursor.getString(5)));
58 public static MobileLedgerProfile loadUUIDFromDB(String profileUUID) {
59 SQLiteDatabase db = MLDB.getReadableDatabase();
64 Boolean useAuthentication;
65 try (Cursor cursor = db.rawQuery("SELECT name, url, use_authentication, auth_user, " +
66 "auth_password FROM profiles WHERE uuid=?",
67 new String[]{profileUUID}))
69 if (cursor.moveToNext()) {
70 name = cursor.getString(0);
71 url = cursor.getString(1);
72 useAuthentication = cursor.getInt(2) == 1;
73 authUser = useAuthentication ? cursor.getString(3) : null;
74 authPassword = useAuthentication ? cursor.getString(4) : null;
77 name = "Unknown profile";
78 url = "Https://server/url";
79 useAuthentication = false;
80 authUser = authPassword = null;
84 return new MobileLedgerProfile(profileUUID, name, url, useAuthentication, authUser,
87 public String getUuid() {
90 public String getName() {
93 public String getUrl() {
96 public boolean isUseAuthentication() {
97 return useAuthentication;
99 public String getAuthUserName() {
102 public String getAuthPassword() {
105 public void storeInDB() {
106 SQLiteDatabase db = MLDB.getWritableDatabase();
107 db.beginTransaction();
109 db.execSQL("REPLACE INTO profiles(uuid, name, url, use_authentication, auth_user, " +
110 "auth_password) VALUES(?, ?, ?, ?, ?, ?)",
111 new Object[]{uuid, name, url, useAuthentication,
112 useAuthentication ? authUserName : null,
113 useAuthentication ? authPassword : null
115 db.setTransactionSuccessful();