Manage BDD connection/disconnection.

This commit is contained in:
Geoffrey POUZET 2020-11-04 20:09:41 +01:00
parent a9f937a561
commit 79247d6b34
16 changed files with 867 additions and 1119 deletions

View File

@ -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;
@ -37,18 +46,39 @@ public class Servlet implements ServletContextListener {
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();
}
}
}

View File

@ -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

View File

@ -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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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;
}
}

View File

@ -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;
}
}

View File

@ -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<Integer, Object> 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<Animal> getAnimals(String owner) {
List<Animal> 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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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);
}
}

View File

@ -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<Care> getAllAnimalCares(String owner, int animalId) {
List<Care> ret = new ArrayList<>();
Connection conn = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
conn = mDataSource.getConnection();
statement = conn.prepareStatement("select "
Map<Integer, Object> 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<Integer, Object> 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();
Map<Integer, Object> params = new HashMap<>();
String sql;
if (description != null) {
statement = conn.prepareStatement("INSERT INTO "+TABLE_NAME+" (id, animal, name, description) VALUES (nextval(?), ?, ?, ?);");
statement.setString(4, description);
sql = "INSERT INTO "+TABLE_NAME+" (id, animal, name, description) VALUES (nextval(?), ?, ?, ?);";
params.put(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()");
}
}
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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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);
}
}

View File

@ -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<ConsentBehavior> getAllAnialConsentBehavior(String owner, int animalId) {
List<ConsentBehavior> ret = new ArrayList<>();
Connection conn = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
conn = mDataSource.getConnection();
statement = conn.prepareStatement("select "
Map<Integer, Object> 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<Integer, Object> 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();
Map<Integer, Object> params = new HashMap<>();
String sql;
if (description != null) {
statement = conn.prepareStatement("INSERT INTO "+TABLE_NAME+" (id, animal, name, description) VALUES (nextval(?), ?, ?, ?);");
statement.setString(4, description);
sql = "INSERT INTO "+TABLE_NAME+" (id, animal, name, description) VALUES (nextval(?), ?, ?, ?);";
params.put(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()");
}
}
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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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);
}
}

View File

@ -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;
}
}

View File

@ -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<Integer, Object> 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);
}
}

View File

@ -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 {
//<editor-fold defaultstate="collapsed" desc="Fill methods">
@Override
public void fillAnimalProgression(Animal animal) {
Connection conn = null;
PreparedStatement statement = null;
ResultSet resultSet = null;
try {
conn = mDataSource.getConnection();
statement = conn.prepareStatement("select "
Map<Integer, Object> 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();
+ "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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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()");
}
}
}
}
//</editor-fold>
@ -511,222 +409,90 @@ public class ProgressionDao extends PostgresSqlDao implements IProgressionDao {
//<editor-fold defaultstate="collapsed" desc="Delete methods">
@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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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<Integer, Object> 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);
}
//</editor-fold>
@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<Integer, Object> 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<Integer, Object> 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);
}
}

View File

@ -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<Integer, Object> 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);
boolean ret;
Map<Integer, Object> 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("")) {
statement.setString(3, name);
params.put(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()");
}
}
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<Integer, Object> 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;
}
}

View File

@ -0,0 +1,64 @@
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="fr.geoffrey.medical_training_tracker.config" />
<mvc:annotation-driven />
<bean id="prodDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://127.0.0.1:5432/medical_training</value>
</property>
<property name="username">
<value>postgres</value>
</property>
<property name="password">
<value>toto</value>
</property>
</bean>
<bean id="bddConnectionManager" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.PostgresConnectionManager">
<property name="dataSource">
<ref bean="prodDataSource"/>
</property>
</bean>
<bean id="userDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.UserBddDao"></bean>
<bean id="animalDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.AnimalDao"></bean>
<bean id="careDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.CareDao"></bean>
<bean id="consentBehaviorDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.ConsentBehaviorDao"></bean>
<bean id="progressionDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.ProgressionDao"></bean>
<bean id="serviceAnimal" class="fr.geoffrey.medical_training_tracker.services.ServiceAnimal">
<property name="animalDao">
<ref bean="animalDao"/>
</property>
<property name="careDao">
<ref bean="careDao"/>
</property>
<property name="consentBehaviorDao">
<ref bean="consentBehaviorDao"/>
</property>
<property name="progressionDao">
<ref bean="progressionDao"/>
</property>
</bean>
<bean id="serviceRegister" class="fr.geoffrey.medical_training_tracker.services.ServiceRegister">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
</beans>

View File

@ -17,7 +17,7 @@
</bean>
<context:component-scan base-package="fr.geoffrey" />
<context:component-scan base-package="fr.geoffrey.medical_training_tracker.controller" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
@ -30,68 +30,4 @@
<mvc:resources mapping="/static/**" location="/resources/static/" cache-period="31556926"/>
<mvc:annotation-driven />
<bean id="prodDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://127.0.0.1:5432/medical_training</value>
</property>
<property name="username">
<value>postgres</value>
</property>
<property name="password">
<value>toto</value>
</property>
</bean>
<bean id="userDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.UserBddDao">
<property name="dataSource">
<ref bean="prodDataSource"/>
</property>
</bean>
<bean id="animalDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.AnimalDao">
<property name="dataSource">
<ref bean="prodDataSource"/>
</property>
</bean>
<bean id="careDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.CareDao">
<property name="dataSource">
<ref bean="prodDataSource"/>
</property>
</bean>
<bean id="consentBehaviorDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.ConsentBehaviorDao">
<property name="dataSource">
<ref bean="prodDataSource"/>
</property>
</bean>
<bean id="progressionDao" class="fr.geoffrey.medical_training_tracker.dao.bdd.postgres.ProgressionDao">
<property name="dataSource">
<ref bean="prodDataSource"/>
</property>
</bean>
<bean id="serviceAnimal" class="fr.geoffrey.medical_training_tracker.services.ServiceAnimal">
<property name="animalDao">
<ref bean="animalDao"/>
</property>
<property name="careDao">
<ref bean="careDao"/>
</property>
<property name="consentBehaviorDao">
<ref bean="consentBehaviorDao"/>
</property>
<property name="progressionDao">
<ref bean="progressionDao"/>
</property>
</bean>
<bean id="serviceRegister" class="fr.geoffrey.medical_training_tracker.services.ServiceRegister">
<property name="userDao">
<ref bean="userDao"/>
</property>
</bean>
</beans>

View File

@ -4,10 +4,10 @@
<web-app>
<display-name>Medical Training Tracker</display-name>
<context-param>
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet.xml</param-value>
</context-param>
</context-param>-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 13 KiB

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 11 KiB