2014-07-09 04:55:51 +02:00
package mineplex.hub ;
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.sql.SQLException ;
2014-07-10 02:38:11 +02:00
import java.util.HashMap ;
2014-07-09 04:55:51 +02:00
2016-02-19 07:17:38 +01:00
import mineplex.serverdata.database.DBPool ;
2014-07-09 04:55:51 +02:00
public class HubRepository
{
private boolean _us = true ;
private static String CREATE_NEWS_TABLE = " CREATE TABLE IF NOT EXISTS newsList (id INT NOT NULL AUTO_INCREMENT, newsString VARCHAR(256), newsPosition INT, PRIMARY KEY (id)); " ;
private static String RETRIEVE_NEWS_ENTRIES = " SELECT newsString, newsPosition FROM newsList; " ;
2014-07-11 03:10:35 +02:00
private static String RETRIEVE_MAX_NEWS_POSITION = " SELECT MAX(newsPosition) AS newsPosition FROM newsList; " ;
2014-07-09 04:55:51 +02:00
private static String ADD_NEWS_ENTRY = " INSERT INTO newsList (newsString, newsPosition) VALUES(?,?); " ;
2014-07-16 04:31:53 +02:00
//private static String ADD_NEWS_ENTRY = "SET @max = (SELECT MAX(newsPosition) AS newsPosition FROM newsList);INSERT INTO newsList (newsString, newsPosition) VALUES(?,@max + 1);";
2014-07-09 04:55:51 +02:00
private static String SET_NEWS_ENTRY = " UPDATE newsList SET newsString = ? WHERE newsPosition = ?; " ;
private static String DELETE_NEWS_ENTRY = " DELETE FROM newsList WHERE newsPosition = ?; " ;
2014-07-11 03:10:35 +02:00
private static String RECALC_NEWS_POSITIONS = " UPDATE newsList SET newsPosition = newsPosition - 1 WHERE newsPosition > ?; " ;
2014-07-16 04:31:53 +02:00
//private static String DELETE_RECALC_NEWS_ENTRY = "SET @pos = ?;SET @max = (SELECT MAX(newsPosition) AS newsPosition FROM newsList);DELETE FROM newsList WHERE newsPosition = @pos;UPDATE newsList SET newsPosition = IF(@max <> @pos, newsPosition - 1, newsPosition) WHERE newsPosition > @pos;";
2014-07-09 04:55:51 +02:00
2014-08-25 01:26:01 +02:00
public void initialize ( boolean us )
2014-07-09 04:55:51 +02:00
{
_us = us ;
}
2014-07-10 02:38:11 +02:00
public HashMap < String , String > retrieveNewsEntries ( )
{
ResultSet resultSet = null ;
PreparedStatement preparedStatement = null ;
HashMap < String , String > newsEntries = new HashMap < String , String > ( ) ;
2015-12-18 06:32:07 +01:00
try ( Connection connection = DBPool . getMineplex ( ) . getConnection ( ) )
2014-07-10 02:38:11 +02:00
{
2014-10-29 23:35:59 +01:00
preparedStatement = connection . prepareStatement ( RETRIEVE_NEWS_ENTRIES ) ;
resultSet = preparedStatement . executeQuery ( ) ;
while ( resultSet . next ( ) )
2014-07-10 02:38:11 +02:00
{
2014-10-29 23:35:59 +01:00
newsEntries . put ( resultSet . getString ( 2 ) , resultSet . getString ( 1 ) ) ;
2014-07-10 02:38:11 +02:00
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
if ( resultSet ! = null )
{
try
{
resultSet . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
return newsEntries ;
}
public boolean setNewsEntry ( String newsEntry , int newsPosition )
{
int result = 0 ;
PreparedStatement preparedStatement = null ;
2014-10-29 23:35:59 +01:00
2015-12-18 06:32:07 +01:00
try ( Connection connection = DBPool . getMineplex ( ) . getConnection ( ) )
2014-07-10 02:38:11 +02:00
{
2014-10-29 23:35:59 +01:00
preparedStatement = connection . prepareStatement ( SET_NEWS_ENTRY ) ;
preparedStatement . setString ( 1 , newsEntry ) ;
preparedStatement . setInt ( 2 , newsPosition ) ;
result = preparedStatement . executeUpdate ( ) ;
2014-07-10 02:38:11 +02:00
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
return result ! = 0 ;
}
2014-07-11 03:10:35 +02:00
public int retrieveMaxNewsPosition ( )
{
int result = 0 ;
ResultSet resultSet = null ;
PreparedStatement preparedStatement = null ;
2014-10-29 23:35:59 +01:00
2015-12-18 06:32:07 +01:00
try ( Connection connection = DBPool . getMineplex ( ) . getConnection ( ) )
2014-07-11 03:10:35 +02:00
{
2014-10-29 23:35:59 +01:00
preparedStatement = connection . prepareStatement ( RETRIEVE_MAX_NEWS_POSITION ) ;
resultSet = preparedStatement . executeQuery ( ) ;
while ( resultSet . next ( ) )
2014-07-11 03:10:35 +02:00
{
2014-10-29 23:35:59 +01:00
result = Integer . parseInt ( resultSet . getString ( 1 ) ) ;
2014-07-11 03:10:35 +02:00
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
return result ;
}
public boolean addNewsEntry ( String newsEntry )
{
int result = 0 ;
int maxPos = retrieveMaxNewsPosition ( ) ;
PreparedStatement preparedStatement = null ;
2014-10-29 23:35:59 +01:00
2015-12-18 06:32:07 +01:00
try ( Connection connection = DBPool . getMineplex ( ) . getConnection ( ) )
2014-07-11 03:10:35 +02:00
{
2014-10-29 23:35:59 +01:00
preparedStatement = connection . prepareStatement ( ADD_NEWS_ENTRY ) ;
preparedStatement . setString ( 1 , newsEntry ) ;
preparedStatement . setInt ( 2 , maxPos + 1 ) ;
2014-07-11 03:10:35 +02:00
2014-10-29 23:35:59 +01:00
result = preparedStatement . executeUpdate ( ) ;
2014-07-11 03:10:35 +02:00
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
return result ! = 0 ;
}
public boolean deleteNewsEntry ( int newsPosition )
{
int result = 0 ;
int maxPos = retrieveMaxNewsPosition ( ) ;
PreparedStatement preparedStatement = null ;
2014-10-29 23:35:59 +01:00
2015-12-18 06:32:07 +01:00
try ( Connection connection = DBPool . getMineplex ( ) . getConnection ( ) )
2014-07-11 03:10:35 +02:00
{
2014-10-29 23:35:59 +01:00
//preparedStatement = connection.prepareStatement(DELETE_RECALC_NEWS_ENTRY);
preparedStatement = connection . prepareStatement ( DELETE_NEWS_ENTRY ) ;
preparedStatement . setInt ( 1 , newsPosition ) ;
result = preparedStatement . executeUpdate ( ) ;
if ( result ! = 0 & & maxPos ! = newsPosition )
2014-07-11 03:10:35 +02:00
{
2014-10-29 23:35:59 +01:00
preparedStatement . close ( ) ;
preparedStatement = connection . prepareStatement ( RECALC_NEWS_POSITIONS ) ;
2014-07-11 03:10:35 +02:00
preparedStatement . setInt ( 1 , newsPosition ) ;
2014-10-29 23:35:59 +01:00
result = preparedStatement . executeUpdate ( ) ;
2014-07-11 03:10:35 +02:00
}
}
catch ( Exception exception )
{
exception . printStackTrace ( ) ;
}
finally
{
if ( preparedStatement ! = null )
{
try
{
preparedStatement . close ( ) ;
}
catch ( SQLException e )
{
e . printStackTrace ( ) ;
}
}
}
return result ! = 0 ;
}
2014-07-09 04:55:51 +02:00
}