]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
model: class for working with connection profiles
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / MobileLedgerProfile.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22
23 import net.ktnx.mobileledger.utils.MLDB;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 public final class MobileLedgerProfile {
29     private String uuid;
30     private String name;
31     private String url;
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) {
37         this.uuid = uuid;
38         this.name = name;
39         this.url = url;
40         this.useAuthentication = useAuthentication;
41         this.authUserName = authUserName;
42         this.authPassword = authPassword;
43     }
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))
49         {
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)));
54             }
55         }
56         return result;
57     }
58     public static MobileLedgerProfile loadUUIDFromDB(String profileUUID) {
59         SQLiteDatabase db = MLDB.getReadableDatabase();
60         String name;
61         String url;
62         String authUser;
63         String authPassword;
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}))
68         {
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;
75             }
76             else {
77                 name = "Unknown profile";
78                 url = "Https://server/url";
79                 useAuthentication = false;
80                 authUser = authPassword = null;
81             }
82         }
83
84         return new MobileLedgerProfile(profileUUID, name, url, useAuthentication, authUser,
85                 authPassword);
86     }
87     public String getUuid() {
88         return uuid;
89     }
90     public String getName() {
91         return name;
92     }
93     public String getUrl() {
94         return url;
95     }
96     public boolean isUseAuthentication() {
97         return useAuthentication;
98     }
99     public String getAuthUserName() {
100         return authUserName;
101     }
102     public String getAuthPassword() {
103         return authPassword;
104     }
105     public void storeInDB() {
106         SQLiteDatabase db = MLDB.getWritableDatabase();
107         db.beginTransaction();
108         try {
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
114                     });
115             db.setTransactionSuccessful();
116         }
117         finally {
118             db.endTransaction();
119         }
120     }
121 }