From dc78d69a0083743a6c1810da44173fab3cda7990 Mon Sep 17 00:00:00 2001 From: Marc Beyer Date: Mon, 31 Jan 2022 23:03:29 +0100 Subject: [PATCH] Refactor --- .../java/events/CreateEventController.java | 20 ++--- .../main/java/events/EditEventController.java | 2 +- .../src/main/java/main/MainApplication.java | 36 +++++---- .../src/main/java/main/MainController.java | 37 ++++----- .../src/main/java/main/OptionController.java | 15 ++-- client/app/src/main/java/ui/EventPane.java | 14 ---- .../main/java/users/CreateUserController.java | 78 +++++++++++-------- .../main/java/users/EditUserController.java | 28 +------ .../src/main/java/users/LoginController.java | 8 +- .../src/main/resources/main/create-event.fxml | 2 +- .../src/main/resources/main/edit-event.fxml | 2 +- client/data/src/main/java/config/Config.java | 41 +++++++++- .../src/main/java/config/ConfigLoader.java | 7 +- .../main/java/container/DataController.java | 43 +++++----- 14 files changed, 177 insertions(+), 156 deletions(-) diff --git a/client/app/src/main/java/events/CreateEventController.java b/client/app/src/main/java/events/CreateEventController.java index e2d2b67..0ad9304 100644 --- a/client/app/src/main/java/events/CreateEventController.java +++ b/client/app/src/main/java/events/CreateEventController.java @@ -20,23 +20,23 @@ import java.util.Locale; public class CreateEventController { @FXML - public GridPane mainGrid; + private GridPane mainGrid; @FXML - public JFXDatePicker datePickerDate; + protected JFXDatePicker datePickerDate; @FXML - public JFXTextField textName; + protected JFXTextField textName; @FXML - public JFXComboBox ComboBoxPriotity; + protected JFXComboBox comboBoxPriority; @FXML - public JFXToggleButton toggleBtnIsFullDay; + private JFXToggleButton toggleBtnIsFullDay; @FXML - public JFXToggleButton toggleBtnIsPrivate; + private JFXToggleButton toggleBtnIsPrivate; @FXML - public Label labelError; + private Label labelError; @FXML - public JFXTimePicker timeStart; + private JFXTimePicker timeStart; @FXML - public JFXTimePicker timeEnd; + private JFXTimePicker timeEnd; public CreateEventController() { @@ -64,7 +64,7 @@ public class CreateEventController { Event event = new Event( textName.getText(), - ComboBoxPriotity.getSelectionModel().getSelectedIndex(), + comboBoxPriority.getSelectionModel().getSelectedIndex(), toggleBtnIsFullDay.isSelected(), toggleBtnIsPrivate.isSelected(), timeStart.getValue(), diff --git a/client/app/src/main/java/events/EditEventController.java b/client/app/src/main/java/events/EditEventController.java index ca3833b..e813221 100644 --- a/client/app/src/main/java/events/EditEventController.java +++ b/client/app/src/main/java/events/EditEventController.java @@ -17,7 +17,7 @@ public class EditEventController extends CreateEventController{ textName.setText(currentEvent.getName()); datePickerDate.setValue(currentEvent.getDate().toLocalDate()); - ComboBoxPriotity.getSelectionModel().select(currentEvent.getPriority()); + comboBoxPriority.getSelectionModel().select(currentEvent.getPriority()); //timeEnd.setValue(currentEvent.getEnd()); } diff --git a/client/app/src/main/java/main/MainApplication.java b/client/app/src/main/java/main/MainApplication.java index 2ed843c..403a853 100644 --- a/client/app/src/main/java/main/MainApplication.java +++ b/client/app/src/main/java/main/MainApplication.java @@ -2,16 +2,13 @@ package main; import config.Config; import config.ConfigLoader; -import helper.Tuple; +import container.DataController; +import container.HttpRequest; import javafx.application.Application; -import javafx.event.ActionEvent; import javafx.fxml.FXMLLoader; -import javafx.scene.Node; import javafx.scene.Scene; import javafx.stage.Modality; import javafx.stage.Stage; -import container.DataController; -import container.HttpRequest; import java.io.IOException; import java.util.Objects; @@ -41,6 +38,9 @@ public class MainApplication extends Application { config.setToken(HttpRequest.TOKEN); ConfigLoader.save(config); } + + DataController.SERVER_URL = config.toServerUrl(); + // Load main-scene loadMainScene(stage); @@ -60,22 +60,30 @@ public class MainApplication extends Application { stage.show(); } - private void loadLoginScene() throws IOException { - FXMLLoader fxmlLoaderLogin = new FXMLLoader(MainApplication.class.getResource("../users/login.fxml")); - Scene sceneLogin = new Scene(fxmlLoaderLogin.load(), 650, 500); - sceneLogin.getStylesheets().add(Objects.requireNonNull( - MainApplication.class.getResource("../users/login.css")).toExternalForm() + private void loadLoginScene() { + loadScene( + "Anmelden", + "../users/login.fxml", + "../users/login.css", + 650, + 500 ); - Stage stageLogin = new Stage(); - stageLogin.setTitle("Anmelden"); - stageLogin.setScene(sceneLogin); - stageLogin.showAndWait(); } public static void main(String[] args) { launch(); } + public static void loadScene( + String title, + String fxml, + String css, + int width, + int height + ) { + loadScene(title, fxml, css, width, height, null); + } + public static void loadScene( String title, String fxml, diff --git a/client/app/src/main/java/main/MainController.java b/client/app/src/main/java/main/MainController.java index 0f8ba7d..541db6c 100644 --- a/client/app/src/main/java/main/MainController.java +++ b/client/app/src/main/java/main/MainController.java @@ -2,45 +2,40 @@ package main; import config.Config; import config.ConfigLoader; +import container.DataController; +import container.Event; +import container.HttpRequest; import events.EditEventController; -import ui.DayPane; -import ui.EventPane; -import ui.SvgBtnCreator; import helper.HttpRequestException; +import javafx.event.ActionEvent; import javafx.fxml.FXML; -import javafx.fxml.FXMLLoader; import javafx.scene.Node; -import javafx.scene.Scene; -import javafx.scene.control.*; +import javafx.scene.control.Alert; +import javafx.scene.control.Button; +import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; -import javafx.stage.Modality; import javafx.stage.Stage; -import container.DataController; -import container.Event; +import ui.DayPane; +import ui.EventPane; +import ui.SvgBtnCreator; -import javafx.event.ActionEvent; -import container.HttpRequest; - -import java.io.IOException; import java.time.Duration; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.ArrayList; -import java.util.Objects; public class MainController { @FXML - public VBox leftNav; + private VBox leftNav; @FXML - public GridPane mainGridPane; + private GridPane mainGridPane; @FXML - public HBox buttonBox; + private HBox buttonBox; @FXML private GridPane calendarGrid; - @FXML private javafx.scene.control.Label LabelMonth; @@ -110,8 +105,7 @@ public class MainController { "create-event.fxml", "create-event.css", 650, - 650, - null + 650 ); updateEvents(); } @@ -122,8 +116,7 @@ public class MainController { "option-view.fxml", "option-view.css", 650, - 600, - null + 600 ); } diff --git a/client/app/src/main/java/main/OptionController.java b/client/app/src/main/java/main/OptionController.java index 9cf0d85..0cf0c33 100644 --- a/client/app/src/main/java/main/OptionController.java +++ b/client/app/src/main/java/main/OptionController.java @@ -27,17 +27,17 @@ import java.util.Optional; public class OptionController { @FXML - public JFXButton updateUserBtn; + private JFXButton updateUserBtn; @FXML - public JFXButton deleteUserBtn; + private JFXButton deleteUserBtn; @FXML - public JFXButton createUserBtn; + private JFXButton createUserBtn; @FXML - public JFXToggleButton saveLoginTBtn; + private JFXToggleButton saveLoginTBtn; @FXML - public Label labelError; + private Label labelError; @FXML - public GridPane mainGrid; + private GridPane mainGrid; private JFXComboBox comboBox; private DataController dataController; @@ -79,8 +79,7 @@ public class OptionController { "../users/create-user.fxml", "../users/create-user.css", 800, - 650, - null + 650 ); } diff --git a/client/app/src/main/java/ui/EventPane.java b/client/app/src/main/java/ui/EventPane.java index ae77e2c..8e74cc2 100644 --- a/client/app/src/main/java/ui/EventPane.java +++ b/client/app/src/main/java/ui/EventPane.java @@ -1,26 +1,12 @@ package ui; -import container.DataController; import container.Event; -import events.EditEventController; -import helper.HttpRequestException; -import javafx.fxml.FXMLLoader; import javafx.geometry.Pos; import javafx.scene.Group; -import javafx.scene.Scene; -import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; -import javafx.stage.Modality; -import javafx.stage.Stage; -import main.MainApplication; - -import java.io.IOException; -import java.time.Duration; -import java.time.LocalDateTime; -import java.util.Objects; public class EventPane extends VBox { diff --git a/client/app/src/main/java/users/CreateUserController.java b/client/app/src/main/java/users/CreateUserController.java index 82f795d..3b5d622 100644 --- a/client/app/src/main/java/users/CreateUserController.java +++ b/client/app/src/main/java/users/CreateUserController.java @@ -16,40 +16,24 @@ import java.util.Objects; public class CreateUserController { - public TextField textName; - public PasswordField textPassword; - public PasswordField textPasswordSecond; - public ToggleButton checkButtonIsAdmin; - public TextField textLogin; - public TextField textForename; - public Label labelError; + @FXML + protected TextField textName; + @FXML + protected PasswordField textPassword; + @FXML + protected PasswordField textPasswordSecond; + @FXML + protected ToggleButton checkButtonIsAdmin; + @FXML + protected TextField textLogin; + @FXML + protected TextField textForename; + @FXML + protected Label labelError; @FXML protected void createUser(ActionEvent event) { - if (textLogin.getText().trim().isEmpty()){ - labelError.setText("Bitte Login Namen angeben"); - return; - } - if (textForename.getText().trim().isEmpty()) { - labelError.setText("Bitte Vornamen eingeben!"); - return; - } - if (textName.getText().trim().isEmpty()) { - labelError.setText("Bitte Nachnamen eingeben!"); - return; - } - if (textPassword.getText().trim().isEmpty()) { - labelError.setText("Bitte Passwort eingeben!"); - return; - } - if (textPassword.getText().trim().length() < 8) { - labelError.setText("Das Passwort muss mindestens 8 Zeichen lang sein!"); - return; - } - if (!Objects.equals(textPassword.getText(), textPasswordSecond.getText())){ - labelError.setText("Passwörter stimmen nicht überein!"); - return; - } + if (validateNameAndLogin() || validatePassword()) return; User user = new User(); user.setLogin(textLogin.getText().trim()); @@ -69,6 +53,38 @@ public class CreateUserController { stage.close(); } + protected boolean validatePassword() { + if (textPassword.getText().trim().isEmpty()) { + labelError.setText("Bitte Passwort eingeben!"); + return true; + } + if (textPassword.getText().trim().length() < 8) { + labelError.setText("Das Passwort muss mindestens 8 Zeichen lang sein!"); + return true; + } + if (!Objects.equals(textPassword.getText(), textPasswordSecond.getText())){ + labelError.setText("Passwörter stimmen nicht überein!"); + return true; + } + return false; + } + + protected boolean validateNameAndLogin() { + if (textLogin.getText().trim().isEmpty()){ + labelError.setText("Bitte Login Namen angeben"); + return true; + } + if (textForename.getText().trim().isEmpty()) { + labelError.setText("Bitte Vornamen eingeben!"); + return true; + } + if (textName.getText().trim().isEmpty()) { + labelError.setText("Bitte Nachnamen eingeben!"); + return true; + } + return false; + } + protected void sendHttpRequest(User user) throws HttpRequestException { DataController dataController = new DataController(); dataController.createUser(user); diff --git a/client/app/src/main/java/users/EditUserController.java b/client/app/src/main/java/users/EditUserController.java index fa021e4..703eb3d 100644 --- a/client/app/src/main/java/users/EditUserController.java +++ b/client/app/src/main/java/users/EditUserController.java @@ -8,8 +8,6 @@ import javafx.scene.Node; import javafx.scene.control.Alert; import javafx.stage.Stage; -import java.util.Objects; - public class EditUserController extends CreateUserController{ private User currentUser; @@ -28,34 +26,12 @@ public class EditUserController extends CreateUserController{ @Override protected void createUser(ActionEvent event){ - if (textLogin.getText().trim().isEmpty()){ - labelError.setText("Bitte Login Namen angeben"); - return; - } - if (textForename.getText().trim().isEmpty()) { - labelError.setText("Bitte Vornamen eingeben!"); - return; - } - if (textName.getText().trim().isEmpty()) { - labelError.setText("Bitte Nachnamen eingeben!"); - return; - } + if (validateNameAndLogin()) return; User user = new User(); if(!textPassword.getText().trim().isEmpty() || !textPasswordSecond.getText().trim().isEmpty()){ - if (textPassword.getText().trim().isEmpty()) { - labelError.setText("Bitte Passwort eingeben!"); - return; - } - if (textPassword.getText().trim().length() < 8) { - labelError.setText("Das Passwort muss mindestens 8 Zeichen lang sein!"); - return; - } - if (!Objects.equals(textPassword.getText(), textPasswordSecond.getText())){ - labelError.setText("Passwörter stimmen nicht überein!"); - return; - } + if (validatePassword()) return; user.setPassword(textPassword.getText().trim()); } diff --git a/client/app/src/main/java/users/LoginController.java b/client/app/src/main/java/users/LoginController.java index 37da4be..c5cb83f 100644 --- a/client/app/src/main/java/users/LoginController.java +++ b/client/app/src/main/java/users/LoginController.java @@ -10,13 +10,13 @@ import container.DataController; public class LoginController { @FXML - public JFXTextField userField; + private JFXTextField userField; @FXML - public JFXPasswordField passField; + private JFXPasswordField passField; @FXML - public Label userErrLabel; + private Label userErrLabel; @FXML - public Label passErrLabel; + private Label passErrLabel; @FXML protected void login(ActionEvent event) { diff --git a/client/app/src/main/resources/main/create-event.fxml b/client/app/src/main/resources/main/create-event.fxml index 272fca2..b674c45 100644 --- a/client/app/src/main/resources/main/create-event.fxml +++ b/client/app/src/main/resources/main/create-event.fxml @@ -43,7 +43,7 @@ - + diff --git a/client/app/src/main/resources/main/edit-event.fxml b/client/app/src/main/resources/main/edit-event.fxml index 922fe93..b052b58 100644 --- a/client/app/src/main/resources/main/edit-event.fxml +++ b/client/app/src/main/resources/main/edit-event.fxml @@ -43,7 +43,7 @@ - + diff --git a/client/data/src/main/java/config/Config.java b/client/data/src/main/java/config/Config.java index 07e8b2f..518838c 100644 --- a/client/data/src/main/java/config/Config.java +++ b/client/data/src/main/java/config/Config.java @@ -4,15 +4,26 @@ public class Config { private boolean saveLogin; private long id; private String token; + private String connectionMethod; + private String hostAddress; + private int port; public Config(){ - + saveLogin = false; + id = -1; + token = ""; + connectionMethod = "http"; + hostAddress = "localhost"; + port = 8080; } public Config(boolean saveLogin, long id, String token) { this.saveLogin = saveLogin; this.id = id; this.token = token; + connectionMethod = "http"; + hostAddress = "localhost"; + port = 8080; } public boolean isSaveLogin() { @@ -38,4 +49,32 @@ public class Config { public void setToken(String token) { this.token = token; } + + public String getConnectionMethod() { + return connectionMethod; + } + + public void setConnectionMethod(String connectionMethod) { + this.connectionMethod = connectionMethod; + } + + public String getHostAddress() { + return hostAddress; + } + + public void setHostAddress(String hostAddress) { + this.hostAddress = hostAddress; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String toServerUrl(){ + return getConnectionMethod() + "://" + getHostAddress() + ":" + getPort(); + } } diff --git a/client/data/src/main/java/config/ConfigLoader.java b/client/data/src/main/java/config/ConfigLoader.java index ca1989e..f78c894 100644 --- a/client/data/src/main/java/config/ConfigLoader.java +++ b/client/data/src/main/java/config/ConfigLoader.java @@ -28,9 +28,12 @@ public class ConfigLoader { objectMapper.findAndRegisterModules(); try { - Files.writeString(Paths.get( - "config.json"), + Files.writeString( + Paths.get("config.json"), objectMapper.writeValueAsString(config) + .replace(",", ",\n\t") + .replace("{", "{\n\t") + .replace("}", "\n}") ); } catch (IOException e) { e.printStackTrace(); diff --git a/client/data/src/main/java/container/DataController.java b/client/data/src/main/java/container/DataController.java index c60f909..1c9d5d5 100644 --- a/client/data/src/main/java/container/DataController.java +++ b/client/data/src/main/java/container/DataController.java @@ -12,20 +12,21 @@ import java.util.*; public class DataController { public static long USER_ID = -1; + public static String SERVER_URL = "http://localhost:8080"; - private static final String ALL_EVENTS_ENDPOINT = "http://localhost:8080/event/all"; - private static final String ADD_EVENT_ENDPOINT = "http://localhost:8080/event/add"; - private static final String DELETE_EVENT_ENDPOINT = "http://localhost:8080/event/del"; - private static final String EDIT_EVENT_ENDPOINT = "http://localhost:8080/event/edit"; + private static final String ALL_EVENTS_ENDPOINT = "/event/all"; + private static final String ADD_EVENT_ENDPOINT = "/event/add"; + private static final String DELETE_EVENT_ENDPOINT = "/event/del"; + private static final String EDIT_EVENT_ENDPOINT = "/event/edit"; - private static final String ALL_USER_ENDPOINT = "http://localhost:8080/user/all"; - private static final String ADD_USER_ENDPOINT = "http://localhost:8080/user/add"; - private static final String DELETE_USER_ENDPOINT = "http://localhost:8080/user/del"; - private static final String EDIT_USER_ENDPOINT = "http://localhost:8080/user/edit"; + private static final String ALL_USER_ENDPOINT = "/user/all"; + private static final String ADD_USER_ENDPOINT = "/user/add"; + private static final String DELETE_USER_ENDPOINT = "/user/del"; + private static final String EDIT_USER_ENDPOINT = "/user/edit"; - private static final String LOGIN_ENDPOINT = "http://localhost:8080/user/login"; - private static final String LOGIN_WITH_TOKEN_ENDPOINT = "http://localhost:8080/user/login-with-token"; - private static final String HEADER_TEST_ENDPOINT = "http://localhost:8080/vpr/header-test"; + private static final String LOGIN_ENDPOINT = "/user/login"; + private static final String LOGIN_WITH_TOKEN_ENDPOINT = "/user/login-with-token"; + private static final String HEADER_TEST_ENDPOINT = "/vpr/header-test"; private final HttpRequest httpRequest; @@ -36,7 +37,7 @@ public class DataController { public boolean login(String username, String password) { try { Tuple response = httpRequest.sendPostRequest( - LOGIN_ENDPOINT, + SERVER_URL + LOGIN_ENDPOINT, "login=" + username + "&password=" + password, false @@ -57,7 +58,7 @@ public class DataController { try { HttpRequest.TOKEN = token; Tuple response = httpRequest.sendPostRequest( - LOGIN_WITH_TOKEN_ENDPOINT, + SERVER_URL + LOGIN_WITH_TOKEN_ENDPOINT, "userId=" + userId, true ); @@ -79,7 +80,7 @@ public class DataController { *********/ public void createEvent(Event event) throws HttpRequestException { sendBasicHttpRequest( - ADD_EVENT_ENDPOINT, + SERVER_URL + ADD_EVENT_ENDPOINT, event.getAsUrlParam(), true ); @@ -87,7 +88,7 @@ public class DataController { public void deleteEvent(int userId, int eventId, LocalDateTime date) throws HttpRequestException { sendBasicHttpRequest( - DELETE_EVENT_ENDPOINT, + SERVER_URL + DELETE_EVENT_ENDPOINT, "userId=" + userId + "&eventId=" + eventId + "&date=" + date.toLocalDate(), true ); @@ -95,7 +96,7 @@ public class DataController { public void editEvent(Event oldEvent, Event event) throws HttpRequestException { sendBasicHttpRequest( - EDIT_EVENT_ENDPOINT, + SERVER_URL + EDIT_EVENT_ENDPOINT, "eventId=" + oldEvent.getId() + "&userId=" + oldEvent.getOwnerId() + "&date=" + oldEvent.getDate().toLocalDate() + @@ -113,7 +114,7 @@ public class DataController { public ArrayList getAllVisibleEvents(LocalDateTime startDate, LocalDateTime endDate) throws HttpRequestException { try { Tuple response = httpRequest.sendPostRequest( - ALL_EVENTS_ENDPOINT, + SERVER_URL + ALL_EVENTS_ENDPOINT, "userId=" + USER_ID + "&startDate=" + startDate.toLocalDate() + "&endDate=" + endDate.toLocalDate(), true ); @@ -141,7 +142,7 @@ public class DataController { public List getAllUser() throws HttpRequestException { String userJSON = sendBasicHttpRequest( - ALL_USER_ENDPOINT, + SERVER_URL + ALL_USER_ENDPOINT, "", true ); @@ -164,7 +165,7 @@ public class DataController { public void createUser(User user) throws HttpRequestException { sendBasicHttpRequest( - ADD_USER_ENDPOINT, + SERVER_URL + ADD_USER_ENDPOINT, "name=" + user.getName() + "&forename=" + user.getForename() + "&login=" + user.getLogin() + @@ -176,7 +177,7 @@ public class DataController { public void deleteUser(User user) throws HttpRequestException { sendBasicHttpRequest( - DELETE_USER_ENDPOINT, + SERVER_URL + DELETE_USER_ENDPOINT, "userId=" + user.getUserId(), true ); @@ -192,7 +193,7 @@ public class DataController { System.out.println(urlParam); sendBasicHttpRequest( - EDIT_USER_ENDPOINT, + SERVER_URL + EDIT_USER_ENDPOINT, urlParam, true );