diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/Servlet.java b/src/main/java/fr/geoffrey/medical_training_tracker/Servlet.java index 718b87b..4b652d3 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/Servlet.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/Servlet.java @@ -5,6 +5,12 @@ import fr.geoffrey.medical_training_tracker.dao.ICareDao; import fr.geoffrey.medical_training_tracker.dao.IConsentBehaviorDao; import fr.geoffrey.medical_training_tracker.dao.IProgressionDao; import fr.geoffrey.medical_training_tracker.dao.IUserDao; +import fr.geoffrey.medical_training_tracker.dao.bdd.BddConnectionManager; +import fr.geoffrey.medical_training_tracker.dao.bdd.postgres.AnimalDao; +import fr.geoffrey.medical_training_tracker.dao.bdd.postgres.CareDao; +import fr.geoffrey.medical_training_tracker.dao.bdd.postgres.ConsentBehaviorDao; +import fr.geoffrey.medical_training_tracker.dao.bdd.postgres.ProgressionDao; +import fr.geoffrey.medical_training_tracker.dao.bdd.postgres.UserBddDao; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.springframework.beans.factory.annotation.Autowired; @@ -17,6 +23,9 @@ import org.springframework.web.context.support.WebApplicationContextUtils; */ public class Servlet implements ServletContextListener { + @Autowired + BddConnectionManager bddConnectionManager; + @Autowired IUserDao userDao; @@ -36,19 +45,40 @@ public class Servlet implements ServletContextListener { public void contextInitialized(ServletContextEvent sce) { AutowireCapableBeanFactory autowireCapableBeanFactory = WebApplicationContextUtils.getRequiredWebApplicationContext(sce.getServletContext()).getAutowireCapableBeanFactory(); autowireCapableBeanFactory.autowireBean(this); + + bddConnectionManager.start(); + if (userDao instanceof UserBddDao) { + ((UserBddDao) userDao).setBddConnectionManager(bddConnectionManager); + } if (!userDao.isStorageExist()) { userDao.createStorage(); } + + if (animalDao instanceof AnimalDao) { + ((AnimalDao) animalDao).setBddConnectionManager(bddConnectionManager); + } if (!animalDao.isStorageExist()) { animalDao.createStorage(); } + + if (careDao instanceof CareDao) { + ((CareDao) careDao).setBddConnectionManager(bddConnectionManager); + } if (!careDao.isStorageExist()) { careDao.createStorage(); } + + if (consentBehaviorDao instanceof ConsentBehaviorDao) { + ((ConsentBehaviorDao) consentBehaviorDao).setBddConnectionManager(bddConnectionManager); + } if (!consentBehaviorDao.isStorageExist()) { consentBehaviorDao.createStorage(); } + + if (progressionDao instanceof ProgressionDao) { + ((ProgressionDao) progressionDao).setBddConnectionManager(bddConnectionManager); + } if (!progressionDao.isStorageExist()) { progressionDao.createStorage(); } @@ -56,5 +86,8 @@ public class Servlet implements ServletContextListener { @Override public void contextDestroyed(ServletContextEvent sce) { + if (bddConnectionManager != null) { + bddConnectionManager.close(); + } } } diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/config/SecSecurityConfig.java b/src/main/java/fr/geoffrey/medical_training_tracker/config/SecSecurityConfig.java index 38496ed..3b82a82 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/config/SecSecurityConfig.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/config/SecSecurityConfig.java @@ -30,8 +30,6 @@ import fr.geoffrey.medical_training_tracker.config.encoder.Encoder; import fr.geoffrey.medical_training_tracker.controller.IndexController; import fr.geoffrey.medical_training_tracker.dao.bean.User; import fr.geoffrey.medical_training_tracker.dao.IUserDao; -import org.springframework.security.web.authentication.rememberme.InMemoryTokenRepositoryImpl; -import org.springframework.security.web.authentication.rememberme.PersistentTokenRepository; @Configuration @EnableWebSecurity diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/BddConnectionManager.java b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/BddConnectionManager.java new file mode 100644 index 0000000..c4692b1 --- /dev/null +++ b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/BddConnectionManager.java @@ -0,0 +1,179 @@ +package fr.geoffrey.medical_training_tracker.dao.bdd; + +import fr.geoffrey.medical_training_tracker.controller.IndexController; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Map; +import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * + * @author Geoffrey + */ +public abstract class BddConnectionManager extends Thread { + + private boolean run = true; + private long tempoCheckDataSourceConnection = 0; + private static final long TIMER_CHECK_DATA_SOURCE_CONNECTION_OK_MS = 10 * 60 * 1_000; // 10 min + private static final long TIMER_CHECK_DATA_SOURCE_CONNECTION_NOK_MS = 30_000; // 30 sec + private final Object sync = new Object(); + @Autowired + private DataSource _dataSource; + private Connection _dataSourceConnection; + + public void setDataSource(DataSource mDataSource) { + synchronized (sync) { + this._dataSource = mDataSource; + } + } + + @Override + public void start() { + checkDataSourceConnection(); + super.start(); + } + + protected Connection getDataSourceConnection() { + Connection ret; + synchronized (sync) { + try { + ret = _dataSource.getConnection(); + } catch (SQLException ex) { + ret = null; + System.err.println(IndexController.LOG_TAG + " SQLException -> getDataSourceConnection() - Failed get connection - " + ex.getMessage()); + } + } + return ret; + } + + @Override + public void run() { + while (run) { + long now = System.currentTimeMillis(); + if (tempoCheckDataSourceConnection == 0 || (tempoCheckDataSourceConnection - now <= 0)) { + checkDataSourceConnection(); + } + try { + Thread.sleep(1_000); + } catch (InterruptedException ex) { + } + } + } + + public void close() { + run = false; + synchronized (sync) { + if (_dataSourceConnection != null) { + try { + _dataSourceConnection.close(); + } catch (SQLException ex) { + System.err.println(IndexController.LOG_TAG + " SQLException -> close() - Close BDD connection - " + ex.getMessage()); + } + } + } + } + + private void checkDataSourceConnection() { + boolean mDataSourceIsNull; + synchronized (sync) { + mDataSourceIsNull = _dataSource == null; + } + if (mDataSourceIsNull) { + return; + } + long now = System.currentTimeMillis(); + Connection connection; + synchronized (sync) { + connection = _checkDataSourceConnection(_dataSourceConnection); + if (connection == null) { + _dataSourceConnection = null; + } else if (connection != null && connection != _dataSourceConnection) { + _dataSourceConnection = connection; + } + } + if (connection != null) { + tempoCheckDataSourceConnection = now + TIMER_CHECK_DATA_SOURCE_CONNECTION_OK_MS; + } else { + tempoCheckDataSourceConnection = now + TIMER_CHECK_DATA_SOURCE_CONNECTION_NOK_MS; + } + } + + /** + * Check data connection. + * @param connection connection to check. + * @return connection if connected, null otherwise. + */ + protected abstract Connection _checkDataSourceConnection(Connection connection); + + public Connection getConnection() { + Connection ret; + synchronized (sync) { + ret = _dataSourceConnection; + } + return ret; + } + + protected PreparedStatement preareStatement(String sql, Map params) { + PreparedStatement statement; + try { + synchronized (sync) { + statement = _dataSourceConnection.prepareStatement(sql); + } + if (params != null) { + for (Integer index : params.keySet()) { + Object param = params.get(index); + if (param instanceof String) { + statement.setString(index, (String) param); + } else if (param instanceof Integer) { + statement.setInt(index, (int) param); + } + } + } + System.out.println(IndexController.LOG_TAG + " SQL : "+statement.toString()); + } catch (SQLException ex) { + System.err.println(IndexController.LOG_TAG + " SQLException -> preareStatement() - " + ex.getMessage()); + statement = null; + tempoCheckDataSourceConnection = 0; + } + return statement; + } + + public ResultSet executeQuery(String sql, Map params) { + ResultSet resultSet = null; + boolean isDataSourceConnectionNull; + synchronized (sync) { + isDataSourceConnectionNull = _dataSourceConnection == null; + } + if (!isDataSourceConnectionNull) { + PreparedStatement statement = preareStatement(sql, params); + try { + resultSet = statement.executeQuery(); + } catch (SQLException ex) { + System.err.println(IndexController.LOG_TAG + " SQLException -> executeQuery() - " + ex.getMessage()); + tempoCheckDataSourceConnection = 0; + } + } + return resultSet; + } + + public int executeUpdate(String sql, Map params) { + int ret = -1; + boolean isDataSourceConnectionNull; + synchronized (sync) { + isDataSourceConnectionNull = _dataSourceConnection == null; + } + if (!isDataSourceConnectionNull) { + PreparedStatement statement = preareStatement(sql, params); + try { + ret = statement.executeUpdate(); + } catch (SQLException ex) { + System.err.println(IndexController.LOG_TAG + " SQLException -> executeQuery() - " + ex.getMessage()); + tempoCheckDataSourceConnection = 0; + } + } + return ret; + } +} diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/SqlDao.java b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/SqlDao.java index e1ea2d3..aa13ecc 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/SqlDao.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/SqlDao.java @@ -1,12 +1,10 @@ package fr.geoffrey.medical_training_tracker.dao.bdd; -import javax.sql.DataSource; - public class SqlDao { - protected DataSource mDataSource; + protected BddConnectionManager bddConnectionManager; - public void setDataSource(DataSource mDataSource) { - this.mDataSource = mDataSource; + public void setBddConnectionManager(BddConnectionManager bddConnectionManager) { + this.bddConnectionManager = bddConnectionManager; } } diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/AnimalDao.java b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/AnimalDao.java index 2db08f0..9bd4263 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/AnimalDao.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/AnimalDao.java @@ -3,12 +3,12 @@ package fr.geoffrey.medical_training_tracker.dao.bdd.postgres; import fr.geoffrey.medical_training_tracker.controller.IndexController; import fr.geoffrey.medical_training_tracker.dao.IAnimalDao; import fr.geoffrey.medical_training_tracker.dao.bean.Animal; -import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @@ -34,17 +34,15 @@ public class AnimalDao extends PostgresSqlDao implements IAnimalDao { public Animal getAnimal(String owner, int animalId) { Animal animal = null; - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("SELECT * FROM "+TABLE_NAME+" WHERE owner = ? AND id = ?;"); - statement.setString(1, owner); - statement.setInt(2, animalId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); + Map params = new HashMap<>(); + String sql = "SELECT * FROM "+TABLE_NAME+" WHERE owner = ? AND id = ?;"; + params.put(1, owner); + params.put(2, animalId); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { animal = new Animal(); @@ -52,7 +50,6 @@ public class AnimalDao extends PostgresSqlDao implements IAnimalDao { animal.setName(resultSet.getString("name")); animal.setOwner(resultSet.getString("owner")); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> getAnimal()"); @@ -64,20 +61,6 @@ public class AnimalDao extends PostgresSqlDao implements IAnimalDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> getAnimal()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> getAnimal()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> getAnimal()"); - } - } } return animal; } @@ -86,16 +69,13 @@ public class AnimalDao extends PostgresSqlDao implements IAnimalDao { public List getAnimals(String owner) { List ret = new ArrayList<>(); - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("SELECT * FROM "+TABLE_NAME+" WHERE owner = ? ORDER BY name ASC;"); - statement.setString(1, owner); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); + Map params = new HashMap<>(); + String sql = "SELECT * FROM "+TABLE_NAME+" WHERE owner = ? ORDER BY name ASC;"; + params.put(1, owner); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + try { if (resultSet != null) { while (resultSet.next()) { Animal animal = new Animal(); @@ -105,7 +85,6 @@ public class AnimalDao extends PostgresSqlDao implements IAnimalDao { ret.add(animal); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> getAnimals()"); @@ -117,122 +96,42 @@ public class AnimalDao extends PostgresSqlDao implements IAnimalDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> getAnimals()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> getAnimals()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> getAnimals()"); - } - } } return ret; } @Override public void addAnimal(String name, String owner) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("INSERT INTO "+TABLE_NAME+" (id, name, owner) VALUES (nextval(?), ?, ?);"); - statement.setString(1, SEQUENCE_NAME); - statement.setString(2, name); - statement.setString(3, owner); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> addAnimal()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> addAnimal()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> addAnimal()"); - } - } - } + Map params = new HashMap<>(); + String sql = "INSERT INTO "+TABLE_NAME+" (id, name, owner) VALUES (nextval(?), ?, ?);"; + + params.put(1, SEQUENCE_NAME); + params.put(2, name); + params.put(3, owner); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void deleteAnimal(int animalId, String owner) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("DELETE FROM "+TABLE_NAME+" WHERE id = ? AND owner = ?;"); - statement.setInt(1, animalId); - statement.setString(2, owner); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> deleteAnimal()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> deleteAnimal()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> deleteAnimal()"); - } - } - } + Map params = new HashMap<>(); + String sql = "DELETE FROM "+TABLE_NAME+" WHERE id = ? AND owner = ?;"; + + params.put(1, animalId); + params.put(2, owner); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void changeName(int animalId, String owner, String name) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("UPDATE "+TABLE_NAME+" SET name = ? WHERE id = ? AND owner = ?;"); - statement.setString(1, name); - statement.setInt(2, animalId); - statement.setString(3, owner); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> changeName()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> changeName()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> changeName()"); - } - } - } + Map params = new HashMap<>(); + String sql = "UPDATE "+TABLE_NAME+" SET name = ? WHERE id = ? AND owner = ?;"; + + params.put(1, name); + params.put(2, animalId); + params.put(3, owner); + + bddConnectionManager.executeUpdate(sql, params); } } diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/CareDao.java b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/CareDao.java index d58fea3..6221eb3 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/CareDao.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/CareDao.java @@ -3,12 +3,12 @@ package fr.geoffrey.medical_training_tracker.dao.bdd.postgres; import fr.geoffrey.medical_training_tracker.controller.IndexController; import fr.geoffrey.medical_training_tracker.dao.ICareDao; import fr.geoffrey.medical_training_tracker.dao.bean.Care; -import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @@ -34,21 +34,19 @@ public class CareDao extends PostgresSqlDao implements ICareDao { public List getAllAnimalCares(String owner, int animalId) { List ret = new ArrayList<>(); - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + TABLE_NAME+".id as care_id, "+TABLE_NAME+".name as care_name, "+TABLE_NAME+".description as care_desc " + "from "+TABLE_NAME+" " + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? ORDER BY "+TABLE_NAME+".name ASC;"); - statement.setString(1, owner); - statement.setInt(2, animalId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? ORDER BY "+TABLE_NAME+".name ASC;"; + params.put(1, owner); + params.put(2, animalId); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { Care care = new Care(); @@ -57,7 +55,6 @@ public class CareDao extends PostgresSqlDao implements ICareDao { care.setDescription(resultSet.getString("care_desc")); ret.add(care); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> getAllAnialCares() - " + ex.getMessage()); @@ -70,20 +67,6 @@ public class CareDao extends PostgresSqlDao implements ICareDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> getAllAnialCares()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> getAllAnialCares()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> getAllAnialCares()"); - } - } } return ret; } @@ -92,22 +75,20 @@ public class CareDao extends PostgresSqlDao implements ICareDao { public Care getAnimalCare(String owner, int animalId, int careId) { Care ret = null; - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + TABLE_NAME+".id as care_id, "+TABLE_NAME+".name as care_name, "+TABLE_NAME+".description as care_desc " + "from "+TABLE_NAME+" " + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".id = ?;"); - statement.setString(1, owner); - statement.setInt(2, animalId); - statement.setInt(3, careId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".id = ?;"; + params.put(1, owner); + params.put(2, animalId); + params.put(3, careId); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { ret = new Care(); @@ -115,7 +96,6 @@ public class CareDao extends PostgresSqlDao implements ICareDao { ret.setName(resultSet.getString("care_name")); ret.setDescription(resultSet.getString("care_desc")); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> getAnimalCare() - " + ex.getMessage()); @@ -128,167 +108,66 @@ public class CareDao extends PostgresSqlDao implements ICareDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> getAnimalCare()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> getAnimalCare()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> getAnimalCare()"); - } - } } return ret; } @Override public void addCare(int animalId, String name, String description) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - if (description != null) { - statement = conn.prepareStatement("INSERT INTO "+TABLE_NAME+" (id, animal, name, description) VALUES (nextval(?), ?, ?, ?);"); - statement.setString(4, description); - } else { - statement = conn.prepareStatement("INSERT INTO "+TABLE_NAME+" (id, animal, name) VALUES (nextval(?), ?, ?);"); - } - statement.setString(1, SEQUENCE_NAME); - statement.setInt(2, animalId); - statement.setString(3, name); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> addCare()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> addCare()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> addCare()"); - } - } + Map params = new HashMap<>(); + String sql; + if (description != null) { + sql = "INSERT INTO "+TABLE_NAME+" (id, animal, name, description) VALUES (nextval(?), ?, ?, ?);"; + params.put(4, description); + } else { + sql = "INSERT INTO "+TABLE_NAME+" (id, animal, name) VALUES (nextval(?), ?, ?);"; } + + params.put(1, SEQUENCE_NAME); + params.put(2, animalId); + params.put(3, name); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void deleteCare(int careId, int animalId, String owner) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("DELETE FROM "+TABLE_NAME+" WHERE animal = (" + Map params = new HashMap<>(); + String sql = "DELETE FROM "+TABLE_NAME+" WHERE animal = (" + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?) " - + "AND id = ?"); - statement.setString(1, owner); - statement.setInt(2, animalId); - statement.setInt(3, careId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> deleteCare()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> deleteCare()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> deleteCare()"); - } - } - } + + "AND id = ?"; + + params.put(1, owner); + params.put(2, animalId); + params.put(3, careId); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void deleteAllAnimalCares(int animalId, String owner) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("DELETE FROM "+TABLE_NAME+" WHERE animal = (" - + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?)"); - statement.setString(1, owner); - statement.setInt(2, animalId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> deleteAllAnimalCares()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> deleteAllAnimalCares()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> deleteAllAnimalCares()"); - } - } - } + Map params = new HashMap<>(); + String sql = "DELETE FROM "+TABLE_NAME+" WHERE animal = (" + + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?)"; + + params.put(1, owner); + params.put(2, animalId); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void updateCare(int careId, int animalId, String owner, String name, String description) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("UPDATE "+TABLE_NAME+" SET name = ?, description = ? WHERE id = ? AND animal = (" - + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?)"); - statement.setString(1, name); - statement.setString(2, description); - statement.setInt(3, careId); - statement.setString(4, owner); - statement.setInt(5, animalId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> updateCare()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> updateCare()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> updateCare()"); - } - } - } + Map params = new HashMap<>(); + String sql = "UPDATE "+TABLE_NAME+" SET name = ?, description = ? WHERE id = ? AND animal = (" + + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?)"; + + params.put(1, name); + params.put(2, description); + params.put(3, careId); + params.put(4, owner); + params.put(5, animalId); + + bddConnectionManager.executeUpdate(sql, params); } } diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/ConsentBehaviorDao.java b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/ConsentBehaviorDao.java index 638c4cf..c001519 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/ConsentBehaviorDao.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/ConsentBehaviorDao.java @@ -3,12 +3,12 @@ package fr.geoffrey.medical_training_tracker.dao.bdd.postgres; import fr.geoffrey.medical_training_tracker.controller.IndexController; import fr.geoffrey.medical_training_tracker.dao.IConsentBehaviorDao; import fr.geoffrey.medical_training_tracker.dao.bean.ConsentBehavior; -import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; /** * @@ -34,21 +34,19 @@ public class ConsentBehaviorDao extends PostgresSqlDao implements IConsentBehavi public List getAllAnialConsentBehavior(String owner, int animalId) { List ret = new ArrayList<>(); - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + TABLE_NAME+".id as cb_id, "+TABLE_NAME+".name as cb_name, "+TABLE_NAME+".description as cb_desc " + "from "+TABLE_NAME+" " + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? ORDER BY "+TABLE_NAME+".name ASC;"); - statement.setString(1, owner); - statement.setInt(2, animalId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? ORDER BY "+TABLE_NAME+".name ASC;"; + params.put(1, owner); + params.put(2, animalId); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { ConsentBehavior consentBehavior = new ConsentBehavior(); @@ -57,7 +55,6 @@ public class ConsentBehaviorDao extends PostgresSqlDao implements IConsentBehavi consentBehavior.setDescription(resultSet.getString("cb_desc")); ret.add(consentBehavior); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> getAllAnialConsentBehavior() - " + ex.getMessage()); @@ -70,20 +67,6 @@ public class ConsentBehaviorDao extends PostgresSqlDao implements IConsentBehavi System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> getAllAnialConsentBehavior()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> getAllAnialConsentBehavior()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> getAllAnialConsentBehavior()"); - } - } } return ret; } @@ -92,22 +75,20 @@ public class ConsentBehaviorDao extends PostgresSqlDao implements IConsentBehavi public ConsentBehavior getAnimalConsentBehavior(String owner, int animalId, int consentBehaviorId) { ConsentBehavior ret = null; - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + TABLE_NAME+".id as cb_id, "+TABLE_NAME+".name as cb_name, "+TABLE_NAME+".description as cb_desc " + "from "+TABLE_NAME+" " + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".id = ?;"); - statement.setString(1, owner); - statement.setInt(2, animalId); - statement.setInt(3, consentBehaviorId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".id = ?;"; + params.put(1, owner); + params.put(2, animalId); + params.put(3, consentBehaviorId); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { ret = new ConsentBehavior(); @@ -115,7 +96,6 @@ public class ConsentBehaviorDao extends PostgresSqlDao implements IConsentBehavi ret.setName(resultSet.getString("cb_name")); ret.setDescription(resultSet.getString("cb_desc")); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> getAnimalConsentBehavior() - " + ex.getMessage()); @@ -128,167 +108,66 @@ public class ConsentBehaviorDao extends PostgresSqlDao implements IConsentBehavi System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> getAnimalConsentBehavior()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> getAnimalConsentBehavior()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> getAnimalConsentBehavior()"); - } - } } return ret; } @Override public void addConsentBehavior(int animalId, String name, String description) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - if (description != null) { - statement = conn.prepareStatement("INSERT INTO "+TABLE_NAME+" (id, animal, name, description) VALUES (nextval(?), ?, ?, ?);"); - statement.setString(4, description); - } else { - statement = conn.prepareStatement("INSERT INTO "+TABLE_NAME+" (id, animal, name) VALUES (nextval(?), ?, ?);"); - } - statement.setString(1, SEQUENCE_NAME); - statement.setInt(2, animalId); - statement.setString(3, name); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> addConsentBehavior()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> addConsentBehavior()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> addConsentBehavior()"); - } - } + Map params = new HashMap<>(); + String sql; + if (description != null) { + sql = "INSERT INTO "+TABLE_NAME+" (id, animal, name, description) VALUES (nextval(?), ?, ?, ?);"; + params.put(4, description); + } else { + sql = "INSERT INTO "+TABLE_NAME+" (id, animal, name) VALUES (nextval(?), ?, ?);"; } + + params.put(1, SEQUENCE_NAME); + params.put(2, animalId); + params.put(3, name); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void deleteConsentBehavior(int consentBehaviorId, int animalId, String owner) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("DELETE FROM "+TABLE_NAME+" WHERE animal = (" + Map params = new HashMap<>(); + String sql = "DELETE FROM "+TABLE_NAME+" WHERE animal = (" + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?) " - + "AND id = ?"); - statement.setString(1, owner); - statement.setInt(2, animalId); - statement.setInt(3, consentBehaviorId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> deleteCare()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> deleteCare()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> deleteCare()"); - } - } - } + + "AND id = ?;"; + + params.put(1, owner); + params.put(2, animalId); + params.put(3, consentBehaviorId); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void deleteAllAnimalConsentBehavior(int animalId, String owner) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("DELETE FROM "+TABLE_NAME+" WHERE animal = (" - + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?)"); - statement.setString(1, owner); - statement.setInt(2, animalId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> deleteAllAnimalCares()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> deleteAllAnimalCares()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> deleteAllAnimalCares()"); - } - } - } + Map params = new HashMap<>(); + String sql = "DELETE FROM "+TABLE_NAME+" WHERE animal = (" + + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?);"; + + params.put(1, owner); + params.put(2, animalId); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void updateConsentBehavior(int consentBehaviorId, int animalId, String owner, String name, String description) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("UPDATE "+TABLE_NAME+" SET name = ?, description = ? WHERE id = ? AND animal = (" - + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?)"); - statement.setString(1, name); - statement.setString(2, description); - statement.setInt(3, consentBehaviorId); - statement.setString(4, owner); - statement.setInt(5, animalId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> updateConsentBehavior()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> updateConsentBehavior()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> updateConsentBehavior()"); - } - } - } + Map params = new HashMap<>(); + String sql = "UPDATE "+TABLE_NAME+" SET name = ?, description = ? WHERE id = ? AND animal = (" + + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?);"; + + params.put(1, name); + params.put(2, description); + params.put(3, consentBehaviorId); + params.put(4, owner); + params.put(5, animalId); + + bddConnectionManager.executeUpdate(sql, params); } } diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/PostgresConnectionManager.java b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/PostgresConnectionManager.java new file mode 100644 index 0000000..8904233 --- /dev/null +++ b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/PostgresConnectionManager.java @@ -0,0 +1,53 @@ +package fr.geoffrey.medical_training_tracker.dao.bdd.postgres; + +import fr.geoffrey.medical_training_tracker.controller.IndexController; +import fr.geoffrey.medical_training_tracker.dao.bdd.BddConnectionManager; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class PostgresConnectionManager extends BddConnectionManager { + + @Override + protected Connection _checkDataSourceConnection(Connection connection) { + Connection ret = null; + Connection _connection = null; + + if (connection == null) { + _connection = getDataSourceConnection(); + } else { + _connection = connection; + } + + PreparedStatement statement = null; + ResultSet resultSet = null; + if (_connection != null) { + try { + statement = _connection.prepareStatement("SELECT version();"); + resultSet = statement.executeQuery(); + ret = _connection; + } catch (SQLException ex) { + ret = null; + System.err.println(IndexController.LOG_TAG + " SQLException -> _checkDataSourceConnection() - " + ex.getMessage()); + ex.printStackTrace(); + } finally { + if (resultSet != null) { + try { + resultSet.close(); + } catch (SQLException ex) { + System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> _checkDataSourceConnection()"); + } + } + if (statement != null) { + try { + statement.close(); + } catch (SQLException ex) { + System.err.println(IndexController.LOG_TAG + " Failed close statement -> _checkDataSourceConnection()"); + } + } + } + } + return ret; + } +} diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/PostgresSqlDao.java b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/PostgresSqlDao.java index 66e610f..e811643 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/PostgresSqlDao.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/PostgresSqlDao.java @@ -2,10 +2,10 @@ package fr.geoffrey.medical_training_tracker.dao.bdd.postgres; import fr.geoffrey.medical_training_tracker.controller.IndexController; import fr.geoffrey.medical_training_tracker.dao.bdd.SqlDao; -import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; /** * @@ -16,20 +16,15 @@ public class PostgresSqlDao extends SqlDao { protected boolean _isTableExist(String tableName) { boolean ret = false; - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select tableowner from pg_tables where tablename=?;"); - statement.setString(1, tableName); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); + Map params = new HashMap<>(); + String sql = "select tableowner from pg_tables where tablename = ?;"; + params.put(1, tableName); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { ret = resultSet != null && resultSet.next(); - if (resultSet != null) { - resultSet.close(); - } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> _isTableExist()"); System.err.println(ex.getMessage()); @@ -42,86 +37,16 @@ public class PostgresSqlDao extends SqlDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> _isTableExist()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> _isTableExist()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> _isTableExist()"); - } - } } return ret; } protected void _createTable(String sql) { - Connection conn = null; - PreparedStatement statementCreateTable = null; - - try { - conn = mDataSource.getConnection(); - - statementCreateTable = conn.prepareStatement(sql); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statementCreateTable.toString()); - statementCreateTable.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> _createTable()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statementCreateTable != null) { - try { - statementCreateTable.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> _createTable()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> _createTable()"); - } - } - } + bddConnectionManager.executeUpdate(sql, null); } protected void _createSequence(String sequenceName) { - Connection conn = null; - PreparedStatement statementCreateSequence = null; - - try { - conn = mDataSource.getConnection(); - - statementCreateSequence = conn.prepareStatement("CREATE SEQUENCE " + sequenceName + " MINVALUE 0 CACHE 5 START 1;"); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statementCreateSequence.toString()); - statementCreateSequence.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> _createSequence()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statementCreateSequence != null) { - try { - statementCreateSequence.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> _createSequence()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> _createSequence()"); - } - } - } + bddConnectionManager.executeUpdate("CREATE SEQUENCE " + sequenceName + " MINVALUE 0 CACHE 5 START 1;", null); } } diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/ProgressionDao.java b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/ProgressionDao.java index dd5d2d5..59d5904 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/ProgressionDao.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/ProgressionDao.java @@ -6,10 +6,10 @@ import fr.geoffrey.medical_training_tracker.dao.bean.Animal; import fr.geoffrey.medical_training_tracker.dao.bean.Care; import fr.geoffrey.medical_training_tracker.dao.bean.ConsentBehavior; import fr.geoffrey.medical_training_tracker.dao.bean.Progression; -import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; /** * @@ -34,23 +34,21 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { // @Override public void fillAnimalProgression(Animal animal) { - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + AnimalDao.TABLE_NAME+".id as animal_id, "+AnimalDao.TABLE_NAME+".name as animal_name, "+AnimalDao.TABLE_NAME+".owner as animal_owner, " + TABLE_NAME+".id as progression_id, "+TABLE_NAME+".progression_by_me as prog_by_me, "+TABLE_NAME+".progression_by_someone_else as prog_by_someone_else, "+TABLE_NAME+".progression_by_veterinary as prog_by_vet, " + TABLE_NAME+".care as care_id, "+TABLE_NAME+".consent_behavior as consent_behavior_id " - + "from "+TABLE_NAME+" " - + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ?;"); - statement.setString(1, animal.getOwner()); - statement.setInt(2, animal.getId()); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); - + + "from "+TABLE_NAME+" " + + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ?;"; + + params.put(1, animal.getOwner()); + params.put(2, animal.getId()); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { Progression progression = new Progression(); @@ -71,7 +69,6 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { animal.addProgression(progression); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> getProgression() - " + ex.getMessage()); @@ -84,31 +81,13 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> getProgression()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> getProgression()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> getProgression()"); - } - } } } @Override public void fillAnimalProgressionByCare(Animal animal, int careId) { - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + AnimalDao.TABLE_NAME+".id as animal_id, "+AnimalDao.TABLE_NAME+".name as animal_name, "+AnimalDao.TABLE_NAME+".owner as animal_owner, " + TABLE_NAME+".id as progression_id, "+TABLE_NAME+".progression_by_me as prog_by_me, "+TABLE_NAME+".progression_by_someone_else as prog_by_someone_else, "+TABLE_NAME+".progression_by_veterinary as prog_by_vet, " + TABLE_NAME+".care as care_id, "+TABLE_NAME+".consent_behavior as consent_behavior_id, " @@ -116,13 +95,15 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { + "from "+TABLE_NAME+" " + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " + "left join "+ConsentBehaviorDao.TABLE_NAME+" on "+TABLE_NAME+".consent_behavior = "+ConsentBehaviorDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".care = ? ORDER BY "+ConsentBehaviorDao.TABLE_NAME+".name ASC;"); - statement.setString(1, animal.getOwner()); - statement.setInt(2, animal.getId()); - statement.setInt(3, careId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); - + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".care = ? ORDER BY "+ConsentBehaviorDao.TABLE_NAME+".name ASC;"; + + params.put(1, animal.getOwner()); + params.put(2, animal.getId()); + params.put(3, careId); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { Progression progression = new Progression(); @@ -148,7 +129,6 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { animal.addProgression(progression); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> fillAnimalProgressionByCare() - " + ex.getMessage()); @@ -161,31 +141,13 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> fillAnimalProgressionByCare()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> fillAnimalProgressionByCare()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> fillAnimalProgressionByCare()"); - } - } } } @Override public void fillAnimalProgressionByConsentBehavior(Animal animal, int consentBehaviorId) { - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + AnimalDao.TABLE_NAME+".id as animal_id, "+AnimalDao.TABLE_NAME+".name as animal_name, "+AnimalDao.TABLE_NAME+".owner as animal_owner, " + TABLE_NAME+".id as progression_id, "+TABLE_NAME+".progression_by_me as prog_by_me, "+TABLE_NAME+".progression_by_someone_else as prog_by_someone_else, "+TABLE_NAME+".progression_by_veterinary as prog_by_vet, " + TABLE_NAME+".care as care_id, "+TABLE_NAME+".consent_behavior as consent_behavior_id, " @@ -193,13 +155,15 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { + "from "+TABLE_NAME+" " + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " + "left join "+CareDao.TABLE_NAME+" on "+TABLE_NAME+".care = "+CareDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".consent_behavior = ? ORDER BY "+CareDao.TABLE_NAME+".name ASC;"); - statement.setString(1, animal.getOwner()); - statement.setInt(2, animal.getId()); - statement.setInt(3, consentBehaviorId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); - + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".consent_behavior = ? ORDER BY "+CareDao.TABLE_NAME+".name ASC;"; + + params.put(1, animal.getOwner()); + params.put(2, animal.getId()); + params.put(3, consentBehaviorId); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { Progression progression = new Progression(); @@ -225,7 +189,6 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { animal.addProgression(progression); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> fillAnimalProgressionByConsentBehavior() - " + ex.getMessage()); @@ -238,31 +201,13 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> fillAnimalProgressionByConsentBehavior()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> fillAnimalProgressionByConsentBehavior()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> fillAnimalProgressionByConsentBehavior()"); - } - } } } @Override public void fillAnimalProgressionByMe(Animal animal, String progressionByMe) { - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + AnimalDao.TABLE_NAME+".id as animal_id, "+AnimalDao.TABLE_NAME+".name as animal_name, "+AnimalDao.TABLE_NAME+".owner as animal_owner, " + TABLE_NAME+".id as progression_id, "+TABLE_NAME+".progression_by_me as prog_by_me, "+TABLE_NAME+".progression_by_someone_else as prog_by_someone_else, "+TABLE_NAME+".progression_by_veterinary as prog_by_vet, " + TABLE_NAME+".care as care_id, "+TABLE_NAME+".consent_behavior as consent_behavior_id, " @@ -272,13 +217,15 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " + "left join "+CareDao.TABLE_NAME+" on "+TABLE_NAME+".care = "+CareDao.TABLE_NAME+".id " + "left join "+ConsentBehaviorDao.TABLE_NAME+" on "+TABLE_NAME+".consent_behavior = "+ConsentBehaviorDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".progression_by_me = ?;"); - statement.setString(1, animal.getOwner()); - statement.setInt(2, animal.getId()); - statement.setString(3, progressionByMe); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); - + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".progression_by_me = ?;"; + + params.put(1, animal.getOwner()); + params.put(2, animal.getId()); + params.put(3, progressionByMe); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { Progression progression = new Progression(); @@ -309,7 +256,6 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { animal.addProgression(progression); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> fillAnimalProgressionByMe() - " + ex.getMessage()); @@ -322,31 +268,13 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> fillAnimalProgressionByMe()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> fillAnimalProgressionByMe()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> fillAnimalProgressionByMe()"); - } - } } } @Override public void fillAnimalProgressionBySomeoneElse(Animal animal, String progressionBySomeoneElse) { - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + AnimalDao.TABLE_NAME+".id as animal_id, "+AnimalDao.TABLE_NAME+".name as animal_name, "+AnimalDao.TABLE_NAME+".owner as animal_owner, " + TABLE_NAME+".id as progression_id, "+TABLE_NAME+".progression_by_me as prog_by_me, "+TABLE_NAME+".progression_by_someone_else as prog_by_someone_else, "+TABLE_NAME+".progression_by_veterinary as prog_by_vet, " + TABLE_NAME+".care as care_id, "+TABLE_NAME+".consent_behavior as consent_behavior_id, " @@ -356,13 +284,15 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " + "left join "+CareDao.TABLE_NAME+" on "+TABLE_NAME+".care = "+CareDao.TABLE_NAME+".id " + "left join "+ConsentBehaviorDao.TABLE_NAME+" on "+TABLE_NAME+".consent_behavior = "+ConsentBehaviorDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".progression_by_someone_else = ?;"); - statement.setString(1, animal.getOwner()); - statement.setInt(2, animal.getId()); - statement.setString(3, progressionBySomeoneElse); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); - + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".progression_by_someone_else = ?;"; + + params.put(1, animal.getOwner()); + params.put(2, animal.getId()); + params.put(3, progressionBySomeoneElse); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { Progression progression = new Progression(); @@ -393,7 +323,6 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { animal.addProgression(progression); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> fillAnimalProgressionBySomeoneElse() - " + ex.getMessage()); @@ -406,31 +335,13 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> fillAnimalProgressionBySomeoneElse()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> fillAnimalProgressionBySomeoneElse()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> fillAnimalProgressionBySomeoneElse()"); - } - } } } @Override public void fillAnimalProgressionByVeterinary(Animal animal, String progressionByVeterinary) { - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("select " + Map params = new HashMap<>(); + String sql = "select " + AnimalDao.TABLE_NAME+".id as animal_id, "+AnimalDao.TABLE_NAME+".name as animal_name, "+AnimalDao.TABLE_NAME+".owner as animal_owner, " + TABLE_NAME+".id as progression_id, "+TABLE_NAME+".progression_by_me as prog_by_me, "+TABLE_NAME+".progression_by_someone_else as prog_by_someone_else, "+TABLE_NAME+".progression_by_veterinary as prog_by_vet, " + TABLE_NAME+".care as care_id, "+TABLE_NAME+".consent_behavior as consent_behavior_id, " @@ -440,13 +351,15 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { + "left join "+AnimalDao.TABLE_NAME+" on "+TABLE_NAME+".animal = "+AnimalDao.TABLE_NAME+".id " + "left join "+CareDao.TABLE_NAME+" on "+TABLE_NAME+".care = "+CareDao.TABLE_NAME+".id " + "left join "+ConsentBehaviorDao.TABLE_NAME+" on "+TABLE_NAME+".consent_behavior = "+ConsentBehaviorDao.TABLE_NAME+".id " - + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".progression_by_veterinary = ?;"); - statement.setString(1, animal.getOwner()); - statement.setInt(2, animal.getId()); - statement.setString(3, progressionByVeterinary); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - resultSet = statement.executeQuery(); - + + "where "+AnimalDao.TABLE_NAME+".owner = ? and "+AnimalDao.TABLE_NAME+".id = ? and "+TABLE_NAME+".progression_by_veterinary = ?;"; + + params.put(1, animal.getOwner()); + params.put(2, animal.getId()); + params.put(3, progressionByVeterinary); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { Progression progression = new Progression(); @@ -477,7 +390,6 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { animal.addProgression(progression); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> fillAnimalProgressionByVeterinary() - " + ex.getMessage()); @@ -490,20 +402,6 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> fillAnimalProgressionByVeterinary()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> fillAnimalProgressionByVeterinary()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> fillAnimalProgressionByVeterinary()"); - } - } } } // @@ -511,222 +409,90 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao { // @Override public void deleteProgression(int progressionId, int animalId, String owner) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("DELETE FROM "+TABLE_NAME+" WHERE animal = (" + Map params = new HashMap<>(); + String sql = "DELETE FROM "+TABLE_NAME+" WHERE animal = (" + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?) " - + "AND id = ?"); - statement.setString(1, owner); - statement.setInt(2, animalId); - statement.setInt(3, progressionId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> deleteProgression()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> deleteProgression()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> deleteProgression()"); - } - } - } + + "AND id = ?;"; + + params.put(1, owner); + params.put(2, animalId); + params.put(3, progressionId); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void deleteAllAnimalProgression(int animalId, String owner) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("DELETE FROM "+TABLE_NAME+" WHERE animal = (" - + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?)"); - statement.setString(1, owner); - statement.setInt(2, animalId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> deleteAllAnimalProgression()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> deleteAllAnimalProgression()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> deleteAllAnimalProgression()"); - } - } - } + Map params = new HashMap<>(); + String sql = "DELETE FROM "+TABLE_NAME+" WHERE animal = (" + + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?);"; + + params.put(1, owner); + params.put(2, animalId); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void deleteAllAnimalCareProgression(int animalId, String owner, int careId) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("DELETE FROM "+TABLE_NAME+" WHERE animal = (" + Map params = new HashMap<>(); + String sql = "DELETE FROM "+TABLE_NAME+" WHERE animal = (" + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?) " - + "AND care = (SELECT id FROM "+CareDao.TABLE_NAME+" WHERE animal = ? AND id = ?)"); - statement.setString(1, owner); - statement.setInt(2, animalId); - statement.setInt(3, animalId); - statement.setInt(4, careId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> deleteAllAnimalCareProgression()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> deleteAllAnimalCareProgression()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> deleteAllAnimalCareProgression()"); - } - } - } + + "AND care = (SELECT id FROM "+CareDao.TABLE_NAME+" WHERE animal = ? AND id = ?);"; + + params.put(1, owner); + params.put(2, animalId); + params.put(3, animalId); + params.put(4, careId); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void deleteAllAnimalConsentBehaviorProgression(int animalId, String owner, int consentBehaviorId) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("DELETE FROM "+TABLE_NAME+" WHERE animal = (" + Map params = new HashMap<>(); + String sql = "DELETE FROM "+TABLE_NAME+" WHERE animal = (" + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?) " - + "AND consent_behavior = (SELECT id FROM "+ConsentBehaviorDao.TABLE_NAME+" WHERE animal = ? AND id = ?)"); - statement.setString(1, owner); - statement.setInt(2, animalId); - statement.setInt(3, animalId); - statement.setInt(4, consentBehaviorId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> deleteAllAnimalConsentBehaviorProgression()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> deleteAllAnimalConsentBehaviorProgression()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> deleteAllAnimalConsentBehaviorProgression()"); - } - } - } + + "AND consent_behavior = (SELECT id FROM "+ConsentBehaviorDao.TABLE_NAME+" WHERE animal = ? AND id = ?);"; + + params.put(1, owner); + params.put(2, animalId); + params.put(3, animalId); + params.put(4, consentBehaviorId); + + bddConnectionManager.executeUpdate(sql, params); } // @Override public void addProgression(int animalId, int careId, int consentBehaviorId) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("INSERT INTO "+TABLE_NAME+" (id, animal, care, consent_behavior, progression_by_me, progression_by_someone_else, progression_by_veterinary) VALUES (nextval(?), ?, ?, ?, ?, ?, ?);"); - statement.setString(1, SEQUENCE_NAME); - statement.setInt(2, animalId); - statement.setInt(3, careId); - statement.setInt(4, consentBehaviorId); - statement.setString(5, Progression.PROGRESSION_VALUE_NOT_TRAINED); - statement.setString(6, Progression.PROGRESSION_VALUE_NOT_TRAINED); - statement.setString(7, Progression.PROGRESSION_VALUE_NOT_TRAINED); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> addProgression()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> addProgression()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> addProgression()"); - } - } - } + Map params = new HashMap<>(); + String sql = "INSERT INTO "+TABLE_NAME+" (id, animal, care, consent_behavior, progression_by_me, progression_by_someone_else, progression_by_veterinary) VALUES (nextval(?), ?, ?, ?, ?, ?, ?);"; + + params.put(1, SEQUENCE_NAME); + params.put(2, animalId); + params.put(3, careId); + params.put(4, consentBehaviorId); + params.put(5, Progression.PROGRESSION_VALUE_NOT_TRAINED); + params.put(6, Progression.PROGRESSION_VALUE_NOT_TRAINED); + params.put(7, Progression.PROGRESSION_VALUE_NOT_TRAINED); + + bddConnectionManager.executeUpdate(sql, params); } @Override public void updateProgression(int animalId, String owner, int progressionId, String progressionByMe, String progressionBySomeoneElse, String progressionByVeterinary) { - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("UPDATE "+TABLE_NAME+" SET progression_by_me = ?, progression_by_someone_else = ?, progression_by_veterinary = ? WHERE id = ? AND animal = (" - + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?)"); - statement.setString(1, progressionByMe); - statement.setString(2, progressionBySomeoneElse); - statement.setString(3, progressionByVeterinary); - statement.setInt(4, progressionId); - statement.setString(5, owner); - statement.setInt(6, animalId); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> updateCare()"); - System.err.println(ex.getMessage()); - ex.printStackTrace(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> updateCare()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> updateCare()"); - } - } - } + Map params = new HashMap<>(); + String sql = "UPDATE "+TABLE_NAME+" SET progression_by_me = ?, progression_by_someone_else = ?, progression_by_veterinary = ? WHERE id = ? AND animal = (" + + "SELECT id FROM "+AnimalDao.TABLE_NAME+" WHERE owner = ? AND id = ?);"; + + params.put(1, progressionByMe); + params.put(2, progressionBySomeoneElse); + params.put(3, progressionByVeterinary); + params.put(4, progressionId); + params.put(5, owner); + params.put(6, animalId); + + bddConnectionManager.executeUpdate(sql, params); } } diff --git a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/UserBddDao.java b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/UserBddDao.java index 8edab2f..ecca3a1 100644 --- a/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/UserBddDao.java +++ b/src/main/java/fr/geoffrey/medical_training_tracker/dao/bdd/postgres/UserBddDao.java @@ -1,14 +1,14 @@ package fr.geoffrey.medical_training_tracker.dao.bdd.postgres; import fr.geoffrey.medical_training_tracker.controller.IndexController; -import java.sql.Connection; -import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import fr.geoffrey.medical_training_tracker.dao.bean.User; import fr.geoffrey.medical_training_tracker.dao.IUserDao; import fr.geoffrey.medical_training_tracker.dao.UserAlreadyExistException; +import java.util.HashMap; +import java.util.Map; public class UserBddDao extends PostgresSqlDao implements IUserDao { @@ -28,16 +28,14 @@ public class UserBddDao extends PostgresSqlDao implements IUserDao { public User getUser(String login) { User ret = null; - Connection conn = null; - PreparedStatement statement = null; - ResultSet resultSet = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("SELECT * FROM "+TABLE_NAME+" WHERE login = ?;"); - statement.setString(1, login); - System.out.println(IndexController.LOG_TAG+" SQL -> "+statement.toString()); - resultSet = statement.executeQuery(); + Map params = new HashMap<>(); + String sql = "SELECT * FROM "+TABLE_NAME+" WHERE login = ?;"; + params.put(1, login); + + ResultSet resultSet = bddConnectionManager.executeQuery(sql, params); + + try { if (resultSet != null) { while (resultSet.next()) { ret = new User(); @@ -45,7 +43,6 @@ public class UserBddDao extends PostgresSqlDao implements IUserDao { ret.setPassword(resultSet.getString("password")); ret.setName(resultSet.getString("name")); } - resultSet.close(); } } catch (SQLException ex) { System.err.println(IndexController.LOG_TAG + " SQLException -> getUser()"); @@ -57,95 +54,41 @@ public class UserBddDao extends PostgresSqlDao implements IUserDao { System.err.println(IndexController.LOG_TAG + " Failed close ResultSet -> getUser()"); } } - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> getUser()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> getUser()"); - } - } } return ret; } @Override public boolean addUser(String login, String hashPassword, String name) throws UserAlreadyExistException { - boolean ret = false; - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("INSERT INTO "+TABLE_NAME+" (login, password, name) VALUES (?, ?, ?);"); - statement.setString(1, login); - statement.setString(2, hashPassword); - if (name != null && !name.equals("")) { - statement.setString(3, name); - } else { - statement.setString(3, login); - } - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - ret = true; - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> addUser()"); - throw new UserAlreadyExistException(); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> addUser()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> addUser()"); - } - } + boolean ret; + + Map params = new HashMap<>(); + String sql = "INSERT INTO "+TABLE_NAME+" (login, password, name) VALUES (?, ?, ?);"; + + params.put(1, login); + params.put(2, hashPassword); + if (name != null && !name.equals("")) { + params.put(3, name); + } else { + params.put(3, login); } + + ret = bddConnectionManager.executeUpdate(sql, params) != -1; + return ret; } @Override public boolean updatePassword(String username, String hashedPassword) { - boolean ret = false; - Connection conn = null; - PreparedStatement statement = null; - try { - conn = mDataSource.getConnection(); - statement = conn.prepareStatement("UPDATE "+TABLE_NAME+" SET password = ? WHERE login = ?;"); - statement.setString(1, hashedPassword); - statement.setString(2, username); - System.out.println(IndexController.LOG_TAG + " SQL -> " + statement.toString()); - statement.executeUpdate(); - ret = true; - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " SQLException -> updatePassword()"); - } finally { - if (statement != null) { - try { - statement.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close statement -> updatePassword()"); - } - } - if (conn != null) { - try { - conn.close(); - } catch (SQLException ex) { - System.err.println(IndexController.LOG_TAG + " Failed close connection -> updatePassword()"); - } - } - } + boolean ret; + Map params = new HashMap<>(); + String sql = "UPDATE "+TABLE_NAME+" SET password = ? WHERE login = ?;"; + + params.put(1, hashedPassword); + params.put(2, username); + + ret = bddConnectionManager.executeUpdate(sql, params) != -1; + return ret; } } diff --git a/src/main/webapp/WEB-INF/applicationContext.xml b/src/main/webapp/WEB-INF/applicationContext.xml new file mode 100644 index 0000000..906b5cd --- /dev/null +++ b/src/main/webapp/WEB-INF/applicationContext.xml @@ -0,0 +1,64 @@ + + + + + + + + org.postgresql.Driver + + + jdbc:postgresql://127.0.0.1:5432/medical_training + + + postgres + + + toto + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/webapp/WEB-INF/dispatcher-servlet.xml b/src/main/webapp/WEB-INF/dispatcher-servlet.xml index 1e67f32..ec1b517 100644 --- a/src/main/webapp/WEB-INF/dispatcher-servlet.xml +++ b/src/main/webapp/WEB-INF/dispatcher-servlet.xml @@ -17,7 +17,7 @@ - + @@ -30,68 +30,4 @@ - - - - - org.postgresql.Driver - - - jdbc:postgresql://127.0.0.1:5432/medical_training - - - postgres - - - toto - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 9379259..ca9f144 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -4,10 +4,10 @@ Medical Training Tracker - + org.springframework.web.context.ContextLoaderListener diff --git a/src/main/webapp/resources/static/img/logo-white.svg b/src/main/webapp/resources/static/img/logo-white.svg new file mode 100644 index 0000000..c728244 --- /dev/null +++ b/src/main/webapp/resources/static/img/logo-white.svg @@ -0,0 +1,97 @@ + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + diff --git a/src/main/webapp/resources/static/img/logo_.svg b/src/main/webapp/resources/static/img/logo_.svg new file mode 100644 index 0000000..d108185 --- /dev/null +++ b/src/main/webapp/resources/static/img/logo_.svg @@ -0,0 +1,99 @@ + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + +