Refactoring
This commit is contained in:
parent
4cde30a6e0
commit
a30e86016c
@ -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<T extends Entity> {
|
||||
|
||||
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);
|
||||
|
@ -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;
|
||||
|
@ -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<Student> createStudentDao() {
|
||||
return new StudentDao(DaoFactory.connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dao<Teacher> createTeacherDao() {
|
||||
return new TeacherDao(DaoFactory.connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dao<Discipline> createDisciplineDao() {
|
||||
return new DisciplineDao(DaoFactory.connection);
|
||||
}
|
||||
}
|
@ -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<Student> createStudentDao() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TeacherDao createTeacherDao() {
|
||||
public Dao<Teacher> createTeacherDao() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DisciplineDao createDisciplineDao() {
|
||||
public Dao<Discipline> createDisciplineDao() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
@ -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() {
|
||||
|
@ -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);
|
||||
}
|
@ -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:");
|
||||
}
|
||||
|
@ -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<Student> createStudentDao() {
|
||||
return new StudentDao(DaoFactory.CONNECTION_WRAPPER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dao<Teacher> createTeacherDao() {
|
||||
return new TeacherDao(DaoFactory.CONNECTION_WRAPPER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dao<Discipline> createDisciplineDao() {
|
||||
return new DisciplineDao(DaoFactory.CONNECTION_WRAPPER);
|
||||
}
|
||||
}
|
@ -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<Discipline> {
|
||||
new Discipline(2, "Qsdf")
|
||||
));
|
||||
|
||||
public DisciplineDao(Connection connection) {
|
||||
super(connection);
|
||||
public DisciplineDao(ConnectionWrapper connectionWrapper) {
|
||||
super(connectionWrapper);
|
||||
}
|
||||
|
||||
@Override
|
@ -1,4 +1,4 @@
|
||||
package fr.gasser.daoexample.dao;
|
||||
package fr.gasser.daoexample.dao.dummy;
|
||||
|
||||
public enum FactoryType {
|
||||
DaoFactory,
|
@ -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<Student> {
|
||||
new Student(2, "Qsdf", "Jklm"))
|
||||
);
|
||||
|
||||
StudentDao(Connection connection) {
|
||||
super(connection);
|
||||
StudentDao(ConnectionWrapper connectionWrapper) {
|
||||
super(connectionWrapper);
|
||||
}
|
||||
|
||||
@Override
|
@ -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<Teacher> {
|
||||
new Teacher(2, "Sit", "Amet"))
|
||||
);
|
||||
|
||||
TeacherDao(Connection connection) {
|
||||
super(connection);
|
||||
TeacherDao(ConnectionWrapper connectionWrapper) {
|
||||
super(connectionWrapper);
|
||||
}
|
||||
|
||||
@Override
|
@ -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<Student> createStudentDao() {
|
@ -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<Student> {
|
||||
|
||||
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<Student> {
|
||||
@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<Student> {
|
||||
|
||||
@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<Student> {
|
||||
@Override
|
||||
public List<Student> findAll() {
|
||||
List<Student> 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<Student> {
|
||||
|
||||
@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<Student> {
|
||||
@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<Student> {
|
||||
|
||||
/**
|
||||
* Quick testing of CRUD operations
|
||||
* @param args
|
||||
* TODO: write some unit tests
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
DaoAbstractFactory df = DaoAbstractFactory.createFactory(FactoryType.SqliteDaoFactory);
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user