diff options
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/database/Convertable.java (renamed from src/org/traccar/database/JsonConvertable.java) | 11 | ||||
-rw-r--r-- | src/org/traccar/database/DataManager.java | 35 | ||||
-rw-r--r-- | src/org/traccar/database/ObjectConverter.java | 97 | ||||
-rw-r--r-- | src/org/traccar/database/ResultSetConverter.java | 6 | ||||
-rw-r--r-- | src/org/traccar/http/DeviceServlet.java | 29 | ||||
-rw-r--r-- | src/org/traccar/http/MainServlet.java | 2 | ||||
-rw-r--r-- | src/org/traccar/model/Data.java | 19 | ||||
-rw-r--r-- | src/org/traccar/model/Device.java | 53 | ||||
-rw-r--r-- | src/org/traccar/model/Position.java | 23 | ||||
-rw-r--r-- | src/org/traccar/model/User.java | 11 |
10 files changed, 233 insertions, 53 deletions
diff --git a/src/org/traccar/database/JsonConvertable.java b/src/org/traccar/database/Convertable.java index 37af9a617..4653a815a 100644 --- a/src/org/traccar/database/JsonConvertable.java +++ b/src/org/traccar/database/Convertable.java @@ -15,16 +15,17 @@ */ package org.traccar.database; -import java.text.DateFormat; -import java.text.SimpleDateFormat; +import java.sql.ResultSet; import javax.json.JsonObject; -public interface JsonConvertable { - - public static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"); +public interface Convertable { public JsonObject toJson(); public void fromJson(JsonObject json) throws Exception; + //public void toRecord(); + + public void fromRecord(ResultSet record); + } diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index f60fd4cb3..417fbb6b4 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -266,8 +266,8 @@ public class DataManager { "device_id INT NOT NULL," + "read BOOLEAN DEFAULT true NOT NULL," + "write BOOLEAN DEFAULT true NOT NULL," + - "FOREIGN KEY (user_id) REFERENCES user(id)," + - "FOREIGN KEY (device_id) REFERENCES device(id));" + + "FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE," + + "FOREIGN KEY (device_id) REFERENCES device(id) ON DELETE CASCADE);" + "CREATE INDEX user_device_user_id ON user_device(user_id);" + @@ -391,7 +391,6 @@ public class DataManager { } public JsonArray getDevices(long userId) throws SQLException { - Connection connection = dataSource.getConnection(); try { PreparedStatement statement = connection.prepareStatement( @@ -400,7 +399,16 @@ public class DataManager { try { statement.setLong(1, userId); - return ResultSetConverter.convert(statement.executeQuery()); + ResultSet result = statement.executeQuery(); + + List<Device> list = new LinkedList<Device>(); + while (result.next()) { + Device device = new Device(); + device.fromRecord(result); + list.add(device); + } + + return ObjectConverter.arrayToJson(list); } finally { statement.close(); } @@ -471,5 +479,24 @@ public class DataManager { connection.close(); } } + + public void linkDevice(long userId, long deviceId) throws SQLException { + + Connection connection = dataSource.getConnection(); + try { + PreparedStatement statement = connection.prepareStatement( + "INSERT INTO user_device (user_id, device_id) VALUES (?, ?);"); + try { + statement.setLong(1, userId); + statement.setLong(2, deviceId); + + statement.execute(); + } finally { + statement.close(); + } + } finally { + connection.close(); + } + } } diff --git a/src/org/traccar/database/ObjectConverter.java b/src/org/traccar/database/ObjectConverter.java index 4ab83faec..8d6e73123 100644 --- a/src/org/traccar/database/ObjectConverter.java +++ b/src/org/traccar/database/ObjectConverter.java @@ -15,22 +15,113 @@ */ package org.traccar.database; +import java.sql.ResultSet; +import java.sql.ResultSetMetaData; +import java.sql.SQLException; +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 JsonArray convert(Collection<? extends JsonConvertable> collection) { + + 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 (JsonConvertable object : collection) { + for (Convertable object : collection) { array.add(object.toJson()); } return array.build(); } + private static String getColumnName(String key) { + return key.replaceAll("([A-Z])", "_$1").toLowerCase(); + } + + private static boolean hasColumn(ResultSet resultSet, String columnName) throws SQLException { + ResultSetMetaData metaData = resultSet.getMetaData(); + for (int i = 1; i <= metaData.getColumnCount(); i++) { + if (columnName.equalsIgnoreCase(metaData.getColumnName(i))) { + return true; + } + } + return false; + } + + public static String getString(JsonObject json, String key) { + if (json.containsKey(key)) { + return json.getString(key); + } + return null; + } + + public static String getString(ResultSet record, String key) throws SQLException { + String column = getColumnName(key); + if (hasColumn(record, column)) { + return record.getString(column); + } + 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 long getLong(ResultSet record, String key) throws SQLException { + String column = getColumnName(key); + if (hasColumn(record, column)) { + return record.getLong(column); + } + 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 Date getDate(ResultSet record, String key) throws SQLException { + String column = getColumnName(key); + if (hasColumn(record, column)) { + return record.getDate(column); + } + 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/ResultSetConverter.java b/src/org/traccar/database/ResultSetConverter.java index 92cb8fa4e..2bac01393 100644 --- a/src/org/traccar/database/ResultSetConverter.java +++ b/src/org/traccar/database/ResultSetConverter.java @@ -18,8 +18,6 @@ package org.traccar.database; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; -import java.text.DateFormat; -import java.text.SimpleDateFormat; import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonArrayBuilder; @@ -64,10 +62,10 @@ public class ResultSetConverter { obj.add(columnName, rs.getString(columnName)); break; case java.sql.Types.DATE: - obj.add(columnName, JsonConvertable.dateFormat.format(rs.getDate(columnName))); + obj.add(columnName, ObjectConverter.dateFormat.format(rs.getDate(columnName))); break; case java.sql.Types.TIMESTAMP: - obj.add(columnName, JsonConvertable.dateFormat.format(rs.getTimestamp(columnName))); + obj.add(columnName, ObjectConverter.dateFormat.format(rs.getTimestamp(columnName))); break; default: break; diff --git a/src/org/traccar/http/DeviceServlet.java b/src/org/traccar/http/DeviceServlet.java index 260083e0e..5cda5708e 100644 --- a/src/org/traccar/http/DeviceServlet.java +++ b/src/org/traccar/http/DeviceServlet.java @@ -17,13 +17,18 @@ package org.traccar.http; 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.helper.Log; +import org.traccar.model.Device; public class DeviceServlet extends HttpServlet { @@ -65,6 +70,30 @@ public class DeviceServlet extends HttpServlet { } private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException { + + 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); + } catch (ParseException error) { + Log.warning(error); + } + + JsonObjectBuilder result = Json.createObjectBuilder(); + + try { + result.add("success", true); + Context.getDataManager().addDevice(device); + Context.getDataManager().linkDevice(userId, device.getId()); + } catch(SQLException error) { + result.add("success", false); + result.add("error", error.getMessage()); + } + + resp.getWriter().println(result.build().toString()); } private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException { diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java index 62345489e..10920accb 100644 --- a/src/org/traccar/http/MainServlet.java +++ b/src/org/traccar/http/MainServlet.java @@ -173,7 +173,7 @@ public class MainServlet extends HttpServlet { JsonObjectBuilder result = Json.createObjectBuilder(); result.add("success", true); - result.add("data", ObjectConverter.convert(positions.values())); + result.add("data", ObjectConverter.arrayToJson(positions.values())); positions.clear(); try { diff --git a/src/org/traccar/model/Data.java b/src/org/traccar/model/Data.java index 1cef813a3..e329e6c1e 100644 --- a/src/org/traccar/model/Data.java +++ b/src/org/traccar/model/Data.java @@ -15,15 +15,16 @@ */ package org.traccar.model; +import java.sql.ResultSet; import java.text.ParseException; import java.util.Date; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; -import org.traccar.database.JsonConvertable; -import static org.traccar.database.JsonConvertable.dateFormat; +import org.traccar.database.Convertable; +import org.traccar.database.ObjectConverter; -public class Data implements JsonConvertable { +public class Data implements Convertable { private long id; public long getId() { return id; } @@ -50,8 +51,8 @@ public class Data implements JsonConvertable { JsonObjectBuilder json = Json.createObjectBuilder(); json.add("id", id); json.add("deviceId", deviceId); - json.add("serverTime", dateFormat.format(serverTime)); - json.add("deviceTime", dateFormat.format(deviceTime)); + json.add("serverTime", ObjectConverter.dateFormat.format(serverTime)); + json.add("deviceTime", ObjectConverter.dateFormat.format(deviceTime)); //json.add("extendedInfo", extendedInfo); return json.build(); } @@ -60,9 +61,13 @@ public class Data implements JsonConvertable { public void fromJson(JsonObject json) throws ParseException { id = json.getJsonNumber("id").longValue(); deviceId = json.getJsonNumber("deviceId").longValue(); - serverTime = dateFormat.parse(json.getString("serverTime")); - deviceTime = dateFormat.parse(json.getString("deviceTime")); + serverTime = ObjectConverter.dateFormat.parse(json.getString("serverTime")); + deviceTime = ObjectConverter.dateFormat.parse(json.getString("deviceTime")); //extendedInfo = json.getString("extendedInfo"); } + @Override + public void fromRecord(ResultSet record) { + } + } diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index 4e2f57690..067d80f36 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -15,21 +15,25 @@ */ 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.JsonConvertable; +import org.traccar.database.Convertable; +import org.traccar.database.ObjectConverter; +import org.traccar.helper.Log; -public class Device implements JsonConvertable { +public class Device implements Convertable { private long id; public long getId() { return id; } public void setId(long id) { this.id = id; } private String name; - public String getName() { return uniqueId; } + public String getName() { return name; } public void setName(String name) { this.name = name; } private String uniqueId; @@ -47,25 +51,40 @@ public class Device implements JsonConvertable { @Override public JsonObject toJson() { JsonObjectBuilder json = Json.createObjectBuilder(); - json.add("id", id); - json.add("name", name); - json.add("uniqueId", uniqueId); - json.add("status", status); - json.add("lastUpdate", dateFormat.format(lastUpdate)); - json.add("positionId", positionId); - json.add("dataId", dataId); + 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 = json.getJsonNumber("id").longValue(); - name = json.getString("name"); - uniqueId = json.getString("uniqueId"); - status = json.getString("status"); - lastUpdate = dateFormat.parse(json.getString("lastUpdate")); - positionId = json.getJsonNumber("positionId").longValue(); - dataId = json.getJsonNumber("dataId").longValue(); + 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"); + } + + @Override + public void fromRecord(ResultSet record) { + try { + id = ObjectConverter.getLong(record, "id"); + name = ObjectConverter.getString(record, "name"); + uniqueId = ObjectConverter.getString(record, "uniqueId"); + status = ObjectConverter.getString(record, "status"); + lastUpdate = ObjectConverter.getDate(record, "lastUpdate"); + positionId = ObjectConverter.getLong(record, "positionId"); + dataId = ObjectConverter.getLong(record, "dataId"); + } catch (SQLException error) { + Log.warning(error); + } } } diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index 7f042c409..92d5bbe5e 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -15,15 +15,16 @@ */ package org.traccar.model; +import java.sql.ResultSet; import java.text.ParseException; import java.util.Date; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; -import org.traccar.database.JsonConvertable; -import static org.traccar.database.JsonConvertable.dateFormat; +import org.traccar.database.Convertable; +import org.traccar.database.ObjectConverter; -public class Position implements JsonConvertable { +public class Position implements Convertable { private long id; public long getId() { return id; } @@ -87,9 +88,9 @@ public class Position implements JsonConvertable { JsonObjectBuilder json = Json.createObjectBuilder(); json.add("id", id); json.add("deviceId", deviceId); - json.add("serverTime", dateFormat.format(serverTime)); - json.add("deviceTime", dateFormat.format(deviceTime)); - json.add("fixTime", dateFormat.format(fixTime)); + 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); @@ -105,9 +106,9 @@ public class Position implements JsonConvertable { public void fromJson(JsonObject json) throws ParseException { id = json.getJsonNumber("id").longValue(); deviceId = json.getJsonNumber("deviceId").longValue(); - serverTime = dateFormat.parse(json.getString("serverTime")); - deviceTime = dateFormat.parse(json.getString("deviceTime")); - fixTime = dateFormat.parse(json.getString("fixTime")); + 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(); @@ -118,4 +119,8 @@ public class Position implements JsonConvertable { //extendedInfo = json.getString("extendedInfo"); } + @Override + public void fromRecord(ResultSet record) { + } + } diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java index a3eaf5a10..77740e987 100644 --- a/src/org/traccar/model/User.java +++ b/src/org/traccar/model/User.java @@ -15,12 +15,13 @@ */ package org.traccar.model; +import java.sql.ResultSet; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonObjectBuilder; -import org.traccar.database.JsonConvertable; +import org.traccar.database.Convertable; -public class User implements JsonConvertable { +public class User implements Convertable { private long id; @@ -79,5 +80,9 @@ public class User implements JsonConvertable { longitude = json.getJsonNumber("longitude").doubleValue(); zoom = json.getJsonNumber("zoom").intValue(); } - + + @Override + public void fromRecord(ResultSet record) { + } + } |