From a5f47492857a175548beff07cfb565129f687ae2 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 28 Apr 2015 22:41:07 +1200 Subject: Implement API to create devices --- src/org/traccar/database/Convertable.java | 31 ++++++++ src/org/traccar/database/DataManager.java | 35 ++++++++- src/org/traccar/database/JsonConvertable.java | 30 -------- src/org/traccar/database/ObjectConverter.java | 97 +++++++++++++++++++++++- src/org/traccar/database/ResultSetConverter.java | 6 +- 5 files changed, 158 insertions(+), 41 deletions(-) create mode 100644 src/org/traccar/database/Convertable.java delete mode 100644 src/org/traccar/database/JsonConvertable.java (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/Convertable.java b/src/org/traccar/database/Convertable.java new file mode 100644 index 000000000..4653a815a --- /dev/null +++ b/src/org/traccar/database/Convertable.java @@ -0,0 +1,31 @@ +/* + * 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.sql.ResultSet; +import javax.json.JsonObject; + +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 list = new LinkedList(); + 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/JsonConvertable.java b/src/org/traccar/database/JsonConvertable.java deleted file mode 100644 index 37af9a617..000000000 --- a/src/org/traccar/database/JsonConvertable.java +++ /dev/null @@ -1,30 +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.SimpleDateFormat; -import javax.json.JsonObject; - -public interface JsonConvertable { - - public static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"); - - 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 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 collection) { + + public static final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ssZ"); + + public static JsonArray arrayToJson(Collection 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; -- cgit v1.2.3