Skip to content

Commit

Permalink
Merge pull request #1 from Oskarowski/prep-for-publication
Browse files Browse the repository at this point in the history
Prepare Repository for changing visibility
  • Loading branch information
Oskarowski authored Jun 23, 2024
2 parents 8108dc1 + 97149de commit a65fe49
Show file tree
Hide file tree
Showing 53 changed files with 840 additions and 267 deletions.
2 changes: 0 additions & 2 deletions Dao/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.16</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
</dependencies>

Expand Down
4 changes: 2 additions & 2 deletions Dao/src/test/java/sudoku/dao/FileSudokuBoardDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.junit.jupiter.api.Assertions;
import sudoku.dao.factories.SudokuBoardDaoFactory;
import sudoku.dao.interfaces.Dao;
import sudoku.model.exceptions.InvalidSudokuException;
import sudoku.model.exceptions.FillingBoardSudokuException;
import sudoku.model.models.SudokuBoard;
import sudoku.model.solver.BacktrackingSudokuSolver;

Expand Down Expand Up @@ -38,7 +38,7 @@ public void testWriteAndRead() {
SudokuBoard sampleBoard = new SudokuBoard(new BacktrackingSudokuSolver());
try {
sampleBoard.solveGame();
} catch (InvalidSudokuException e) {
} catch (FillingBoardSudokuException e) {
e.printStackTrace();
}

Expand Down
4 changes: 2 additions & 2 deletions JdbcDao/pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
Expand Down
31 changes: 17 additions & 14 deletions JdbcDao/src/main/java/sudoku/jdbcdao/JdbcSudokuBoardDao.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sudoku.jdbcdao;

import static sudokujdbc.jooq.generated.Tables.SUDOKU_BOARD;
import static sudokujdbc.jooq.generated.Tables.SUDOKU_FIELD;
import static sudokujdbc.jooq.generated.Tables.SUDOKU_BOARDS;
import static sudokujdbc.jooq.generated.Tables.SUDOKU_FIELDS;

import java.sql.Connection;
import java.sql.DriverManager;
Expand All @@ -26,6 +26,9 @@ public class JdbcSudokuBoardDao implements Dao<SudokuBoard> {
private DSLContext dsl;

public JdbcSudokuBoardDao(String url) {
if (url == null || url.trim().isEmpty()) {
throw new IllegalArgumentException("URL cannot be null or empty");
}
this.url = url;
}

Expand All @@ -48,18 +51,18 @@ public void write(String name, SudokuBoard board) throws JdbcDaoWriteException,
connect();
connection.setAutoCommit(false);

var boardId = dsl.insertInto(SUDOKU_BOARD)
.columns(SUDOKU_BOARD.NAME)
var boardId = dsl.insertInto(SUDOKU_BOARDS)
.columns(SUDOKU_BOARDS.NAME)
.values(name)
.returning(SUDOKU_BOARD.ID)
.returning(SUDOKU_BOARDS.ID)
.fetchOne()
.getId();

for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
int value = board.getField(row, col).getValue();
dsl.insertInto(SUDOKU_FIELD)
.columns(SUDOKU_FIELD.BOARD_ID, SUDOKU_FIELD.ROW, SUDOKU_FIELD.COLUMN, SUDOKU_FIELD.VALUE)
dsl.insertInto(SUDOKU_FIELDS)
.columns(SUDOKU_FIELDS.BOARD_ID, SUDOKU_FIELDS.ROW, SUDOKU_FIELDS.COLUMN, SUDOKU_FIELDS.VALUE)
.values(boardId, row, col, value)
.execute();
}
Expand All @@ -83,8 +86,8 @@ public SudokuBoard read(String name) throws JdbcDaoReadException {
try {
connect();

var boardRecord = dsl.selectFrom(SUDOKU_BOARD)
.where(SUDOKU_BOARD.NAME.eq(name))
var boardRecord = dsl.selectFrom(SUDOKU_BOARDS)
.where(SUDOKU_BOARDS.NAME.eq(name))
.fetchOne();

if (boardRecord == null) {
Expand All @@ -93,8 +96,8 @@ public SudokuBoard read(String name) throws JdbcDaoReadException {

SudokuBoard board = new SudokuBoard(new BacktrackingSudokuSolver());

dsl.selectFrom(SUDOKU_FIELD)
.where(SUDOKU_FIELD.BOARD_ID.eq(boardRecord.getId()))
dsl.selectFrom(SUDOKU_FIELDS)
.where(SUDOKU_FIELDS.BOARD_ID.eq(boardRecord.getId()))
.forEach(record -> {
int row = record.getRow();
int col = record.getColumn();
Expand All @@ -114,9 +117,9 @@ public SudokuBoard read(String name) throws JdbcDaoReadException {
public List<String> names() throws JdbcDaoReadException {
try {
connect();
return dsl.select(SUDOKU_BOARD.NAME)
.from(SUDOKU_BOARD)
.fetch(SUDOKU_BOARD.NAME);
return dsl.select(SUDOKU_BOARDS.NAME)
.from(SUDOKU_BOARDS)
.fetch(SUDOKU_BOARDS.NAME);
} catch (Exception e) {
throw new JdbcDaoReadException(e);
} finally {
Expand Down
16 changes: 16 additions & 0 deletions JdbcDao/src/main/java/sudoku/jdbcdao/database/database-schema.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
```mermaid
erDiagram
SUDOKU_BOARDS {
int id PK
string name
}
SUDOKU_FIELDS {
int id PK
int board_id FK
int row
int column
int value
}
SUDOKU_FIELDS }|--|| SUDOKU_BOARDS : board_id
```
6 changes: 3 additions & 3 deletions JdbcDao/src/main/java/sudoku/jdbcdao/database/schema.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
CREATE TABLE IF NOT EXISTS SUDOKU_BOARD (
CREATE TABLE IF NOT EXISTS SUDOKU_BOARDS (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE
);

CREATE TABLE IF NOT EXISTS SUDOKU_FIELD (
CREATE TABLE IF NOT EXISTS SUDOKU_FIELDS (
id INTEGER PRIMARY KEY AUTOINCREMENT,
board_id INTEGER,
row INTEGER,
column INTEGER,
value INTEGER,
FOREIGN KEY (board_id) REFERENCES SUDOKU_BOARD (id)
FOREIGN KEY (board_id) REFERENCES SUDOKU_BOARDS (id)
);
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
package sudoku.jdbcdao.exceptions;

import java.util.Locale;
import java.util.ResourceBundle;

/**
* Represents a localized exception base class that can be thrown by the JdbcDao
* module.
* This exception is used to provide localized error messages to the user.
*/
public class LocalizedException extends Exception {
/**
* Retrieves the localized message for the given message key.
*
* @param messageKey the key of the message to retrieve
* @return the localized message associated with the key
*/
private static String getLocalizedMessage(String messageKey) {
ResourceBundle bundle = ResourceBundle.getBundle("JdbcDaoExceptions"); // remember that resource bundles must be
ResourceBundle bundle = ResourceBundle.getBundle("JdbcDaoExceptions", Locale.getDefault());
return bundle.getString(messageKey);
}

/**
* Constructs a new LocalizedException with the specified messenger.
*
* @param messenger the JdbcDaoMessageKey representing the localized message
* key.
*/
public LocalizedException(JdbcDaoMessageKey messenger) {
super(getLocalizedMessage(messenger.getKey()));
}

/**
* Constructs a new LocalizedException with the specified messenger and cause.
*
* @param messenger the JdbcDaoMessageKey representing the localized message
* key.
* @param cause the Throwable that caused this exception to be thrown.
*/
public LocalizedException(JdbcDaoMessageKey messenger, Throwable cause) {
super(getLocalizedMessage(messenger.getKey()), cause);
}
Expand Down
45 changes: 22 additions & 23 deletions JdbcDao/src/test/java/sudoku/jdbcdao/JdbcSudokuBoardDaoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,42 +15,40 @@
import java.sql.SQLException;

import static org.junit.jupiter.api.Assertions.*;
import static sudokujdbc.jooq.generated.Tables.SUDOKU_BOARD;
import static sudokujdbc.jooq.generated.Tables.SUDOKU_FIELD;
import static sudokujdbc.jooq.generated.Tables.SUDOKU_BOARDS;
import static sudokujdbc.jooq.generated.Tables.SUDOKU_FIELDS;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class JdbcSudokuBoardDaoTest {

private static final String TEST_DB_URL = "jdbc:sqlite:target/test_sudoku.db";
private Connection connection;
private DSLContext dsl;


@BeforeAll
public void setUp() throws SQLException {
connection = DriverManager.getConnection(TEST_DB_URL);
dsl = DSL.using(connection, SQLDialect.SQLITE);

dsl.createTableIfNotExists(SUDOKU_BOARD)
.columns(SUDOKU_BOARD.fields())
.execute();

dsl.createTableIfNotExists(SUDOKU_FIELD)
.columns(SUDOKU_FIELD.fields())
.execute();
}
private DSLContext dsl;

@BeforeAll
public void setUp() throws SQLException {
connection = DriverManager.getConnection(TEST_DB_URL);
dsl = DSL.using(connection, SQLDialect.SQLITE);

dsl.createTableIfNotExists(SUDOKU_BOARDS)
.columns(SUDOKU_BOARDS.fields())
.execute();

dsl.createTableIfNotExists(SUDOKU_FIELDS)
.columns(SUDOKU_FIELDS.fields())
.execute();
}

@AfterEach
public void tearDown() throws SQLException {
dsl.deleteFrom(SUDOKU_FIELD).execute();
dsl.deleteFrom(SUDOKU_BOARD).execute();
dsl.deleteFrom(SUDOKU_FIELDS).execute();
dsl.deleteFrom(SUDOKU_BOARDS).execute();
}

@AfterAll
public void cleanUp() throws SQLException {
dsl.dropTableIfExists(SUDOKU_FIELD).execute();
dsl.dropTableIfExists(SUDOKU_BOARD).execute();
dsl.dropTableIfExists(SUDOKU_FIELDS).execute();
dsl.dropTableIfExists(SUDOKU_BOARDS).execute();
connection.close();
}

Expand Down Expand Up @@ -98,7 +96,8 @@ public void testReadNonExistentBoard() {
@Test
public void testWriteException() {
try (Dao<SudokuBoard> dao = new JdbcSudokuBoardDao(TEST_DB_URL)) {
assertThrows(JdbcDaoWriteException.class, () -> dao.write("", new SudokuBoard(new BacktrackingSudokuSolver())));
assertThrows(JdbcDaoWriteException.class,
() -> dao.write("", new SudokuBoard(new BacktrackingSudokuSolver())));
} catch (Exception e) {
fail(e);
}
Expand Down
Binary file modified JdbcDao/src/test/java/sudoku/jdbcdao/resources/test_sudoku.db
Binary file not shown.
Binary file modified JdbcDao/sudoku.db
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package sudoku.model.exceptions;

/**
* Exception thrown when there is an error during the process of filling a
* Sudoku board.
* This exception typically indicates issues encountered while populating the
* board with values.
*
* <p>
* This exception extends {@link LocalizedModelException}, which provides
* support
* for localized error messages based on {@link ModelMessageKey}.
* </p>
*
* <p>
* The default error message is retrieved using the
* {@link ModelMessageKey#FILLING_BOARD_ERROR}
* key from the resource bundle.
* </p>
*
* <p>
* This exception can optionally wrap another throwable as its cause, providing
* additional context or information about the error.
* </p>
*
* @see LocalizedModelException
* @see ModelMessageKey
*/
public class FillingBoardSudokuException extends LocalizedModelException {
/**
* Constructs a new FillingBoardSudokuException with a default error message
* key.
* The error message will be retrieved based on
* {@link ModelMessageKey#FILLING_BOARD_ERROR}.
*/
public FillingBoardSudokuException() {
super(ModelMessageKey.FILLING_BOARD_ERROR);
}

/**
* Constructs a new FillingBoardSudokuException with the specified cause and
* a default error message key.
*
* <p>
* The error message will be retrieved based on
* {@link ModelMessageKey#FILLING_BOARD_ERROR}.
* </p>
*
* @param cause the cause of the exception
*/
public FillingBoardSudokuException(Throwable cause) {
super(ModelMessageKey.FILLING_BOARD_ERROR, cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,53 @@
package sudoku.model.exceptions;

public class InvalidSudokuException extends Exception {
public InvalidSudokuException(String message) {
super(message);
/**
* Exception thrown when a trouble within the Sudoku board is determined.
* This exception typically indicates that the Sudoku board does not adhere
* to the rules of Sudoku, such as having duplicate numbers in rows, columns,
* or boxes.
*
* <p>
* This exception extends {@link LocalizedModelException}, which provides
* support
* for localized error messages based on {@link ModelMessageKey}.
* </p>
*
* <p>
* The default error message is retrieved using the
* {@link ModelMessageKey#INVALID_SUDOKU_ERROR}
* key from the resource bundle.
* </p>
*
* <p>
* This exception can optionally wrap another throwable as its cause, providing
* additional context or information about the error.
* </p>
*
* @see LocalizedModelException
* @see ModelMessageKey
*/
public class InvalidSudokuException extends LocalizedModelException {
/**
* Constructs a new InvalidSudokuException with a default error message key.
* The error message will be retrieved based on
* {@link ModelMessageKey#INVALID_SUDOKU_ERROR}.
*/
public InvalidSudokuException() {
super(ModelMessageKey.INVALID_SUDOKU_ERROR);
}

/**
* Constructs a new InvalidSudokuException with the specified cause and
* a default error message key.
*
* <p>
* The error message will be retrieved based on
* {@link ModelMessageKey#INVALID_SUDOKU_ERROR}.
* </p>
*
* @param cause the cause of the exception
*/
public InvalidSudokuException(Throwable cause) {
super(ModelMessageKey.INVALID_SUDOKU_ERROR, cause);
}
}
Loading

0 comments on commit a65fe49

Please sign in to comment.