Skip to content

Commit bb121b3

Browse files
committed
#178: refresh heimatapi connection (to use latest credentials) when validating or saving settings.
* otherwise old settings remain due to spring bean lifecycle
1 parent 58c27d5 commit bb121b3

File tree

3 files changed

+60
-29
lines changed

3 files changed

+60
-29
lines changed

src/main/java/de/doubleslash/keeptime/controller/HeimatController.java

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@ public class HeimatController {
2727
private final Controller controller;
2828
private final HeimatSettings heimatSettings;
2929
private final ExternalProjectsMappingsRepository externalProjectsMappingsRepository;
30-
private final HeimatAPI heimatAPI;
30+
31+
private HeimatAPI heimatAPI;
3132

3233
@Autowired
3334
public HeimatController(HeimatSettings heimatSettings,
3435
ExternalProjectsMappingsRepository externalProjectsMappingsRepository, final Controller controller) {
3536
this.heimatSettings = heimatSettings;
3637
this.controller = controller;
3738
this.externalProjectsMappingsRepository = externalProjectsMappingsRepository;
38-
39-
heimatAPI = new HeimatAPI(heimatSettings.getHeimatUrl(), heimatSettings.getHeimatPat());
39+
this.heimatAPI = new HeimatAPI(heimatSettings.getHeimatUrl(), heimatSettings.getHeimatPat());
4040
}
4141

4242
// for testing only
@@ -48,6 +48,24 @@ public HeimatController(HeimatSettings heimatSettings,
4848
this.heimatAPI = heimatAPI;
4949
}
5050

51+
/**
52+
* can be called when heimat settings have changed
53+
*/
54+
public void refreshConnection() {
55+
heimatAPI = new HeimatAPI(heimatSettings.getHeimatUrl(), heimatSettings.getHeimatPat());
56+
}
57+
58+
/**
59+
* throws SecurityException when login or url is not valid
60+
*/
61+
public void tryLogin() {
62+
try {
63+
heimatAPI.isLoginValid();
64+
} catch (Exception e) {
65+
throw new SecurityException("Could not connect to HEIMAT API. Maybe wrong configuration?", e);
66+
}
67+
}
68+
5169
public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<Work> currentWorkItems) {
5270
final List<HeimatTask> heimatTasks = heimatAPI.getMyTasks(currentReportDate);
5371
final List<HeimatTime> heimatTimes = heimatAPI.getMyTimes(currentReportDate);
@@ -62,7 +80,8 @@ public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<
6280
.collect(Collectors.toCollection(() -> new TreeSet<>(
6381
Comparator.comparing(Project::getIndex))));
6482
final Map<Long, List<HeimatTime>> taskIdToHeimatTimesMap = heimatTimes.stream()
65-
.collect(Collectors.groupingBy(HeimatTime::taskId));
83+
.collect(Collectors.groupingBy(
84+
HeimatTime::taskId));
6685

6786
for (final Project project : workedProjectsSet) {
6887
String heimatNotes = "";
@@ -83,7 +102,7 @@ public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<
83102

84103
final List<HeimatTime> heimatTimesForTaskId = taskIdToHeimatTimesMap.get(
85104
optHeimatMapping.get().getExternalTaskId());
86-
if(heimatTimesForTaskId != null) {
105+
if (heimatTimesForTaskId != null) {
87106
optionalAlreadyBookedTimes = heimatTimesForTaskId;
88107
}
89108
if (!optionalAlreadyBookedTimes.isEmpty()) {
@@ -155,10 +174,9 @@ public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<
155174
list.add(mapping);
156175
});
157176

158-
taskIdToHeimatTimesMap.forEach((id,times) -> {
177+
taskIdToHeimatTimesMap.forEach((id, times) -> {
159178
final Optional<ExternalProjectMapping> mapping = mappedProjects.stream()
160-
.filter(mp -> mp.getExternalTaskId()
161-
== id)
179+
.filter(mp -> mp.getExternalTaskId() == id)
162180
.findAny();
163181
if (mapping.isEmpty())
164182
return;
@@ -176,9 +194,13 @@ public List<Mapping> getTableRows(final LocalDate currentReportDate, final List<
176194

177195
final Mapping mapping2 = new Mapping(id, true,
178196
"Present in HEIMAT but not KeepTime\n\n" + externalProjectMapping.getExternalTaskName() + "\n"
179-
+ externalProjectMapping.getExternalProjectName(), times,
180-
mappedProjects.stream().filter(mp->mp.getExternalTaskId() == id).map(
181-
ExternalProjectMapping::getProject).toList(), heimatNotes, "", heimatTimeSeconds, 0);
197+
+ externalProjectMapping.getExternalProjectName(), times, mappedProjects.stream()
198+
.filter(
199+
mp -> mp.getExternalTaskId()
200+
== id)
201+
.map(ExternalProjectMapping::getProject)
202+
.toList(), heimatNotes, "",
203+
heimatTimeSeconds, 0);
182204
list.add(mapping2);
183205
});
184206

@@ -195,9 +217,7 @@ private static long addHeimatTimes(final List<HeimatTime> optionalAlreadyBookedT
195217

196218
private static String addHeimatNotes(final List<HeimatTime> optionalAlreadyBookedTimes) {
197219
String heimatNotes;
198-
heimatNotes = optionalAlreadyBookedTimes.stream()
199-
.map(HeimatTime::note)
200-
.collect(Collectors.joining(". "));
220+
heimatNotes = optionalAlreadyBookedTimes.stream().map(HeimatTime::note).collect(Collectors.joining(". "));
201221
return heimatNotes;
202222
}
203223

@@ -247,7 +267,10 @@ public List<HeimatTask> getTasks(final LocalDate forDate) {
247267
uniqueMap.putIfAbsent(obj.id(), obj);
248268
}
249269

250-
return uniqueMap.values().stream().sorted(Comparator.comparing(HeimatTask::taskHolderName).thenComparing(HeimatTask::name)).toList();
270+
return uniqueMap.values()
271+
.stream()
272+
.sorted(Comparator.comparing(HeimatTask::taskHolderName).thenComparing(HeimatTask::name))
273+
.toList();
251274
}
252275

253276
public record UserMapping(Mapping mapping, boolean shouldSync, String userNotes, int userMinutes) {}

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Map;
2929
import java.util.Properties;
3030

31+
import de.doubleslash.keeptime.controller.HeimatController;
3132
import de.doubleslash.keeptime.exceptions.FXMLLoaderException;
3233
import de.doubleslash.keeptime.model.settings.HeimatSettings;
3334
import de.doubleslash.keeptime.rest.integration.heimat.HeimatAPI;
@@ -229,13 +230,14 @@ public class SettingsController {
229230
@FXML
230231
private Label mapProjectsButtonLabel;
231232

232-
private final String propertiesFilePath = "application.properties";
233+
private static final String propertiesFilePath = "application.properties";
233234

234235
private static final String GITHUB_PAGE = "https://www.github.com/doubleSlashde/KeepTime";
235236
private static final String GITHUB_ISSUE_PAGE = GITHUB_PAGE + "/issues";
236237

237238
private static final Color HYPERLINK_COLOR = Color.rgb(0, 115, 170);
238239
private final ApplicationProperties applicationProperties;
240+
private final HeimatController heimatController;
239241

240242
private static final Logger LOG = LoggerFactory.getLogger(SettingsController.class);
241243

@@ -248,11 +250,12 @@ public class SettingsController {
248250
ViewController mainscreen;
249251

250252
public SettingsController(final Model model, final Controller controller,
251-
ApplicationProperties applicationProperties) {
253+
ApplicationProperties applicationProperties, HeimatController heimatController) {
252254
this.model = model;
253255
this.controller = controller;
254256
this.applicationProperties = applicationProperties;
255257

258+
this.heimatController = heimatController;
256259
}
257260

258261
@FXML
@@ -375,12 +378,7 @@ private void initialize() {
375378
controller.updateFeatureSettings(useHotkeyCheckBox.isSelected(), emptyNoteReminderCheckBox.isSelected(),
376379
emptyNoteReminderOnlyForWorkEntryCheckBox.isSelected(), confirmCloseCheckBox.isSelected());
377380

378-
379-
controller.updateHeimatSettings(
380-
heimatActivationCheckbox.isSelected(),
381-
heimatUrlTextField.getText(),
382-
heimatPatTextField.getText());
383-
381+
updateHeimatSettings();
384382
thisStage.close();
385383
});
386384

@@ -421,10 +419,11 @@ private void initializeHeimat() {
421419
heimatExpiresLabel.setText("Does not seem to be valid");
422420
}
423421
});
422+
heimatValidateConnectionLabel.setText("Not validated.");
424423
heimatValidateConnectionButton.setOnAction(ae -> {
425-
final HeimatAPI heimatAPI = new HeimatAPI(heimatUrlTextField.getText() , heimatPatTextField.getText());
424+
updateHeimatSettings();
426425
try {
427-
heimatAPI.isLoginValid();
426+
heimatController.tryLogin();
428427
heimatValidateConnectionLabel.setText("Connection is valid.");
429428
}catch(Exception e){
430429
LOG.error("Error while validating Heimat connection", e);
@@ -443,7 +442,6 @@ private void initializeHeimat() {
443442
heimatUrlTextField.setText(heimatSettings.getHeimatUrl());
444443
heimatPatTextField.setText(heimatSettings.getHeimatPat());
445444

446-
mapProjectsButtonLabel.setText("Please save settings before mapping projects.");
447445
heimatMapProjectsButton.disableProperty().bind(heimatActivationCheckbox.selectedProperty().not());
448446

449447
heimatMapProjectsButton.setOnAction((ae) -> {
@@ -456,6 +454,14 @@ private void initializeHeimat() {
456454
});
457455
}
458456

457+
private void updateHeimatSettings() {
458+
controller.updateHeimatSettings(
459+
heimatActivationCheckbox.isSelected(),
460+
heimatUrlTextField.getText(),
461+
heimatPatTextField.getText());
462+
heimatController.refreshConnection();
463+
}
464+
459465
private void showMapProjectsStage() {
460466
try{
461467
// Settings stage
@@ -700,6 +706,8 @@ void update() {
700706
.bind(emptyNoteReminderCheckBox.selectedProperty().not());
701707
emptyNoteReminderOnlyForWorkEntryCheckBox.setSelected(model.remindIfNotesAreEmptyOnlyForWorkEntry.get());
702708
confirmCloseCheckBox.setSelected(model.confirmClose.get());
709+
710+
heimatValidateConnectionLabel.setText("Not validated.");
703711
}
704712

705713
public void setStage(final Stage thisStage) {

src/main/resources/layouts/settings.fxml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,10 @@
524524
<children>
525525
<VBox styleClass="settingsBorder" stylesheets="@../css/settingsv2.css">
526526
<children>
527-
<HBox alignment="CENTER_LEFT">
527+
<HBox alignment="CENTER_LEFT" spacing="5.0">
528528
<children>
529529
<Button fx:id="heimatValidateConnectionButton" mnemonicParsing="false" text="Validate connection" />
530-
<Label fx:id="heimatValidateConnectionLabel" />
530+
<Label fx:id="heimatValidateConnectionLabel" disable="true" />
531531
</children>
532532
</HBox>
533533
</children>
@@ -541,7 +541,7 @@
541541
<HBox alignment="CENTER_LEFT" spacing="5.0">
542542
<children>
543543
<Button fx:id="heimatMapProjectsButton" mnemonicParsing="false" text="Map projects" />
544-
<Label fx:id="mapProjectsButtonLabel" disable="true" />
544+
<Label fx:id="mapProjectsButtonLabel" disable="true" text="Please validate connection before mapping projects." />
545545
</children>
546546
</HBox>
547547
</children>

0 commit comments

Comments
 (0)