Skip to content

Commit

Permalink
[Gepardec/mega#735] Homogenize sync tasks and services
Browse files Browse the repository at this point in the history
  • Loading branch information
Ollitod committed Feb 16, 2024
1 parent 7740f1f commit 4141b7d
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 127 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.gepardec.mega.application.schedule;

import com.gepardec.mega.domain.utils.DateUtils;
import com.gepardec.mega.notification.mail.ReminderEmailSender;
import com.gepardec.mega.notification.mail.receiver.MailReceiver;
import com.gepardec.mega.service.api.EnterpriseSyncService;
Expand All @@ -12,9 +11,10 @@
import jakarta.enterprise.context.Dependent;
import jakarta.inject.Inject;

import java.time.LocalDate;
import java.util.concurrent.TimeUnit;

import static com.gepardec.mega.domain.utils.DateUtils.getFirstDayOfCurrentMonth;

/**
* @author Thomas Herzog <herzog.thomas81@gmail.com>
* @since 10/3/2020
Expand Down Expand Up @@ -49,27 +49,27 @@ public class Schedules {
every = "PT30M",
delay = 15, delayUnit = TimeUnit.SECONDS)
// We need to wait for liquibase to finish, but is executed in parallel
void syncEmployeesWithDatabase() {
void syncEmployees() {
syncService.syncEmployees();
}

@Scheduled(identity = "Generate step entries on the last day of a month at 00:00",
cron = "0 0 0 L * ? *")
void generateStepEntriesDefault() {
stepEntrySyncService.generateStepEntriesFromScheduler();
void generateStepEntries() {
stepEntrySyncService.generateStepEntries(getFirstDayOfCurrentMonth());
}

@Scheduled(identity = "Update project entries every 30 minutes",
every = "PT30M",
delay = 30, delayUnit = TimeUnit.SECONDS)
void generateProjects() {
projectSyncService.generateProjects();
projectSyncService.generateProjects(getFirstDayOfCurrentMonth());
}

@Scheduled(identity = "Generate enterprise entries on the last day of a month at 00:00",
cron = "0 0 0 L * ? *")
void generateEnterpriseEntries() {
enterpriseSyncService.generateEnterpriseEntries(LocalDate.now().withDayOfMonth(1));
enterpriseSyncService.generateEnterpriseEntries(getFirstDayOfCurrentMonth());
}

@Scheduled(identity = "Send E-Mail reminder to Users",
Expand All @@ -89,6 +89,6 @@ void receiveMails() {
@Scheduled(identity = "Take existing PrematureEmployeeChecks and update StepEntries accordingly on the last day of a month at 06:00",
cron = "0 0 6 L * ? *")
void syncPrematureEmployeeChecksWithStepEntries() {
prematureEmployeeCheckSyncService.syncPrematureEmployeeChecksWithStepEntries(DateUtils.getCurrentYearMonth());
prematureEmployeeCheckSyncService.syncPrematureEmployeeChecksWithStepEntries(getFirstDayOfCurrentMonth());
}
}
5 changes: 2 additions & 3 deletions src/main/java/com/gepardec/mega/domain/utils/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ public static LocalDate getFirstDayOfCurrentMonth() {
return LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
}

public static YearMonth getCurrentYearMonth() {
LocalDate today = today();
return YearMonth.of(today().getYear(), today.getMonth());
public static LocalDate getFirstOfYearMonth(YearMonth yearMonth) {
return yearMonth.atDay(1);
}
}
91 changes: 30 additions & 61 deletions src/main/java/com/gepardec/mega/rest/impl/SyncResourceImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.gepardec.mega.rest.impl;

import com.gepardec.mega.domain.utils.DateUtils;
import com.gepardec.mega.rest.api.SyncResource;
import com.gepardec.mega.service.api.EnterpriseSyncService;
import com.gepardec.mega.service.api.PrematureEmployeeCheckSyncService;
Expand All @@ -14,6 +13,10 @@

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.function.Function;

import static com.gepardec.mega.domain.utils.DateUtils.getFirstDayOfCurrentMonth;
import static com.gepardec.mega.domain.utils.DateUtils.getFirstOfYearMonth;

@RequestScoped
@IfBuildProperty(name = "mega.endpoint.test.enable", stringValue = "true", enableIfMissing = true)
Expand All @@ -34,82 +37,30 @@ public class SyncResourceImpl implements SyncResource {
@Inject
PrematureEmployeeCheckSyncService prematureEmployeeCheckSyncService;

@Override
public Response syncProjects(YearMonth from, YearMonth to) {
if (from == null) {
return Response.ok(projectSyncService.generateProjects()).build();
}

if (to == null) {
return Response.ok(projectSyncService.generateProjects(from.atDay(1))).build();
}

while (from.isBefore(to)) {
projectSyncService.generateProjects(from.atDay(1));
from = from.plusMonths(1);
}

return Response.ok(projectSyncService.generateProjects(from.atDay(1))).build();
}

@Override
public Response syncEmployees() {
syncService.syncEmployees();
return Response.ok("ok").build();
}

@Override
public Response generateEnterpriseEntries(YearMonth from, YearMonth to) {
if (from == null) {
return Response.ok(enterpriseSyncService.generateEnterpriseEntries(LocalDate.now().withDayOfMonth(1))).build();
}
if (to == null) {
return Response.ok(enterpriseSyncService.generateEnterpriseEntries(from.atDay(1))).build();
}

while (from.isBefore(to)) {
enterpriseSyncService.generateEnterpriseEntries(from.atDay(1));
from = from.plusMonths(1);
}
public Response syncProjects(YearMonth from, YearMonth to) {
return syncFromTo(projectSyncService::generateProjects, from, to);
}

return Response.ok(enterpriseSyncService.generateEnterpriseEntries(from.atDay(1))).build();
@Override
public Response generateEnterpriseEntries(YearMonth from, YearMonth to) {
return syncFromTo(enterpriseSyncService::generateEnterpriseEntries, from, to);
}

@Override
public Response generateStepEntries(YearMonth from, YearMonth to) {
if (from == null) {
return Response.ok(stepEntrySyncService.generateStepEntriesFromEndpoint()).build();
}
if (to == null) {
return Response.ok(stepEntrySyncService.generateStepEntriesFromEndpoint(from)).build();
}

while (from.isBefore(to)) {
stepEntrySyncService.generateStepEntriesFromEndpoint(from);
from = from.plusMonths(1);
}

return Response.ok(stepEntrySyncService.generateStepEntriesFromEndpoint(from)).build();
return syncFromTo(stepEntrySyncService::generateStepEntries, from, to);
}

@Override
public Response syncPrematureEmployeeChecks(YearMonth from, YearMonth to) {
if (from == null) {
prematureEmployeeCheckSyncService.syncPrematureEmployeeChecksWithStepEntries(DateUtils.getCurrentYearMonth());
return Response.ok().build();
}

if (to == null) {
prematureEmployeeCheckSyncService.syncPrematureEmployeeChecksWithStepEntries(from);
return Response.ok().build();
}

do {
prematureEmployeeCheckSyncService.syncPrematureEmployeeChecksWithStepEntries(from);
from = from.plusMonths(1);
} while (from.compareTo(to) <= 0);

return Response.ok().build();
return syncFromTo(prematureEmployeeCheckSyncService::syncPrematureEmployeeChecksWithStepEntries, from, to);
}

@Override
Expand All @@ -118,7 +69,25 @@ public Response syncAll(YearMonth from, YearMonth to) {
syncProjects(from, to);
generateEnterpriseEntries(from, to);
generateStepEntries(from, to);
syncPrematureEmployeeChecks(from, to);

return Response.ok("ok").build();
}

private Response syncFromTo(Function<LocalDate, Boolean> syncFunction, YearMonth from, YearMonth to) {
if (from == null) {
return Response.ok(syncFunction.apply(getFirstDayOfCurrentMonth())).build();
}

if (to == null) {
return Response.ok(syncFunction.apply(getFirstOfYearMonth(from))).build();
}

while (from.isBefore(to)) {
syncFunction.apply(getFirstOfYearMonth(from));
from = from.plusMonths(1);
}

return Response.ok(syncFunction.apply(getFirstOfYearMonth(from))).build();
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.gepardec.mega.service.api;

import java.time.YearMonth;
import java.time.LocalDate;

public interface PrematureEmployeeCheckSyncService {

boolean syncPrematureEmployeeChecksWithStepEntries(YearMonth yearMonth);
boolean syncPrematureEmployeeChecksWithStepEntries(LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@

public interface ProjectSyncService {

boolean generateProjects();

boolean generateProjects(LocalDate date);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.gepardec.mega.service.api;

import java.time.YearMonth;
import java.time.LocalDate;

public interface StepEntrySyncService {

boolean generateStepEntriesFromEndpoint();

boolean generateStepEntriesFromEndpoint(YearMonth date);

boolean generateStepEntriesFromScheduler();
boolean generateStepEntries(LocalDate date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.slf4j.Logger;

import java.time.LocalDate;
import java.time.YearMonth;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -33,18 +32,17 @@ public class PrematureEmployeeCheckSyncServiceImpl implements PrematureEmployeeC
Logger logger;

@Override
public boolean syncPrematureEmployeeChecksWithStepEntries(YearMonth yearMonth) {
public boolean syncPrematureEmployeeChecksWithStepEntries(LocalDate date) {
boolean allEntriesUpdated = true;
LocalDate selectedMonth = yearMonth.atDay(1);
List<PrematureEmployeeCheck> prematureEmployeeCheckEntities = prematureEmployeeCheckService.findAllForMonth(selectedMonth)
List<PrematureEmployeeCheck> prematureEmployeeCheckEntities = prematureEmployeeCheckService.findAllForMonth(date)
.stream()
.filter(pec -> pec.getState().equals(PrematureEmployeeCheckState.DONE) || pec.getState().equals(PrematureEmployeeCheckState.IN_PROGRESS))
.collect(Collectors.toList());

logger.info(
String.format("Syncing %s PrematureEmployeeChecks with StepEntries for Month: %s",
prematureEmployeeCheckEntities.size(),
selectedMonth)
date)
);

for (PrematureEmployeeCheck pec : prematureEmployeeCheckEntities) {
Expand All @@ -63,7 +61,7 @@ public boolean syncPrematureEmployeeChecksWithStepEntries(YearMonth yearMonth) {
}
}

prematureEmployeeCheckService.deleteAllForMonthWithState(selectedMonth, List.of(PrematureEmployeeCheckState.CANCELLED, PrematureEmployeeCheckState.IN_PROGRESS));
prematureEmployeeCheckService.deleteAllForMonthWithState(date, List.of(PrematureEmployeeCheckState.CANCELLED, PrematureEmployeeCheckState.IN_PROGRESS));

return allEntriesUpdated;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ public boolean generateProjects(LocalDate date) {
return !projects.isEmpty();
}


@Override
public boolean generateProjects() {
return generateProjects(LocalDate.now().minusMonths(1).withDayOfMonth(1));
}

private List<com.gepardec.mega.db.entity.project.Project> createProjects(List<User> activeUsers, List<Project> projects, LocalDate date) {
return projects.stream()
.map(project -> createProjectEntityFromProject(activeUsers, project, date))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,7 @@ public class StepEntrySyncServiceImpl implements StepEntrySyncService {
NotificationConfig notificationConfig;

@Override
public boolean generateStepEntriesFromEndpoint() {
return generateStepEntries(LocalDate.now().minusMonths(1).with(TemporalAdjusters.firstDayOfMonth()));
}

@Override
public boolean generateStepEntriesFromEndpoint(YearMonth date) {
return generateStepEntries(date.atDay(1));
}

@Override
public boolean generateStepEntriesFromScheduler() {
return generateStepEntries(LocalDate.now().with(TemporalAdjusters.firstDayOfMonth()));
}


private boolean generateStepEntries(LocalDate date) {
public boolean generateStepEntries(LocalDate date) {
final StopWatch stopWatch = new StopWatch();
stopWatch.start();

Expand Down
Loading

0 comments on commit 4141b7d

Please sign in to comment.