|
|
|
@ -7,7 +7,6 @@ |
|
|
|
*/ |
|
|
|
package org.telegram.database; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.telegram.structure.WeatherAlert; |
|
|
|
|
|
|
|
import java.sql.PreparedStatement; |
|
|
|
@ -23,7 +22,6 @@ import java.util.List; |
|
|
|
* @version 2.0 |
|
|
|
* Database Manager to perform database operations |
|
|
|
*/ |
|
|
|
@Slf4j |
|
|
|
public class DatabaseManager { |
|
|
|
private static volatile DatabaseManager instance; |
|
|
|
private static volatile ConnectionDB connetion; |
|
|
|
@ -34,7 +32,8 @@ public class DatabaseManager { |
|
|
|
private DatabaseManager() { |
|
|
|
connetion = new ConnectionDB(); |
|
|
|
final int currentVersion = connetion.checkVersion(); |
|
|
|
log.info("Current db version: " + currentVersion); |
|
|
|
// log.info("Current db version: " + currentVersion);
|
|
|
|
System.out.println("Current db version: " + currentVersion); |
|
|
|
if (currentVersion < CreationStrings.version) { |
|
|
|
recreateTable(currentVersion); |
|
|
|
} |
|
|
|
@ -95,11 +94,13 @@ public class DatabaseManager { |
|
|
|
} |
|
|
|
connetion.commitTransaction(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error updating DB", e); |
|
|
|
// log.error("Error updating DB", e);
|
|
|
|
System.out.println("Error updating DB" + e); |
|
|
|
try { |
|
|
|
connetion.rollbackTransaction(); |
|
|
|
} catch (SQLException ex) { |
|
|
|
log.error("Error rollingback the transaction", ex); |
|
|
|
// log.error("Error rollingback the transaction", ex);
|
|
|
|
System.out.println("Error rollingback the transaction" + ex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
@ -187,7 +188,8 @@ public class DatabaseManager { |
|
|
|
|
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error setting user state for commands bot", e); |
|
|
|
// log.error("Error setting user state for commands bot", e);
|
|
|
|
System.out.println("Error setting user state for commands bot" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -202,7 +204,8 @@ public class DatabaseManager { |
|
|
|
status = result.getInt("status"); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting user state for command bot", e); |
|
|
|
// log.error("Error getting user state for command bot", e);
|
|
|
|
System.out.println("Error getting user state for command bot" + e); |
|
|
|
} |
|
|
|
return status == 1; |
|
|
|
} |
|
|
|
@ -217,7 +220,8 @@ public class DatabaseManager { |
|
|
|
|
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting file", e); |
|
|
|
// log.error("Error getting file", e);
|
|
|
|
System.out.println("Error getting file" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -233,7 +237,7 @@ public class DatabaseManager { |
|
|
|
} |
|
|
|
result.close(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting files for user", e); |
|
|
|
System.out.println("Error getting files for user" + e); |
|
|
|
} |
|
|
|
return files; |
|
|
|
} |
|
|
|
@ -246,7 +250,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setInt(2, status); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting user for file", e); |
|
|
|
System.out.println("Error getting user for file" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -259,7 +263,7 @@ public class DatabaseManager { |
|
|
|
|
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error deleting user file", e); |
|
|
|
System.out.println("Error deleting user file" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -274,7 +278,7 @@ public class DatabaseManager { |
|
|
|
status = result.getInt("status"); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting user status", e); |
|
|
|
System.out.println("Error getting user status" + e); |
|
|
|
} |
|
|
|
return status; |
|
|
|
} |
|
|
|
@ -287,7 +291,7 @@ public class DatabaseManager { |
|
|
|
final ResultSet result = preparedStatement.executeQuery(); |
|
|
|
exists = result.next(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error checking file", e); |
|
|
|
System.out.println("Error checking file" + e); |
|
|
|
} |
|
|
|
return exists; |
|
|
|
} |
|
|
|
@ -300,7 +304,7 @@ public class DatabaseManager { |
|
|
|
|
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error deleting files", e); |
|
|
|
System.out.println("Error deleting files" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -314,7 +318,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setString(3, cityName); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error adding recent weather", e); |
|
|
|
System.out.println("Error adding recent weather" + e); |
|
|
|
} |
|
|
|
cleanUpRecent(userId); |
|
|
|
return updatedRows > 0; |
|
|
|
@ -330,7 +334,7 @@ public class DatabaseManager { |
|
|
|
recentWeather.add(result.getString("cityName")); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting recent weather", e); |
|
|
|
System.out.println("Error getting recent weather" + e); |
|
|
|
} |
|
|
|
|
|
|
|
return recentWeather; |
|
|
|
@ -343,7 +347,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setLong(2, userId); |
|
|
|
preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error cleaning up recent user", e); |
|
|
|
System.out.println("Error cleaning up recent user" + e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@ -361,7 +365,7 @@ public class DatabaseManager { |
|
|
|
} |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error adding user for direction", e); |
|
|
|
System.out.println("Error adding user for direction" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -376,7 +380,7 @@ public class DatabaseManager { |
|
|
|
status = result.getInt("status"); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting user destination status", e); |
|
|
|
System.out.println("Error getting user destination status" + e); |
|
|
|
} |
|
|
|
return status; |
|
|
|
} |
|
|
|
@ -391,7 +395,7 @@ public class DatabaseManager { |
|
|
|
messageId = result.getInt("messageId"); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting user destination message id", e); |
|
|
|
System.out.println("Error getting user destination message id" + e); |
|
|
|
} |
|
|
|
return messageId; |
|
|
|
} |
|
|
|
@ -406,7 +410,7 @@ public class DatabaseManager { |
|
|
|
origin = result.getString("origin"); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error get user origin", e); |
|
|
|
System.out.println("Error get user origin" + e); |
|
|
|
} |
|
|
|
return origin; |
|
|
|
} |
|
|
|
@ -419,7 +423,7 @@ public class DatabaseManager { |
|
|
|
|
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error deleting user directions", e); |
|
|
|
System.out.println("Error deleting user directions" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -432,7 +436,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setInt(2, updateId); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error adding last update", e); |
|
|
|
System.out.println("Error adding last update" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -447,7 +451,7 @@ public class DatabaseManager { |
|
|
|
updateId = result.getInt("updateId"); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting last update", e); |
|
|
|
System.out.println("Error getting last update" + e); |
|
|
|
} |
|
|
|
return updateId; |
|
|
|
} |
|
|
|
@ -462,7 +466,7 @@ public class DatabaseManager { |
|
|
|
languageCode = result.getString("languageCode"); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting user language", e); |
|
|
|
System.out.println("Error getting user language" + e); |
|
|
|
} |
|
|
|
return languageCode; |
|
|
|
} |
|
|
|
@ -475,7 +479,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setString(2, language); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error updating user language", e); |
|
|
|
System.out.println("Error updating user language" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -491,7 +495,7 @@ public class DatabaseManager { |
|
|
|
state = result.getInt("state"); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting weather state", e); |
|
|
|
System.out.println("Error getting weather state" + e); |
|
|
|
} |
|
|
|
return state; |
|
|
|
} |
|
|
|
@ -505,7 +509,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setInt(3, state); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error insert weather state", e); |
|
|
|
System.out.println("Error insert weather state" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -521,7 +525,7 @@ public class DatabaseManager { |
|
|
|
cityId = result.getInt("cityId"); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting recent weather by city", e); |
|
|
|
System.out.println("Error getting recent weather by city" + e); |
|
|
|
} |
|
|
|
|
|
|
|
return cityId; |
|
|
|
@ -540,7 +544,7 @@ public class DatabaseManager { |
|
|
|
addNewUserWeatherOptions(userId); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting wether options", e); |
|
|
|
System.out.println("Error getting wether options" + e); |
|
|
|
} |
|
|
|
return options; |
|
|
|
} |
|
|
|
@ -552,7 +556,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setLong(1, userId); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error adding new user weather options", e); |
|
|
|
System.out.println("Error adding new user weather options" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -565,7 +569,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setLong(2, userId); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error updating weather language options", e); |
|
|
|
System.out.println("Error updating weather language options" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -578,7 +582,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setLong(2, userId); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error adding weather unit option", e); |
|
|
|
System.out.println("Error adding weather unit option" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -592,7 +596,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setString(3, cityName); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error creating weather alert", e); |
|
|
|
System.out.println("Error creating weather alert" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -607,7 +611,7 @@ public class DatabaseManager { |
|
|
|
alertCitiesNames.add(result.getString("cityName")); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting alerts by user", e); |
|
|
|
System.out.println("Error getting alerts by user" + e); |
|
|
|
} |
|
|
|
|
|
|
|
return alertCitiesNames; |
|
|
|
@ -621,7 +625,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setString(2, cityName); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error delete city alert", e); |
|
|
|
System.out.println("Error delete city alert" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -633,7 +637,7 @@ public class DatabaseManager { |
|
|
|
preparedStatement.setLong(1, userId); |
|
|
|
updatedRows = preparedStatement.executeUpdate(); |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error deleting alerts for user", e); |
|
|
|
System.out.println("Error deleting alerts for user" + e); |
|
|
|
} |
|
|
|
return updatedRows > 0; |
|
|
|
} |
|
|
|
@ -652,7 +656,7 @@ public class DatabaseManager { |
|
|
|
allAlerts.add(weatherAlert); |
|
|
|
} |
|
|
|
} catch (SQLException e) { |
|
|
|
log.error("Error getting all alerts", e); |
|
|
|
System.out.println("Error getting all alerts" + e); |
|
|
|
} |
|
|
|
|
|
|
|
return allAlerts; |
|
|
|
|