From d29548bd7d04375578361f6a75659c046a270b75 Mon Sep 17 00:00:00 2001 From: WillemDalton Date: Tue, 11 Nov 2025 16:42:39 -0500 Subject: fixed exporting xml --- data/personaldb.json | 14 +- .../userinput/menus/ImportExportMenu.java | 2 + src/main/java/design/persistence/JSONHandler.java | 2 +- .../design/persistence/JSONPersonalDatabase.java | 11 +- .../java/design/persistence/PersonalDatabase.java | 4 - src/main/java/design/persistence/XMLHandler.java | 11 +- test.json | 150 +++++++++++++++++++++ test.xml | 121 +++++++++++++++++ thing.json | 150 +++++++++++++++++++++ 9 files changed, 449 insertions(+), 16 deletions(-) create mode 100644 test.json create mode 100644 test.xml create mode 100644 thing.json diff --git a/data/personaldb.json b/data/personaldb.json index 51c0ff7..6d14087 100644 --- a/data/personaldb.json +++ b/data/personaldb.json @@ -9,6 +9,16 @@ "rounds": [], "invites": [] }, + { + "clubs": [], + "nextClubId": 1, + "username": "GUYHERE", + "passwordHash": 3556498, + "fullName": "GUYHERE", + "courses": [], + "rounds": [], + "invites": [] + }, { "clubs": [ { @@ -91,11 +101,11 @@ } ], "currentHoleIndex": 2, + "totalDistance": 1108.0, "currentHole": { "number": 3, "par": 4 }, - "totalDistance": 1108.0, "totalSwings": 5 }, { @@ -127,11 +137,11 @@ } ], "currentHoleIndex": 9, + "totalDistance": 204.0, "currentHole": { "number": 10, "par": 3 }, - "totalDistance": 204.0, "totalSwings": 1 } ], diff --git a/src/main/java/design/controller/userinput/menus/ImportExportMenu.java b/src/main/java/design/controller/userinput/menus/ImportExportMenu.java index db861c5..dbad0e3 100644 --- a/src/main/java/design/controller/userinput/menus/ImportExportMenu.java +++ b/src/main/java/design/controller/userinput/menus/ImportExportMenu.java @@ -69,6 +69,8 @@ public class ImportExportMenu extends Menu { } catch (IOException e) { System.err.println("I/O error: " + e.getMessage()); } + + new ImportExportMenu().present(); } private static String getExtension(String fileName) { diff --git a/src/main/java/design/persistence/JSONHandler.java b/src/main/java/design/persistence/JSONHandler.java index 06ed202..419d080 100644 --- a/src/main/java/design/persistence/JSONHandler.java +++ b/src/main/java/design/persistence/JSONHandler.java @@ -7,7 +7,7 @@ public class JSONHandler implements DataHandler { public void exportPersonalData(File fileName) throws IOException { - JSONPersonalDatabase.instance().exportData(); // fix this + JSONPersonalDatabase.instance().exportData(fileName); // fix this } public void importPersonalData(File fileName) throws IOException { diff --git a/src/main/java/design/persistence/JSONPersonalDatabase.java b/src/main/java/design/persistence/JSONPersonalDatabase.java index 2f003cb..8d263fc 100644 --- a/src/main/java/design/persistence/JSONPersonalDatabase.java +++ b/src/main/java/design/persistence/JSONPersonalDatabase.java @@ -13,6 +13,8 @@ import design.model.League; import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; import java.util.HashMap; import java.util.Map; @@ -99,13 +101,12 @@ public class JSONPersonalDatabase implements PersonalDatabase { save(); } - @Override - public File exportData() throws IOException{ - save(); - return file; + + public File exportData(File newFile) throws IOException{ + Files.copy(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + return newFile; } - @Override public void importData(File newFile) throws IOException { Golfer[] newGolfers = mapper.readValue(newFile, Golfer[].class); cache.clear(); diff --git a/src/main/java/design/persistence/PersonalDatabase.java b/src/main/java/design/persistence/PersonalDatabase.java index 186474e..63f9182 100644 --- a/src/main/java/design/persistence/PersonalDatabase.java +++ b/src/main/java/design/persistence/PersonalDatabase.java @@ -19,8 +19,4 @@ public interface PersonalDatabase { void removeGolfer(Golfer golfer) throws IOException; void updateGolfer(Golfer golfer) throws IOException; - - void importData(File newFile) throws IOException; - - File exportData() throws IOException; } diff --git a/src/main/java/design/persistence/XMLHandler.java b/src/main/java/design/persistence/XMLHandler.java index 413defc..dc67687 100644 --- a/src/main/java/design/persistence/XMLHandler.java +++ b/src/main/java/design/persistence/XMLHandler.java @@ -3,7 +3,9 @@ import java.io.File; import java.io.IOException; import design.model.DataHandler; +import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import design.model.Golfer; import com.fasterxml.jackson.dataformat.xml.XmlMapper; @@ -16,10 +18,11 @@ public class XMLHandler implements DataHandler private final XmlMapper xmlMapper = new XmlMapper(); public void exportPersonalData(File fileName) throws IOException { - File jsonData = JSONPersonalDatabase.instance().exportData(); - Golfer[] golfers = jsonMapper.readValue(jsonData, Golfer[].class); - File xmlFile = new File(fileName + ".xml"); - xmlMapper.writerWithDefaultPrettyPrinter().writeValue(xmlFile, golfers); + File jsonData = JSONPersonalDatabase.instance().exportData(fileName); + JsonNode golfersNode = jsonMapper.readTree(jsonData); + ObjectNode root = xmlMapper.createObjectNode(); + root.set("golfers", golfersNode); + xmlMapper.writerWithDefaultPrettyPrinter().writeValue(fileName, root); } public void importPersonalData(File fileName) throws IOException { diff --git a/test.json b/test.json new file mode 100644 index 0000000..febfcc3 --- /dev/null +++ b/test.json @@ -0,0 +1,150 @@ +[ + { + "clubs": [], + "nextClubId": 1, + "username": "test", + "passwordHash": 3556498, + "fullName": "test", + "courses": [], + "rounds": [], + "invites": [] + }, + { + "clubs": [], + "nextClubId": 1, + "username": "GUYHERE", + "passwordHash": 3556498, + "fullName": "GUYHERE", + "courses": [], + "rounds": [], + "invites" : [] + }, + { + "clubs": [ + { + "id": 1, + "manufacture": "Bobby", + "nickname": "swen 261", + "clubType": "DRIVER" + }, + { + "id": 2, + "manufacture": "Bobby", + "nickname": "swen 262", + "clubType": "PUTTER" + }, + { + "id": 3, + "manufacture": "sowclub", + "nickname": "man", + "clubType": "WOOD" + } + ], + "nextClubId": 4, + "username": "john_doe", + "passwordHash": 46792755, + "fullName": "John Doe", + "courses": [ + 2, + 1 + ], + "rounds": [ + { + "course": 1, + "dateTime": [ + 2025, + 10, + 7, + 13, + 54, + 37, + 429963500 + ], + "startingHole": { + "number": 1, + "par": 5 + }, + "plays": [ + { + "holeNumber": 1, + "swings": [ + { + "distance": 100, + "clubUsed": 1 + }, + { + "distance": 5, + "clubUsed": 2 + }, + { + "distance": 1, + "clubUsed": 2 + } + ], + "swingCount": 3, + "distance": 106 + }, + { + "holeNumber": 2, + "swings": [ + { + "distance": 1000, + "clubUsed": 1 + }, + { + "distance": 2, + "clubUsed": 1 + } + ], + "swingCount": 2, + "distance": 1002 + } + ], + "currentHoleIndex": 2, + "totalDistance": 1108.0, + "totalSwings": 5, + "currentHole": { + "number": 3, + "par": 4 + } + }, + { + "course": 1, + "dateTime": [ + 2025, + 10, + 8, + 20, + 19, + 38, + 968996400 + ], + "startingHole": { + "number": 9, + "par": 3 + }, + "plays": [ + { + "holeNumber": 9, + "swings": [ + { + "distance": 204, + "clubUsed": 1 + } + ], + "swingCount": 1, + "distance": 204 + } + ], + "currentHoleIndex": 9, + "totalDistance": 204.0, + "totalSwings": 1, + "currentHole": { + "number": 10, + "par": 3 + } + } + ], + "invites": [] + } +] \ No newline at end of file diff --git a/test.xml b/test.xml new file mode 100644 index 0000000..e710b47 --- /dev/null +++ b/test.xml @@ -0,0 +1,121 @@ + + + 1 + test + 3556498 + test + + + 1 + GUYHERE + 3556498 + GUYHERE + + + + 1 + Bobby + swen 261 + DRIVER + + + 2 + Bobby + swen 262 + PUTTER + + + 3 + sowclub + man + WOOD + + 4 + john_doe + 46792755 + John Doe + 2 + 1 + + 1 + 2025 + 10 + 7 + 13 + 54 + 37 + 429963500 + + 1 + 5 + + + 1 + + 100 + 1 + + + 5 + 2 + + + 1 + 2 + + 3 + 106 + + + 2 + + 1000 + 1 + + + 2 + 1 + + 2 + 1002 + + 2 + 1108.0 + + 3 + 4 + + 5 + + + 1 + 2025 + 10 + 8 + 20 + 19 + 38 + 968996400 + + 9 + 3 + + + 9 + + 204 + 1 + + 1 + 204 + + 9 + 204.0 + + 10 + 3 + + 1 + + + diff --git a/thing.json b/thing.json new file mode 100644 index 0000000..6d14087 --- /dev/null +++ b/thing.json @@ -0,0 +1,150 @@ +[ + { + "clubs": [], + "nextClubId": 1, + "username": "test", + "passwordHash": 3556498, + "fullName": "test", + "courses": [], + "rounds": [], + "invites": [] + }, + { + "clubs": [], + "nextClubId": 1, + "username": "GUYHERE", + "passwordHash": 3556498, + "fullName": "GUYHERE", + "courses": [], + "rounds": [], + "invites": [] + }, + { + "clubs": [ + { + "id": 1, + "manufacture": "Bobby", + "nickname": "swen 261", + "clubType": "DRIVER" + }, + { + "id": 2, + "manufacture": "Bobby", + "nickname": "swen 262", + "clubType": "PUTTER" + }, + { + "id": 3, + "manufacture": "sowclub", + "nickname": "man", + "clubType": "WOOD" + } + ], + "nextClubId": 4, + "username": "john_doe", + "passwordHash": 46792755, + "fullName": "John Doe", + "courses": [ + 2, + 1 + ], + "rounds": [ + { + "course": 1, + "dateTime": [ + 2025, + 10, + 7, + 13, + 54, + 37, + 429963500 + ], + "startingHole": { + "number": 1, + "par": 5 + }, + "plays": [ + { + "holeNumber": 1, + "swings": [ + { + "distance": 100, + "clubUsed": 1 + }, + { + "distance": 5, + "clubUsed": 2 + }, + { + "distance": 1, + "clubUsed": 2 + } + ], + "swingCount": 3, + "distance": 106 + }, + { + "holeNumber": 2, + "swings": [ + { + "distance": 1000, + "clubUsed": 1 + }, + { + "distance": 2, + "clubUsed": 1 + } + ], + "swingCount": 2, + "distance": 1002 + } + ], + "currentHoleIndex": 2, + "totalDistance": 1108.0, + "currentHole": { + "number": 3, + "par": 4 + }, + "totalSwings": 5 + }, + { + "course": 1, + "dateTime": [ + 2025, + 10, + 8, + 20, + 19, + 38, + 968996400 + ], + "startingHole": { + "number": 9, + "par": 3 + }, + "plays": [ + { + "holeNumber": 9, + "swings": [ + { + "distance": 204, + "clubUsed": 1 + } + ], + "swingCount": 1, + "distance": 204 + } + ], + "currentHoleIndex": 9, + "totalDistance": 204.0, + "currentHole": { + "number": 10, + "par": 3 + }, + "totalSwings": 1 + } + ], + "invites": [] + } +] \ No newline at end of file -- cgit v1.2.3