package eu.reply.lea.services.yms.task.sequence;


import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;

import eu.reply.lea.services.yms.task.TaskType;
import eu.reply.lea.services.yms.task.locationaware.domain.DefaultTaskUnlockStrategy;
import eu.reply.lea.services.yms.task.locationaware.domain.LocationAwareTask;
import eu.reply.lea.services.yms.task.locationaware.domain.LocationAwareTaskHandler;
import eu.reply.lea.services.yms.task.locationaware.domain.TaskQueueService;
import eu.reply.lea.services.yms.task.locationaware.domain.TaskUnlockStrategy;
import eu.reply.lea.services.yms.task.locationaware.domain.mission.MissionTask;
import eu.reply.lea.services.yms.task.locationaware.domain.mission.MissionTaskUnlockStrategy;
import eu.reply.sce.backend.core.jee.log.Logged;

@ApplicationScoped
@Logged
public class TaskUnlockService {

    @Inject
    private LocationAwareTaskHandler taskHandler;

    @Inject
    private TaskQueueService taskQueueService;

    @Inject
    private MissionTaskUnlockStrategy missionTaskUnlockStrategy;

    @Inject
    private DefaultTaskUnlockStrategy defaultTaskUnlockStrategy;


    private Map<TaskType, TaskUnlockStrategy> strategyMap;

    @Inject
    private Logger log;

    @PostConstruct
    public void init() {
        strategyMap = new HashMap<>();
        strategyMap.put(TaskType.MISSION, missionTaskUnlockStrategy);
        // Add other task types if needed, or use default for all others
    }


    public Boolean unlockNextTask(LocationAwareTasksSequence<LocationAwareTask<?,?,?>> tasksSequence) {
        if (tasksSequence.isBlocked()){
            log.info("unlock next task: Task sequence is blocked {}", tasksSequence);
            return false;
        }
        if (tasksSequence.areThereTasksInExecution()){
            log.info("unlock next task: There are tasks in execution {}", tasksSequence);
            return false;
        }
        LocationAwareTask<?,?,?> task = tasksSequence.getNextBlockedTask();
        if (task == null) {
            log.info("No Blocked Tasks found: {}", task);
            return false;
        }

        TaskUnlockStrategy strategy = strategyMap.getOrDefault(task.getTaskType(), defaultTaskUnlockStrategy);
        boolean result = strategy.unlockTask(task);

        if (result) {
            log.info("Task processed successfully: {}", task);
        } else {
            log.info("Task could not be processed: {}", task);
        }

        return result;
    }






}


package eu.reply.lea.services.yms.task.locationaware.domain;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import eu.reply.sce.backend.core.jee.log.Logged;

@ApplicationScoped
@Logged
public class DefaultTaskUnlockStrategy implements TaskUnlockStrategy {

    @Inject
    private LocationAwareTaskHandler taskHandler;

    @Inject
    private TaskQueueService taskQueueService;

    @Override
    public boolean unlockTask(LocationAwareTask<?, ?, ?> task) {
        if (task.getHeaders().isEnableQueue() && !taskQueueService.isTaskQueued(task)){
            taskQueueService.queueTasks(task.getLogisticUnitReference());
        }else{
            return taskHandler.unlockTask(task);
        }
        return false;
    }

}
package eu.reply.lea.services.yms.task.locationaware.domain.mission;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import java.util.List;

import eu.reply.lea.services.yms.task.locationaware.domain.LocationAwareTask;
import eu.reply.lea.services.yms.task.locationaware.domain.LocationAwareTaskHandler;
import eu.reply.lea.services.yms.task.locationaware.domain.TaskQueueService;
import eu.reply.lea.services.yms.task.locationaware.domain.TaskUnlockStrategy;
import eu.reply.sce.backend.core.jee.log.Logged;

@ApplicationScoped
@Logged
public class MissionTaskUnlockStrategy implements TaskUnlockStrategy {

    @Inject
    private LocationAwareTaskHandler taskHandler;

    @Inject
    private TaskQueueService taskQueueService;

    @Override
    public boolean unlockTask(LocationAwareTask<?, ?, ?> task) {
        if (!(task instanceof MissionTask)) {
            throw new IllegalArgumentException("Expected MissionTask");
        }
        MissionTask missionTask = (MissionTask) task;

        if (missionTask.getHeaders() != null
                && missionTask.getHeaders().isEnableQueue()
                && !taskQueueService.isTaskQueued(task)) {
            taskQueueService.queueTasks(task.getLogisticUnitReference());
            return false;
        }

        if (task.getSequence().getNextTask().getHeaders().isEnableQueue() && !taskQueueService.isTaskQueued(task.getSequence().getNextTask())) {
            taskQueueService.queueTasks(task.getLogisticUnitReference());
            return false;
        }

        return taskHandler.unlockTask(task);

        return false; // Cannot unlock task at this time.
    }
}
Do you think there are problems with regarding DDD principles