AndroidAPS/app/src/main/java/info/nightscout/androidaps/db/DbRequest.java

106 lines
3.2 KiB
Java
Raw Normal View History

2017-02-24 13:02:44 +01:00
package info.nightscout.androidaps.db;
2017-02-17 13:18:36 +01:00
import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;
2017-02-24 13:02:44 +01:00
import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable;
2017-02-17 13:18:36 +01:00
2017-02-17 21:24:30 +01:00
import org.json.JSONException;
2017-02-17 13:18:36 +01:00
import org.json.JSONObject;
2017-02-24 13:02:44 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
2017-02-17 13:18:36 +01:00
2018-07-30 15:46:20 +02:00
import info.nightscout.androidaps.logging.L;
2017-02-17 13:18:36 +01:00
/**
* Created by mike on 27.02.2016.
2017-02-17 21:24:30 +01:00
* <p>
2017-02-17 13:18:36 +01:00
* Allowed actions "dbAdd" || "dbUpdate" || "dbUpdateUnset" || "dbRemove"
*/
2017-02-24 13:02:44 +01:00
@DatabaseTable(tableName = DatabaseHelper.DATABASE_DBREQUESTS)
2017-02-17 13:18:36 +01:00
public class DbRequest {
2018-07-30 15:46:20 +02:00
private static Logger log = LoggerFactory.getLogger(L.DATABASE);
2017-02-24 13:02:44 +01:00
2017-05-21 22:05:03 +02:00
@DatabaseField(id = true)
2017-02-24 13:02:44 +01:00
public String nsClientID = null;
@DatabaseField
2017-02-17 13:18:36 +01:00
public String action = null;
2017-02-24 13:02:44 +01:00
@DatabaseField
2017-02-17 13:18:36 +01:00
public String collection = null;
2017-02-24 13:02:44 +01:00
@DatabaseField
public String data = null;
@DatabaseField
2017-02-17 13:18:36 +01:00
public String _id = null;
2017-02-17 21:24:30 +01:00
public DbRequest() {
}
2017-02-17 13:18:36 +01:00
// dbAdd
2017-02-17 21:24:30 +01:00
public DbRequest(String action, String collection, String nsClientID, JSONObject data) {
2017-02-17 13:18:36 +01:00
this.action = action;
this.collection = collection;
2017-02-24 13:02:44 +01:00
this.data = data.toString();
2017-02-17 21:24:30 +01:00
this.nsClientID = nsClientID;
2017-02-17 13:18:36 +01:00
this._id = "";
}
// dbUpdate, dbUpdateUnset
2017-02-17 21:24:30 +01:00
public DbRequest(String action, String collection, String nsClientID, String _id, JSONObject data) {
2017-02-17 13:18:36 +01:00
this.action = action;
this.collection = collection;
2017-02-24 13:02:44 +01:00
this.data = data.toString();
2017-02-17 21:24:30 +01:00
this.nsClientID = nsClientID;
2017-02-17 13:18:36 +01:00
this._id = _id;
}
// dbRemove
2017-02-17 21:24:30 +01:00
public DbRequest(String action, String collection, String nsClientID, String _id) {
2017-02-17 13:18:36 +01:00
this.action = action;
this.collection = collection;
2017-02-24 13:02:44 +01:00
this.data = new JSONObject().toString();
2017-02-17 21:24:30 +01:00
this.nsClientID = nsClientID;
2017-02-17 13:18:36 +01:00
this._id = _id;
}
public String hash() {
return Hashing.sha1().hashString(action + collection + _id + data.toString(), Charsets.UTF_8).toString();
}
2017-02-17 21:24:30 +01:00
public JSONObject toJSON() {
JSONObject object = new JSONObject();
try {
object.put("action", action);
object.put("collection", collection);
2017-02-24 13:02:44 +01:00
object.put("data", new JSONObject(data));
2017-02-17 21:24:30 +01:00
if (_id != null) object.put("_id", _id);
if (nsClientID != null) object.put("nsClientID", nsClientID);
} catch (JSONException e) {
log.error("Unhandled exception", e);
2017-02-17 21:24:30 +01:00
}
return object;
}
public static DbRequest fromJSON(JSONObject jsonObject) {
DbRequest result = new DbRequest();
try {
if (jsonObject.has("action"))
result.action = jsonObject.getString("action");
if (jsonObject.has("collection"))
result.collection = jsonObject.getString("collection");
if (jsonObject.has("data"))
2017-02-24 13:02:44 +01:00
result.data = jsonObject.getJSONObject("data").toString();
2017-02-17 21:24:30 +01:00
if (jsonObject.has("_id"))
result._id = jsonObject.getString("_id");
if (jsonObject.has("nsClientID"))
result.nsClientID = jsonObject.getString("nsClientID");
} catch (JSONException e) {
log.error("Unhandled exception", e);
2017-02-17 21:24:30 +01:00
}
return result;
}
2017-02-17 13:18:36 +01:00
}