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-18 08:42:08 +01:00
ResultSet setunconfigured = jdbc . executeQuery ( "SELECT * FROM `devices` WHERE `CityID`=-1" );
ResultSet setconfigured = jdbc . executeQuery ( "SELECT * FROM `devices` INNER JOIN `cities` ON devices.CityID = cities.id" );
2020-01-16 17:27:16 +01:00
sb . append ( "{\"data\":[" );
2020-01-13 21:00:33 +01:00
try {
2020-01-18 08:42:08 +01:00
setconfigured . last ();
int configsize = setconfigured . getRow ();
setconfigured . first ();
setconfigured . previous ();
2020-01-16 17:27:16 +01:00
2020-01-18 08:42:08 +01:00
while ( setunconfigured . next ()) {
int deviceid = setunconfigured . getInt ( "DeviceID" );
int cityid = setunconfigured . getInt ( "CityID" );
2020-01-16 17:27:16 +01:00
2020-01-20 18:20:37 +01:00
sb . append ( "{\"deviceid\":\"" ). append ( deviceid ). append ( "\",\"cityid\":\"" ). append ( cityid ). append ( "\"}" );
2020-01-18 08:42:08 +01:00
2020-01-18 10:17:33 +01:00
if ( ! ( setunconfigured . isLast () && configsize == 0 )) {
2020-01-13 21:00:33 +01:00
sb . append ( "," );
}
}
2020-01-18 08:42:08 +01:00
2020-01-18 10:17:33 +01:00
while ( setconfigured . next ()) {
int deviceid = setconfigured . getInt ( "DeviceID" );
int cityid = setconfigured . getInt ( "CityID" );
2020-01-18 08:42:08 +01:00
2020-01-18 10:17:33 +01:00
String devicename = setconfigured . getString ( "DeviceName" );
String devicelocation = setconfigured . getString ( "DeviceLocation" );
2020-01-18 08:42:08 +01:00
2020-01-18 10:17:33 +01:00
String cityname = setconfigured . getString ( "name" );
String wastetype = setconfigured . getString ( "wastetype" );
String zone = setconfigured . getString ( "zone" );
2020-01-18 08:42:08 +01:00
2020-01-20 18:20:37 +01:00
sb . append ( "{\"deviceid\":\"" ). append ( deviceid ). append ( "\",\"cityid\":\"" ). append ( cityid ). append ( "\",\"devicename\":\"" ). append ( devicename ). append ( "\",\"devicelocation\":\"" ). append ( devicelocation ). append ( "\",\"cityname\":\"" ). append ( cityname ). append ( "\",\"wastetype\":\"" ). append ( wastetype ). append ( "\",\"zone\":\"" ). append ( zone ). append ( "\"}" );
2020-01-18 08:42:08 +01:00
2020-01-18 10:17:33 +01:00
if ( ! setconfigured . isLast ()) {
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-18 08:42:08 +01:00
setunconfigured = jdbc . executeQuery ( "select * from cities" );
Log . debug ( setunconfigured . toString ());
2020-01-16 17:27:16 +01:00
sb . append ( "{" );
try {
String prev = "" ;
2020-01-18 08:42:08 +01:00
while ( setunconfigured . next ()) {
2020-01-20 18:20:37 +01:00
if ( ! prev . equals ( setunconfigured . getString ( "name" ))) {
2020-01-18 08:42:08 +01:00
if ( ! setunconfigured . isFirst ()) {
2020-01-16 17:27:16 +01:00
sb . append ( "," );
}
2020-01-20 18:20:37 +01:00
sb . append ( "\"" ). append ( setunconfigured . getString ( "name" )). append ( "\":\"" ). append ( setunconfigured . getString ( "name" )). append ( "\"" );
2020-01-16 17:27:16 +01:00
}
2020-01-18 08:42:08 +01:00
prev = setunconfigured . 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-18 08:42:08 +01:00
setunconfigured = jdbc . executeQuery ( "select * from cities WHERE `name`='" + params . get ( "cityname" ) + "' ORDER BY zone ASC" );
Log . debug ( setunconfigured . 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-18 08:42:08 +01:00
while ( setunconfigured . next ()) {
2020-01-20 18:20:37 +01:00
if ( prev != setunconfigured . getInt ( "zone" )) {
sb . append ( "\"" ). append ( setunconfigured . getInt ( "zone" )). append ( "\":\"" ). append ( setunconfigured . getInt ( "zone" )). append ( "\"" );
2020-01-18 08:42:08 +01:00
if ( ! setunconfigured . isLast ()) {
2020-01-16 17:27:16 +01:00
sb . append ( "," );
}
}
2020-01-18 08:42:08 +01:00
prev = setunconfigured . getInt ( "zone" );
2020-01-16 17:27:16 +01:00
}
} catch ( SQLException e ) {
e . printStackTrace ();
}
sb . append ( "}" );
break ;
case "gettypes" :
2020-01-18 08:42:08 +01:00
setunconfigured = jdbc . executeQuery ( "select * from cities WHERE `name`='" + params . get ( "cityname" ) + "' AND `zone`='" + params . get ( "zonename" ) + "' ORDER BY zone ASC" );
Log . debug ( setunconfigured . 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-18 08:42:08 +01:00
while ( setunconfigured . next ()) {
2020-01-20 18:20:37 +01:00
if ( ! prev . equals ( setunconfigured . getString ( "wastetype" ))) {
2020-01-18 08:42:08 +01:00
sb . append ( "\"" + setunconfigured . getString ( "wastetype" ) + "\":\"" + setunconfigured . getString ( "wastetype" ) + "\"" );
if ( ! setunconfigured . isLast ()) {
2020-01-16 17:27:16 +01:00
sb . append ( "," );
}
}
2020-01-18 08:42:08 +01:00
prev = setunconfigured . 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" :
2020-01-18 08:42:08 +01:00
setunconfigured = jdbc . executeQuery ( "select * from cities WHERE `name`='" + params . get ( "cityname" ) + "' AND `zone`='" + params . get ( "zonename" ) + "' AND `wastetype`='" + params . get ( "wastetype" ) + "'" );
2020-01-16 21:12:05 +01:00
try {
2020-01-18 08:42:08 +01:00
setunconfigured . last ();
if ( setunconfigured . getRow () != 1 ) {
// TODO: 17.01.20 error handling
2020-01-16 21:12:05 +01:00
} else {
2020-01-18 08:42:08 +01:00
int id = setunconfigured . getInt ( "id" );
jdbc . executeUpdate ( "UPDATE devices SET `CityID`='" + id + "',`DeviceName`='" + params . get ( "devicename" ) + "',`DeviceLocation`='" + params . get ( "devicelocation" ) + "' WHERE `DeviceID`='" + params . get ( "deviceid" ) + "'" );
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-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" :
// TODO: 18.01.20
break ;
case "addtodb" :
// TODO: 18.01.20
2020-01-18 11:02:00 +01:00
//ResultSet seti = jdbc.executeUpdate("UPDATE devices SET `CityID`='" + id + "',`DeviceName`='" + params.get("devicename") + "',`DeviceLocation`='" + params.get("devicelocation") + "' WHERE `DeviceID`='" + params.get("deviceid") + "'"); ");
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
}
}