aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-04-28 22:41:07 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-04-28 22:41:07 +1200
commita5f47492857a175548beff07cfb565129f687ae2 (patch)
treed5650b6f5db7de9296d8d00cab505b9f594112f6 /src/org/traccar/database
parentc14f99819add0971a82ced01bf6d54ed77aedb74 (diff)
downloadtrackermap-server-a5f47492857a175548beff07cfb565129f687ae2.tar.gz
trackermap-server-a5f47492857a175548beff07cfb565129f687ae2.tar.bz2
trackermap-server-a5f47492857a175548beff07cfb565129f687ae2.zip
Implement API to create devices
Diffstat (limited to 'src/org/traccar/database')
-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.java35
-rw-r--r--src/org/traccar/database/ObjectConverter.java97
-rw-r--r--src/org/traccar/database/ResultSetConverter.java6
4 files changed, 133 insertions, 16 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;