auto create shows on plan creation

This commit is contained in:
2025-11-28 20:18:06 +01:00
parent 9015157d37
commit 0240d9e795
2 changed files with 57 additions and 27 deletions

View File

@@ -9,31 +9,38 @@ import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
@ApplicationScoped @ApplicationScoped
public class ShowPlanner { public class ShowPlanner {
private static final ReentrantLock LOCK = new ReentrantLock();
@Inject @Inject
RequestReply requester; RequestReply requester;
@Scheduled(every = "1h", delay = 10, delayUnit = TimeUnit.SECONDS) @Scheduled(every = "1h", delay = 10, delayUnit = TimeUnit.SECONDS)
void planShows() throws Exception { public void planShows() throws Exception {
CommandWrapper requestPlans = new CommandListPlan().serialize().generateIds(); LOCK.lock();
try {
LocalDateTime today = LocalDateTime.now();
LocalDateTime todayPlus14 = LocalDateTime.now().plusDays(14);
List<String> planFilter = List.of("le;first;date;" + todayPlus14);
CommandWrapper requestPlans = new CommandListPlan(planFilter).serialize().generateIds();
List<Plan> plans = CommandListPlanResponse.deserialize(requester.request(requestPlans)).getList(); List<Plan> plans = CommandListPlanResponse.deserialize(requester.request(requestPlans)).getList();
CommandWrapper requestShows = new CommandListVorstellung().serialize().tx(requestPlans); List<String> showFilter = List.of("ge;start;date;" + today.minusDays(1), "le;start;date;" + todayPlus14.plusDays(1));
CommandWrapper requestShows = new CommandListVorstellung(showFilter).serialize().tx(requestPlans);
List<Vorstellung> shows = CommandListVorstellungResponse.deserialize(requester.request(requestShows)).getList(); List<Vorstellung> shows = CommandListVorstellungResponse.deserialize(requester.request(requestShows)).getList();
LocalDate todayPlus14 = LocalDate.now().plusDays(14);
for (Plan plan : plans) { for (Plan plan : plans) {
if (plan.getFirst().isAfter(todayPlus14)) continue;
for (int i = 0; i < 14; i++) { for (int i = 0; i < 14; i++) {
LocalDate day = LocalDate.now().plusDays(i); LocalDate day = LocalDate.now().plusDays(i);
if (day.getDayOfWeek().getValue() != plan.getWeekday()) continue; if (day.getDayOfWeek().getValue() != plan.getWeekday()) continue;
boolean exists = shows.stream().anyMatch(show -> show.getHall().getId().equals(plan.getHall().getId()) boolean exists = shows.stream().anyMatch(show -> show.getHall().getId().equals(plan.getHall().getId())
&& show.getMovie().getId().equals(plan.getMovie().getId()) //&& show.getMovie().getId().equals(plan.getMovie().getId()) // nicht notwendige Überprüfung!
&& show.getStart().toLocalTime().equals(plan.getTime()) && show.getStart().toLocalTime().equals(plan.getTime())
&& show.getStart().toLocalDate().equals(day)); && show.getStart().toLocalDate().equals(day));
if (exists) continue; if (exists) continue;
@@ -52,6 +59,9 @@ public class ShowPlanner {
CommandWrapper commit = new CommandListVorstellung().serialize().tx(requestPlans).commit(); CommandWrapper commit = new CommandListVorstellung().serialize().tx(requestPlans).commit();
CommandListVorstellungResponse.deserialize(requester.request(commit)); CommandListVorstellungResponse.deserialize(requester.request(commit));
} finally {
LOCK.unlock();
}
} }
} }

View File

@@ -1,9 +1,13 @@
package de.infinimotion.backend.util; package de.infinimotion.backend.util;
import de.infinimotion.backend.task.ShowPlanner;
import de.infinimotion.model.backend.CommandCreatePlanResponse;
import de.infinimotion.model.backend.CommandException; import de.infinimotion.model.backend.CommandException;
import de.infinimotion.model.backend.CommandWrapper; import de.infinimotion.model.backend.CommandWrapper;
import de.infinimotion.model.backend.RequestReply; import de.infinimotion.model.backend.RequestReply;
import io.quarkus.scheduler.Scheduler;
import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import org.eclipse.microprofile.reactive.messaging.Channel; import org.eclipse.microprofile.reactive.messaging.Channel;
import org.eclipse.microprofile.reactive.messaging.Emitter; import org.eclipse.microprofile.reactive.messaging.Emitter;
import org.eclipse.microprofile.reactive.messaging.Incoming; import org.eclipse.microprofile.reactive.messaging.Incoming;
@@ -55,6 +59,7 @@ public class BetterRequestReply implements RequestReply {
if (CommandException.isType(response)) { if (CommandException.isType(response)) {
throw new RuntimeException(CommandException.deserialize(response).getException()); throw new RuntimeException(CommandException.deserialize(response).getException());
} }
intercept(response);
return response; return response;
} finally { } finally {
RESPONSES.remove(wrapper.getRequest()); RESPONSES.remove(wrapper.getRequest());
@@ -62,4 +67,19 @@ public class BetterRequestReply implements RequestReply {
} }
} }
@Inject
ShowPlanner showPlanner;
private void intercept(CommandWrapper response) {
if (response.getType().equals(CommandCreatePlanResponse.class.getSimpleName())) {
new Thread(() -> {
try {
showPlanner.planShows();
} catch (Exception e) {
throw new RuntimeException(e);
}
}).start();
}
}
} }