<commit_before>package net.hawkengine.services;

import net.hawkengine.model.ExecTask;
import net.hawkengine.model.FetchArtifactTask;
import net.hawkengine.model.FetchMaterialTask;
import net.hawkengine.model.JobDefinition;
import net.hawkengine.model.ServiceResult;
import net.hawkengine.model.TaskDefinition;
import net.hawkengine.model.UploadArtifactTask;
import net.hawkengine.model.enums.TaskType;
import net.hawkengine.services.interfaces.IJobDefinitionService;
import net.hawkengine.services.interfaces.ITaskDefinitionService;

import java.util.ArrayList;
import java.util.List;

public class TaskDefinitionService extends CrudService<TaskDefinition> implements ITaskDefinitionService {
    private IJobDefinitionService jobDefinitionService;
    private String successMessage = "retrieved successfully";
    private String failureMessage = "not found";

    public TaskDefinitionService() {
        super.setObjectType("TaskDefinition");
        this.jobDefinitionService = new JobDefinitionService();
    }

    public TaskDefinitionService(IJobDefinitionService jobDefinitionService) {
        super.setObjectType("TaskDefinition");
        this.jobDefinitionService = jobDefinitionService;
    }

    @Override
    public ServiceResult getById(String taskDefinitionId) {
        List<JobDefinition> jobDefinitions = (List<JobDefinition>) this.jobDefinitionService.getAll().getObject();
        TaskDefinition result = null;

        for (JobDefinition jobDefinition : jobDefinitions) {
            List<TaskDefinition> taskDefinitions = jobDefinition.getTaskDefinitions();
            for (TaskDefinition taskDefinition : taskDefinitions) {
                if (taskDefinition.getId().equals(taskDefinitionId)) {
                    result = taskDefinition;
                    return super.createServiceResult(result, false, this.successMessage);
                }
            }
        }

        return super.createServiceResult(result, true, this.failureMessage);
    }

    @Override
    public ServiceResult getAll() {
        List<JobDefinition> jobDefinitions = (List<JobDefinition>) this.jobDefinitionService.getAll().getObject();
        List<TaskDefinition> taskDefinitions = new ArrayList<>();

        for (JobDefinition jobDefinition : jobDefinitions) {
            List<TaskDefinition> taskJobDefinitions = jobDefinition.getTaskDefinitions();
            taskDefinitions.addAll(taskJobDefinitions);
        }

        return super.createServiceResultArray(taskDefinitions, false, this.successMessage);
    }

    @Override
    public ServiceResult add(ExecTask taskDefintion) {
        ServiceResult result = this.addTask(taskDefintion);
        return result;
    }

    @Override
    public ServiceResult add(FetchMaterialTask taskDefintion) {
        ServiceResult result = this.addTask(taskDefintion);
        return result;
    }

    @Override
    public ServiceResult add(FetchArtifactTask taskDefinition) {
        ServiceResult result = this.addTask(taskDefinition);
        return result;
    }

    @Override
    public ServiceResult add(UploadArtifactTask taskDefinition) {
        ServiceResult result = this.addTask(taskDefinition);
        return result;
    }

    public ServiceResult addTask(TaskDefinition taskDefinition) {
        JobDefinition jobDefinition = (JobDefinition) this.jobDefinitionService.getById(taskDefinition.getJobDefinitionId()).getObject();
        List<TaskDefinition> taskDefinitions = jobDefinition.getTaskDefinitions();
//        boolean hasNameCollision = this.checkForNameCollision(taskDefinitions, taskDefinition);
//        if (hasNameCollision) {
//            return super.createServiceResult(taskDefinition, true, "with the same name exists");
//        }

        taskDefinitions.add(taskDefinition);
        jobDefinition.setTaskDefinitions(taskDefinitions);
        JobDefinition updatedJobDefinition = (JobDefinition) this.jobDefinitionService.update(jobDefinition).getObject();

        TaskDefinition result = this.extractTaskDefinitionFromJobDefinition(updatedJobDefinition, taskDefinition.getId());
        if (result == null) {
            return super.createServiceResult(result, true, "not added successfully");
        }

        return super.createServiceResult(result, false, "added successfully");
    }

    @Override
    public ServiceResult update(ExecTask taskDefintion) {
        ServiceResult result = this.updateTask(taskDefintion);
        return result;
    }

    @Override
    public ServiceResult update(FetchMaterialTask taskDefintion) {
        ServiceResult result = this.updateTask(taskDefintion);
        return result;
    }

    @Override
    public ServiceResult update(FetchArtifactTask taskDefinition) {
        ServiceResult result = this.updateTask(taskDefinition);
        return result;
    }

    @Override
    public ServiceResult update(UploadArtifactTask taskDefinition) {
        ServiceResult result = this.updateTask(taskDefinition);
        return result;
    }

    public ServiceResult updateTask(TaskDefinition taskDefinition) {
        TaskDefinition result = null;

        JobDefinition jobDefinition = (JobDefinition) this.jobDefinitionService.getById(taskDefinition.getJobDefinitionId()).getObject();
        List<TaskDefinition> taskDefinitions = jobDefinition.getTaskDefinitions();
//        boolean hasNameCollision = this.checkForNameCollision(taskDefinitions, taskDefinition);
//        if (hasNameCollision) {
//            return super.createServiceResult(taskDefinition, true, "with the same name exists");
//        }

        int lengthOfTaskDefinitions = taskDefinitions.size();
        for (int i = 0; i < lengthOfTaskDefinitions; i++) {
            TaskDefinition definition = taskDefinitions.get(i);
            if (definition.getId().equals(taskDefinition.getId())) {
                Class resultTaskClass = this.getTaskDefinitionType(taskDefinition);
                try {
                    result = (TaskDefinition) resultTaskClass.newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    e.printStackTrace();
                }

                taskDefinitions.set(i, taskDefinition);
                jobDefinition.setTaskDefinitions(taskDefinitions);
                JobDefinition updatedJobDefinition = (JobDefinition) this.jobDefinitionService.update(jobDefinition).getObject();
                result = this.extractTaskDefinitionFromJobDefinition(updatedJobDefinition, taskDefinition.getId());
                break;
            }
        }

        if (result == null) {
            return super.createServiceResult(result, true, "not found");
        }

        return super.createServiceResult(result, false, "updated successfully");
    }

    @Override
    public ServiceResult delete(String taskDefinitionId) {
        boolean isRemoved = false;
        TaskDefinition taskDefinitionToDelete = (TaskDefinition) this.getById(taskDefinitionId).getObject();
        if (taskDefinitionToDelete == null) {
            return super.createServiceResult(taskDefinitionToDelete, true, "does not exists");
        }

        JobDefinition jobDefinition = (JobDefinition) this.jobDefinitionService
                .getById(taskDefinitionToDelete.getJobDefinitionId())
                .getObject();
        List<TaskDefinition> taskDefinitions = jobDefinition.getTaskDefinitions();

        int lengthOfTaskDefinitions = taskDefinitions.size();
        if (lengthOfTaskDefinitions > 1) {
            for (int i = 0; i < lengthOfTaskDefinitions; i++) {
                TaskDefinition definition = taskDefinitions.get(i);
                if (definition.getId().equals(taskDefinitionToDelete.getId())) {
                    taskDefinitions.remove(definition);
                    isRemoved = true;
                    break;
                }
            }
        } else {
            return super.createServiceResult(taskDefinitionToDelete, true, "cannot delete the last job definition");
        }<commit_msg>Small typo fix in TaskDefinitionService<commit_after>package net.hawkengine.services;

import net.hawkengine.model.ExecTask;
import net.hawkengine.model.FetchArtifactTask;
import net.hawkengine.model.FetchMaterialTask;
import net.hawkengine.model.JobDefinition;
import net.hawkengine.model.ServiceResult;
import net.hawkengine.model.TaskDefinition;
import net.hawkengine.model.UploadArtifactTask;
import net.hawkengine.model.enums.TaskType;
import net.hawkengine.services.interfaces.IJobDefinitionService;
import net.hawkengine.services.interfaces.ITaskDefinitionService;

import java.util.ArrayList;
import java.util.List;

public class TaskDefinitionService extends CrudService<TaskDefinition> implements ITaskDefinitionService {
    private IJobDefinitionService jobDefinitionService;
    private String successMessage = "retrieved successfully";
    private String failureMessage = "not found";

    public TaskDefinitionService() {
        super.setObjectType("TaskDefinition");
        this.jobDefinitionService = new JobDefinitionService();
    }

    public TaskDefinitionService(IJobDefinitionService jobDefinitionService) {
        super.setObjectType("TaskDefinition");
        this.jobDefinitionService = jobDefinitionService;
    }

    @Override
    public ServiceResult getById(String taskDefinitionId) {
        List<JobDefinition> jobDefinitions = (List<JobDefinition>) this.jobDefinitionService.getAll().getObject();
        TaskDefinition result = null;

        for (JobDefinition jobDefinition : jobDefinitions) {
            List<TaskDefinition> taskDefinitions = jobDefinition.getTaskDefinitions();
            for (TaskDefinition taskDefinition : taskDefinitions) {
                if (taskDefinition.getId().equals(taskDefinitionId)) {
                    result = taskDefinition;
                    return super.createServiceResult(result, false, this.successMessage);
                }
            }
        }

        return super.createServiceResult(result, true, this.failureMessage);
    }

    @Override
    public ServiceResult getAll() {
        List<JobDefinition> jobDefinitions = (List<JobDefinition>) this.jobDefinitionService.getAll().getObject();
        List<TaskDefinition> taskDefinitions = new ArrayList<>();

        for (JobDefinition jobDefinition : jobDefinitions) {
            List<TaskDefinition> taskJobDefinitions = jobDefinition.getTaskDefinitions();
            taskDefinitions.addAll(taskJobDefinitions);
        }

        return super.createServiceResultArray(taskDefinitions, false, this.successMessage);
    }

    @Override
    public ServiceResult add(ExecTask taskDefintion) {
        ServiceResult result = this.addTask(taskDefintion);
        return result;
    }

    @Override
    public ServiceResult add(FetchMaterialTask taskDefintion) {
        ServiceResult result = this.addTask(taskDefintion);
        return result;
    }

    @Override
    public ServiceResult add(FetchArtifactTask taskDefinition) {
        ServiceResult result = this.addTask(taskDefinition);
        return result;
    }

    @Override
    public ServiceResult add(UploadArtifactTask taskDefinition) {
        ServiceResult result = this.addTask(taskDefinition);
        return result;
    }

    public ServiceResult addTask(TaskDefinition taskDefinition) {
        JobDefinition jobDefinition = (JobDefinition) this.jobDefinitionService.getById(taskDefinition.getJobDefinitionId()).getObject();
        List<TaskDefinition> taskDefinitions = jobDefinition.getTaskDefinitions();
//        boolean hasNameCollision = this.checkForNameCollision(taskDefinitions, taskDefinition);
//        if (hasNameCollision) {
//            return super.createServiceResult(taskDefinition, true, "with the same name exists");
//        }

        taskDefinitions.add(taskDefinition);
        jobDefinition.setTaskDefinitions(taskDefinitions);
        JobDefinition updatedJobDefinition = (JobDefinition) this.jobDefinitionService.update(jobDefinition).getObject();

        TaskDefinition result = this.extractTaskDefinitionFromJobDefinition(updatedJobDefinition, taskDefinition.getId());
        if (result == null) {
            return super.createServiceResult(result, true, "not added successfully");
        }

        return super.createServiceResult(result, false, "added successfully");
    }

    @Override
    public ServiceResult update(ExecTask taskDefintion) {
        ServiceResult result = this.updateTask(taskDefintion);
        return result;
    }

    @Override
    public ServiceResult update(FetchMaterialTask taskDefintion) {
        ServiceResult result = this.updateTask(taskDefintion);
        return result;
    }

    @Override
    public ServiceResult update(FetchArtifactTask taskDefinition) {
        ServiceResult result = this.updateTask(taskDefinition);
        return result;
    }

    @Override
    public ServiceResult update(UploadArtifactTask taskDefinition) {
        ServiceResult result = this.updateTask(taskDefinition);
        return result;
    }

    public ServiceResult updateTask(TaskDefinition taskDefinition) {
        TaskDefinition result = null;

        JobDefinition jobDefinition = (JobDefinition) this.jobDefinitionService.getById(taskDefinition.getJobDefinitionId()).getObject();
        List<TaskDefinition> taskDefinitions = jobDefinition.getTaskDefinitions();
//        boolean hasNameCollision = this.checkForNameCollision(taskDefinitions, taskDefinition);
//        if (hasNameCollision) {
//            return super.createServiceResult(taskDefinition, true, "with the same name exists");
//        }

        int lengthOfTaskDefinitions = taskDefinitions.size();
        for (int i = 0; i < lengthOfTaskDefinitions; i++) {
            TaskDefinition definition = taskDefinitions.get(i);
            if (definition.getId().equals(taskDefinition.getId())) {
                Class resultTaskClass = this.getTaskDefinitionType(taskDefinition);
                try {
                    result = (TaskDefinition) resultTaskClass.newInstance();
                } catch (InstantiationException | IllegalAccessException e) {
                    e.printStackTrace();
                }

                taskDefinitions.set(i, taskDefinition);
                jobDefinition.setTaskDefinitions(taskDefinitions);
                JobDefinition updatedJobDefinition = (JobDefinition) this.jobDefinitionService.update(jobDefinition).getObject();
                result = this.extractTaskDefinitionFromJobDefinition(updatedJobDefinition, taskDefinition.getId());
                break;
            }
        }

        if (result == null) {
            return super.createServiceResult(result, true, "not found");
        }

        return super.createServiceResult(result, false, "updated successfully");
    }

    @Override
    public ServiceResult delete(String taskDefinitionId) {
        boolean isRemoved = false;
        TaskDefinition taskDefinitionToDelete = (TaskDefinition) this.getById(taskDefinitionId).getObject();
        if (taskDefinitionToDelete == null) {
            return super.createServiceResult(taskDefinitionToDelete, true, "does not exists");
        }

        JobDefinition jobDefinition = (JobDefinition) this.jobDefinitionService
                .getById(taskDefinitionToDelete.getJobDefinitionId())
                .getObject();
        List<TaskDefinition> taskDefinitions = jobDefinition.getTaskDefinitions();

        int lengthOfTaskDefinitions = taskDefinitions.size();
        if (lengthOfTaskDefinitions > 1) {
            for (int i = 0; i < lengthOfTaskDefinitions; i++) {
                TaskDefinition definition = taskDefinitions.get(i);
                if (definition.getId().equals(taskDefinitionToDelete.getId())) {
                    taskDefinitions.remove(definition);
                    isRemoved = true;
                    break;
                }
            }
        } else {
            return super.createServiceResult(taskDefinitionToDelete, true, "cannot delete the last task definition");
        }
{'fraction_non_alphanumeric': 0.08307903095223267, 'fraction_numerical': 0.00037366880488260573, 'mean_word_length': 3.120605594046703, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4743623', 'n_tokens_mistral': 3890, 'n_tokens_neox': 3714, 'n_words': 780}