package io.teiler.server.services.util;

import io.teiler.server.dto.Debt;
import io.teiler.server.dto.Person;
import io.teiler.server.persistence.entities.PersonEntity;
import io.teiler.server.persistence.repositories.PersonRepository;
import io.teiler.server.util.exceptions.PeopleNameConflictException;
import io.teiler.server.util.exceptions.PersonHasUnsettledDebtsException;
import io.teiler.server.util.exceptions.PersonInactiveException;
import io.teiler.server.util.exceptions.PersonNotFoundException;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PersonUtil {

    @Autowired
    private PersonRepository personRepository;

    public PersonUtil() { /* intentionally empty */ }

    /**
     * Checks whether the name of a Person is unique within a Group.
     *
     * @param groupId Id of the Group
     * @param name Name of the Person
     * @throws PeopleNameConflictException Name already exists in Group
     */
    public void checkNamesAreUnique(String groupId, String name) throws PeopleNameConflictException {
        PersonEntity foundPerson = personRepository.getByName(groupId, name);

        // If the person was found AND is active, throw an exception
        // NEVER change the order around on this statement, otherwise you may get null pointers
        if (foundPerson != null && foundPerson.getActive()) {
            throw new PeopleNameConflictException();
        }
    }

    /**
     * Checks whether a Person exists within a Group.
     *
     * @param groupId Id of the Group
     * @param personId Id of the Person
     * @throws PersonNotFoundException Person does not exists within Group
     */
    public void checkPersonBelongsToThisGroup(String groupId, int personId) throws PersonNotFoundException {
        if (personRepository.getByGroupAndPersonId(groupId, personId) == null) {
            throw new PersonNotFoundException();
        }
    }

    /**
     * Checks whether a Person exists.
     *
     * @param personId Id of the Person
     * @throws PersonNotFoundException Person does not exist
     */
    public void checkPersonExists(int personId) throws PersonNotFoundException {
        if (personRepository.getById(personId) == null) {
            throw new PersonNotFoundException();
        }
    }

    public java.util.List<Person> filterInactivePeople(java.util.List<Person> people) {
        return people.stream().filter(Person::isActive).collect(Collectors.toList());
    }

    /**
     * Check if this person is active.
     *
     * @param personId The person to check
     * @throws PersonInactiveException The person is inactive
     */
    public void checkPersonIsActive(int personId) throws PersonInactiveException {
        if (!personRepository.getById(personId).getActive()) {
            throw new PersonInactiveException();
        }
    }

    public void checkPersonHasNoDebts(int personId, List<Debt> debts) {
        for (Debt debt : debts) {
            if (debt.getPerson().getId() == personId && debt.getBalance() != 0) {
                throw new PersonHasUnsettledDebtsException();
            }
        }
    }
}

@@ -507,67 +507,8 @@
 on;%0A
-import java.util.List;%0Aimport java.util.stream.Collectors;%0A
 impo
@@ -614,16 +614,76 @@
 rvice;%0A%0A
+import java.util.List;%0Aimport java.util.stream.Collectors;%0A%0A
 @Service
@@ -969,16 +969,19 @@
 am name 
+   
 Name of 
@@ -1632,32 +1632,33 @@
  @param groupId 
+ 
 Id of the Group%0A
@@ -3048,54 +3048,42 @@
     
-for (Debt debt : debts) %7B%0A            if (debt
+if (debts.stream().anyMatch(d -%3E d
 .get
@@ -3115,19 +3115,16 @@
 nId && d
-ebt
 .getBala
@@ -3134,23 +3134,20 @@
 () != 0)
+)
  %7B%0A
-    
         
@@ -3200,22 +3200,8 @@
 ();%0A
-            %7D%0A
     

add cyrill formatter