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-18 08:42:08 +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-18 08:42:08 +01:00
sb . append ( " { \" deviceid \" : \" " + deviceid + " \" , \" cityid \" : \" " + cityid + " \" } " ) ;
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-18 10:17:33 +01:00
sb . append ( " { \" deviceid \" : \" " + deviceid + " \" , \" cityid \" : \" " + cityid + " \" , \" devicename \" : \" " + devicename + " \" , \" devicelocation \" : \" " + devicelocation + " \" , \" cityname \" : \" " + cityname + " \" , \" wastetype \" : \" " + wastetype + " \" , \" zone \" : \" " + zone + " \" } " ) ;
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 ( ) ) {
if ( prev . equals ( setunconfigured . getString ( " name " ) ) ) {
2020-01-16 17:27:16 +01:00
} else {
2020-01-18 08:42:08 +01:00
if ( ! setunconfigured . isFirst ( ) ) {
2020-01-16 17:27:16 +01:00
sb . append ( " , " ) ;
}
2020-01-18 08:42:08 +01:00
sb . append ( " \" " + setunconfigured . getString ( " name " ) + " \" : \" " + setunconfigured . getString ( " name " ) + " \" " ) ;
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 ( ) ) {
if ( prev = = setunconfigured . getInt ( " zone " ) ) {
2020-01-16 17:27:16 +01:00
} else {
2020-01-18 08:42:08 +01:00
sb . append ( " \" " + setunconfigured . getInt ( " zone " ) + " \" : \" " + setunconfigured . getInt ( " zone " ) + " \" " ) ;
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 ( ) ) {
if ( prev = = setunconfigured . getString ( " wastetype " ) ) {
2020-01-16 17:27:16 +01:00
} else {
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 {
int res = jdbc . executeUpdate ( " DELETE FROM devices WHERE `DeviceID`=' " + params . get ( " id " ) + " ' " ) ;
} 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
setunconfigured = jdbc . executeQuery ( " select * from cities WHERE `name`=' " + params . get ( " cityname " ) + " ' AND `zone`=' " + params . get ( " zonename " ) + " ' AND `wastetype`=' " + params . get ( " wastetype " ) + " ' " ) ;
try {
setunconfigured . last ( ) ;
if ( setunconfigured . getRow ( ) ! = 1 ) {
// TODO: 17.01.20 error handling
} else {
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 \" } " ) ;
}
} catch ( SQLException e ) {
e . printStackTrace ( ) ;
}
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
}
}