aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-05-03 12:09:00 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-05-03 12:09:00 +1200
commit18127a2dfc362ffd7de4110e013a10a9f894469b (patch)
tree24ced00567acbc751985b71ffd99ce067213a34d
parent427113e0aabf1b8ab54288263c3e809f292a5a05 (diff)
downloadtrackermap-server-18127a2dfc362ffd7de4110e013a10a9f894469b.tar.gz
trackermap-server-18127a2dfc362ffd7de4110e013a10a9f894469b.tar.bz2
trackermap-server-18127a2dfc362ffd7de4110e013a10a9f894469b.zip
Implmenet reflection json conversion
-rw-r--r--src/org/traccar/database/Convertable.java26
-rw-r--r--src/org/traccar/database/ObjectConverter.java86
-rw-r--r--src/org/traccar/database/QueryBuilder.java13
-rw-r--r--src/org/traccar/http/AsyncServlet.java3
-rw-r--r--src/org/traccar/http/DeviceServlet.java21
-rw-r--r--src/org/traccar/http/JsonConverter.java120
-rw-r--r--src/org/traccar/http/MainServlet.java1
-rw-r--r--src/org/traccar/model/Data.java30
-rw-r--r--src/org/traccar/model/Device.java35
-rw-r--r--src/org/traccar/model/Position.java46
-rw-r--r--src/org/traccar/model/User.java47
11 files changed, 141 insertions, 287 deletions
diff --git a/src/org/traccar/database/Convertable.java b/src/org/traccar/database/Convertable.java
deleted file mode 100644
index cc1a88ceb..000000000
--- a/src/org/traccar/database/Convertable.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.traccar.database;
-
-import javax.json.JsonObject;
-
-public interface Convertable {
-
- public JsonObject toJson();
-
- public void fromJson(JsonObject json) throws Exception;
-
-}
diff --git a/src/org/traccar/database/ObjectConverter.java b/src/org/traccar/database/ObjectConverter.java
deleted file mode 100644
index 5a7f7083a..000000000
--- a/src/org/traccar/database/ObjectConverter.java
+++ /dev/null
@@ -1,86 +0,0 @@
-/*
- * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.traccar.database;
-
-import java.text.DateFormat;
-import java.text.ParseException;
-import java.text.SimpleDateFormat;
-import java.util.Collection;
-import java.util.Date;
-import javax.json.Json;
-import javax.json.JsonArray;
-import javax.json.JsonArrayBuilder;
-import javax.json.JsonObject;
-import javax.json.JsonObjectBuilder;
-import org.traccar.helper.Log;
-
-public class ObjectConverter {
-
- public static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
-
- public static JsonArray arrayToJson(Collection<? extends Convertable> collection) {
-
- JsonArrayBuilder array = Json.createArrayBuilder();
-
- for (Convertable object : collection) {
- array.add(object.toJson());
- }
-
- return array.build();
- }
-
- public static String getString(JsonObject json, String key) {
- if (json.containsKey(key)) {
- return json.getString(key);
- }
- return null;
- }
-
- public static void putString(JsonObjectBuilder json, String key, String value) {
- if (value != null) {
- json.add(key, value);
- }
- }
-
- public static long getLong(JsonObject json, String key) {
- if (json.containsKey(key)) {
- return json.getJsonNumber(key).longValue();
- }
- return 0;
- }
-
- public static void putLong(JsonObjectBuilder json, String key, long value) {
- json.add(key, value);
- }
-
- public static Date getDate(JsonObject json, String key) {
- if (json.containsKey(key)) {
- try {
- return dateFormat.parse(json.getString(key));
- } catch (ParseException error) {
- Log.warning(error);
- }
- }
- return null;
- }
-
- public static void putDate(JsonObjectBuilder json, String key, Date value) {
- if (value != null) {
- json.add(key, dateFormat.format(value));
- }
- }
-
-}
diff --git a/src/org/traccar/database/QueryBuilder.java b/src/org/traccar/database/QueryBuilder.java
index 29a737f20..d05f48c09 100644
--- a/src/org/traccar/database/QueryBuilder.java
+++ b/src/org/traccar/database/QueryBuilder.java
@@ -15,7 +15,6 @@
*/
package org.traccar.database;
-import java.beans.Introspector;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
@@ -176,17 +175,17 @@ public class QueryBuilder {
String name = method.getName().substring(3);
try {
if (method.getReturnType().equals(boolean.class)) {
- return setBoolean(name, (Boolean) method.invoke(object));
+ setBoolean(name, (Boolean) method.invoke(object));
} else if (method.getReturnType().equals(int.class)) {
- return setInteger(name, (Integer) method.invoke(object));
+ setInteger(name, (Integer) method.invoke(object));
} else if (method.getReturnType().equals(long.class)) {
- return setLong(name, (Long) method.invoke(object));
+ setLong(name, (Long) method.invoke(object));
} else if (method.getReturnType().equals(double.class)) {
- return setDouble(name, (Double) method.invoke(object));
+ setDouble(name, (Double) method.invoke(object));
} else if (method.getReturnType().equals(String.class)) {
- return setString(name, (String) method.invoke(object));
+ setString(name, (String) method.invoke(object));
} else if (method.getReturnType().equals(Date.class)) {
- return setDate(name, (Date) method.invoke(object));
+ setDate(name, (Date) method.invoke(object));
}
} catch (IllegalAccessException error) {
} catch (InvocationTargetException error) {
diff --git a/src/org/traccar/http/AsyncServlet.java b/src/org/traccar/http/AsyncServlet.java
index c66126667..927e93c78 100644
--- a/src/org/traccar/http/AsyncServlet.java
+++ b/src/org/traccar/http/AsyncServlet.java
@@ -20,7 +20,6 @@ import org.jboss.netty.util.TimerTask;
import org.traccar.Context;
import org.traccar.GlobalTimer;
import org.traccar.database.DataCache;
-import org.traccar.database.ObjectConverter;
import org.traccar.helper.Log;
import org.traccar.model.Position;
@@ -160,7 +159,7 @@ public class AsyncServlet extends HttpServlet {
JsonObjectBuilder result = Json.createObjectBuilder();
result.add("success", true);
- result.add("data", ObjectConverter.arrayToJson(positions.values()));
+ result.add("data", JsonConverter.arrayToJson(positions.values()));
positions.clear();
try {
diff --git a/src/org/traccar/http/DeviceServlet.java b/src/org/traccar/http/DeviceServlet.java
index 98f00b320..81ec168d5 100644
--- a/src/org/traccar/http/DeviceServlet.java
+++ b/src/org/traccar/http/DeviceServlet.java
@@ -19,16 +19,12 @@ import java.io.IOException;
import java.sql.SQLException;
import java.text.ParseException;
import javax.json.Json;
-import javax.json.JsonObject;
import javax.json.JsonObjectBuilder;
-import javax.json.JsonReader;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.traccar.Context;
-import org.traccar.database.ObjectConverter;
-import org.traccar.helper.Log;
import org.traccar.model.Device;
public class DeviceServlet extends HttpServlet {
@@ -61,7 +57,7 @@ public class DeviceServlet extends HttpServlet {
try {
result.add("success", true);
- result.add("data", ObjectConverter.arrayToJson(
+ result.add("data", JsonConverter.arrayToJson(
Context.getDataManager().getDevices(userId)));
} catch(SQLException error) {
result.add("success", false);
@@ -75,13 +71,10 @@ public class DeviceServlet extends HttpServlet {
long userId = (Long) req.getSession().getAttribute(MainServlet.USER_ID);
- JsonReader jsonReader = Json.createReader(req.getReader());
- JsonObject jsonObject = jsonReader.readObject();
Device device = new Device();
try {
- device.fromJson(jsonObject);
+ JsonConverter.objectFromJson(req.getReader(), device);
} catch (ParseException error) {
- Log.warning(error);
}
JsonObjectBuilder result = Json.createObjectBuilder();
@@ -100,13 +93,10 @@ public class DeviceServlet extends HttpServlet {
private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
- JsonReader jsonReader = Json.createReader(req.getReader());
- JsonObject jsonObject = jsonReader.readObject();
Device device = new Device();
try {
- device.fromJson(jsonObject);
+ JsonConverter.objectFromJson(req.getReader(), device);
} catch (ParseException error) {
- Log.warning(error);
}
JsonObjectBuilder result = Json.createObjectBuilder();
@@ -124,13 +114,10 @@ public class DeviceServlet extends HttpServlet {
private void remove(HttpServletRequest req, HttpServletResponse resp) throws IOException {
- JsonReader jsonReader = Json.createReader(req.getReader());
- JsonObject jsonObject = jsonReader.readObject();
Device device = new Device();
try {
- device.fromJson(jsonObject);
+ JsonConverter.objectFromJson(req.getReader(), device);
} catch (ParseException error) {
- Log.warning(error);
}
JsonObjectBuilder result = Json.createObjectBuilder();
diff --git a/src/org/traccar/http/JsonConverter.java b/src/org/traccar/http/JsonConverter.java
new file mode 100644
index 000000000..1b02e33d2
--- /dev/null
+++ b/src/org/traccar/http/JsonConverter.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.http;
+
+import java.beans.Introspector;
+import java.io.Reader;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.text.DateFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Collection;
+import java.util.Date;
+import javax.json.Json;
+import javax.json.JsonArray;
+import javax.json.JsonArrayBuilder;
+import javax.json.JsonObject;
+import javax.json.JsonObjectBuilder;
+import org.traccar.model.Factory;
+
+public class JsonConverter {
+
+ public static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ");
+
+ public static <T extends Factory> T objectFromJson(Reader reader, T prototype) throws ParseException {
+ return objectFromJson(Json.createReader(reader).readObject(), prototype);
+ }
+
+ public static <T extends Factory> T objectFromJson(JsonObject json, T prototype) throws ParseException {
+
+ T object = (T) prototype.create();
+
+ Method[] methods = prototype.getClass().getMethods();
+
+ for (final Method method : methods) {
+ if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) {
+
+ final String name = Introspector.decapitalize(method.getName().substring(3));
+ Class<?> parameterType = method.getParameterTypes()[0];
+
+ if (json.containsKey(name)) try {
+ if (parameterType.equals(boolean.class)) {
+ method.invoke(object, json.getBoolean(name));
+ } else if (parameterType.equals(int.class)) {
+ method.invoke(object, json.getJsonNumber(name).intValue());
+ } else if (parameterType.equals(long.class)) {
+ method.invoke(object, json.getJsonNumber(name).longValue());
+ } else if (parameterType.equals(double.class)) {
+ method.invoke(object, json.getJsonNumber(name).doubleValue());
+ } else if (parameterType.equals(String.class)) {
+ method.invoke(object, json.getString(name));
+ } else if (parameterType.equals(Date.class)) {
+ method.invoke(object, dateFormat.parse(json.getString(name)));
+ }
+ } catch (IllegalAccessException error) {
+ } catch (InvocationTargetException error) {
+ }
+ }
+ }
+
+ return object;
+ }
+
+ public static <T> JsonObject objectToJson(T object) {
+
+ JsonObjectBuilder json = Json.createObjectBuilder();
+
+ Method[] methods = object.getClass().getMethods();
+
+ for (Method method : methods) {
+ if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
+ String name = method.getName().substring(3);
+ try {
+ if (method.getReturnType().equals(boolean.class)) {
+ json.add(name, (Boolean) method.invoke(object));
+ } else if (method.getReturnType().equals(int.class)) {
+ json.add(name, (Integer) method.invoke(object));
+ } else if (method.getReturnType().equals(long.class)) {
+ json.add(name, (Long) method.invoke(object));
+ } else if (method.getReturnType().equals(double.class)) {
+ json.add(name, (Double) method.invoke(object));
+ } else if (method.getReturnType().equals(String.class)) {
+ json.add(name, (String) method.invoke(object));
+ } else if (method.getReturnType().equals(Date.class)) {
+ json.add(name, dateFormat.format((Date) method.invoke(object)));
+ }
+ } catch (IllegalAccessException error) {
+ } catch (InvocationTargetException error) {
+ }
+ }
+ }
+
+ return json.build();
+ }
+
+ public static JsonArray arrayToJson(Collection<?> array) {
+
+ JsonArrayBuilder json = Json.createArrayBuilder();
+
+ for (Object object : array) {
+ json.add(objectToJson(object));
+ }
+
+ return json.build();
+ }
+
+}
diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java
index 2ba97ed57..a881d3f45 100644
--- a/src/org/traccar/http/MainServlet.java
+++ b/src/org/traccar/http/MainServlet.java
@@ -34,7 +34,6 @@ import org.jboss.netty.util.TimerTask;
import org.traccar.Context;
import org.traccar.GlobalTimer;
import org.traccar.database.DataCache;
-import org.traccar.database.ObjectConverter;
import org.traccar.helper.Log;
import org.traccar.model.Position;
diff --git a/src/org/traccar/model/Data.java b/src/org/traccar/model/Data.java
index d49bb0da6..d8f898317 100644
--- a/src/org/traccar/model/Data.java
+++ b/src/org/traccar/model/Data.java
@@ -15,17 +15,11 @@
*/
package org.traccar.model;
-import java.text.ParseException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
-import javax.json.Json;
-import javax.json.JsonObject;
-import javax.json.JsonObjectBuilder;
-import org.traccar.database.Convertable;
-import org.traccar.database.ObjectConverter;
-public class Data implements Convertable {
+public class Data {
private long id;
public long getId() { return id; }
@@ -57,26 +51,4 @@ public class Data implements Convertable {
return MiscFormatter.toXmlString(other);
}
- @Override
- public JsonObject toJson() {
- JsonObjectBuilder json = Json.createObjectBuilder();
- json.add("id", id);
- json.add("protocol", protocol);
- json.add("deviceId", deviceId);
- json.add("serverTime", ObjectConverter.dateFormat.format(serverTime));
- json.add("deviceTime", ObjectConverter.dateFormat.format(deviceTime));
- //json.add("extendedInfo", extendedInfo);
- return json.build();
- }
-
- @Override
- public void fromJson(JsonObject json) throws ParseException {
- id = json.getJsonNumber("id").longValue();
- protocol = json.getString("protocol");
- deviceId = json.getJsonNumber("deviceId").longValue();
- serverTime = ObjectConverter.dateFormat.parse(json.getString("serverTime"));
- deviceTime = ObjectConverter.dateFormat.parse(json.getString("deviceTime"));
- //extendedInfo = json.getString("extendedInfo");
- }
-
}
diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java
index 2fb26d282..b40e694b8 100644
--- a/src/org/traccar/model/Device.java
+++ b/src/org/traccar/model/Device.java
@@ -15,18 +15,9 @@
*/
package org.traccar.model;
-import java.sql.ResultSet;
-import java.sql.SQLException;
-import java.text.ParseException;
import java.util.Date;
-import javax.json.Json;
-import javax.json.JsonObject;
-import javax.json.JsonObjectBuilder;
-import org.traccar.database.Convertable;
-import org.traccar.database.ObjectConverter;
-import org.traccar.helper.Log;
-public class Device implements Convertable, Factory {
+public class Device implements Factory {
@Override
public Device create() {
@@ -53,28 +44,4 @@ public class Device implements Convertable, Factory {
private long dataId;
- @Override
- public JsonObject toJson() {
- JsonObjectBuilder json = Json.createObjectBuilder();
- ObjectConverter.putLong(json, "id", id);
- ObjectConverter.putString(json, "name", name);
- ObjectConverter.putString(json, "uniqueId", uniqueId);
- ObjectConverter.putString(json, "status", status);
- ObjectConverter.putDate(json, "lastUpdate", lastUpdate);
- ObjectConverter.putLong(json, "positionId", positionId);
- ObjectConverter.putLong(json, "dataId", dataId);
- return json.build();
- }
-
- @Override
- public void fromJson(JsonObject json) throws ParseException {
- id = ObjectConverter.getLong(json, "id");
- name = ObjectConverter.getString(json, "name");
- uniqueId = ObjectConverter.getString(json, "uniqueId");
- status = ObjectConverter.getString(json, "status");
- lastUpdate = ObjectConverter.getDate(json, "lastUpdate");
- positionId = ObjectConverter.getLong(json, "positionId");
- dataId = ObjectConverter.getLong(json, "dataId");
- }
-
}
diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java
index c3fe60fc0..21951d115 100644
--- a/src/org/traccar/model/Position.java
+++ b/src/org/traccar/model/Position.java
@@ -15,17 +15,11 @@
*/
package org.traccar.model;
-import java.text.ParseException;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
-import javax.json.Json;
-import javax.json.JsonObject;
-import javax.json.JsonObjectBuilder;
-import org.traccar.database.Convertable;
-import org.traccar.database.ObjectConverter;
-public class Position implements Convertable {
+public class Position {
private long id;
public long getId() { return id; }
@@ -94,42 +88,4 @@ public class Position implements Convertable {
return MiscFormatter.toXmlString(other);
}
- @Override
- public JsonObject toJson() {
- JsonObjectBuilder json = Json.createObjectBuilder();
- json.add("id", id);
- json.add("protocol", protocol);
- json.add("deviceId", deviceId);
- json.add("serverTime", ObjectConverter.dateFormat.format(serverTime));
- json.add("deviceTime", ObjectConverter.dateFormat.format(deviceTime));
- json.add("fixTime", ObjectConverter.dateFormat.format(fixTime));
- json.add("valid", valid);
- json.add("latitude", latitude);
- json.add("longitude", longitude);
- json.add("altitude", altitude);
- json.add("speed", speed);
- json.add("course", course);
- json.add("address", address);
- //json.add("extendedInfo", extendedInfo);
- return json.build();
- }
-
- @Override
- public void fromJson(JsonObject json) throws ParseException {
- id = json.getJsonNumber("id").longValue();
- protocol = json.getString("protocol");
- deviceId = json.getJsonNumber("deviceId").longValue();
- serverTime = ObjectConverter.dateFormat.parse(json.getString("serverTime"));
- deviceTime = ObjectConverter.dateFormat.parse(json.getString("deviceTime"));
- fixTime = ObjectConverter.dateFormat.parse(json.getString("fixTime"));
- valid = json.getBoolean("valid");
- latitude = json.getJsonNumber("latitude").doubleValue();
- longitude = json.getJsonNumber("longitude").doubleValue();
- altitude = json.getJsonNumber("altitude").doubleValue();
- speed = json.getJsonNumber("speed").doubleValue();
- course = json.getJsonNumber("course").doubleValue();
- address = json.getString("address");
- //extendedInfo = json.getString("extendedInfo");
- }
-
}
diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java
index 02e5c0c4f..ea4617e54 100644
--- a/src/org/traccar/model/User.java
+++ b/src/org/traccar/model/User.java
@@ -15,18 +15,19 @@
*/
package org.traccar.model;
-import javax.json.Json;
-import javax.json.JsonObject;
-import javax.json.JsonObjectBuilder;
-import org.traccar.database.Convertable;
-
-public class User implements Convertable {
+public class User {
private long id;
+ public long getId() { return id; }
+ public void setId(long id) { this.id = id; }
private String email;
+ public String getEmail() { return email; }
+ public void setEmail(String email) { this.email = email; }
private String password;
+ public String getPassword() { return password; }
+ public void setPassword(String password) { this.password = password; }
private boolean readonly;
@@ -46,38 +47,4 @@ public class User implements Convertable {
private int zoom;
- @Override
- public JsonObject toJson() {
- JsonObjectBuilder json = Json.createObjectBuilder();
- json.add("id", id);
- json.add("email", email);
- json.add("password", password);
- json.add("readonly", readonly);
- json.add("admin", admin);
- json.add("map", map);
- json.add("language", language);
- json.add("distanceUnit", distanceUnit);
- json.add("speedUnit", speedUnit);
- json.add("latitude", latitude);
- json.add("longitude", longitude);
- json.add("zoom", zoom);
- return json.build();
- }
-
- @Override
- public void fromJson(JsonObject json) {
- id = json.getJsonNumber("id").longValue();
- email = json.getString("email");
- password = json.getString("password");
- readonly = json.getBoolean("readonly");
- admin = json.getBoolean("admin");
- map = json.getString("map");
- language = json.getString("language");
- distanceUnit = json.getString("distanceUnit");
- speedUnit = json.getString("speedUnit");
- latitude = json.getJsonNumber("latitude").doubleValue();
- longitude = json.getJsonNumber("longitude").doubleValue();
- zoom = json.getJsonNumber("zoom").intValue();
- }
-
}