Skip to content

Commit

Permalink
global works-on-public-holiday
Browse files Browse the repository at this point in the history
  • Loading branch information
bseber committed Jan 19, 2024
1 parent a69d22d commit 1addae8
Show file tree
Hide file tree
Showing 15 changed files with 332 additions and 320 deletions.
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.FederalStateSettings;
import de.focusshift.zeiterfassung.settings.FederalStateSettingsService;
import de.focusshift.zeiterfassung.timeclock.HasTimeClock;
import de.focusshift.zeiterfassung.workingtime.WorkingTime;
Expand Down Expand Up @@ -87,8 +88,9 @@ String get(@PathVariable("userId") Long userId, Model model,

final List<WorkingTime> workingTimes = workingTimeService.getAllWorkingTimesByUser(new UserLocalId(userId));
final List<WorkingTimeListEntryDto> workingTimeDtos = workingTimesToDtos(workingTimes);
final FederalStateSettings federalStateSettings = federalStateSettingsService.getFederalStateSettings();

prepareGetWorkingTimesModel(model, query, userId, workingTimeDtos, securityContext);
prepareGetWorkingTimesModel(model, query, userId, workingTimeDtos, securityContext, federalStateSettings);

if (hasText(turboFrame)) {
return "usermanagement/users::#" + turboFrame;
Expand Down Expand Up @@ -170,7 +172,7 @@ ModelAndView createNewWorkingTime(@PathVariable("userId") Long userId, Model mod
final UserLocalId userLocalId = new UserLocalId(workingTimeDto.getUserId());
final LocalDate validFrom = workingTimeDto.getValidFrom();
final FederalState federalState = workingTimeDto.getFederalState();
final boolean worksOnPublicHoliday = workingTimeDto.isWorksOnPublicHoliday();
final boolean worksOnPublicHoliday = workingTimeDto.getWorksOnPublicHoliday();
final EnumMap<DayOfWeek, Duration> workdays = workingTimeDtoToWorkdays(workingTimeDto);

workingTimeService.createWorkingTime(userLocalId, validFrom, federalState, worksOnPublicHoliday, workdays);
Expand Down Expand Up @@ -213,7 +215,7 @@ ModelAndView updateWorkingTime(@PathVariable("userId") Long userId, Model model,

final LocalDate validFrom = workingTimeDto.getValidFrom();
final FederalState federalState = workingTimeDto.getFederalState();
final boolean worksOnPublicHoliday = workingTimeDto.isWorksOnPublicHoliday();
final Boolean worksOnPublicHoliday = workingTimeDto.getWorksOnPublicHoliday();
final EnumMap<DayOfWeek, Duration> workdays = workingTimeDtoToWorkdays(workingTimeDto);
workingTimeService.updateWorkingTime(WorkingTimeId.fromString(workingTimeDto.getId()), validFrom, federalState, worksOnPublicHoliday, workdays);

Expand Down Expand Up @@ -272,7 +274,8 @@ SUNDAY, hoursToDuration(duration.applyAsDouble(SUNDAY))

private void prepareGetWorkingTimesModel(Model model, String query, Long userId,
List<WorkingTimeListEntryDto> workingTimeDtos,
SecurityContext securityContext) {
SecurityContext securityContext,
FederalStateSettings federalStateSettings) {

final List<UserDto> users = userManagementService.findAllUsers(query)
.stream()
Expand All @@ -284,7 +287,7 @@ 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();
final FederalState globalFederalState = federalStateSettings.federalState();

model.addAttribute("query", query);
model.addAttribute("slug", "working-time");
Expand All @@ -302,10 +305,14 @@ private void prepareGetWorkingTimesModel(Model model, String query, Long userId,

private void prepareWorkingTimeCreateOrEditModel(Model model, String query, Long userId, WorkingTimeDto workingTimeDto, SecurityContext securityContext) {

prepareGetWorkingTimesModel(model, query, userId, List.of(), securityContext);
final FederalStateSettings federalStateSettings = federalStateSettingsService.getFederalStateSettings();

prepareGetWorkingTimesModel(model, query, userId, List.of(), securityContext, federalStateSettings);

model.addAttribute("section", "working-time-edit");
model.addAttribute("workingTime", workingTimeDto);
// number is required for the messages choice pattern which does not work with boolean
model.addAttribute("globalWorksOnPublicHoliday", federalStateSettings.worksOnPublicHoliday() ? 1 : 0);
model.addAttribute("federalStateSelect", federalStateSelectDto(workingTimeDto.getFederalState(), true));
}

Expand Down Expand Up @@ -360,37 +367,32 @@ private WorkingTimeListEntryDto workingTimeListEntryDto(WorkingTime workingTime)

private static WorkingTimeDto workingTimeToDto(WorkingTime workingTime) {

final WorkingTimeDto.Builder builder = WorkingTimeDto.builder();
final WorkingTimeDto dto = new WorkingTimeDto();

if (workingTime.hasDifferentWorkingHours()) {
final Map<DayOfWeek, Consumer<Double>> setter = Map.of(
MONDAY, builder::workingTimeMonday,
TUESDAY, builder::workingTimeTuesday,
WEDNESDAY, builder::workingTimeWednesday,
THURSDAY, builder::workingTimeThursday,
FRIDAY, builder::workingTimeFriday,
SATURDAY, builder::workingTimeSaturday,
SUNDAY, builder::workingTimeSunday
);
workingTime.workdays().forEach((dayOfWeek, workDayDuration) ->
setter.get(dayOfWeek).accept(workDayDuration.hoursDoubleValue())
);
dto.setWorkingTimeMonday(workingTime.getMonday().hoursDoubleValue());
dto.setWorkingTimeTuesday(workingTime.getTuesday().hoursDoubleValue());
dto.setWorkingTimeWednesday(workingTime.getWednesday().hoursDoubleValue());
dto.setWorkingTimeThursday(workingTime.getThursday().hoursDoubleValue());
dto.setWorkingTimeFriday(workingTime.getFriday().hoursDoubleValue());
dto.setWorkingTimeSaturday(workingTime.getSaturday().hoursDoubleValue());
dto.setWorkingTimeSunday(workingTime.getSunday().hoursDoubleValue());
} else {
// every day has the same hours
// -> individual input fields should be empty
// -> working time input should be set
builder.workingTime(workingTime.workdays().get(MONDAY).hoursDoubleValue());
dto.setWorkingTime(workingTime.getMonday().hoursDoubleValue());
}

return builder
.id(workingTime.id().value())
.validFrom(workingTime.validFrom().orElse(null))
.minValidFrom(workingTime.minValidFrom().orElse(null))
.maxValidFrom(workingTime.validTo().orElse(null))
.federalState(workingTime.federalState())
.worksOnPublicHoliday(workingTime.worksOnPublicHoliday())
.userId(workingTime.userIdComposite().localId().value())
.workday(workingTime.actualWorkingDays())
.build();
dto.setId(workingTime.id().value());
dto.setValidFrom(workingTime.validFrom().orElse(null));
dto.setMinValidFrom(workingTime.minValidFrom().orElse(null));
dto.setMaxValidFrom(workingTime.validTo().orElse(null));
dto.setFederalState(workingTime.federalState());
dto.setWorksOnPublicHoliday(workingTime.isWorksOnPublicHolidayGlobal() ? null : workingTime.worksOnPublicHoliday());
dto.setUserId(workingTime.userIdComposite().localId().value());
dto.setWorkday(workingTime.actualWorkingDays().stream().map(DayOfWeek::name).map(String::toLowerCase).toList());

return dto;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.focusshift.zeiterfassung.usermanagement;

import de.focusshift.zeiterfassung.publicholiday.FederalState;
import jakarta.annotation.Nullable;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -20,7 +20,7 @@ class WorkingTimeDto {
@DateTimeFormat(pattern = "yyyy-MM-dd")
private LocalDate maxValidFrom;
private FederalState federalState;
private boolean worksOnPublicHoliday;
private Boolean worksOnPublicHoliday;
private List<String> workday = new ArrayList<>();
private Double workingTime;
private Double workingTimeMonday;
Expand Down Expand Up @@ -79,11 +79,12 @@ public void setFederalState(FederalState federalState) {
this.federalState = federalState;
}

public boolean isWorksOnPublicHoliday() {
@Nullable
public Boolean getWorksOnPublicHoliday() {
return worksOnPublicHoliday;
}

public void setWorksOnPublicHoliday(boolean worksOnPublicHoliday) {
public void setWorksOnPublicHoliday(@Nullable Boolean worksOnPublicHoliday) {
this.worksOnPublicHoliday = worksOnPublicHoliday;
}

Expand Down Expand Up @@ -244,23 +245,23 @@ public String toString() {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WorkingTimeDto that = (WorkingTimeDto) o;
return userId.equals(that.userId)
&& Objects.equals(id, that.id)
&& Objects.equals(validFrom, that.validFrom)
&& Objects.equals(minValidFrom, that.minValidFrom)
&& Objects.equals(maxValidFrom, that.maxValidFrom)
&& Objects.equals(federalState, that.federalState)
&& Objects.equals(worksOnPublicHoliday, that.worksOnPublicHoliday)
&& workday.equals(that.workday)
&& Objects.equals(workingTime, that.workingTime)
&& Objects.equals(workingTimeMonday, that.workingTimeMonday)
&& Objects.equals(workingTimeTuesday, that.workingTimeTuesday)
&& Objects.equals(workingTimeWednesday, that.workingTimeWednesday)
&& Objects.equals(workingTimeThursday, that.workingTimeThursday)
&& Objects.equals(workingTimeFriday, that.workingTimeFriday)
&& Objects.equals(workingTimeSaturday, that.workingTimeSaturday)
&& Objects.equals(workingTimeSunday, that.workingTimeSunday);
WorkingTimeDto dto = (WorkingTimeDto) o;
return Objects.equals(userId, dto.userId)
&& Objects.equals(id, dto.id)
&& Objects.equals(validFrom, dto.validFrom)
&& Objects.equals(minValidFrom, dto.minValidFrom)
&& Objects.equals(maxValidFrom, dto.maxValidFrom)
&& federalState == dto.federalState
&& Objects.equals(worksOnPublicHoliday, dto.worksOnPublicHoliday)
&& Objects.equals(workday, dto.workday)
&& Objects.equals(workingTime, dto.workingTime)
&& Objects.equals(workingTimeMonday, dto.workingTimeMonday)
&& Objects.equals(workingTimeTuesday, dto.workingTimeTuesday)
&& Objects.equals(workingTimeWednesday, dto.workingTimeWednesday)
&& Objects.equals(workingTimeThursday, dto.workingTimeThursday)
&& Objects.equals(workingTimeFriday, dto.workingTimeFriday)
&& Objects.equals(workingTimeSaturday, dto.workingTimeSaturday)
&& Objects.equals(workingTimeSunday, dto.workingTimeSunday);
}

@Override
Expand All @@ -269,128 +270,4 @@ public int hashCode() {
workday, workingTime, workingTimeMonday, workingTimeTuesday, workingTimeWednesday, workingTimeThursday,
workingTimeFriday, workingTimeSaturday, workingTimeSunday);
}

static Builder builder() {
return new Builder();
}

static class Builder {
private Long userId;
private String uuid;
private LocalDate validFrom;
private LocalDate minValidFrom;
private LocalDate maxValidFrom;
private FederalState federalState;
private boolean worksOnPublicHoliday;
private List<DayOfWeek> workday = new ArrayList<>();
private Double workingTime;
private Double workingTimeMonday;
private Double workingTimeTuesday;
private Double workingTimeWednesday;
private Double workingTimeThursday;
private Double workingTimeFriday;
private Double workingTimeSaturday;
private Double workingTimeSunday;

Builder userId(Long userId) {
this.userId = userId;
return this;
}

Builder id(String uuid) {
this.uuid = uuid;
return this;
}

Builder validFrom(LocalDate validFrom) {
this.validFrom = validFrom;
return this;
}

Builder minValidFrom(LocalDate minValidFrom) {
this.minValidFrom = minValidFrom;
return this;
}

Builder maxValidFrom(LocalDate maxValidFrom) {
this.maxValidFrom = maxValidFrom;
return this;
}

Builder federalState(FederalState federalState) {
this.federalState = federalState;
return this;
}

Builder worksOnPublicHoliday(boolean worksOnPublicHoliday) {
this.worksOnPublicHoliday = worksOnPublicHoliday;
return this;
}

Builder workday(List<DayOfWeek> workday) {
this.workday = workday;
return this;
}

Builder workingTime(Double workingTime) {
this.workingTime = workingTime;
return this;
}

Builder workingTimeMonday(Double workingTimeMonday) {
this.workingTimeMonday = workingTimeMonday;
return this;
}

Builder workingTimeTuesday(Double workingTimeTuesday) {
this.workingTimeTuesday = workingTimeTuesday;
return this;
}

Builder workingTimeWednesday(Double workingTimeWednesday) {
this.workingTimeWednesday = workingTimeWednesday;
return this;
}

Builder workingTimeThursday(Double workingTimeThursday) {
this.workingTimeThursday = workingTimeThursday;
return this;
}

Builder workingTimeFriday(Double workingTimeFriday) {
this.workingTimeFriday = workingTimeFriday;
return this;
}

Builder workingTimeSaturday(Double workingTimeSaturday) {
this.workingTimeSaturday = workingTimeSaturday;
return this;
}

Builder workingTimeSunday(Double workingTimeSunday) {
this.workingTimeSunday = workingTimeSunday;
return this;
}

WorkingTimeDto build() {
final WorkingTimeDto workingTimeDto = new WorkingTimeDto();
workingTimeDto.setUserId(userId);
workingTimeDto.setId(uuid);
workingTimeDto.setValidFrom(validFrom);
workingTimeDto.setMinValidFrom(minValidFrom);
workingTimeDto.setMaxValidFrom(maxValidFrom);
workingTimeDto.setFederalState(federalState);
workingTimeDto.setWorksOnPublicHoliday(worksOnPublicHoliday);
workingTimeDto.setWorkday(workday.stream().map(DayOfWeek::name).map(String::toLowerCase).toList());
workingTimeDto.setWorkingTime(workingTime);
workingTimeDto.setWorkingTimeMonday(workingTimeMonday);
workingTimeDto.setWorkingTimeTuesday(workingTimeTuesday);
workingTimeDto.setWorkingTimeWednesday(workingTimeWednesday);
workingTimeDto.setWorkingTimeThursday(workingTimeThursday);
workingTimeDto.setWorkingTimeFriday(workingTimeFriday);
workingTimeDto.setWorkingTimeSaturday(workingTimeSaturday);
workingTimeDto.setWorkingTimeSunday(workingTimeSunday);
return workingTimeDto;
}
}
}
Loading

0 comments on commit 1addae8

Please sign in to comment.