-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add global setting for federal-state / public-holidays (#543)
closes #516 Here are some things you should have thought about: **Multi-Tenancy** - [x] Extended new entities with `AbstractTenantAwareEntity`? - [x] New entity added to `TenantAwareDatabaseConfiguration`? - [x] Tested with `dev-multitenant` profile? <!-- Thanks for contributing to the zeiterfassung. Please review the following notes before submitting you pull request. Please look for other issues or pull requests which already work on this topic. Is somebody already on it? Do you need to synchronize? # Security Vulnerabilities 🛑 STOP! 🛑 If your contribution fixes a security vulnerability, please do not submit it. Instead, please write an E-Mail to info@focus-shift.de with all the information to recreate the security vulnerability. # Describing Your Changes If, having reviewed the notes above, you're ready to submit your pull request, please provide a brief description of the proposed changes. If they: 🐞 fix a bug, please describe the broken behaviour and how the changes fix it. Please label with 'type: bug' and 'status: new' 🎁 make an enhancement, please describe the new functionality and why you believe it's useful. Please label with 'type: enhancement' and 'status: new' If your pull request relates to any existing issues, please reference them by using the issue number prefixed with #. -->
- Loading branch information
Showing
54 changed files
with
2,493 additions
and
1,037 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/java/de/focusshift/zeiterfassung/CachedSupplier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/main/java/de/focusshift/zeiterfassung/settings/FederalStateSelectDtoFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package de.focusshift.zeiterfassung.settings; | ||
|
||
import de.focusshift.zeiterfassung.publicholiday.FederalState; | ||
import de.focusshift.zeiterfassung.web.html.HtmlOptgroupDto; | ||
import de.focusshift.zeiterfassung.web.html.HtmlOptionDto; | ||
import de.focusshift.zeiterfassung.web.html.HtmlSelectDto; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class FederalStateSelectDtoFactory { | ||
|
||
private FederalStateSelectDtoFactory() { | ||
// | ||
} | ||
|
||
public static HtmlSelectDto federalStateSelectDto(FederalState selectedFederalState) { | ||
return federalStateSelectDto(selectedFederalState, false); | ||
} | ||
|
||
public static HtmlSelectDto federalStateSelectDto(FederalState selectedFederalState, boolean includeGlobalSettingElement) { | ||
|
||
final ArrayList<HtmlOptgroupDto> countries = new ArrayList<>(); | ||
|
||
final List<HtmlOptionDto> generalOptions = new ArrayList<>(); | ||
if (includeGlobalSettingElement) { | ||
final HtmlOptionDto globalOption = new HtmlOptionDto("federalState.GLOBAL", FederalState.GLOBAL.name(), FederalState.GLOBAL.equals(selectedFederalState)); | ||
generalOptions.add(globalOption); | ||
} | ||
|
||
final HtmlOptionDto noneOption = new HtmlOptionDto("federalState.NONE", FederalState.NONE.name(), FederalState.NONE.equals(selectedFederalState)); | ||
generalOptions.add(noneOption); | ||
|
||
countries.add(new HtmlOptgroupDto("country.general", generalOptions)); | ||
|
||
FederalState.federalStatesTypesByCountry().forEach((country, federalStates) -> { | ||
final List<HtmlOptionDto> options = federalStates.stream() | ||
.map(federalState -> new HtmlOptionDto(federalStateMessageKey(federalState), federalState.name(), federalState.equals(selectedFederalState))) | ||
.toList(); | ||
final HtmlOptgroupDto optgroup = new HtmlOptgroupDto("country." + country, options); | ||
countries.add(optgroup); | ||
}); | ||
|
||
return new HtmlSelectDto(countries); | ||
} | ||
|
||
public static String federalStateMessageKey(FederalState federalState) { | ||
return "federalState." + federalState.name(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/de/focusshift/zeiterfassung/settings/FederalStateSettings.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package de.focusshift.zeiterfassung.settings; | ||
|
||
import de.focusshift.zeiterfassung.publicholiday.FederalState; | ||
|
||
/** | ||
* Global federal-state settings. Can be overridden for an individual person. | ||
* | ||
* @param federalState the default federal-state and public holiday regulations | ||
* @param worksOnPublicHoliday whether persons are working on public holidays or not | ||
*/ | ||
public record FederalStateSettings(FederalState federalState, boolean worksOnPublicHoliday) { | ||
|
||
public static final FederalStateSettings DEFAULT = new FederalStateSettings(FederalState.NONE, false); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/de/focusshift/zeiterfassung/settings/FederalStateSettingsDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package de.focusshift.zeiterfassung.settings; | ||
|
||
import de.focusshift.zeiterfassung.publicholiday.FederalState; | ||
|
||
record FederalStateSettingsDto(FederalState federalState, boolean worksOnPublicHoliday) { | ||
} |
82 changes: 82 additions & 0 deletions
82
src/main/java/de/focusshift/zeiterfassung/settings/FederalStateSettingsEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package de.focusshift.zeiterfassung.settings; | ||
|
||
import de.focusshift.zeiterfassung.publicholiday.FederalState; | ||
import de.focusshift.zeiterfassung.tenancy.tenant.AbstractTenantAwareEntity; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Enumerated; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.SequenceGenerator; | ||
|
||
import java.util.Objects; | ||
|
||
import static jakarta.persistence.EnumType.STRING; | ||
|
||
@Entity(name = "settings_federal_state") | ||
public class FederalStateSettingsEntity extends AbstractTenantAwareEntity { | ||
|
||
@Id | ||
@Column(name = "id", unique = true, nullable = false, updatable = false) | ||
@SequenceGenerator(name = "settings_federal_state_seq", sequenceName = "settings_federal_state_seq") | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "settings_federal_state_seq") | ||
protected Long id; | ||
|
||
@Column(name = "federal_state") | ||
@Enumerated(STRING) | ||
private FederalState federalState; | ||
|
||
@Column(name = "works_on_public_holiday") | ||
private boolean worksOnPublicHoliday; | ||
|
||
protected FederalStateSettingsEntity() { | ||
super(null); | ||
} | ||
|
||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public FederalState getFederalState() { | ||
return federalState; | ||
} | ||
|
||
public void setFederalState(FederalState federalState) { | ||
this.federalState = federalState; | ||
} | ||
|
||
public boolean isWorksOnPublicHoliday() { | ||
return worksOnPublicHoliday; | ||
} | ||
|
||
public void setWorksOnPublicHoliday(boolean worksOnPublicHoliday) { | ||
this.worksOnPublicHoliday = worksOnPublicHoliday; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
FederalStateSettingsEntity that = (FederalStateSettingsEntity) o; | ||
return Objects.equals(id, that.id); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(id); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "FederalStateSettingsEntity{" + | ||
"id=" + id + | ||
", federalState=" + federalState + | ||
", worksOnPublicHoliday=" + worksOnPublicHoliday + | ||
'}'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/de/focusshift/zeiterfassung/settings/FederalStateSettingsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package de.focusshift.zeiterfassung.settings; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import java.util.Optional; | ||
|
||
interface FederalStateSettingsRepository extends CrudRepository<FederalStateSettingsEntity, Long> { | ||
|
||
Optional<FederalStateSettingsEntity> findByTenantId(String tenantId); | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/de/focusshift/zeiterfassung/settings/FederalStateSettingsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package de.focusshift.zeiterfassung.settings; | ||
|
||
public interface FederalStateSettingsService { | ||
|
||
FederalStateSettings getFederalStateSettings(); | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/de/focusshift/zeiterfassung/settings/SettingsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package de.focusshift.zeiterfassung.settings; | ||
|
||
import de.focus_shift.launchpad.api.HasLaunchpad; | ||
import de.focusshift.zeiterfassung.timeclock.HasTimeClock; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.servlet.ModelAndView; | ||
|
||
import static de.focusshift.zeiterfassung.settings.FederalStateSelectDtoFactory.federalStateSelectDto; | ||
|
||
@Controller | ||
@RequestMapping("/settings") | ||
@PreAuthorize("hasAuthority('ZEITERFASSUNG_WORKING_TIME_EDIT_GLOBAL')") | ||
class SettingsController implements HasLaunchpad, HasTimeClock { | ||
|
||
private final SettingsService settingsService; | ||
|
||
SettingsController(SettingsService settingsService) { | ||
this.settingsService = settingsService; | ||
} | ||
|
||
@GetMapping | ||
String getSettings() { | ||
return "redirect:settings/federal-state"; | ||
} | ||
|
||
@GetMapping("/federal-state") | ||
String getFederalStateSettings(Model model) { | ||
|
||
final FederalStateSettings settings = settingsService.getFederalStateSettings(); | ||
final FederalStateSettingsDto federalStateSettingsDto = toFederalStateSettingsDto(settings); | ||
|
||
model.addAttribute("federalStateSettings", federalStateSettingsDto); | ||
model.addAttribute("federalStateSelect", federalStateSelectDto(federalStateSettingsDto.federalState())); | ||
|
||
return "settings/settings"; | ||
} | ||
|
||
@PostMapping("/federal-state") | ||
ModelAndView saveSettings(@ModelAttribute("federalStateSettings") FederalStateSettingsDto federalStateSettings, BindingResult result) { | ||
|
||
if (result.hasErrors()) { | ||
return new ModelAndView("settings/settings"); | ||
} | ||
|
||
settingsService.updateFederalStateSettings(federalStateSettings.federalState(), federalStateSettings.worksOnPublicHoliday()); | ||
|
||
return new ModelAndView("redirect:/settings/federal-state"); | ||
} | ||
|
||
private FederalStateSettingsDto toFederalStateSettingsDto(FederalStateSettings federalStateSettings) { | ||
return new FederalStateSettingsDto(federalStateSettings.federalState(), federalStateSettings.worksOnPublicHoliday()); | ||
} | ||
} |
Oops, something went wrong.