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 {
private static final int VERSION = 1;
private List<DatabaseEntry> _entries = new ArrayList<>();
private long _counter = 0;
public byte[] serialize() throws Exception {
JSONArray array = new JSONArray();
@ -20,6 +22,7 @@ public class Database {
JSONObject obj = new JSONObject();
obj.put("version", VERSION);
obj.put("entries", array);
obj.put("counter", _counter);
return obj.toString().getBytes("UTF-8");
}
@ -33,6 +36,8 @@ public class Database {
throw new Exception("Unsupported version");
}
_counter = obj.getLong("counter");
JSONArray array = obj.getJSONArray("entries");
for (int i = 0; i < array.length(); i++) {
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);
}

View file

@ -8,9 +8,10 @@ import java.io.Serializable;
import me.impy.aegis.crypto.KeyInfo;
public class DatabaseEntry implements Serializable {
public String _name = "";
public String _icon = "";
public KeyInfo _info;
private long _id = -1;
private String _name = "";
private String _icon = "";
private KeyInfo _info;
public DatabaseEntry(KeyInfo info) {
_info = info;
@ -18,16 +19,21 @@ public class DatabaseEntry implements Serializable {
public JSONObject serialize() throws JSONException {
JSONObject obj = new JSONObject();
obj.put("id", _id);
obj.put("name", _name);
obj.put("url", _info.getURL());
return obj;
}
public void deserialize(JSONObject obj) throws Exception {
_id = obj.getLong("id");
_name = obj.getString("name");
_info = KeyInfo.fromURL(obj.getString("url"));
}
public long getID() {
return _id;
}
public String getName() {
return _name;
}
@ -38,6 +44,12 @@ public class DatabaseEntry implements Serializable {
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) {
_name = name;
}