From bb8a796bd42659d7577512097bf82e6afd63733a Mon Sep 17 00:00:00 2001 From: Georg-Notebook Date: Wed, 14 Apr 2021 22:11:53 +0200 Subject: [PATCH] DB -> Tour add, edit, del --- src/tourplaner/Main.java | 2 + src/tourplaner/business/TourPlaner.java | 45 ++++++++++++++++++--- src/tourplaner/data/DbConnect.java | 30 +++++++++++++- src/tourplaner/object/Tour.java | 10 +++++ src/tourplaner/ui/TourplanerController.java | 2 + src/tourplaner/viewmodels/ViewModel.java | 14 +++++-- 6 files changed, 92 insertions(+), 11 deletions(-) diff --git a/src/tourplaner/Main.java b/src/tourplaner/Main.java index fee0c1c..c55b8ff 100644 --- a/src/tourplaner/Main.java +++ b/src/tourplaner/Main.java @@ -8,6 +8,7 @@ import javafx.scene.image.Image; import javafx.stage.Stage; import tourplaner.business.ConfigHelper; +import tourplaner.business.TourPlaner; import java.io.IOException; @@ -23,6 +24,7 @@ public class Main extends Application { Parent root = FXMLLoader.load(Main.class.getResource("tourplaner.fxml")); primaryStage.setTitle(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "apptitle")); primaryStage.getIcons().add(new Image("file:"+ ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "logo"))); + TourPlaner.init(); primaryStage.setScene(new Scene(root, 1500, 1000)); primaryStage.show(); } diff --git a/src/tourplaner/business/TourPlaner.java b/src/tourplaner/business/TourPlaner.java index 36a60d5..beae0a1 100644 --- a/src/tourplaner/business/TourPlaner.java +++ b/src/tourplaner/business/TourPlaner.java @@ -13,20 +13,53 @@ import java.util.ArrayList; public class TourPlaner{ private Logger logger; - public TourPlaner(){ -// LogHelper.info(ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "message"), -// ConfigHelper.getIniString(ConfigHelper.getStandartConfig(), "start", "app")); + /** + * Erstellt alle DB Tabellen falls nötig + */ + public static void init(){ new DbConnect().init(); - } - public ArrayList getAllTours(){ + /** + * Holt alle touren aus der DB + * @return Alle touren als ArrayList + */ + public static ArrayList getAllTours(){ DbConnect dbConnect = new DbConnect(); ArrayList touren = dbConnect.getAllTouren(); return touren; } - public String getMapJson(String start, String ziel){ + /** + * Bearbeitet eine Tour + * @param oldname Alter Tourname + * @param tour Neuer Tourname + * @return false bei error + */ + public static boolean editTour(String oldname, Tour tour){ + DbConnect dbConnect = new DbConnect(); + return dbConnect.editTour(oldname, tour); + } + + public static boolean delTour(String tourname){ + DbConnect dbConnect = new DbConnect(); + return dbConnect.delTour(tourname); + } + + /** + * Fügt eine Tour hinzu + * @param newTour Neue Tour + * @return false bei error + */ + public static boolean addTour(Tour newTour){ + DbConnect dbConnect = new DbConnect(); + return dbConnect.addTour(newTour); + } + + + + public static String getMapJson(String start, String ziel){ + // TODO: 14.04.2021 Map Quest return start + " " + ziel; } } diff --git a/src/tourplaner/data/DbConnect.java b/src/tourplaner/data/DbConnect.java index 65c0910..7112486 100644 --- a/src/tourplaner/data/DbConnect.java +++ b/src/tourplaner/data/DbConnect.java @@ -38,6 +38,34 @@ public class DbConnect { } + /** + * Bearbeitet eine Tour + * @param oldname Alter Tour name + * @param tour Neues Tour Object + * @return false bei error + */ + public boolean editTour(String oldname, Tour tour) { + return PostgresHelper.executeUpdate("UPDATE public.tour SET tourname = '"+tour.getName()+"', dauer = 1, mapjson = '"+tour.getMapJson()+"', start = '"+tour.getStart()+"', ziel = '"+tour.getZiel()+"', strecke = "+tour.getStrecke()+" WHERE tourname ='" + oldname + "';"); + } + + /** + * Fügt eine Tour hinzu + * @param tour Neue Tour + * @return false bei error + */ + public boolean addTour(Tour tour){ + return PostgresHelper.executeUpdate("INSERT INTO public.tour (tourname, dauer, mapjson, start, ziel, strecke) VALUES ('"+tour.getName()+"', "+tour.getDauer()+", '"+tour.getMapJson()+"', '"+tour.getStart()+"', '"+tour.getZiel()+"', "+tour.getStrecke()+")"); + } + + /** + * Löscht eine Tour anhand des Tournamens + * @param tourname Tourname der zu löschen ist + * @return false bei error + */ + public boolean delTour(String tourname){ + return PostgresHelper.executeUpdate("DELETE FROM public.tour WHERE tourname = '"+tourname+"'"); + } + /** * Holt alle Touren aus der Datenbank * @return Null bei fehler, sonst eine List aus den IDs @@ -50,7 +78,7 @@ public class DbConnect { ArrayList touren = new ArrayList<>(); try { stmt = this.c.createStatement(); - ResultSet rs = stmt.executeQuery("select tourname, dauer, mapjson, start, ziel, strecke from tour;"); + ResultSet rs = stmt.executeQuery("select * from tour;"); while (rs.next()) { tourname = rs.getString("tourname"); mapjson = rs.getString("mapjson"); diff --git a/src/tourplaner/object/Tour.java b/src/tourplaner/object/Tour.java index 01524c9..6c5cbae 100644 --- a/src/tourplaner/object/Tour.java +++ b/src/tourplaner/object/Tour.java @@ -21,6 +21,16 @@ public class Tour { this.log = new ArrayList<>(); } + public void setTour(Tour tour){ + this.dauer = tour.getDauer(); + this.mapJson = tour.getMapJson(); + this.strecke = tour.getStrecke(); + this.name = tour.getName(); + this.start = tour.getStart(); + this.ziel = tour.getZiel(); + this.log = tour.getLogs(); + } + /** * Holt einen einzigen Log Eintrag anhand der Id * @param id Id des Eintrags der zu besorgen ist diff --git a/src/tourplaner/ui/TourplanerController.java b/src/tourplaner/ui/TourplanerController.java index aa3a267..8cfc812 100644 --- a/src/tourplaner/ui/TourplanerController.java +++ b/src/tourplaner/ui/TourplanerController.java @@ -49,6 +49,8 @@ public class TourplanerController implements Initializable { @FXML private void editTourBtn(){ this.viewModel.editTour(); + beschreibungTableView.getItems().removeIf(s -> true); //Leert die Table View komplett + beschreibungTableView.getItems().add(this.viewModel.getTour(this.viewModel.getSelectedTour().getName())); } @FXML diff --git a/src/tourplaner/viewmodels/ViewModel.java b/src/tourplaner/viewmodels/ViewModel.java index 5db0c9a..45f5bf2 100644 --- a/src/tourplaner/viewmodels/ViewModel.java +++ b/src/tourplaner/viewmodels/ViewModel.java @@ -60,8 +60,11 @@ public class ViewModel { this.tourData.removeIf(tour -> tour.getName().equals(this.selectedTour.getName())); this.tourNamen.removeIf(tour -> tour.equals(this.selectedTour.getName())); - this.tourData.add(new Tour(this.neueTourName, "TBD", "TBD", 0, this.neueTourStart, this.neueTourZiel)); + Tour newTour = new Tour(this.neueTourName, "TBD", "TBD", 0, this.neueTourStart, this.neueTourZiel); + this.tourData.add(newTour); this.tourNamen.add(this.neueTourName); + LogHelper.info("Edit Tour: " + this.neueTourName + " upload zu Postgres war: " + TourPlaner.editTour(this.selectedTour.getName(), newTour), "ViewModel - EditTour"); + selectTour(this.neueTourName); } } } @@ -140,7 +143,7 @@ public class ViewModel { return false; } } - return false; + return true; } @@ -152,8 +155,10 @@ public class ViewModel { tourStartInput(""); tourZielInput(""); if (getTour(this.neueTourName) == null) { - tourData.add(new Tour(this.neueTourName, "TBD", "TBD", 0, this.neueTourStart, this.neueTourZiel)); + Tour newTour = new Tour(this.neueTourName, "1", "1", 0, this.neueTourStart, this.neueTourZiel); + tourData.add(newTour); tourNamen.add(this.neueTourName); + TourPlaner.addTour(newTour); } this.neueTourStart = null; this.neueTourZiel = null; @@ -483,7 +488,7 @@ public class ViewModel { public ObservableList getTourData() { tourData.removeAll(); // ObservableList data = ; - tourData.addAll(new TourPlaner().getAllTours()); + tourData.addAll(TourPlaner.getAllTours()); return tourData; } @@ -497,6 +502,7 @@ public class ViewModel { tourNamen.removeIf(s -> s.equals(tourname)); logData.removeIf(s -> true); setSelectedTour(null); + TourPlaner.delTour(tourname); }catch (NullPointerException e){ LogHelper.error(ConfigHelper.getLangIniString("keinetourselected"), ConfigHelper.getLangIniString("deltournoselect"));