Skip to content

Commit 584f2df

Browse files
committed
IPTE-165: Code Refactoring
1 parent 2b06ec8 commit 584f2df

File tree

1 file changed

+52
-57
lines changed

1 file changed

+52
-57
lines changed

src/main/java/de/doubleslash/keeptime/view/SettingsController.java

Lines changed: 52 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.File;
2020
import java.io.FileOutputStream;
2121
import java.io.IOException;
22+
import java.io.InputStream;
2223
import java.nio.file.Paths;
2324
import java.sql.SQLException;
2425
import java.util.Comparator;
@@ -246,16 +247,13 @@ private void initialize() {
246247

247248
LOG.debug("saveButton.setOnAction");
248249

249-
250-
251-
252250
saveButton.setOnAction(ae -> {
253251
LOG.info("Save clicked");
254252

255253
//*******************************************************************************
256-
// String authPortValue = authPort.getText();
257-
// propertyWrite("server.port",authPortValue);
258-
Properties properties = new Properties();
254+
// String authPortValue = authPort.getText();
255+
// propertyWrite("server.port",authPortValue);
256+
Properties properties = new Properties();
259257
try {
260258
properties.load(getClass().getClassLoader().getResourceAsStream(propertiesFilePath));
261259

@@ -264,7 +262,7 @@ private void initialize() {
264262
properties.setProperty("server.port", authPortValue);
265263
// properties.setProperty("spring.main.web-application-type", "none");
266264

267-
System.err.println(authPortValue);
265+
System.err.println(authPortValue);
268266
FileOutputStream outputStream = new FileOutputStream(propertiesFilePath);
269267
properties.store(outputStream, null);
270268
outputStream.close();
@@ -273,16 +271,8 @@ private void initialize() {
273271
e.printStackTrace();
274272
}
275273

276-
277-
278-
279-
280274
RadioButton selectedRadioButton = (RadioButton) toggleGroup.getSelectedToggle();
281275

282-
283-
284-
285-
286276
if (selectedRadioButton == radioApiOff) {
287277
handleApiOff();
288278
} else if (selectedRadioButton == radioApiOn) {
@@ -359,38 +349,7 @@ private void initialize() {
359349

360350
}
361351

362-
private void handleApiOff() {
363-
propertyWrite("spring.main.web-application-type", "none");
364-
365-
366-
System.out.println("API ist ausgeschaltet");
367-
}
368-
369-
private void handleApiOn() {
370-
371-
372-
373-
// Hier den Code ausführen, der bei Auswahl von "On" ausgeführt werden soll
374-
username = authName.getText();
375-
password = authPassword.getText();
376-
377-
User user = new User();
378-
Authorities authorities = new Authorities();
379-
userRepository.deleteAll();
380-
authoritiesRepository.deleteAll();
381-
user.setUserName(username);
382-
user.setPassword("{noop}" + password);
383-
user.setEnabled(true);
384352

385-
authorities.setUserName(username);
386-
authorities.setAuthority("ROLE_USER");
387-
388-
userRepository.save(user);
389-
authoritiesRepository.save(authorities);
390-
System.out.println("API ist eingeschaltet und Benutzer erstellt.");
391-
propertyWrite("spring.main.web-application-type", "");
392-
393-
}
394353

395354
private static void setRegionSvg(Region region, double requiredWidth, double requiredHeight, RESOURCE resource) {
396355

@@ -646,17 +605,53 @@ private void showLicense(final Licenses license) {
646605
alert.show();
647606
}
648607
}
608+
private void handleApiOff() {
609+
setWebApplicationType("none");
610+
System.out.println("API ist ausgeschaltet");
611+
}
612+
613+
private void handleApiOn() {
614+
username = authName.getText();
615+
password = authPassword.getText();
616+
617+
createAndSaveUser(username, password);
618+
619+
System.out.println("API ist eingeschaltet und Benutzer erstellt.");
620+
setWebApplicationType("");
621+
propertyWrite("server.port",authPort.getText());
622+
}
649623

650-
private void propertyWrite(String key,String value){
651-
Properties properties = new Properties();
652-
try {
653-
properties.load(getClass().getClassLoader().getResourceAsStream(propertiesFilePath));
654-
properties.setProperty(key, value);
655-
FileOutputStream outputStream = new FileOutputStream(propertiesFilePath);
656-
properties.store(outputStream, null);
657-
outputStream.close();
658-
} catch (IOException e) {
659-
e.printStackTrace();
624+
private void setWebApplicationType(String value) {
625+
propertyWrite("spring.main.web-application-type", value);
626+
}
627+
628+
private void createAndSaveUser(String username, String password) {
629+
User user = new User();
630+
Authorities authorities = new Authorities();
631+
632+
userRepository.deleteAll();
633+
authoritiesRepository.deleteAll();
634+
635+
user.setUserName(username);
636+
user.setPassword("{noop}" + password);
637+
user.setEnabled(true);
638+
639+
authorities.setUserName(username);
640+
authorities.setAuthority("ROLE_USER");
641+
642+
userRepository.save(user);
643+
authoritiesRepository.save(authorities);
644+
}
645+
646+
private void propertyWrite(String key, String value) {
647+
Properties properties = new Properties();
648+
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFilePath);
649+
FileOutputStream outputStream = new FileOutputStream(propertiesFilePath)) {
650+
properties.load(inputStream);
651+
properties.setProperty(key, value);
652+
properties.store(outputStream, null);
653+
} catch (IOException e) {
654+
e.printStackTrace();
655+
}
660656
}
661-
}
662657
}

0 commit comments

Comments
 (0)