diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java index 44e746b..01fc055 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/Dao.java @@ -1,16 +1,16 @@ package fr.gasser.daoexample.dao; import fr.gasser.daoexample.model.Entity; -import fr.gasser.daoexample.sql.Connection; +import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import java.util.List; public abstract class Dao { - protected Connection connection; + protected final ConnectionWrapper connectionWrapper; - public Dao(Connection connection) { - this.connection = connection; + protected Dao(ConnectionWrapper connectionWrapper) { + this.connectionWrapper = connectionWrapper; } public abstract boolean create(T obj); diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java index 0eab54b..07c8d8a 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/DaoAbstractFactory.java @@ -1,5 +1,8 @@ package fr.gasser.daoexample.dao; +import fr.gasser.daoexample.dao.dummy.DaoFactory; +import fr.gasser.daoexample.dao.dummy.FactoryType; +import fr.gasser.daoexample.dao.sqlite.SqliteDaoFactory; import fr.gasser.daoexample.model.Discipline; import fr.gasser.daoexample.model.Student; import fr.gasser.daoexample.model.Teacher; diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/DaoFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/DaoFactory.java deleted file mode 100644 index 77bc207..0000000 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/DaoFactory.java +++ /dev/null @@ -1,26 +0,0 @@ -package fr.gasser.daoexample.dao; - -import fr.gasser.daoexample.model.Discipline; -import fr.gasser.daoexample.model.Student; -import fr.gasser.daoexample.model.Teacher; -import fr.gasser.daoexample.sql.Connection; -import fr.gasser.daoexample.sql.DummyConnection; - -public class DaoFactory implements DaoAbstractFactory { - private static Connection connection = new DummyConnection(); - - @Override - public Dao createStudentDao() { - return new StudentDao(DaoFactory.connection); - } - - @Override - public Dao createTeacherDao() { - return new TeacherDao(DaoFactory.connection); - } - - @Override - public Dao createDisciplineDao() { - return new DisciplineDao(DaoFactory.connection); - } -} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/XmlDaoFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/XmlDaoFactory.java index 247d6a7..76344c3 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/XmlDaoFactory.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/XmlDaoFactory.java @@ -1,18 +1,22 @@ package fr.gasser.daoexample.dao; +import fr.gasser.daoexample.model.Discipline; +import fr.gasser.daoexample.model.Student; +import fr.gasser.daoexample.model.Teacher; + public class XmlDaoFactory implements DaoAbstractFactory { @Override - public StudentDao createStudentDao() { + public Dao createStudentDao() { return null; } @Override - public TeacherDao createTeacherDao() { + public Dao createTeacherDao() { return null; } @Override - public DisciplineDao createDisciplineDao() { + public Dao createDisciplineDao() { return null; } } diff --git a/persistence/src/main/java/fr/gasser/daoexample/sql/Connection.java b/persistence/src/main/java/fr/gasser/daoexample/dao/connection/ConnectionWrapper.java similarity index 65% rename from persistence/src/main/java/fr/gasser/daoexample/sql/Connection.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/connection/ConnectionWrapper.java index c610453..2c33474 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/sql/Connection.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/connection/ConnectionWrapper.java @@ -1,8 +1,8 @@ -package fr.gasser.daoexample.sql; +package fr.gasser.daoexample.dao.connection; import java.sql.SQLException; -public interface Connection { +public interface ConnectionWrapper { void connect() throws SQLException; void close() throws SQLException; diff --git a/persistence/src/main/java/fr/gasser/daoexample/sql/DummyConnection.java b/persistence/src/main/java/fr/gasser/daoexample/dao/connection/DummyConnectionWrapper.java similarity index 63% rename from persistence/src/main/java/fr/gasser/daoexample/sql/DummyConnection.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/connection/DummyConnectionWrapper.java index 936647e..0590415 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/sql/DummyConnection.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/connection/DummyConnectionWrapper.java @@ -1,6 +1,6 @@ -package fr.gasser.daoexample.sql; +package fr.gasser.daoexample.dao.connection; -public class DummyConnection implements Connection { +public class DummyConnectionWrapper implements ConnectionWrapper { @Override public void connect() { diff --git a/persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnectionException.java b/persistence/src/main/java/fr/gasser/daoexample/dao/connection/SQLiteConnectionException.java similarity index 54% rename from persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnectionException.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/connection/SQLiteConnectionException.java index c217ac1..9246187 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnectionException.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/connection/SQLiteConnectionException.java @@ -1,8 +1,8 @@ -package fr.gasser.daoexample.sql; +package fr.gasser.daoexample.dao.connection; import java.sql.SQLException; -public class SQLiteConnectionException extends SQLException { +class SQLiteConnectionException extends SQLException { public SQLiteConnectionException(String message) { super(message); } diff --git a/persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnection.java b/persistence/src/main/java/fr/gasser/daoexample/dao/connection/SQLiteConnectionWrapper.java similarity index 79% rename from persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnection.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/connection/SQLiteConnectionWrapper.java index 2697fcd..772378d 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/sql/SQLiteConnection.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/connection/SQLiteConnectionWrapper.java @@ -1,4 +1,4 @@ -package fr.gasser.daoexample.sql; +package fr.gasser.daoexample.dao.connection; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -6,17 +6,17 @@ import org.slf4j.LoggerFactory; import java.sql.DriverManager; import java.sql.SQLException; -public class SQLiteConnection implements Connection { - private static final Logger LOGGER = LoggerFactory.getLogger(SQLiteConnection.class); +public class SQLiteConnectionWrapper implements ConnectionWrapper { + private static final Logger LOGGER = LoggerFactory.getLogger(SQLiteConnectionWrapper.class); - private String dbpath; + private final String dbpath; private java.sql.Connection connection = null; - public SQLiteConnection(String dBPath) { + private SQLiteConnectionWrapper(String dBPath) { dbpath = dBPath; } - public SQLiteConnection() { + public SQLiteConnectionWrapper() { this(":memory:"); } diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DaoFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DaoFactory.java new file mode 100644 index 0000000..8dd21e7 --- /dev/null +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DaoFactory.java @@ -0,0 +1,28 @@ +package fr.gasser.daoexample.dao.dummy; + +import fr.gasser.daoexample.dao.Dao; +import fr.gasser.daoexample.dao.DaoAbstractFactory; +import fr.gasser.daoexample.model.Discipline; +import fr.gasser.daoexample.model.Student; +import fr.gasser.daoexample.model.Teacher; +import fr.gasser.daoexample.dao.connection.ConnectionWrapper; +import fr.gasser.daoexample.dao.connection.DummyConnectionWrapper; + +public class DaoFactory implements DaoAbstractFactory { + private static final ConnectionWrapper CONNECTION_WRAPPER = new DummyConnectionWrapper(); + + @Override + public Dao createStudentDao() { + return new StudentDao(DaoFactory.CONNECTION_WRAPPER); + } + + @Override + public Dao createTeacherDao() { + return new TeacherDao(DaoFactory.CONNECTION_WRAPPER); + } + + @Override + public Dao createDisciplineDao() { + return new DisciplineDao(DaoFactory.CONNECTION_WRAPPER); + } +} diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/DisciplineDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DisciplineDao.java similarity index 82% rename from persistence/src/main/java/fr/gasser/daoexample/dao/DisciplineDao.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DisciplineDao.java index 9f7fb25..af85000 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/DisciplineDao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/DisciplineDao.java @@ -1,7 +1,8 @@ -package fr.gasser.daoexample.dao; +package fr.gasser.daoexample.dao.dummy; +import fr.gasser.daoexample.dao.Dao; import fr.gasser.daoexample.model.Discipline; -import fr.gasser.daoexample.sql.Connection; +import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import java.util.ArrayList; import java.util.Arrays; @@ -16,8 +17,8 @@ public class DisciplineDao extends Dao { new Discipline(2, "Qsdf") )); - public DisciplineDao(Connection connection) { - super(connection); + public DisciplineDao(ConnectionWrapper connectionWrapper) { + super(connectionWrapper); } @Override diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/FactoryType.java similarity index 68% rename from persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/dummy/FactoryType.java index 008e26c..22e3300 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/FactoryType.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/FactoryType.java @@ -1,4 +1,4 @@ -package fr.gasser.daoexample.dao; +package fr.gasser.daoexample.dao.dummy; public enum FactoryType { DaoFactory, diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/StudentDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/StudentDao.java similarity index 82% rename from persistence/src/main/java/fr/gasser/daoexample/dao/StudentDao.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/dummy/StudentDao.java index 50fad64..321d559 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/StudentDao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/StudentDao.java @@ -1,7 +1,8 @@ -package fr.gasser.daoexample.dao; +package fr.gasser.daoexample.dao.dummy; +import fr.gasser.daoexample.dao.Dao; import fr.gasser.daoexample.model.Student; -import fr.gasser.daoexample.sql.Connection; +import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import java.util.ArrayList; import java.util.Arrays; @@ -16,8 +17,8 @@ public class StudentDao extends Dao { new Student(2, "Qsdf", "Jklm")) ); - StudentDao(Connection connection) { - super(connection); + StudentDao(ConnectionWrapper connectionWrapper) { + super(connectionWrapper); } @Override diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/TeacherDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/TeacherDao.java similarity index 82% rename from persistence/src/main/java/fr/gasser/daoexample/dao/TeacherDao.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/dummy/TeacherDao.java index 6e0d864..97948da 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/TeacherDao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/dummy/TeacherDao.java @@ -1,7 +1,8 @@ -package fr.gasser.daoexample.dao; +package fr.gasser.daoexample.dao.dummy; +import fr.gasser.daoexample.dao.Dao; import fr.gasser.daoexample.model.Teacher; -import fr.gasser.daoexample.sql.Connection; +import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import java.util.ArrayList; import java.util.Arrays; @@ -16,8 +17,8 @@ public class TeacherDao extends Dao { new Teacher(2, "Sit", "Amet")) ); - TeacherDao(Connection connection) { - super(connection); + TeacherDao(ConnectionWrapper connectionWrapper) { + super(connectionWrapper); } @Override diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/SqliteDaoFactory.java b/persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/SqliteDaoFactory.java similarity index 58% rename from persistence/src/main/java/fr/gasser/daoexample/dao/SqliteDaoFactory.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/SqliteDaoFactory.java index bba43a5..7a7140d 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/SqliteDaoFactory.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/SqliteDaoFactory.java @@ -1,14 +1,16 @@ -package fr.gasser.daoexample.dao; +package fr.gasser.daoexample.dao.sqlite; +import fr.gasser.daoexample.dao.Dao; +import fr.gasser.daoexample.dao.DaoAbstractFactory; import fr.gasser.daoexample.model.Discipline; import fr.gasser.daoexample.model.Student; import fr.gasser.daoexample.model.Teacher; -import fr.gasser.daoexample.sql.Connection; -import fr.gasser.daoexample.sql.SQLiteConnection; +import fr.gasser.daoexample.dao.connection.ConnectionWrapper; +import fr.gasser.daoexample.dao.connection.SQLiteConnectionWrapper; public class SqliteDaoFactory implements DaoAbstractFactory { - private static Connection conn = new SQLiteConnection(); + private static final ConnectionWrapper conn = new SQLiteConnectionWrapper(); @Override public Dao createStudentDao() { diff --git a/persistence/src/main/java/fr/gasser/daoexample/dao/StudentSQLiteDao.java b/persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/StudentSQLiteDao.java similarity index 76% rename from persistence/src/main/java/fr/gasser/daoexample/dao/StudentSQLiteDao.java rename to persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/StudentSQLiteDao.java index dfd957e..1478ec8 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/dao/StudentSQLiteDao.java +++ b/persistence/src/main/java/fr/gasser/daoexample/dao/sqlite/StudentSQLiteDao.java @@ -1,7 +1,10 @@ -package fr.gasser.daoexample.dao; +package fr.gasser.daoexample.dao.sqlite; +import fr.gasser.daoexample.dao.Dao; +import fr.gasser.daoexample.dao.DaoAbstractFactory; +import fr.gasser.daoexample.dao.dummy.FactoryType; import fr.gasser.daoexample.model.Student; -import fr.gasser.daoexample.sql.Connection; +import fr.gasser.daoexample.dao.connection.ConnectionWrapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -16,18 +19,18 @@ public class StudentSQLiteDao extends Dao { private static final Logger LOGGER = LoggerFactory.getLogger(StudentSQLiteDao.class); - public StudentSQLiteDao(Connection connection) { - super(connection); + StudentSQLiteDao(ConnectionWrapper connectionWrapper) { + super(connectionWrapper); try { - connection.connect(); - dropAndCreateTable(connection); + connectionWrapper.connect(); + dropAndCreateTable(connectionWrapper); } catch (SQLException e) { LOGGER.error("Unable to connect to the database.", e); } } - private void dropAndCreateTable(Connection connection) throws SQLException { - try (Statement st = connection.getConnection().createStatement()) { + private void dropAndCreateTable(ConnectionWrapper connectionWrapper) throws SQLException { + try (Statement st = connectionWrapper.getConnection().createStatement()) { st.setQueryTimeout(30); // set timeout to 30 sec. st.executeUpdate("DROP TABLE IF EXISTS students"); // In SQLite, table rows normally have a 64-bit signed integer ROWID @@ -41,7 +44,7 @@ public class StudentSQLiteDao extends Dao { @Override public boolean create(Student obj) { final String sql = "INSERT INTO students (name, surname) VALUES (?, ?)"; - try (PreparedStatement st = connection.getConnection().prepareStatement(sql)) { + try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement(sql)) { st.setObject(1, obj.getName()); st.setObject(2, obj.getSurname()); final int ret = st.executeUpdate(); @@ -55,7 +58,7 @@ public class StudentSQLiteDao extends Dao { @Override public Student find(int id) { - try (PreparedStatement st = connection.getConnection().prepareStatement("SELECT rowid,* FROM students WHERE rowid = ?")) { + try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement("SELECT rowid,* FROM students WHERE rowid = ?")) { st.setObject(1, id); final ResultSet rs = st.executeQuery(); final String name = rs.getString("name"); @@ -70,7 +73,7 @@ public class StudentSQLiteDao extends Dao { @Override public List findAll() { List results = new ArrayList<>(); - try (Statement st = connection.getConnection().createStatement()) { + try (Statement st = connectionWrapper.getConnection().createStatement()) { ResultSet rs = st.executeQuery("select rowid,* from students"); while (rs.next()) { final int id = rs.getInt("rowid"); @@ -86,7 +89,7 @@ public class StudentSQLiteDao extends Dao { @Override public boolean update(Student obj) { - try (PreparedStatement st = connection.getConnection().prepareStatement("UPDATE students SET name = ?, surname = ? WHERE rowid = ?")) { + try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement("UPDATE students SET name = ?, surname = ? WHERE rowid = ?")) { st.setString(1, obj.getName()); st.setString(2, obj.getSurname()); st.setInt(3, obj.getId()); @@ -101,7 +104,7 @@ public class StudentSQLiteDao extends Dao { @Override public boolean delete(Student obj) { String deleteSQL = "DELETE FROM students WHERE rowid = ?"; - try (PreparedStatement st = connection.getConnection().prepareStatement(deleteSQL)) { + try (PreparedStatement st = connectionWrapper.getConnection().prepareStatement(deleteSQL)) { st.setInt(1, obj.getId()); st.executeUpdate(); return true; @@ -114,7 +117,7 @@ public class StudentSQLiteDao extends Dao { /** * Quick testing of CRUD operations - * @param args + * TODO: write some unit tests */ public static void main(String[] args) { DaoAbstractFactory df = DaoAbstractFactory.createFactory(FactoryType.SqliteDaoFactory); diff --git a/persistence/src/main/java/fr/gasser/daoexample/model/Discipline.java b/persistence/src/main/java/fr/gasser/daoexample/model/Discipline.java index 07eb89f..c708d3f 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/model/Discipline.java +++ b/persistence/src/main/java/fr/gasser/daoexample/model/Discipline.java @@ -32,15 +32,15 @@ public class Discipline extends Entity { return teachers.add(teacher); } - public boolean removeTeacher(Object o) { - return teachers.remove(o); + public boolean removeTeacher(Teacher t) { + return teachers.remove(t); } public boolean addStudent(Student student) { return students.add(student); } - public boolean removeStudent(Object o) { - return students.remove(o); + public boolean removeStudent(Student s) { + return students.remove(s); } } diff --git a/persistence/src/main/java/fr/gasser/daoexample/model/Entity.java b/persistence/src/main/java/fr/gasser/daoexample/model/Entity.java index 0a18a26..6253bd1 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/model/Entity.java +++ b/persistence/src/main/java/fr/gasser/daoexample/model/Entity.java @@ -2,9 +2,9 @@ package fr.gasser.daoexample.model; public class Entity { - protected int id; + int id; - public Entity(int id) { + Entity(int id) { this.id = id; } diff --git a/persistence/src/main/java/fr/gasser/daoexample/model/Teacher.java b/persistence/src/main/java/fr/gasser/daoexample/model/Teacher.java index 04f7f9f..34009b2 100644 --- a/persistence/src/main/java/fr/gasser/daoexample/model/Teacher.java +++ b/persistence/src/main/java/fr/gasser/daoexample/model/Teacher.java @@ -44,8 +44,8 @@ public class Teacher extends Entity { return disciplines.add(discipline); } - public boolean removeDiscipline(Object o) { - return disciplines.remove(o); + public boolean removeDiscipline(Discipline discipline) { + return disciplines.remove(discipline); } }