alter parts to kotlin

better error handling on register request
This commit is contained in:
lukas-heiligenbrunner 2020-01-31 10:01:09 +01:00
parent 02dff914ca
commit 22a9a5b612
6 changed files with 206 additions and 218 deletions

View File

@ -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, String>?): String
abstract fun myrequest(params: HashMap<String, String>): String
}

View File

@ -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, String>?): String
abstract fun request(params: HashMap<String, String>): String
}

View File

@ -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<String, String> 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();
}
}

View File

@ -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, String>): 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()
}
}

View File

@ -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<String, String> 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}";
}
}

View File

@ -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, String>): 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()
}
}