package at.ac.tuwien.inso.controller.admin;

import at.ac.tuwien.inso.controller.*;
import at.ac.tuwien.inso.controller.admin.forms.*;
import at.ac.tuwien.inso.entity.*;
import at.ac.tuwien.inso.service.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.data.domain.*;
import org.springframework.data.web.*;
import org.springframework.stereotype.*;
import org.springframework.ui.*;
import org.springframework.validation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.*;

import java.io.UnsupportedEncodingException;

import javax.validation.*;

@Controller
@RequestMapping("/admin/users")
public class AdminUsersController {

    @Autowired
    private UisUserService uisUserService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private LecturerService lecturerService;

    @Autowired
    private UserCreationService userCreationService;

    @GetMapping
    public String listUsers(
            @RequestParam(value = "search", defaultValue = "") String searchFilter,
            @PageableDefault Pageable pageable,
            Model model
    ) {
        if (pageable.getPageSize() > Constants.MAX_PAGE_SIZE) {
            pageable = new PageRequest(pageable.getPageNumber(), Constants.MAX_PAGE_SIZE);
        }
        model.addAttribute("page", uisUserService.findAllMatching(searchFilter, pageable));

        return "admin/users";
    }

    @GetMapping("/{userId}")
    public String userDetails(@PathVariable Long userId, Model model) throws UnsupportedEncodingException {
        UisUser user = uisUserService.findOne(userId);
        model.addAttribute("user", user);

        if (user instanceof Student) {
            Iterable<StudyPlanRegistration> studyplans = studentService.findStudyPlanRegistrationsFor((Student) user);
            model.addAttribute("studyplans", studyplans);

            return "admin/student-details";
        }

        Iterable<Subject> subjects = lecturerService.findSubjectsFor((Lecturer) user);
        model.addAttribute("subjects", subjects);
        String qr = lecturerService.generateQRUrl((Lecturer) user);
        model.addAttribute("qr", qr);

        return "admin/lecturer-details";
    }

    @PostMapping("/create")
    public String createUser(@Valid CreateUserForm form,
                             BindingResult bindingResult,
                             RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            redirectAttributes.addFlashAttribute("flashMessage", "admin.users.create.error." + bindingResult.getFieldError().getField());

            return "redirect:/admin/users";
        }

        userCreationService.create(form.toUisUser());

        redirectAttributes.addFlashAttribute("flashMessage", "admin.users.create.success");

        return "redirect:/admin/users";
    }
}

@@ -599,16 +599,39 @@
 ception;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -652,16 +652,85 @@
 ion.*;%0A%0A
+import static at.ac.tuwien.inso.controller.Constants.MAX_PAGE_SIZE;%0A%0A
 @Control
@@ -1143,175 +1143,977 @@
 h%22, 
-defaultValue = %22%22) String searchFilter,%0A            @PageableDefault Pageable pageable,%0A            Model model%0A    ) %7B%0A        if (pageable.getPageSize() %3E Constants.
+required = false) String search,%0A            Model model%0A    ) %7B%0A        if (search == %22%22) %7B%0A            return %22redirect:/admin/users%22;%0A        %7D%0A%0A        return listUsersInternal(search, 1, model);%0A    %7D%0A%0A    @GetMapping(%22/page/%7BpageNumber%7D%22)%0A    public String listUsersForPage(%0A            @RequestParam(value = %22search%22, required = false) String search,%0A            @PathVariable Integer pageNumber,%0A            Model model%0A    ) %7B%0A        if (search == %22%22) %7B%0A            return %22redirect:/admin/users/page/%22 + pageNumber;%0A        %7D%0A%0A        if (pageNumber == 1) %7B%0A            return %22redirect:/admin/users?search=%22 + search;%0A        %7D%0A%0A        return listUsersInternal(search, pageNumber, model);%0A    %7D%0A%0A    private String listUsersInternal(String search, int pageNumber, Model model) %7B%0A        if (search == null) %7B%0A            search = %22%22;%0A        %7D%0A%0A        // Page numbers in the URL start with 1%0A        PageRequest pageable = new PageRequest(pageNumber - 1, 
 MAX_
@@ -2122,18 +2122,18 @@
 GE_SIZE)
- %7B
+;%0A
 %0A       
@@ -2137,89 +2137,409 @@
     
-    pageable = new PageRequest(pageable.getPageNumber(), Constants.MAX_PAGE_SIZE)
+Page%3CUisUser%3E usersPage = uisUserService.findAllMatching(search, pageable);%0A        List%3CUisUser%3E users = usersPage.getContent();%0A%0A        // If the user tries to access a page that doesn't exist%0A        if (users.size() == 0 && usersPage.getTotalElements() != 0) %7B%0A            int lastPage = usersPage.getTotalPages();%0A            return %22redirect:/admin/users/page/%22 + lastPage + %22?search=%22 + search
 ;%0A  
@@ -2546,16 +2546,17 @@
       %7D%0A
+%0A
         
@@ -2587,61 +2587,62 @@
 %22, u
-isUserService.findAllMatching(searchFilter, pageable)
+sersPage);%0A        model.addAttribute(%22search%22, search
 );%0A%0A

Fix user pagination in Controller