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

import at.ac.tuwien.inso.entity.*;
import at.ac.tuwien.inso.exception.*;
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 java.util.*;

@Service
public class GradeServiceImpl implements GradeService {

    private GradeRepository gradeRepository;
    private StudentService studentService;
    private CourseService courseService;
    private LecturerService lecturerService;
    private UserAccountService userAccountService;

    @Autowired
    public GradeServiceImpl(GradeRepository gradeRepository,
                            StudentService studentService,
                            CourseService courseService,
                            LecturerService lecturerService,
                            UserAccountService userAccountService) {
        this.gradeRepository = gradeRepository;
        this.studentService = studentService;
        this.courseService = courseService;
        this.lecturerService = lecturerService;
        this.userAccountService = userAccountService;
    }

    @Override
    public Grade getDefaultGradeForStudentAndCourse(Long studentId, Long courseId) {
        Student student = studentService.findOne(studentId);
        Lecturer lecturer = lecturerService.getLoggedInLecturer();
        Course course = courseService.findOne(courseId);
        if (course == null || lecturer == null || student == null) {
            throw new BusinessObjectNotFoundException("Wrong student or course id");
        }
        if (!course.getStudents().contains(student)) {
            throw new ValidationException("Student not registered for course!");
        }
        return new Grade(course, lecturer, student, Mark.FAILED);
    }

    @Override
    public Grade saveNewGradeForStudentAndCourse(Grade grade) {
        if (!grade.getLecturer().equals(lecturerService.getLoggedInLecturer())) {
            throw new ValidationException("Lecturer is not valid!");
        }
        return gradeRepository.save(grade);
    }

    @Override
    public List<Grade> getGradesForCourseOfLoggedInLecturer(Long courseId) {
        Lecturer lecturer = lecturerService.getLoggedInLecturer();
        return gradeRepository.findByLecturerIdAndCourseId(lecturer.getId(), courseId);
    }

    @Override
    public List<Grade> getGradesForLoggedInStudent() {
        Long studentId =  userAccountService.getCurrentLoggedInUser().getId();
        return gradeRepository.findByStudentAccountId(studentId);
    }

    @Override
    public Grade getForValidation(String identifier) {
        Long gradeId = parseValidationIdentifier(identifier);
        return gradeRepository.findOne(gradeId);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Grade> findAllOfStudent(Student student) {
        return gradeRepository.findAllOfStudent(student);
    }

    private Long parseValidationIdentifier(String identifier) {
        return Long.valueOf(identifier);
    }
}

@@ -34,16 +34,84 @@
 .impl;%0A%0A
+import at.ac.tuwien.inso.controller.lecturer.GradeAuthorizationDTO;%0A
 import a
@@ -245,24 +245,70 @@
 o.service.*;
+%0A%0Aimport org.jboss.aerogear.security.otp.Totp;
 %0Aimport org.
@@ -1362,24 +1362,56 @@
 rade
- getDefaultGrade
+AuthorizationDTO getDefaultGradeAuthorizationDTO
 ForS
@@ -1973,16 +1973,42 @@
 urn new 
+GradeAuthorizationDTO(new 
 Grade(co
@@ -2044,16 +2044,17 @@
 .FAILED)
+)
 ;%0A    %7D%0A
@@ -2126,17 +2126,105 @@
 rade
- grade) %7B
+AuthorizationDTO gradeAuthorizationDTO) %7B%0A        Grade grade = gradeAuthorizationDTO.getGrade();
 %0A   
@@ -2373,32 +2373,316 @@
 d!%22);%0A        %7D%0A
+        String oneTimePassword = gradeAuthorizationDTO.getAuthCode();%0A        Totp authenticator = new Totp(grade.getLecturer().getTwoFactorSecret());%0A        if(!authenticator.verify(oneTimePassword)) %7B%0A            throw new ValidationException(%22Auth-code is not valid!%22);%0A        %7D%0A
         return g
@@ -3547,24 +3547,192 @@
 nt);%0A    %7D%0A%0A
+    @Override%0A    public List%3CMark%3E getMarkOptions() %7B%0A        return Arrays.asList(Mark.EXCELLENT, Mark.GOOD, Mark.SATISFACTORY, Mark.SUFFICIENT, Mark.FAILED);%0A    %7D%0A%0A
     private 

Add two factor authentification checks