mirror of
https://github.com/Akomry/sae302_applicom.git
synced 2025-12-06 11:43:53 +00:00
Merge branch 'dev-sqlpost' into 'dev'
feat(database): persistance des posts dans une base de données sqlite See merge request iut_rt/but2/sae302-applicom/bouclyma!13
This commit is contained in:
commit
d0bea3f7e8
6 changed files with 135 additions and 4 deletions
|
|
@ -59,6 +59,11 @@
|
||||||
<artifactId>json</artifactId>
|
<artifactId>json</artifactId>
|
||||||
<version>20240303</version>
|
<version>20240303</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.xerial</groupId>
|
||||||
|
<artifactId>sqlite-jdbc</artifactId>
|
||||||
|
<version>3.48.0.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ module rtgre.chat {
|
||||||
requires javafx.swing;
|
requires javafx.swing;
|
||||||
requires net.synedra.validatorfx;
|
requires net.synedra.validatorfx;
|
||||||
requires org.json;
|
requires org.json;
|
||||||
|
requires java.sql;
|
||||||
|
|
||||||
|
|
||||||
opens rtgre.chat to javafx.fxml;
|
opens rtgre.chat to javafx.fxml;
|
||||||
|
|
|
||||||
|
|
@ -502,8 +502,4 @@ public class ChatController implements Initializable {
|
||||||
LOGGER.info(contactMap.toString());
|
LOGGER.info(contactMap.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void errorAlert() {
|
|
||||||
new Alert(Alert.AlertType.ERROR, i18nBundle.getString("connectionError")).showAndWait();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
100
chat/src/main/java/rtgre/modeles/DatabaseApi.java
Normal file
100
chat/src/main/java/rtgre/modeles/DatabaseApi.java
Normal file
|
|
@ -0,0 +1,100 @@
|
||||||
|
package rtgre.modeles;
|
||||||
|
|
||||||
|
import javax.xml.transform.Result;
|
||||||
|
import java.io.File;
|
||||||
|
import java.sql.*;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import static rtgre.chat.ChatApplication.LOGGER;
|
||||||
|
|
||||||
|
public class DatabaseApi {
|
||||||
|
private Connection con;
|
||||||
|
private Statement stmt;
|
||||||
|
|
||||||
|
public DatabaseApi() {
|
||||||
|
try {
|
||||||
|
this.con = DriverManager.getConnection("jdbc:sqlite:chat/src/main/resources/rtgre/chat/dbase.db");
|
||||||
|
this.stmt = con.createStatement();
|
||||||
|
initDB(con);
|
||||||
|
LOGGER.info("Database connected!");
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOGGER.severe("Can't connect to database! \n" + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void initDB(Connection con) {
|
||||||
|
try {
|
||||||
|
String sql = "CREATE TABLE IF NOT EXISTS `posts` ("
|
||||||
|
+ " `id` text PRIMARY KEY NOT NULL,"
|
||||||
|
+ " `timestamp` long,"
|
||||||
|
+ " `from` text,"
|
||||||
|
+ " `to` text,"
|
||||||
|
+ " `body` text"
|
||||||
|
+ ");";
|
||||||
|
stmt = con.createStatement();
|
||||||
|
stmt.execute(sql);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOGGER.severe("Cannot initialize database!");
|
||||||
|
System.out.println(e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResultSet getPostById(UUID uuid) {
|
||||||
|
String query = "SELECT * FROM posts WHERE id = " + uuid.toString();
|
||||||
|
try {
|
||||||
|
return stmt.executeQuery(query);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOGGER.severe("Can't get post by id!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResultSet getPostsSince(long timestamp) {
|
||||||
|
String query = "SELECT * FROM posts WHERE timestamp >= " + timestamp + "ORDER BY timestamp DESC";
|
||||||
|
try {
|
||||||
|
return stmt.executeQuery(query);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOGGER.severe("Can't get post since " + timestamp + "!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public ResultSet getPosts() {
|
||||||
|
String query = "SELECT * FROM posts";
|
||||||
|
try {
|
||||||
|
return stmt.executeQuery(query);
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOGGER.severe("Can't get posts!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean addPost(Post post) {
|
||||||
|
String query = "INSERT INTO posts VALUES (?, ?, ?, ?, ?)";
|
||||||
|
try {
|
||||||
|
PreparedStatement pstmt = con.prepareStatement(query);
|
||||||
|
pstmt.setString(1, post.getId().toString());
|
||||||
|
pstmt.setLong(2, post.getTimestamp());
|
||||||
|
pstmt.setString(3, post.getFrom());
|
||||||
|
pstmt.setString(4, post.getTo());
|
||||||
|
pstmt.setString(5, post.getBody());
|
||||||
|
|
||||||
|
pstmt.executeUpdate();
|
||||||
|
return true;
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOGGER.severe("Can't add post!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
try {
|
||||||
|
con.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOGGER.severe("Can't close database connection! Is database connected ?");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,8 +1,12 @@
|
||||||
package rtgre.modeles;
|
package rtgre.modeles;
|
||||||
|
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.Vector;
|
import java.util.Vector;
|
||||||
|
|
||||||
|
import static rtgre.chat.ChatApplication.LOGGER;
|
||||||
|
|
||||||
public class PostVector extends Vector<Post> {
|
public class PostVector extends Vector<Post> {
|
||||||
|
|
||||||
public Post getPostById(UUID uuid) {
|
public Post getPostById(UUID uuid) {
|
||||||
|
|
@ -24,4 +28,21 @@ public class PostVector extends Vector<Post> {
|
||||||
return posts;
|
return posts;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void loadPosts() {
|
||||||
|
try {
|
||||||
|
DatabaseApi database = new DatabaseApi();
|
||||||
|
ResultSet postResult = database.getPosts();
|
||||||
|
while (postResult.next()) {
|
||||||
|
this.add(new Post(
|
||||||
|
UUID.fromString(postResult.getString("id")),
|
||||||
|
postResult.getLong("timestamp"),
|
||||||
|
postResult.getString("from"),
|
||||||
|
postResult.getString("to"),
|
||||||
|
postResult.getString("body")
|
||||||
|
));
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
LOGGER.severe("Cannot load posts!");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package rtgre.server;
|
package rtgre.server;
|
||||||
|
|
||||||
|
import javafx.scene.chart.PieChart;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import rtgre.chat.ChatController;
|
import rtgre.chat.ChatController;
|
||||||
|
|
@ -26,6 +27,7 @@ public class ChatServer {
|
||||||
private PostVector postVector;
|
private PostVector postVector;
|
||||||
private ContactMap contactMap;
|
private ContactMap contactMap;
|
||||||
private RoomMap roomMap;
|
private RoomMap roomMap;
|
||||||
|
private DatabaseApi database;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
|
|
@ -59,6 +61,7 @@ public class ChatServer {
|
||||||
contactMap.loadDefaultContacts();
|
contactMap.loadDefaultContacts();
|
||||||
roomMap.loadDefaultRooms();
|
roomMap.loadDefaultRooms();
|
||||||
roomMap.setLoginSets();
|
roomMap.setLoginSets();
|
||||||
|
postVector.loadPosts();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
|
|
@ -358,6 +361,11 @@ public class ChatServer {
|
||||||
sendEventToContact(contactMap.getContact(post.getTo()), postEvent);
|
sendEventToContact(contactMap.getContact(post.getTo()), postEvent);
|
||||||
|
|
||||||
postVector.add(post);
|
postVector.add(post);
|
||||||
|
|
||||||
|
database = new DatabaseApi();
|
||||||
|
database.addPost(post);
|
||||||
|
database.close();
|
||||||
|
|
||||||
LOGGER.info("Fin de doMessage:dm");
|
LOGGER.info("Fin de doMessage:dm");
|
||||||
} else {
|
} else {
|
||||||
Post post = new Post(
|
Post post = new Post(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue