package at.ac.tuwien.inso.service.impl;

import at.ac.tuwien.inso.controller.lecturer.forms.*;
import at.ac.tuwien.inso.entity.*;
import at.ac.tuwien.inso.repository.*;
import at.ac.tuwien.inso.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.stereotype.*;
import org.springframework.transaction.annotation.*;

import javax.validation.constraints.*;
import java.util.*;
import java.util.stream.*;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private SemesterService semesterService;

    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private SubjectRepository subjectRepository;

    @Autowired
    private StudentRepository studentRepository;

    @Autowired
    private UserAccountService userAccountService;

    @Override
    @Transactional(readOnly = true)
    public List<Course> findCourseForCurrentSemesterWithName(@NotNull String name) {
        Semester semester = semesterService.getCurrentSemester();
        return courseRepository.findAllBySemesterAndSubjectNameLikeIgnoreCase(semester, "%" + name + "%");
    }

    @Override
    @Transactional(readOnly = true)
    public List<Course> findCoursesForCurrentSemesterForLecturer(Lecturer lecturer) {
        Semester semester = semesterService.getCurrentSemester();
        Iterable<Subject> subjectsForLecturer = subjectRepository.findByLecturers_Id(lecturer.getId());
        List<Course> courses = new ArrayList<>();
        subjectsForLecturer.forEach(subject -> courses.addAll(courseRepository.findAllBySemesterAndSubject(semester, subject)));
        return courses;
    }

    @Override
    @Transactional
    public Course saveCourse(AddCourseForm form) {
        Course course = form.getCourse();
        List<Tag> tags = form.getActiveAndInactiveTags().stream()
                .filter(tagBooleanEntry -> tagBooleanEntry.isActive())
                .map(tagBooleanEntry -> tagBooleanEntry.getTag())
                .collect(Collectors.toList());
        tags.forEach(tag -> course.addTags(tag));
        if (!(course.getStudentLimits() > 0)) {
            course.setStudentLimits(1);
        }
        return courseRepository.save(course);
    }

    @Override
    @Transactional(readOnly = true)
    public Course findOne(Long id) {
        return courseRepository.findOne(id);
    }


    @Override
    @Transactional
    public boolean registerStudentForCourse(Course course) {
        Student student = studentRepository.findByUsername(userAccountService.getCurrentLoggedInUser().getUsername());
        if (course.getStudentLimits() <= course.getStudents().size()) {
            return false;
        } else if (course.getStudents().contains(student)) {
            return true;
        } else {
            course.addStudents(student);
            courseRepository.save(course);
            return true;
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<Course> findAllForStudent(Student student) {
        return courseRepository.findAllForStudent(student);
    }

    @Override
    @Transactional
    public void unregisterStudentFromCourse(Student student, Long courseId) {
        Course course = courseRepository.findOne(courseId);
        if (course == null) return;

        course.removeStudents(student);
    }

}

@@ -194,24 +194,44 @@
 .service.*;%0A
+import org.slf4j.*;%0A
 import org.s
@@ -523,16 +523,105 @@
 vice %7B%0A%0A
+    private static final Logger log = LoggerFactory.getLogger(CourseServiceImpl.class);%0A%0A
     @Aut
@@ -3322,95 +3322,310 @@
     
-Course course = courseRepository.findOne(courseId);%0A        if (course == null) return;
+log.info(%22Unregistering student with id %7B%7D from course with id %7B%7D%22, student.getId(), courseId);%0A%0A        Course course = courseRepository.findOne(courseId);%0A        if (course == null) %7B%0A            log.warn(%22Course with id %7B%7D not found. Nothing to unregister%22, courseId);%0A            return;%0A        %7D
 %0A%0A  

add logging for unregister action