diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/NewTaskController.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/NewTaskController.java index 7ff5349..e153322 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/NewTaskController.java +++ b/roster/src/main/java/ch/unisg/roster/roster/adapter/in/web/NewTaskController.java @@ -31,7 +31,7 @@ public class NewTaskController { logger.info("New task with id:" + task.getTaskID()); - NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskType(), + NewTaskCommand command = new NewTaskCommand(task.getTaskID(), task.getTaskUri(), task.getTaskType(), task.getInputData()); boolean success = newTaskUseCase.addNewTaskToQueue(command); diff --git a/roster/src/main/java/ch/unisg/roster/roster/adapter/out/web/LaunchAuctionEventAdapter.java b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/web/LaunchAuctionEventAdapter.java new file mode 100644 index 0000000..77bf619 --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/adapter/out/web/LaunchAuctionEventAdapter.java @@ -0,0 +1,58 @@ +package ch.unisg.roster.roster.adapter.out.web; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.util.HashMap; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +import org.springframework.beans.factory.annotation.Value; + +import ch.unisg.roster.roster.application.port.out.LaunchAuctionEventPort; +import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent; + +public class LaunchAuctionEventAdapter implements LaunchAuctionEventPort { + + @Value("${auction.house.uri}") + String server; + + @Override + public void launchAuctionEvent(LaunchAuctionEvent launchAuctionEvent) { + + var values = new HashMap() {{ + + put("taskUri", launchAuctionEvent.taskUri); + put("taskType", launchAuctionEvent.taskType.getValue()); + + }}; + + var objectMapper = new ObjectMapper(); + String requestBody = null; + try { + requestBody = objectMapper.writeValueAsString(values); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + + HttpClient client = HttpClient.newHttpClient(); + HttpRequest request = HttpRequest.newBuilder() + .uri(URI.create(server+"/auctions/")) + .header("Content-Type", "application/task+json") + .POST(HttpRequest.BodyPublishers.ofString(requestBody)) + .build(); + + try { + HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); + } catch (IOException e) { + e.printStackTrace(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + +} diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/port/in/NewTaskCommand.java b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/NewTaskCommand.java index 5db2b9f..f312518 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/application/port/in/NewTaskCommand.java +++ b/roster/src/main/java/ch/unisg/roster/roster/application/port/in/NewTaskCommand.java @@ -1,5 +1,7 @@ package ch.unisg.roster.roster.application.port.in; +import java.net.URI; + import javax.validation.constraints.NotNull; import ch.unisg.roster.roster.domain.valueobject.ExecutorType; @@ -20,8 +22,12 @@ public class NewTaskCommand extends SelfValidating { @NotNull private final String inputData; - public NewTaskCommand(String taskID, ExecutorType taskType, String inputData) { + @NotNull + private final String taskUri; + + public NewTaskCommand(String taskID, String taskUri, ExecutorType taskType, String inputData) { this.taskID = taskID; + this.taskUri = taskUri; this.taskType = taskType; this.inputData = inputData; this.validateSelf(); diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/port/out/LaunchAuctionEventPort.java b/roster/src/main/java/ch/unisg/roster/roster/application/port/out/LaunchAuctionEventPort.java new file mode 100644 index 0000000..dd828f4 --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/application/port/out/LaunchAuctionEventPort.java @@ -0,0 +1,8 @@ +package ch.unisg.roster.roster.application.port.out; + +import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent; + +public interface LaunchAuctionEventPort { + + void launchAuctionEvent(LaunchAuctionEvent launchAuctionEvent); +} diff --git a/roster/src/main/java/ch/unisg/roster/roster/application/service/NewTaskService.java b/roster/src/main/java/ch/unisg/roster/roster/application/service/NewTaskService.java index c1aab5c..84cae57 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/application/service/NewTaskService.java +++ b/roster/src/main/java/ch/unisg/roster/roster/application/service/NewTaskService.java @@ -6,10 +6,12 @@ import org.springframework.stereotype.Component; import ch.unisg.roster.roster.application.port.in.NewTaskCommand; import ch.unisg.roster.roster.application.port.in.NewTaskUseCase; +import ch.unisg.roster.roster.application.port.out.LaunchAuctionEventPort; import ch.unisg.roster.roster.application.port.out.NewTaskEventPort; import ch.unisg.roster.roster.domain.ExecutorRegistry; import ch.unisg.roster.roster.domain.Roster; import ch.unisg.roster.roster.domain.Task; +import ch.unisg.roster.roster.domain.event.LaunchAuctionEvent; import ch.unisg.roster.roster.domain.event.NewTaskEvent; import lombok.RequiredArgsConstructor; @@ -20,6 +22,8 @@ public class NewTaskService implements NewTaskUseCase { private final NewTaskEventPort newTaskEventPort; + private final LaunchAuctionEventPort launchAuctionEventPort; + /** * Checks if we can execute the give task, if yes the task gets added to the task queue and return true. * If the task can not be executed by an internal or auction house executor, the method return false. @@ -31,7 +35,10 @@ public class NewTaskService implements NewTaskUseCase { ExecutorRegistry executorRegistry = ExecutorRegistry.getInstance(); if (!executorRegistry.containsTaskType(command.getTaskType())) { - return false; + LaunchAuctionEvent launchAuctionEvent = new LaunchAuctionEvent( command.getTaskUri(), + command.getTaskType()); + launchAuctionEventPort.launchAuctionEvent(launchAuctionEvent); + return true; } Task task = new Task(command.getTaskID(), command.getTaskType(), command.getInputData()); diff --git a/roster/src/main/java/ch/unisg/roster/roster/domain/Task.java b/roster/src/main/java/ch/unisg/roster/roster/domain/Task.java index ee30763..25cfcc0 100644 --- a/roster/src/main/java/ch/unisg/roster/roster/domain/Task.java +++ b/roster/src/main/java/ch/unisg/roster/roster/domain/Task.java @@ -12,6 +12,9 @@ public class Task { @Getter private ExecutorType taskType; + @Getter + private String taskUri; + @Getter @Setter private String inputData; @@ -40,6 +43,12 @@ public class Task { this.inputData = inputData; } + public Task(String taskID, String taskUri, ExecutorType taskType) { + this.taskID = taskID; + this.taskUri = taskUri; + this.taskType = taskType; + } + public Task() {} } diff --git a/roster/src/main/java/ch/unisg/roster/roster/domain/event/LaunchAuctionEvent.java b/roster/src/main/java/ch/unisg/roster/roster/domain/event/LaunchAuctionEvent.java new file mode 100644 index 0000000..e68f823 --- /dev/null +++ b/roster/src/main/java/ch/unisg/roster/roster/domain/event/LaunchAuctionEvent.java @@ -0,0 +1,14 @@ +package ch.unisg.roster.roster.domain.event; + +import ch.unisg.roster.roster.domain.valueobject.ExecutorType; + +public class LaunchAuctionEvent { + public final String taskUri; + public final ExecutorType taskType; + + public LaunchAuctionEvent(String taskUri, ExecutorType taskType) { + this.taskUri = taskUri; + this.taskType = taskType; + } + +}