From 26be3ca8ff4c23590fd74d87a7d55cd4ebefa7da Mon Sep 17 00:00:00 2001 From: Maximilien LEDOUX Date: Tue, 8 Mar 2022 09:32:26 +0100 Subject: [PATCH] =?UTF-8?q?Sauvegarde=20et=20lecture=20du=20fichier=20de?= =?UTF-8?q?=20synchronisation=20->=20M=C3=A9thodes=20save=20et=20load=20te?= =?UTF-8?q?rmin=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/lightcontainer/storage/Adapter.java | 11 ++---- .../lightcontainer/storage/JsonAdapter.java | 11 ++++-- .../lightcontainer/storage/Repository.java | 34 +++++++++++++++++-- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/lightcontainer/storage/Adapter.java b/app/src/main/java/lightcontainer/storage/Adapter.java index 7ffb90d..30843ab 100644 --- a/app/src/main/java/lightcontainer/storage/Adapter.java +++ b/app/src/main/java/lightcontainer/storage/Adapter.java @@ -2,15 +2,8 @@ package lightcontainer.storage; public interface Adapter { - /** - * @param appData The appData to stringify - * @return The string representation of AppData - */ - String toString(AppData appData); - /** - * @param appDataString The appData string to parse - * @return AppData - */ + String toString(); + AppData fromString(String appDataString); } diff --git a/app/src/main/java/lightcontainer/storage/JsonAdapter.java b/app/src/main/java/lightcontainer/storage/JsonAdapter.java index 55a4792..af6be59 100644 --- a/app/src/main/java/lightcontainer/storage/JsonAdapter.java +++ b/app/src/main/java/lightcontainer/storage/JsonAdapter.java @@ -6,8 +6,14 @@ import java.util.*; public class JsonAdapter implements Adapter { + private AppData appData; + + public JsonAdapter(AppData appData) { + this.appData = appData; + } + @Override - public String toString(AppData appData) { + public String toString() { return addData(appData); } @@ -81,7 +87,8 @@ public class JsonAdapter implements Adapter { for (User user : users) { appData.addUser(user); } - return appData; + this.appData = appData; + return this.appData; } catch (JsonParseException parseException) { System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log } diff --git a/app/src/main/java/lightcontainer/storage/Repository.java b/app/src/main/java/lightcontainer/storage/Repository.java index 6757ff5..778b979 100644 --- a/app/src/main/java/lightcontainer/storage/Repository.java +++ b/app/src/main/java/lightcontainer/storage/Repository.java @@ -1,14 +1,44 @@ package lightcontainer.storage; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.nio.file.StandardOpenOption; + public class Repository { //static final String CONFIG_FILE_PATH = "../../resources/config.json"; void save(String filePath, Adapter adapter) { - + if (filePath != null) { + String jsonAppData = adapter.toString(); + try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) { + bufferedWriter.write(jsonAppData); + bufferedWriter.flush(); + } catch (IOException e) { + System.out.println("Error while saving configuration file !"); + } + } } AppData load(String filePath, Adapter adapter) { - return null; + String jsonString = readFile(filePath); + return adapter.fromString(jsonString); + } + + private String readFile(String filePath) { + StringBuilder builder = new StringBuilder(); + try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8)) { + while (reader.ready()) { + builder.append(reader.readLine()); + } + } catch (IOException e) { + System.out.println("Error while reading configuration file"); + builder.setLength(0); + } + return builder.toString(); } }