<commit_before>package org.innovateuk.ifs.competitionsetup.core.service;

import org.apache.commons.collections4.map.LinkedMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.innovateuk.ifs.commons.error.Error;
import org.innovateuk.ifs.commons.service.ServiceResult;
import org.innovateuk.ifs.competition.resource.MilestoneResource;
import org.innovateuk.ifs.competition.resource.MilestoneType;
import org.innovateuk.ifs.competition.service.MilestoneRestService;
import org.innovateuk.ifs.competitionsetup.core.form.GenericMilestoneRowForm;
import org.innovateuk.ifs.competitionsetup.core.form.MilestoneTime;
import org.innovateuk.ifs.competitionsetup.milestone.form.MilestonesForm;
import org.innovateuk.ifs.util.TimeZoneUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.innovateuk.ifs.commons.service.ServiceResult.serviceSuccess;

@Service
public class CompetitionSetupMilestoneServiceImpl implements CompetitionSetupMilestoneService {
    private static final Log LOG = LogFactory.getLog(CompetitionSetupMilestoneServiceImpl.class);

    @Autowired
    private MilestoneRestService milestoneRestService;

    @Override
    public ServiceResult<List<MilestoneResource>> createMilestonesForIFSCompetition(Long competitionId) {
        List<MilestoneResource> newMilestones = new ArrayList<>();
        Stream.of(MilestoneType.presetValues()).filter(milestoneType -> !milestoneType.isOnlyNonIfs()).forEach(type ->
            newMilestones.add(milestoneRestService.create(type, competitionId).getSuccess())
        );
        return serviceSuccess(newMilestones);
    }

    @Override
    public ServiceResult<Void> updateMilestonesForCompetition(List<MilestoneResource> milestones, Map<String, GenericMilestoneRowForm> milestoneEntries, Long competitionId) {
        List<MilestoneResource> updatedMilestones = new ArrayList<>();

        milestones.forEach(milestoneResource -> {
            GenericMilestoneRowForm milestoneWithUpdate = milestoneEntries.getOrDefault(milestoneResource.getType().name(), null);

            if(milestoneWithUpdate != null) {
                ZonedDateTime temp = milestoneWithUpdate.getMilestoneAsZonedDateTime();
                if (temp != null) {
                    milestoneResource.setDate(temp);
                    updatedMilestones.add(milestoneResource);
                } else {
                    milestoneRestService
                            .resetMilestone(milestoneResource)
                            .getSuccess();
                }
            }
        });

        return milestoneRestService.updateMilestones(updatedMilestones).toServiceResult();
    }

    @Override
    public List<Error> validateMilestoneDates(Map<String, GenericMilestoneRowForm> milestonesFormEntries) {
        List<Error> errors =  new ArrayList<>();
        milestonesFormEntries.values().forEach(milestone -> {

            Integer day = milestone.getDay();
            Integer month = milestone.getMonth();
            Integer year = milestone.getYear();

            if(!validTimeOfMiddayMilestone(milestone) && errors.isEmpty()) {
                errors.add(new Error("error.milestone.invalid", HttpStatus.BAD_REQUEST));
            }

            if((day == null || month == null || year == null || !isMilestoneDateValid(day, month, year)) && errors.isEmpty()) {
                errors.add(new Error("error.milestone.invalid", HttpStatus.BAD_REQUEST));<commit_msg>addressing major violation introduced when avoiding another<commit_after>package org.innovateuk.ifs.competitionsetup.core.service;

import org.apache.commons.collections4.map.LinkedMap;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.innovateuk.ifs.commons.error.Error;
import org.innovateuk.ifs.commons.service.ServiceResult;
import org.innovateuk.ifs.competition.resource.MilestoneResource;
import org.innovateuk.ifs.competition.resource.MilestoneType;
import org.innovateuk.ifs.competition.service.MilestoneRestService;
import org.innovateuk.ifs.competitionsetup.core.form.GenericMilestoneRowForm;
import org.innovateuk.ifs.competitionsetup.core.form.MilestoneTime;
import org.innovateuk.ifs.competitionsetup.milestone.form.MilestonesForm;
import org.innovateuk.ifs.util.TimeZoneUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.time.DateTimeException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.innovateuk.ifs.commons.service.ServiceResult.serviceSuccess;

@Service
public class CompetitionSetupMilestoneServiceImpl implements CompetitionSetupMilestoneService {
    private static final Log LOG = LogFactory.getLog(CompetitionSetupMilestoneServiceImpl.class);

    @Autowired
    private MilestoneRestService milestoneRestService;

    @Override
    public ServiceResult<List<MilestoneResource>> createMilestonesForIFSCompetition(Long competitionId) {
        List<MilestoneResource> newMilestones = new ArrayList<>();
        Stream.of(MilestoneType.presetValues()).filter(milestoneType -> !milestoneType.isOnlyNonIfs()).forEach(type ->
            newMilestones.add(milestoneRestService.create(type, competitionId).getSuccess())
        );
        return serviceSuccess(newMilestones);
    }

    @Override
    public ServiceResult<Void> updateMilestonesForCompetition(List<MilestoneResource> milestones, Map<String, GenericMilestoneRowForm> milestoneEntries, Long competitionId) {
        List<MilestoneResource> updatedMilestones = new ArrayList<>();

        milestones.forEach(milestoneResource -> {
            GenericMilestoneRowForm milestoneWithUpdate = milestoneEntries.getOrDefault(milestoneResource.getType().name(), null);

            if(milestoneWithUpdate != null) {
                ZonedDateTime temp = milestoneWithUpdate.getMilestoneAsZonedDateTime();
                if (temp != null) {
                    milestoneResource.setDate(temp);
                    updatedMilestones.add(milestoneResource);
                } else {
                    milestoneRestService
                            .resetMilestone(milestoneResource)
                            .getSuccess();
                }
            }
        });

        return milestoneRestService.updateMilestones(updatedMilestones).toServiceResult();
    }

    @Override
    public List<Error> validateMilestoneDates(Map<String, GenericMilestoneRowForm> milestonesFormEntries) {
        List<Error> errors =  new ArrayList<>();
        milestonesFormEntries.values().forEach(milestone -> {

            Integer day = milestone.getDay();
            Integer month = milestone.getMonth();
            Integer year = milestone.getYear();

            if(!validTimeOfMiddayMilestone(milestone) && errors.isEmpty()) {
                errors.add(new Error("error.milestone.invalid", HttpStatus.BAD_REQUEST));
            }

            boolean dateFieldsIncludeNull = (day == null || month == null || year == null);
            if((dateFieldsIncludeNull || !isMilestoneDateValid(day, month, year)) && errors.isEmpty()) {
                errors.add(new Error("error.milestone.invalid", HttpStatus.BAD_REQUEST));
{'fraction_non_alphanumeric': 0.09715242881072027, 'fraction_numerical': 0.00025769875016106174, 'mean_word_length': 3.741600488698839, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 37, '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': '5666310', 'n_tokens_mistral': 2096, 'n_tokens_neox': 2003, 'n_words': 318}