package com.revature.services;

import java.util.List;

import javax.persistence.EntityNotFoundException;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import com.revature.domain.Person;
import com.revature.domain.PersonRole;
import com.revature.repositories.PersonRepository;
import com.revature.repositories.PersonRoleRepository;


@Service
@Transactional(readOnly=false, isolation=Isolation.READ_COMMITTED)
public class PersonLogicImpl implements PersonLogic {

	@Autowired
	private PersonRepository dao;
	
	@Autowired
	private PersonRoleRepository daoRole;
	
	@Override
	public Page<Person> getPersonByFirstName(Pageable pageable, String firstName) {
		
		return dao.findAllByFirstNameIgnoreCase(pageable, firstName);

	}

	@Override
	public Page<Person> getPersonByLastName(Pageable pageable, String lastName) {
		
		return dao.findAllByLastNameIgnoreCase(pageable, lastName);
	}

	@Override
	public Page<Person> getAllTrainees(Pageable pageable) {
		PersonRole personRole = new PersonRole(1, "Trainee");
		return dao.findAllByPersonRole(pageable, personRole);

	}

	@Override
	public Page<Person> getAllTrainers(Pageable pageable) {
		PersonRole personRole = new PersonRole(2, "Trainer");
		return dao.findAllByPersonRole(pageable, personRole);

	}

	@Override
	public Person getPersonById(int id)  {
		
		Person p = null;
		
		try {
			
			p = dao.findOne(id);
			
		} catch (EntityNotFoundException e) {
			
			return p;
		}
		
		return p;
	}

	@Override
	public Page<Person> getAllPersons(Pageable pageable) {
		return dao.findAll(pageable);
	}

	@Override
	public Person updatePerson(Person p) {
		dao.save(p);
		return p;
	}
	


	@Override
	public void deletePerson(Person p) {
		dao.delete(p.getId());
	}

	@Override
	public Page<Person> getByFirstNameAndLastName(Pageable pageable, String firstName, String lastName) {
		return dao.findAllByFirstNameAndLastNameIgnoreCase(pageable, firstName, lastName);
	}

	@Override
	public Person createPerson(Person person) {
		
		List<PersonRole> roleList = daoRole.findAll();
		System.out.println(roleList);

		if (person.getFirstName() == null) {
			throw new ConstraintViolationException("Missing required field firstName (String)", null);
		}
		
		if (person.getLastName() == null) {
			throw new ConstraintViolationException("Missing required field lastName (String)", null);
		}
		
		if (person.getPersonRole() == null) {
			throw new ConstraintViolationException("Missing required field personRole (PersonRole)", null);
		}
			
		
				
		dao.save(person);
		return person;
	}

	@Override
	public Page<Person> getAllPersonsByPersonRole(Pageable pageable, PersonRole personRole) {
		return dao.findAllByPersonRole(pageable, personRole);
	}

}

@@ -25,16 +25,71 @@
 vices;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.Iterator;%0A
 import j
@@ -2368,16 +2368,61 @@
 n) %7B%0A%09%09%0A
+%09%09List%3CInteger%3E roleIds = new ArrayList%3C%3E();%0A
 %09%09List%3CP
@@ -2494,16 +2494,220 @@
 eList);%0A
+%09%09%09%09%0A%09%09Iterator%3CPersonRole%3E iterator = roleList.iterator();%0A%09%09%0A%09%09while(iterator.hasNext()) %7B%0A%09%09%09PersonRole role = iterator.next();%0A%09%09%09roleIds.add(role.getId());%0A%09%09%7D%0A%09%09%0A%09%09System.out.println(roleIds);%0A%09%09%0A%09%09
 %0A%09%09if (p
@@ -2983,46 +2983,191 @@
 %09%0A%09%09
-if (person.getPersonRole() == null
+boolean isValid = false;%0A%09%09%0A%0A%09%09for (Integer roll : roleIds) %7B%0A%09%09%09%09%0A%09%09%09if (person.getPersonRole().getId() == roll) %7B%0A%09%09%09%09%09%0A%09%09%09%09isValid = true;%0A%09%09%09%7D %0A%09%09%09%09%0A%09%09%7D%09%0A%09%09%0A%09%09if (!isValid
 ) %7B%0A
+%09%09%09%0A
 %09%09%09t
@@ -3201,39 +3201,30 @@
 nException(%22
-Missing require
+Invali
 d field pers
@@ -3255,21 +3255,16 @@
 ll);%0A%09%09%7D
-%0A%09%09%09%0A
 %09%09%0A%09%09%09%09%0A

add fix for personRole exception