Converted old databases to using id's for entries

This commit is contained in:
Alexander Bakker 2017-12-27 21:08:24 +01:00
parent 043d70f190
commit 05cfc0bc5f
2 changed files with 20 additions and 6 deletions

View file

@ -36,13 +36,25 @@ public class Database {
throw new Exception("Unsupported version");
}
_counter = obj.getLong("counter");
// if no counter is present, ignore and reset the id of all entries
boolean ignoreID = false;
if (!obj.has("counter")) {
ignoreID = true;
} else {
_counter = obj.getLong("counter");
}
JSONArray array = obj.getJSONArray("entries");
for (int i = 0; i < array.length(); i++) {
DatabaseEntry e = new DatabaseEntry(null);
e.deserialize(array.getJSONObject(i));
_entries.add(e);
DatabaseEntry entry = new DatabaseEntry(null);
entry.deserialize(array.getJSONObject(i), ignoreID);
// if the id was ignored, make sure it receives a new one
if (ignoreID) {
addKey(entry);
} else {
_entries.add(entry);
}
}
}

View file

@ -25,8 +25,10 @@ public class DatabaseEntry implements Serializable {
return obj;
}
public void deserialize(JSONObject obj) throws Exception {
_id = obj.getLong("id");
public void deserialize(JSONObject obj, boolean ignoreID) throws Exception {
if (!ignoreID) {
_id = obj.getLong("id");
}
_name = obj.getString("name");
_info = KeyInfo.fromURL(obj.getString("url"));
}