Skip to content

Commit

Permalink
consider global federal state setting for person WorkingTime
Browse files Browse the repository at this point in the history
  • Loading branch information
bseber committed Jan 19, 2024
1 parent cb3eee0 commit a69d22d
Show file tree
Hide file tree
Showing 14 changed files with 220 additions and 57 deletions.
21 changes: 21 additions & 0 deletions src/main/java/de/focusshift/zeiterfassung/CachedSupplier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.focusshift.zeiterfassung;

import java.util.function.Supplier;

public class CachedSupplier<T> implements Supplier<T> {

private T cachedValue;
private final Supplier<T> supplier;

public CachedSupplier(Supplier<T> supplier) {
this.supplier = supplier;
}

@Override
public T get() {
if (cachedValue == null) {
cachedValue = supplier.get();
}
return cachedValue;
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package de.focusshift.zeiterfassung.publicholiday;

import de.focus_shift.jollyday.core.HolidayManager;
import de.focusshift.zeiterfassung.CachedSupplier;
import de.focusshift.zeiterfassung.settings.FederalStateSettingsService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.Collection;
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import static java.util.stream.Collectors.groupingBy;

@Service
class PublicHolidaysServiceImpl implements PublicHolidaysService {

private final Map<String, HolidayManager> holidayManagers;
private final FederalStateSettingsService federalStateSettingsService;

PublicHolidaysServiceImpl(Map<String, HolidayManager> holidayManagers) {
PublicHolidaysServiceImpl(Map<String, HolidayManager> holidayManagers, FederalStateSettingsService federalStateSettingsService) {
this.holidayManagers = holidayManagers;
this.federalStateSettingsService = federalStateSettingsService;
}

@Override
Expand All @@ -26,20 +31,27 @@ public Map<FederalState, PublicHolidayCalendar> getPublicHolidays(LocalDate from
final LocalDate to = toExclusive.minusDays(1);
final Map<FederalState, PublicHolidayCalendar> calendar = new EnumMap<>(FederalState.class);

final Supplier<FederalState> globalFederalStateSupplier =
new CachedSupplier<>(() -> federalStateSettingsService.getFederalStateSettings().federalState());

for (FederalState federalState : federalStates) {
final Map<LocalDate, List<PublicHoliday>> holidays;
if (federalState == FederalState.NONE) {
holidays = Map.of();
} else {
final HolidayManager holidayManager = holidayManagers.get(federalState.getCountry());
holidays = holidayManager.getHolidays(from, to, federalState.getCodes())
.stream()
.map(holiday -> new PublicHoliday(holiday.getDate(), holiday::getDescription))
.collect(groupingBy(PublicHoliday::date));
}
calendar.put(federalState, new PublicHolidayCalendar(federalState, holidays));
federalState = FederalState.GLOBAL.equals(federalState) ? globalFederalStateSupplier.get() : federalState;
calendar.put(federalState, new PublicHolidayCalendar(federalState, holidays(from, to, federalState)));
}

return calendar;
}

private Map<LocalDate, List<PublicHoliday>> holidays(LocalDate from, LocalDate to, FederalState federalState) {

if (FederalState.NONE.equals(federalState)) {
return Map.of();
}

final HolidayManager holidayManager = holidayManagers.get(federalState.getCountry());
return holidayManager.getHolidays(from, to, federalState.getCodes())
.stream()
.map(holiday -> new PublicHoliday(holiday.getDate(), holiday::getDescription))
.collect(groupingBy(PublicHoliday::date));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @param federalState the default federal-state and public holiday regulations
* @param worksOnPublicHoliday whether persons are working on public holidays or not
*/
record FederalStateSettings(FederalState federalState, boolean worksOnPublicHoliday) {
public record FederalStateSettings(FederalState federalState, boolean worksOnPublicHoliday) {

public static final FederalStateSettings DEFAULT = new FederalStateSettings(FederalState.NONE, false);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.focusshift.zeiterfassung.settings;

public interface FederalStateSettingsService {

FederalStateSettings getFederalStateSettings();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Optional;

@Service
class SettingsService {
class SettingsService implements FederalStateSettingsService {

private final FederalStateSettingsRepository federalStateSettingsRepository;
private final TenantContextHolder tenantContextHolder;
Expand All @@ -18,7 +18,8 @@ class SettingsService {
this.tenantContextHolder = tenantContextHolder;
}

FederalStateSettings getFederalStateSettings() {
@Override
public FederalStateSettings getFederalStateSettings() {
return getFederalStateEntity()
.map(SettingsService::toFederalStateSettings)
.orElse(FederalStateSettings.DEFAULT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.focus_shift.launchpad.api.HasLaunchpad;
import de.focusshift.zeiterfassung.publicholiday.FederalState;
import de.focusshift.zeiterfassung.settings.FederalStateSettingsService;
import de.focusshift.zeiterfassung.timeclock.HasTimeClock;
import de.focusshift.zeiterfassung.workingtime.WorkingTime;
import de.focusshift.zeiterfassung.workingtime.WorkingTimeId;
Expand Down Expand Up @@ -62,16 +63,19 @@ class WorkingTimeController implements HasTimeClock, HasLaunchpad {
private final UserManagementService userManagementService;
private final WorkingTimeService workingTimeService;
private final WorkingTimeDtoValidator validator;
private final FederalStateSettingsService federalStateSettingsService;
private final Clock clock;

WorkingTimeController(UserManagementService userManagementService,
WorkingTimeService workingTimeService,
WorkingTimeDtoValidator validator,
FederalStateSettingsService federalStateSettingsService,
Clock clock) {

this.userManagementService = userManagementService;
this.workingTimeService = workingTimeService;
this.validator = validator;
this.federalStateSettingsService = federalStateSettingsService;
this.clock = clock;
}

Expand Down Expand Up @@ -280,11 +284,15 @@ private void prepareGetWorkingTimesModel(Model model, String query, Long userId,
.or(() -> userManagementService.findUserByLocalId(new UserLocalId(userId)).map(UserManagementController::userToDto))
.orElseThrow(() -> new IllegalArgumentException("could not find person=%s".formatted(userId)));

final FederalState globalFederalState = federalStateSettingsService.getFederalStateSettings().federalState();

model.addAttribute("query", query);
model.addAttribute("slug", "working-time");
model.addAttribute("users", users);
model.addAttribute("selectedUser", selectedUser);
model.addAttribute("workingTimes", workingTimeDtos);
model.addAttribute("globalFederalState", globalFederalState);
model.addAttribute("globalFederalStateMessageKey", federalStateMessageKey(globalFederalState));
model.addAttribute("personSearchFormAction", "/users/" + selectedUser.id());

model.addAttribute("allowedToEditWorkingTime", hasAuthority(ZEITERFASSUNG_WORKING_TIME_EDIT_ALL, securityContext));
Expand All @@ -298,7 +306,7 @@ private void prepareWorkingTimeCreateOrEditModel(Model model, String query, Long

model.addAttribute("section", "working-time-edit");
model.addAttribute("workingTime", workingTimeDto);
model.addAttribute("federalStateSelect", federalStateSelectDto(workingTimeDto.getFederalState()));
model.addAttribute("federalStateSelect", federalStateSelectDto(workingTimeDto.getFederalState(), true));
}

private void clearWorkDayHours(String dayOfWeek, WorkingTimeDto workingTimeDto) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ private WorkingTime defaultWorkingTime(UserIdComposite userIdComposite) {
final Duration eight = Duration.ofHours(8);
return WorkingTime.builder(userIdComposite, null)
.current(true)
.federalState(FederalState.NONE)
.federalState(FederalState.GLOBAL)
.worksOnPublicHoliday(false)
.monday(eight)
.tuesday(eight)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ country.nl=Niederlande

# No Federal States
federalState.NONE=Keine Feiertagesregelung
federalState.default=Globale Feiertagsregelung ({0})
federalState.GLOBAL=Globale Feiertagsregelung ({0})

# Federal States Germany
federalState.GERMANY_BADEN_WUERTTEMBERG=Baden-Württemberg
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ country.nl=The Netherlands

# No Federal States
federalState.NONE=No public holiday regulation
federalState.default=System-wide setting ({0})
federalState.GLOBAL=System-wide setting ({0})

# Federal States Germany
federalState.GERMANY_BADEN_WUERTTEMBERG=Baden-Württemberg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<option
th:each="option : ${optgroup.options}"
th:value="${option.value}"
th:text="${#messages.msg(option.textMessageKey)}"
th:text="${#messages.msg(option.textMessageKey, #messages.msg(globalFederalStateMessageKey))}"
th:selected="${option.selected}"
></option>
</optgroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,43 +49,46 @@
<svg
th:replace="~{icons/chevron-right::svg(className='w-5 h-5 transition-transform group-open:rotate-90')}"
></svg>
<th:block th:switch="${workingTime.validFromIsPast}">
<th:block
th:switch="${workingTime.validFromIsPast}"
th:with="federalStateMsg=${#messages.msg(workingTime.federalStateMessageKey, #messages.msg(globalFederalStateMessageKey))}"
>
<th:block th:case="true">
<th:block
th:if="${workingTime.validFrom == null}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-until(${workingTime.validTo}, ${#messages.msg(workingTime.federalStateMessageKey)})}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-until(${workingTime.validTo}, ${federalStateMsg})}"
>
gültig bis ___
</th:block>
<th:block
th:if="${workingTime.validFrom != null && workingTime.validTo != null}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-from-until(${workingTime.validFrom}, ${workingTime.validTo}, ${#messages.msg(workingTime.federalStateMessageKey)})}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-from-until(${workingTime.validFrom}, ${workingTime.validTo}, ${federalStateMsg})}"
>
gültig von ___ bis ___
</th:block>
<th:block
th:if="${workingTime.validFrom != null && workingTime.validTo == null}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-from(${workingTime.validFrom}, ${#messages.msg(workingTime.federalStateMessageKey)})}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-from(${workingTime.validFrom}, ${federalStateMsg})}"
>
gültig von ___
</th:block>
</th:block>
<th:block th:case="false">
<th:block
th:if="${workingTime.validFrom == null}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-until(${workingTime.validTo}, ${#messages.msg(workingTime.federalStateMessageKey)})}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-until(${workingTime.validTo}, ${federalStateMsg})}"
>
gültig bis ___
</th:block>
<th:block
th:if="${workingTime.validFrom != null && workingTime.validTo != null}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-from-until(${workingTime.validFrom}, ${workingTime.validTo}, ${#messages.msg(workingTime.federalStateMessageKey)})}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-from-until(${workingTime.validFrom}, ${workingTime.validTo}, ${federalStateMsg})}"
>
gültig von ___ bis ___
</th:block>
<th:block
th:if="${workingTime.validFrom != null && workingTime.validTo == null}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-from(${workingTime.validFrom}, ${#messages.msg(workingTime.federalStateMessageKey)})}"
th:text="#{usermanagement.working-time.multiple-working-time-summary.valid-from(${workingTime.validFrom}, ${federalStateMsg})}"
>
gültig von ___
</th:block>
Expand Down Expand Up @@ -185,7 +188,9 @@
th:with="workingTime=${workingTimes[0]}"
>
<div class="mt-8">
<p th:text="${#messages.msg(workingTime.federalStateMessageKey)}">
<p
th:text="${#messages.msg(workingTime.federalStateMessageKey, #messages.msg(globalFederalStateMessageKey))}"
>
Baden-Württemberg
</p>
<div th:replace="~{::working-time-block}"></div>
Expand Down
Loading

0 comments on commit a69d22d

Please sign in to comment.