2020-01-13 21:00:33 +01:00
package com.wasteinformationserver.website.datarequests ;
2020-01-16 17:27:16 +01:00
import com.wasteinformationserver.basicutils.Log ;
2020-01-18 08:42:08 +01:00
import com.wasteinformationserver.db.JDBC ;
2020-01-13 21:00:33 +01:00
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 ) {
2020-01-18 08:42:08 +01:00
JDBC jdbc = null ;
2020-01-13 21:00:33 +01:00
try {
2020-01-20 18:20:37 +01:00
jdbc = JDBC . getInstance ();
2020-01-13 21:00:33 +01:00
} catch ( IOException e ) {
e . printStackTrace ();
}
2020-01-16 17:27:16 +01:00
StringBuilder sb = new StringBuilder ();
2020-01-13 21:00:33 +01:00
switch ( params . get ( "action" )) {
case "getdevices" :
2020-01-23 17:10:22 +01:00
ResultSet deviceset = jdbc . executeQuery ( "SELECT * FROM `devices" );
2020-01-18 08:42:08 +01:00
2020-01-16 17:27:16 +01:00
sb . append ( "{\"data\":[" );
2020-01-13 21:00:33 +01:00
try {
2020-01-23 17:10:22 +01:00
while ( deviceset . next ()) {
int deviceid = deviceset . getInt ( "DeviceID" );
int cityid = deviceset . getInt ( "CityID" );
2020-01-16 17:27:16 +01:00
2020-01-23 17:10:22 +01:00
if ( cityid == - 1 ) {
sb . append ( "{\"deviceid\":\"" ). append ( deviceid ). append ( "\",\"cityid\":\"" ). append ( cityid ). append ( "\"}" );
} else {
String devicename = deviceset . getString ( "DeviceName" );
String devicelocation = deviceset . getString ( "DeviceLocation" );
2020-01-16 17:27:16 +01:00
2020-01-23 17:10:22 +01:00
sb . append ( "{\"deviceid\":\"" ). append ( deviceid ). append ( "\",\"devicename\":\"" ). append ( devicename ). append ( "\",\"devicelocation\":\"" ). append ( devicelocation ). append ( "\",\"devices\":[" );
2020-01-18 08:42:08 +01:00
2020-01-23 17:10:22 +01:00
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 ( "]}" );
2020-01-13 21:00:33 +01:00
}
2020-01-23 17:10:22 +01:00
if ( ! ( deviceset . isLast ())) {
2020-01-18 10:17:33 +01:00
sb . append ( "," );
2020-01-18 08:42:08 +01:00
}
2020-01-18 10:17:33 +01:00
}
2020-01-13 21:00:33 +01:00
sb . append ( "]}" );
} catch ( SQLException e ) {
e . printStackTrace ();
}
2020-01-16 17:27:16 +01:00
break ;
case "getCitynames" :
2020-01-23 17:10:22 +01:00
deviceset = jdbc . executeQuery ( "select * from cities" );
Log . debug ( deviceset . toString ());
2020-01-16 17:27:16 +01:00
sb . append ( "{" );
try {
String prev = "" ;
2020-01-23 17:10:22 +01:00
while ( deviceset . next ()) {
if ( ! prev . equals ( deviceset . getString ( "name" ))) {
if ( ! deviceset . isFirst ()) {
2020-01-16 17:27:16 +01:00
sb . append ( "," );
}
2020-01-23 17:10:22 +01:00
sb . append ( "\"" ). append ( deviceset . getString ( "name" )). append ( "\":\"" ). append ( deviceset . getString ( "name" )). append ( "\"" );
2020-01-16 17:27:16 +01:00
}
2020-01-23 17:10:22 +01:00
prev = deviceset . getString ( "name" );
2020-01-16 17:27:16 +01:00
}
} catch ( SQLException e ) {
e . printStackTrace ();
}
sb . append ( "}" );
Log . debug ( sb . toString ());
break ;
case "getzones" :
2020-01-23 17:10:22 +01:00
deviceset = jdbc . executeQuery ( "select * from cities WHERE `name`='" + params . get ( "cityname" ) + "' ORDER BY zone ASC" );
Log . debug ( deviceset . toString ());
2020-01-16 21:12:05 +01:00
sb . append ( "{" );
2020-01-16 17:27:16 +01:00
try {
int prev = 42 ;
2020-01-23 17:10:22 +01:00
while ( deviceset . next ()) {
if ( prev != deviceset . getInt ( "zone" )) {
sb . append ( "\"" ). append ( deviceset . getInt ( "zone" )). append ( "\":\"" ). append ( deviceset . getInt ( "zone" )). append ( "\"" );
if ( ! deviceset . isLast ()) {
2020-01-16 17:27:16 +01:00
sb . append ( "," );
}
}
2020-01-23 17:10:22 +01:00
prev = deviceset . getInt ( "zone" );
2020-01-16 17:27:16 +01:00
}
} catch ( SQLException e ) {
e . printStackTrace ();
}
sb . append ( "}" );
break ;
case "gettypes" :
2020-01-23 17:10:22 +01:00
deviceset = jdbc . executeQuery ( "select * from cities WHERE `name`='" + params . get ( "cityname" ) + "' AND `zone`='" + params . get ( "zonename" ) + "' ORDER BY zone ASC" );
Log . debug ( deviceset . toString ());
2020-01-16 21:12:05 +01:00
sb . append ( "{" );
2020-01-16 17:27:16 +01:00
try {
String prev = "42" ;
2020-01-23 17:10:22 +01:00
while ( deviceset . next ()) {
if ( ! prev . equals ( deviceset . getString ( "wastetype" ))) {
sb . append ( "\"" + deviceset . getString ( "wastetype" ) + "\":\"" + deviceset . getString ( "wastetype" ) + "\"" );
if ( ! deviceset . isLast ()) {
2020-01-16 17:27:16 +01:00
sb . append ( "," );
}
}
2020-01-23 17:10:22 +01:00
prev = deviceset . getString ( "wastetype" );
2020-01-16 17:27:16 +01:00
}
} catch ( SQLException e ) {
e . printStackTrace ();
}
sb . append ( "}" );
2020-01-13 21:00:33 +01:00
break ;
2020-01-16 21:12:05 +01:00
case "savetodb" :
try {
2020-01-23 17:10:22 +01:00
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 ) {
2020-01-18 08:42:08 +01:00
// TODO: 17.01.20 error handling
2020-01-16 21:12:05 +01:00
} else {
2020-01-23 17:10:22 +01:00
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" ) + "'" );
2020-01-18 08:42:08 +01:00
sb . append ( "{\"success\":\"true\"}" );
2020-01-16 21:12:05 +01:00
}
} catch ( SQLException e ) {
e . printStackTrace ();
}
2020-01-18 08:42:08 +01:00
break ;
case "deleteDevice" :
try {
2020-01-20 18:20:37 +01:00
jdbc . executeUpdate ( "DELETE FROM devices WHERE `DeviceID`='" + params . get ( "id" ) + "'" );
2020-01-23 17:10:22 +01:00
jdbc . executeUpdate ( "DELETE FROM device_city WHERE `DeviceID`='" + params . get ( "id" ) + "'" );
2020-01-18 08:42:08 +01:00
} catch ( SQLException e ) {
e . printStackTrace ();
}
sb . append ( "{\"status\":\"success\"}" );
2020-01-16 21:12:05 +01:00
break ;
2020-01-18 10:17:33 +01:00
case "getDeviceNumber" :
2020-01-23 17:10:22 +01:00
try {
ResultSet numberset = jdbc . executeQuery ( "SELECT * FROM devices" );
numberset . last ();
int devicenr = numberset . getRow ();
sb . append ( "{\"devicenr\":\"" + devicenr + "\"}" );
} catch ( SQLException e ) {
e . printStackTrace ();
}
2020-01-18 10:17:33 +01:00
break ;
case "addtodb" :
2020-01-23 17:10:22 +01:00
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}" );
2020-01-18 10:17:33 +01:00
break ;
2020-01-13 21:00:33 +01:00
}
2020-01-16 17:27:16 +01:00
return sb . toString ();
2020-01-13 21:00:33 +01:00
}
}