From 22a9a5b6129ac413358fa39436e03804cba58e4b Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Fri, 31 Jan 2020 10:01:09 +0100 Subject: [PATCH] alter parts to kotlin better error handling on register request --- .../website/basicrequest/GetRequest.kt | 2 +- .../website/basicrequest/PostRequest.kt | 2 +- .../website/datarequests/DeviceRequest.java | 181 ------------------ .../website/datarequests/DeviceRequest.kt | 166 ++++++++++++++++ .../website/datarequests/RegisterRequest.java | 35 ---- .../website/datarequests/RegisterRequest.kt | 38 ++++ 6 files changed, 206 insertions(+), 218 deletions(-) delete mode 100644 src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java create mode 100644 src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.kt delete mode 100644 src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java create mode 100644 src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.kt diff --git a/src/java/com/wasteinformationserver/website/basicrequest/GetRequest.kt b/src/java/com/wasteinformationserver/website/basicrequest/GetRequest.kt index 0dd601f..b78434d 100644 --- a/src/java/com/wasteinformationserver/website/basicrequest/GetRequest.kt +++ b/src/java/com/wasteinformationserver/website/basicrequest/GetRequest.kt @@ -34,5 +34,5 @@ abstract class GetRequest : HttpHandler { * @param params received get params from com.wasteinformationserver.website * @return json reply to com.wasteinformationserver.website */ - abstract fun myrequest(params: HashMap?): String + abstract fun myrequest(params: HashMap): String } \ No newline at end of file diff --git a/src/java/com/wasteinformationserver/website/basicrequest/PostRequest.kt b/src/java/com/wasteinformationserver/website/basicrequest/PostRequest.kt index 6e5330e..8edc8e2 100644 --- a/src/java/com/wasteinformationserver/website/basicrequest/PostRequest.kt +++ b/src/java/com/wasteinformationserver/website/basicrequest/PostRequest.kt @@ -40,5 +40,5 @@ abstract class PostRequest : HttpHandler { * @param params received get params from com.wasteinformationserver.website * @return json reply to com.wasteinformationserver.website */ - abstract fun request(params: HashMap?): String + abstract fun request(params: HashMap): String } \ No newline at end of file diff --git a/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java b/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java deleted file mode 100644 index de7cee3..0000000 --- a/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.java +++ /dev/null @@ -1,181 +0,0 @@ -package com.wasteinformationserver.website.datarequests; - -import com.wasteinformationserver.basicutils.Log; -import com.wasteinformationserver.db.JDBC; -import com.wasteinformationserver.website.basicrequest.PostRequest; - -import java.io.IOException; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.HashMap; - -public class DeviceRequest extends PostRequest { - @Override - public String request(HashMap params) { - - JDBC jdbc = null; - try { - jdbc = JDBC.getInstance(); - } catch (IOException e) { - e.printStackTrace(); - } - - StringBuilder sb = new StringBuilder(); - switch (params.get("action")) { - case "getdevices": - ResultSet deviceset = jdbc.executeQuery("SELECT * FROM `devices"); - - sb.append("{\"data\":["); - try { - while (deviceset.next()) { - int deviceid = deviceset.getInt("DeviceID"); - int cityid = deviceset.getInt("CityID"); - - if (cityid == -1) { - sb.append("{\"deviceid\":\"").append(deviceid).append("\",\"cityid\":\"").append(cityid).append("\"}"); - } else { - String devicename = deviceset.getString("DeviceName"); - String devicelocation = deviceset.getString("DeviceLocation"); - - sb.append("{\"deviceid\":\"").append(deviceid).append("\",\"devicename\":\"").append(devicename).append("\",\"devicelocation\":\"").append(devicelocation).append("\",\"devices\":["); - - ResultSet devicecities = jdbc.executeQuery("SELECT * FROM `device_city` INNER JOIN `cities` ON device_city.CityID=cities.id WHERE `DeviceID`='" + deviceid + "'"); - while (devicecities.next()) { - int cityidd = devicecities.getInt("id"); - String cityname = devicecities.getString("name"); - String wastetype = devicecities.getString("wastetype"); - String zone = devicecities.getString("zone"); - - sb.append("{\"cityid\":\"").append(cityidd).append("\",\"cityname\":\"").append(cityname).append("\",\"wastetype\":\"").append(wastetype).append("\",\"zone\":\"").append(zone).append("\"}"); - if (!(devicecities.isLast())) { - sb.append(","); - } - } - sb.append("]}"); - } - if (!(deviceset.isLast())) { - sb.append(","); - } - } - sb.append("]}"); - } catch (SQLException e) { - e.printStackTrace(); - } - - break; - case "getCitynames": - deviceset = jdbc.executeQuery("select * from cities"); - Log.Log.debug(deviceset.toString()); - sb.append("{"); - try { - String prev = ""; - while (deviceset.next()) { - if (!prev.equals(deviceset.getString("name"))) { - if (!deviceset.isFirst()) { - sb.append(","); - } - sb.append("\"").append(deviceset.getString("name")).append("\":\"").append(deviceset.getString("name")).append("\""); - } - prev = deviceset.getString("name"); - } - } catch (SQLException e) { - e.printStackTrace(); - } - sb.append("}"); - Log.Log.debug(sb.toString()); - break; - case "getzones": - deviceset = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' ORDER BY zone ASC"); - Log.Log.debug(deviceset.toString()); - sb.append("{"); - try { - int prev = 42; - while (deviceset.next()) { - if (prev != deviceset.getInt("zone")) { - sb.append("\"").append(deviceset.getInt("zone")).append("\":\"").append(deviceset.getInt("zone")).append("\""); - if (!deviceset.isLast()) { - sb.append(","); - } - } - prev = deviceset.getInt("zone"); - } - } catch (SQLException e) { - e.printStackTrace(); - } - sb.append("}"); - break; - case "gettypes": - deviceset = jdbc.executeQuery("select * from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' ORDER BY zone ASC"); - Log.Log.debug(deviceset.toString()); - sb.append("{"); - try { - String prev = "42"; - while (deviceset.next()) { - if (!prev.equals(deviceset.getString("wastetype"))) { - sb.append("\"" + deviceset.getString("wastetype") + "\":\"" + deviceset.getString("wastetype") + "\""); - if (!deviceset.isLast()) { - sb.append(","); - } - } - prev = deviceset.getString("wastetype"); - } - } catch (SQLException e) { - e.printStackTrace(); - } - sb.append("}"); - break; - case "savetodb": - try { - ResultSet cityset = jdbc.executeQuery("SELECT id from cities WHERE `name`='" + params.get("cityname") + "' AND `zone`='" + params.get("zonename") + "' AND `wastetype`='" + params.get("wastetype") + "'"); - cityset.last(); - if (cityset.getRow() != 1) { - // TODO: 17.01.20 error handling - } else { - int cityid = cityset.getInt("id"); - - jdbc.executeUpdate("INSERT INTO `device_city` (`DeviceID`, `CityID`) VALUES ('" + params.get("deviceid") + "', '" + cityid + "');"); - jdbc.executeUpdate("UPDATE devices SET `CityID`='0',`DeviceName`='" + params.get("devicename") + "',`DeviceLocation`='" + params.get("devicelocation") + "' WHERE `DeviceID`='" + params.get("deviceid") + "'"); - sb.append("{\"success\":\"true\"}"); - } - } catch (SQLException e) { - e.printStackTrace(); - } - break; - case "deleteDevice": - try { - jdbc.executeUpdate("DELETE FROM devices WHERE `DeviceID`='" + params.get("id") + "'"); - jdbc.executeUpdate("DELETE FROM device_city WHERE `DeviceID`='" + params.get("id") + "'"); - } catch (SQLException e) { - e.printStackTrace(); - } - sb.append("{\"status\":\"success\"}"); - break; - case "getDeviceNumber": - try { - ResultSet numberset = jdbc.executeQuery("SELECT * FROM devices"); - numberset.last(); - int devicenr = numberset.getRow(); - - sb.append("{\"devicenr\":\"" + devicenr + "\"}"); - } catch (SQLException e) { - e.printStackTrace(); - } - break; - case "addtodb": - int cityid = -1; - try { - ResultSet device = jdbc.executeQuery("SELECT * FROM cities WHERE name='" + params.get("cityname") + "' AND wastetype='" + params.get("wastetype") + "' AND zone='" + params.get("zonename") + "'"); - device.first(); - cityid = device.getInt("id"); - jdbc.executeUpdate("INSERT INTO `device_city` (`DeviceID`, `CityID`) VALUES ('" + params.get("deviceid") + "', '" + cityid + "');"); - } catch (SQLException e) { - e.printStackTrace(); - } - - - sb.append("{\"success\":true}"); - break; - } - return sb.toString(); - } -} diff --git a/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.kt b/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.kt new file mode 100644 index 0000000..0dc5f27 --- /dev/null +++ b/src/java/com/wasteinformationserver/website/datarequests/DeviceRequest.kt @@ -0,0 +1,166 @@ +package com.wasteinformationserver.website.datarequests + +import com.wasteinformationserver.basicutils.Log.Log.debug +import com.wasteinformationserver.basicutils.Log.Log.error +import com.wasteinformationserver.db.JDBC +import com.wasteinformationserver.website.basicrequest.PostRequest +import java.io.IOException +import java.sql.ResultSet +import java.sql.SQLException +import java.util.* + +class DeviceRequest : PostRequest() { + override fun request(params: HashMap): String { + var jdbc: JDBC? = null + try { + jdbc = JDBC.getInstance() + } catch (e: IOException) { + e.printStackTrace() + } + val sb = StringBuilder() + var deviceset: ResultSet + when (params["action"]) { + "getdevices" -> { + deviceset = jdbc!!.executeQuery("SELECT * FROM `devices") + sb.append("{\"data\":[") + try { + while (deviceset.next()) { + val deviceid = deviceset.getInt("DeviceID") + val cityid = deviceset.getInt("CityID") + if (cityid == -1) { + sb.append("{\"deviceid\":\"").append(deviceid).append("\",\"cityid\":\"").append(cityid).append("\"}") + } else { + val devicename = deviceset.getString("DeviceName") + val devicelocation = deviceset.getString("DeviceLocation") + sb.append("{\"deviceid\":\"").append(deviceid).append("\",\"devicename\":\"").append(devicename).append("\",\"devicelocation\":\"").append(devicelocation).append("\",\"devices\":[") + val devicecities = jdbc.executeQuery("SELECT * FROM `device_city` INNER JOIN `cities` ON device_city.CityID=cities.id WHERE `DeviceID`='$deviceid'") + while (devicecities.next()) { + val cityidd = devicecities.getInt("id") + val cityname = devicecities.getString("name") + val wastetype = devicecities.getString("wastetype") + val zone = devicecities.getString("zone") + sb.append("{\"cityid\":\"").append(cityidd).append("\",\"cityname\":\"").append(cityname).append("\",\"wastetype\":\"").append(wastetype).append("\",\"zone\":\"").append(zone).append("\"}") + if (!devicecities.isLast) { + sb.append(",") + } + } + sb.append("]}") + } + if (!deviceset.isLast) { + sb.append(",") + } + } + sb.append("]}") + } catch (e: SQLException) { + e.printStackTrace() + } + } + "getCitynames" -> { + deviceset = jdbc!!.executeQuery("select * from cities") + debug(deviceset.toString()) + sb.append("{") + try { + var prev = "" + while (deviceset.next()) { + if (prev != deviceset.getString("name")) { + if (!deviceset.isFirst()) { + sb.append(",") + } + sb.append("\"").append(deviceset.getString("name")).append("\":\"").append(deviceset.getString("name")).append("\"") + } + prev = deviceset.getString("name") + } + } catch (e: SQLException) { + e.printStackTrace() + } + sb.append("}") + debug(sb.toString()) + } + "getzones" -> { + deviceset = jdbc!!.executeQuery("select * from cities WHERE `name`='" + params["cityname"] + "' ORDER BY zone ASC") + debug(deviceset.toString()) + sb.append("{") + try { + var prev = 42 + while (deviceset.next()) { + if (prev != deviceset.getInt("zone")) { + sb.append("\"").append(deviceset.getInt("zone")).append("\":\"").append(deviceset.getInt("zone")).append("\"") + if (!deviceset.isLast()) { + sb.append(",") + } + } + prev = deviceset.getInt("zone") + } + } catch (e: SQLException) { + e.printStackTrace() + } + sb.append("}") + } + "gettypes" -> { + deviceset = jdbc!!.executeQuery("select * from cities WHERE `name`='" + params["cityname"] + "' AND `zone`='" + params["zonename"] + "' ORDER BY zone ASC") + debug(deviceset.toString()) + sb.append("{") + try { + var prev = "42" + while (deviceset.next()) { + if (prev != deviceset.getString("wastetype")) { + sb.append("\"" + deviceset.getString("wastetype") + "\":\"" + deviceset.getString("wastetype") + "\"") + if (!deviceset.isLast()) { + sb.append(",") + } + } + prev = deviceset.getString("wastetype") + } + } catch (e: SQLException) { + e.printStackTrace() + } + sb.append("}") + } + "savetodb" -> try { + val cityset = jdbc!!.executeQuery("SELECT id from cities WHERE `name`='" + params["cityname"] + "' AND `zone`='" + params["zonename"] + "' AND `wastetype`='" + params["wastetype"] + "'") + cityset.last() + if (cityset.row != 1) { + error("error saving device to db --> device multiply defined?") + sb.append("{\"success\":\"false\"}") + } else { + val cityid = cityset.getInt("id") + jdbc.executeUpdate("INSERT INTO `device_city` (`DeviceID`, `CityID`) VALUES ('" + params["deviceid"] + "', '" + cityid + "');") + jdbc.executeUpdate("UPDATE devices SET `CityID`='0',`DeviceName`='" + params["devicename"] + "',`DeviceLocation`='" + params["devicelocation"] + "' WHERE `DeviceID`='" + params["deviceid"] + "'") + sb.append("{\"success\":\"true\"}") + } + } catch (e: SQLException) { + e.printStackTrace() + } + "deleteDevice" -> { + try { + jdbc!!.executeUpdate("DELETE FROM devices WHERE `DeviceID`='" + params["id"] + "'") + jdbc.executeUpdate("DELETE FROM device_city WHERE `DeviceID`='" + params["id"] + "'") + } catch (e: SQLException) { + e.printStackTrace() + } + sb.append("{\"status\":\"success\"}") + } + "getDeviceNumber" -> try { + val numberset = jdbc!!.executeQuery("SELECT * FROM devices") + numberset.last() + val devicenr = numberset.row + sb.append("{\"devicenr\":\"$devicenr\"}") + } catch (e: SQLException) { + e.printStackTrace() + } + "addtodb" -> { + var cityid = -1 + try { + val device = jdbc!!.executeQuery("SELECT * FROM cities WHERE name='" + params["cityname"] + "' AND wastetype='" + params["wastetype"] + "' AND zone='" + params["zonename"] + "'") + device.first() + cityid = device.getInt("id") + jdbc.executeUpdate("INSERT INTO `device_city` (`DeviceID`, `CityID`) VALUES ('" + params["deviceid"] + "', '" + cityid + "');") + } catch (e: SQLException) { + e.printStackTrace() + } + sb.append("{\"success\":true}") + } + } + return sb.toString() + } +} \ No newline at end of file diff --git a/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java b/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java deleted file mode 100644 index 4a68fd1..0000000 --- a/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.java +++ /dev/null @@ -1,35 +0,0 @@ -package com.wasteinformationserver.website.datarequests; - -import com.wasteinformationserver.basicutils.Log; -import com.wasteinformationserver.db.JDBC; -import com.wasteinformationserver.website.HttpTools; -import com.wasteinformationserver.website.basicrequest.PostRequest; - -import java.io.IOException; -import java.sql.SQLException; -import java.util.HashMap; - -public class RegisterRequest extends PostRequest { - @Override - public String request(HashMap params) { - Log.Log.debug(params.toString()); - - String passhash = HttpTools.Companion.StringToMD5(params.get("password")); - - JDBC myjd = null; - try { - myjd = JDBC.getInstance(); - } catch (IOException e) { - e.printStackTrace(); - } - //new JDCB("users", "kOpaIJUjkgb9ur6S", "wasteinformation"); - try { - myjd.executeUpdate("INSERT INTO `user` (`username`, `firstName`, `secondName`, `password`, `email`, `logindate`) VALUES ('" + params.get("username") + "', '" + params.get("firstname") + "', '" + params.get("lastname") + "', '" + passhash + "', '" + params.get("email") + "', current_timestamp());"); - } catch (SQLException e) { - e.printStackTrace(); - } - - // TODO: 27.09.19 detect if register process was successful and reply right json - return "{\"accept\": true}"; - } -} diff --git a/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.kt b/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.kt new file mode 100644 index 0000000..52bda99 --- /dev/null +++ b/src/java/com/wasteinformationserver/website/datarequests/RegisterRequest.kt @@ -0,0 +1,38 @@ +package com.wasteinformationserver.website.datarequests + +import com.wasteinformationserver.basicutils.Log +import com.wasteinformationserver.basicutils.Log.Log.debug +import com.wasteinformationserver.db.JDBC +import com.wasteinformationserver.website.HttpTools.Companion.StringToMD5 +import com.wasteinformationserver.website.basicrequest.PostRequest +import java.io.IOException +import java.sql.SQLException +import java.util.* + +class RegisterRequest : PostRequest() { + override fun request(params: HashMap): String { + debug(params.toString()) + val passhash = StringToMD5(params["password"]!!) + var reply: StringBuilder = StringBuilder("{") + try { + var myjd: JDBC = JDBC.getInstance() + + val status = myjd.executeUpdate("INSERT INTO `user` (`username`, `firstName`, `secondName`, `password`, `email`, `logindate`) VALUES ('" + params["username"] + "', '" + params["firstname"] + "', '" + params["lastname"] + "', '" + passhash + "', '" + params["email"] + "', current_timestamp());") + if (status == 1) { + reply.append("\"accept\": true") + } else { + reply.append("\"accept\": false") + } + + } catch (e: IOException) { + Log.error("no connection to db") + reply.append("\"accept\": false") + } catch (e: SQLException) { + e.printStackTrace() + reply.append("\"accept\": false") + } + reply.append("}") + + return reply.toString() + } +} \ No newline at end of file