com.epam.ipodromproject.service.CompetitionService.java Source code

Java tutorial

Introduction

Here is the source code for com.epam.ipodromproject.service.CompetitionService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.epam.ipodromproject.service;

import com.epam.ipodromproject.domain.Competition;
import com.epam.ipodromproject.domain.CompetitionState;
import com.epam.ipodromproject.domain.Horse;
import com.epam.ipodromproject.domain.HorseInfo;
import com.epam.ipodromproject.repository.CompetitionRepository;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Oleh
 */
@Service(value = "competitionService")
public class CompetitionService {

    private CompetitionRepository competitionRepository;

    @Autowired
    public CompetitionService(CompetitionRepository competitionRepository) {
        this.competitionRepository = competitionRepository;
    }

    @Transactional
    public List<Competition> getCompetitionsToMakeABetAfterDate(Date date, int page, int resultsPerPage) {
        return competitionRepository.getAllCompetitionsAfter(date, (page - 1) * resultsPerPage, resultsPerPage);
    }

    public Competition getCompetitionByID(Long competitionID) {
        return competitionRepository.getCompetitionById(competitionID);
    }

    @Transactional
    public List<Competition> getUnregisteredCompetitions(int page, int resultsPerPage) {
        return competitionRepository.getUnoperatedCompetitions((page - 1) * resultsPerPage, resultsPerPage);
    }

    @Transactional
    public boolean addCompetition(Date dateOfCompetition, Horse[] horsesToAdd, String[] horseCoefs) {
        if ((dateOfCompetition == null) || (horsesToAdd.length != horseCoefs.length)
                || (dateOfCompetition.compareTo(new Date()) <= 0)) {
            return false;
        }

        Map<Horse, HorseInfo> horses = new HashMap<>();
        for (int i = 0; i < horseCoefs.length; i++) {
            HorseInfo horseInfo = null;
            try {
                horseInfo = new HorseInfo(Double.valueOf(horseCoefs[i]));
            } catch (NumberFormatException e) {
            }
            if ((horseInfo != null) && (horsesToAdd[i] != null) && (horseInfo.getCoefficient() >= 1)) {
                if (horses.keySet().contains(horsesToAdd[i])) {
                    return false;
                }
                horses.put(horsesToAdd[i], horseInfo);
            }
        }
        if (horses.size() <= 1) {
            return false;
        } else {
            Competition competition = new Competition();
            competition.setState(CompetitionState.NOT_REGISTERED);
            competition.setDateOfStart(dateOfCompetition);
            competition.setHorses(horses);
            competitionRepository.save(competition);
            return true;
        }
    }

    @Transactional
    public void cancelCompetition(Long competitionID) {
        if (competitionID == null) {
            return;
        }
        Competition competition = getCompetitionByID(competitionID);
        if (CompetitionState.NOT_REGISTERED.equals(competition.getState())) {
            competition.setState(CompetitionState.CANCELLED);
            competitionRepository.update(competition);
        }
    }

    public boolean[] checkIfHorsesCanBeInCompetition(Horse[] horses) {
        boolean[] result = new boolean[horses.length];
        Set<Horse> horsesSet = new HashSet<>();
        for (int i = 0; i < horses.length; i++) {
            if ((horses[i] == null) || (horsesSet.contains(horses[i]))) {
                result[i] = false;
            } else {
                result[i] = true;
                horsesSet.add(horses[i]);
            }
        }
        return result;
    }

    public boolean[] checkIfCoefficientsCanBeInCompetition(String[] coefficients) {
        boolean[] result = new boolean[coefficients.length];
        double coefficient;
        for (int i = 0; i < coefficients.length; i++) {
            coefficient = 0;
            try {
                coefficient = Double.valueOf(coefficients[i]);
            } catch (NumberFormatException e) {
            }
            result[i] = coefficient > 1;
        }
        return result;
    }

    public boolean canRegisterResult(Long competitionID, String[] placesTaken) {
        if (placesTaken == null) {
            return false;
        }
        int[] places = returnPrimitives(placesTaken);
        return checkIfPlacesOk(places);
    }

    private int[] returnPrimitives(String[] placesTaken) {
        int[] places = new int[placesTaken.length];
        try {
            for (int i = 0; i < places.length; i++) {
                places[i] = Integer.parseInt(placesTaken[i]);
            }
        } catch (NumberFormatException e) {

        }
        return places;
    }

    private boolean checkIfPlacesOk(int[] places) {
        return (simpleCheck(places) && hardCheck(places));
    }

    private boolean simpleCheck(int[] places) {
        long rez = 0;
        for (int i = 0; i < places.length; i++) {
            if ((places[0] <= 0) || (places[i] > places.length)) {
                return false;
            }
            rez += places[i];
        }
        return (2 * rez == (long) places.length * (places.length + 1));
    }

    private boolean hardCheck(int[] places) {
        Arrays.sort(places);
        for (int i = 0; i < places.length; i++) {
            if (places[i] != (i + 1)) {
                return false;
            }
        }
        return true;
    }

}