Reintroduce id's for database entries

This commit is contained in:
Alexander Bakker 2017-12-27 21:01:53 +01:00
parent 4d425f5c68
commit 043d70f190
2 changed files with 22 additions and 4 deletions

View file

@ -9,7 +9,9 @@ import java.util.List;
public class Database { public class Database {
private static final int VERSION = 1; private static final int VERSION = 1;
private List<DatabaseEntry> _entries = new ArrayList<>(); private List<DatabaseEntry> _entries = new ArrayList<>();
private long _counter = 0;
public byte[] serialize() throws Exception { public byte[] serialize() throws Exception {
JSONArray array = new JSONArray(); JSONArray array = new JSONArray();
@ -20,6 +22,7 @@ public class Database {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("version", VERSION); obj.put("version", VERSION);
obj.put("entries", array); obj.put("entries", array);
obj.put("counter", _counter);
return obj.toString().getBytes("UTF-8"); return obj.toString().getBytes("UTF-8");
} }
@ -33,6 +36,8 @@ public class Database {
throw new Exception("Unsupported version"); throw new Exception("Unsupported version");
} }
_counter = obj.getLong("counter");
JSONArray array = obj.getJSONArray("entries"); JSONArray array = obj.getJSONArray("entries");
for (int i = 0; i < array.length(); i++) { for (int i = 0; i < array.length(); i++) {
DatabaseEntry e = new DatabaseEntry(null); DatabaseEntry e = new DatabaseEntry(null);
@ -41,7 +46,8 @@ public class Database {
} }
} }
public void addKey(DatabaseEntry entry) { public void addKey(DatabaseEntry entry) throws Exception {
entry.setID(++_counter);
_entries.add(entry); _entries.add(entry);
} }

View file

@ -8,9 +8,10 @@ import java.io.Serializable;
import me.impy.aegis.crypto.KeyInfo; import me.impy.aegis.crypto.KeyInfo;
public class DatabaseEntry implements Serializable { public class DatabaseEntry implements Serializable {
public String _name = ""; private long _id = -1;
public String _icon = ""; private String _name = "";
public KeyInfo _info; private String _icon = "";
private KeyInfo _info;
public DatabaseEntry(KeyInfo info) { public DatabaseEntry(KeyInfo info) {
_info = info; _info = info;
@ -18,16 +19,21 @@ public class DatabaseEntry implements Serializable {
public JSONObject serialize() throws JSONException { public JSONObject serialize() throws JSONException {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
obj.put("id", _id);
obj.put("name", _name); obj.put("name", _name);
obj.put("url", _info.getURL()); obj.put("url", _info.getURL());
return obj; return obj;
} }
public void deserialize(JSONObject obj) throws Exception { public void deserialize(JSONObject obj) throws Exception {
_id = obj.getLong("id");
_name = obj.getString("name"); _name = obj.getString("name");
_info = KeyInfo.fromURL(obj.getString("url")); _info = KeyInfo.fromURL(obj.getString("url"));
} }
public long getID() {
return _id;
}
public String getName() { public String getName() {
return _name; return _name;
} }
@ -38,6 +44,12 @@ public class DatabaseEntry implements Serializable {
return _info; return _info;
} }
void setID(long id) throws Exception {
if (_id != -1) {
throw new Exception("this entry has already received an id");
}
_id = id;
}
public void setName(String name) { public void setName(String name) {
_name = name; _name = name;
} }