mirror of
https://github.com/Akomry/sae302_applicom.git
synced 2025-12-06 08:43:54 +00:00
feat(database): persistence des posts dans une base de données sqlite
This commit is contained in:
parent
80885345fe
commit
69f8d23735
6 changed files with 135 additions and 4 deletions
|
|
@ -6,6 +6,7 @@ module rtgre.chat {
|
|||
requires javafx.swing;
|
||||
requires net.synedra.validatorfx;
|
||||
requires org.json;
|
||||
requires java.sql;
|
||||
|
||||
|
||||
opens rtgre.chat to javafx.fxml;
|
||||
|
|
|
|||
|
|
@ -502,8 +502,4 @@ public class ChatController implements Initializable {
|
|||
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;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
import java.util.Vector;
|
||||
|
||||
import static rtgre.chat.ChatApplication.LOGGER;
|
||||
|
||||
public class PostVector extends Vector<Post> {
|
||||
|
||||
public Post getPostById(UUID uuid) {
|
||||
|
|
@ -24,4 +28,21 @@ public class PostVector extends Vector<Post> {
|
|||
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;
|
||||
|
||||
import javafx.scene.chart.PieChart;
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
import rtgre.chat.ChatController;
|
||||
|
|
@ -26,6 +27,7 @@ public class ChatServer {
|
|||
private PostVector postVector;
|
||||
private ContactMap contactMap;
|
||||
private RoomMap roomMap;
|
||||
private DatabaseApi database;
|
||||
|
||||
static {
|
||||
try {
|
||||
|
|
@ -59,6 +61,7 @@ public class ChatServer {
|
|||
contactMap.loadDefaultContacts();
|
||||
roomMap.loadDefaultRooms();
|
||||
roomMap.setLoginSets();
|
||||
postVector.loadPosts();
|
||||
}
|
||||
|
||||
public void close() throws IOException {
|
||||
|
|
@ -358,6 +361,11 @@ public class ChatServer {
|
|||
sendEventToContact(contactMap.getContact(post.getTo()), postEvent);
|
||||
|
||||
postVector.add(post);
|
||||
|
||||
database = new DatabaseApi();
|
||||
database.addPost(post);
|
||||
database.close();
|
||||
|
||||
LOGGER.info("Fin de doMessage:dm");
|
||||
} else {
|
||||
Post post = new Post(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue