Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions nbactions.xml
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>clean</goal>
<goal>javafx:run</goal>
</goals>
</action>
<action>
<actionName>debug</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:run@ide-debug</goal>
</goals>
<properties>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:run@ide-profile</goal>
</goals>
</action>
<action>
<actionName>CUSTOM-jlink</actionName>
<displayName>jlink</displayName>
<goals>
<goal>clean</goal>
<!-- compile not needed with javafx-maven-plugin v0.0.5 -->
<goal>compile</goal>
<goal>javafx:jlink</goal>
</goals>
</action>
</actions>
<actions>
<action>
<actionName>run</actionName>
<packagings>
<packaging>jar</packaging>
</packagings>
<goals>
<goal>clean</goal>
<goal>javafx:run</goal>
</goals>
</action>
<action>
<actionName>debug</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:run@ide-debug</goal>
</goals>
<properties>
<jpda.listen>true</jpda.listen>
</properties>
</action>
<action>
<actionName>profile</actionName>
<goals>
<goal>clean</goal>
<goal>javafx:run@ide-profile</goal>
</goals>
</action>
<action>
<actionName>CUSTOM-jlink</actionName>
<displayName>jlink</displayName>
<goals>
<goal>clean</goal>
<!-- compile not needed with javafx-maven-plugin v0.0.5 -->
<goal>compile</goal>
<goal>javafx:jlink</goal>
</goals>
</action>
</actions>
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<version>3.13.0</version>
<configuration>
<source>17</source>
<target>17</target>
<release>25</release>
</configuration>
</plugin>

Expand All @@ -75,7 +74,8 @@
<configuration>
<options>
<option>--enable-native-access=javafx.graphics</option>
<option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000</option>
<option>-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=localhost:8000
</option>
</options>
</configuration>
</execution>
Expand All @@ -87,4 +87,4 @@
</plugins>
</build>

</project>
</project>
36 changes: 22 additions & 14 deletions src/main/java/com/mycompany/tictactoeserver/App.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
package com.mycompany.tictactoeserver;

import com.mycompany.tictactoeserver.datasource.database.Database;
import com.mycompany.tictactoeserver.domain.exception.DatabaseConnectionException;
import com.mycompany.tictactoeserver.domain.exception.ExceptionHandlerMiddleware;
import java.io.IOException;
import java.net.URL;
import com.mycompany.tictactoeserver.domain.services.communication.MessageRouter;
import com.mycompany.tictactoeserver.domain.utils.exception.DatabaseConnectionException;
import com.mycompany.tictactoeserver.domain.utils.exception.ExceptionHandlerMiddleware;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;

public class App extends Application {

private static Scene scene;

@Override
public void start(Stage stage) throws IOException {
try {
Database db = Database.getInstance();
db.connect();
scene = new Scene(loadFXML("presentation/main_screen"), 640, 480);
stage.setScene(scene);
stage.show();
} catch (DatabaseConnectionException ex) {
System.getLogger(App.class.getName()).log(System.Logger.Level.ERROR, (String) null, ex);
}
setupDependencies();
scene = new Scene(loadFXML("presentation/main_screen"), 640, 480);
stage.setScene(scene);
stage.show();
}

static void setRoot(String fxml) throws IOException {
Expand All @@ -41,7 +38,7 @@ private static Parent loadFXML(String fxml) throws IOException {
return fxmlLoader.load();
}

public static void main(String[] args) {
static void main(String[] args) {

Thread.setDefaultUncaughtExceptionHandler((thread, throwable) -> {
if (throwable instanceof Exception) {
Expand All @@ -51,6 +48,17 @@ public static void main(String[] args) {
}
});


launch();
}

private void setupDependencies() {
try {
Database.getInstance().connect();
} catch (DatabaseConnectionException e) {
ExceptionHandlerMiddleware.getInstance().handleException(e);
}

MessageRouter.getInstance();
}
}
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
package com.mycompany.tictactoeserver.datasource.database;

import com.mycompany.tictactoeserver.domain.exception.DatabaeDisConnectionException;
import com.mycompany.tictactoeserver.domain.exception.DatabaseConnectionException;
import com.mycompany.tictactoeserver.domain.utils.exception.DatabaeDisConnectionException;
import com.mycompany.tictactoeserver.domain.utils.exception.DatabaseConnectionException;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class Database {

private static Database instance = null;
private Connection connection = null;

private Database() { }
private static Database instance = null;

private Database() {
}

public static Database getInstance() {

if (instance == null) {
synchronized (Database.class) {
if (instance == null) {
instance = new Database();
}
}
synchronized (Database.class) {
if (instance == null)
instance = new Database();
}

return instance;
}

public void connect() throws DatabaseConnectionException{
if (connection != null) {
public void connect() throws DatabaseConnectionException {
if (connection != null)
return;
}

try {
connection = DriverManager.getConnection(
Expand All @@ -42,13 +40,13 @@ public void connect() throws DatabaseConnectionException{
}
}

public void disconnect() throws DatabaeDisConnectionException{
public void disconnect() throws DatabaeDisConnectionException {
try {
if (connection != null && !connection.isClosed()) {
connection.close();
}
} catch (SQLException e) {
throw new DatabaeDisConnectionException(e.getStackTrace());
throw new DatabaeDisConnectionException(e.getStackTrace());
} finally {
connection = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.mycompany.tictactoeserver.datasource.database.Database;
import com.mycompany.tictactoeserver.datasource.model.ActivityPoint;
import com.mycompany.tictactoeserver.domain.exception.ActiveSessionExistsException;
import com.mycompany.tictactoeserver.domain.exception.ActivityNotFoundException;
import com.mycompany.tictactoeserver.domain.exception.DataAccessException;
import com.mycompany.tictactoeserver.domain.utils.exception.ActiveSessionExistsException;
import com.mycompany.tictactoeserver.domain.utils.exception.ActivityNotFoundException;
import com.mycompany.tictactoeserver.domain.utils.exception.DataAccessException;

import java.sql.*;
import java.time.LocalDateTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@
*
* @author sheri
*/

import com.mycompany.tictactoeserver.datasource.database.Database;
import com.mycompany.tictactoeserver.datasource.model.Player;
import com.mycompany.tictactoeserver.domain.exception.ExceptionHandlerMiddleware;
import com.mycompany.tictactoeserver.domain.exception.PlayerDeletionException;
import com.mycompany.tictactoeserver.domain.exception.PlayerInsertionException;
import com.mycompany.tictactoeserver.domain.exception.PlayerNotFoundException;
import com.mycompany.tictactoeserver.domain.exception.PlayerUpdateException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mycompany.tictactoeserver.domain.utils.exception.*;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -65,10 +59,10 @@ public Player findByUsername(String username) {
try (PreparedStatement stmt = connection.prepareStatement(sql)) {
stmt.setString(1, username);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {return mapRow(rs);}

else {
return null;
if (rs.next()) {
return mapRow(rs);
} else {
return null;
}
} catch (SQLException e) {
exceptionHandler.handleException(new PlayerNotFoundException(e.getStackTrace()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com.mycompany.tictactoeserver.datasource.database.dao;

import com.mycompany.tictactoeserver.datasource.database.Database;
import com.mycompany.tictactoeserver.domain.exception.*;
import com.mycompany.tictactoeserver.datasource.model.Room;
import com.mycompany.tictactoeserver.domain.utils.exception.RoomCreationException;
import com.mycompany.tictactoeserver.domain.utils.exception.RoomNotFoundException;
import com.mycompany.tictactoeserver.domain.utils.exception.RoomUpdationException;

import java.sql.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
/**
*
* @author mahmoud
* @author mahmoud
*/
public class RoomDao {
public class RoomDAO {


private final Connection connection;

public RoomDao() {
public RoomDAO() {
this.connection = Database.getInstance().getConnection();
}

public void createRoom(Room room) throws RoomCreationException {
String sqlCreation =
"INSERT INTO ROOM (id, first_player_id, second_player_id, status, created_at) " +
"VALUES (?, ?, ?, ?, ?)";
String sqlCreation =
"INSERT INTO ROOM (id, first_player_id, second_player_id, status, created_at) " +
"VALUES (?, ?, ?, ?, ?)";
try (PreparedStatement roomCreationStatement = connection.prepareStatement(sqlCreation)) {
roomCreationStatement.setString(1, room.getId());
roomCreationStatement.setString(2, room.getFirstPlayerId());
Expand All @@ -40,24 +40,24 @@ public void createRoom(Room room) throws RoomCreationException {
throw new RoomCreationException(ex.getStackTrace());
}
}


public void updateRoom(Room room) throws RoomUpdationException {
String sqlUpdate = "UPDATE ROOM SET first_player_id = ?, second_player_id = ?, winner_id = ?, status = ?, created_at = ? WHERE id = ?";

try (PreparedStatement ps = connection.prepareStatement(sqlUpdate)) {
ps.setString(1, room.getFirstPlayerId());
ps.setString(2, room.getSecondPlayerId());
ps.setString(3, room.getWinnerId());
ps.setInt(4, room.getStatus());
ps.setTimestamp(5, Timestamp.valueOf(room.getCreatedAt()));
ps.setString(6, room.getId());
public void updateRoom(Room room) throws RoomUpdationException {
String sqlUpdate = "UPDATE ROOM SET first_player_id = ?, second_player_id = ?, winner_id = ?, status = ?, created_at = ? WHERE id = ?";

ps.executeUpdate();
} catch (SQLException ex) {
throw new RoomUpdationException(ex.getStackTrace());
try (PreparedStatement ps = connection.prepareStatement(sqlUpdate)) {
ps.setString(1, room.getFirstPlayerId());
ps.setString(2, room.getSecondPlayerId());
ps.setString(3, room.getWinnerId());
ps.setInt(4, room.getStatus());
ps.setTimestamp(5, Timestamp.valueOf(room.getCreatedAt()));
ps.setString(6, room.getId());

ps.executeUpdate();
} catch (SQLException ex) {
throw new RoomUpdationException(ex.getStackTrace());
}
}
}

public Room findRoomById(String roomId) throws RoomNotFoundException {
String sqlSearchingByID = "SELECT * FROM ROOM WHERE id = ?";
Expand Down
Loading