package org.synyx.urlaubsverwaltung.overview.calendar;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
import org.synyx.urlaubsverwaltung.api.RestControllerAdviceMarker;
import org.synyx.urlaubsverwaltung.application.domain.Application;
import org.synyx.urlaubsverwaltung.application.service.ApplicationService;
import org.synyx.urlaubsverwaltung.department.DepartmentService;
import org.synyx.urlaubsverwaltung.person.Person;
import org.synyx.urlaubsverwaltung.person.PersonService;
import org.synyx.urlaubsverwaltung.security.SecurityRules;

import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static java.time.format.DateTimeFormatter.ofPattern;
import static java.util.stream.Collectors.toList;
import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.synyx.urlaubsverwaltung.api.RestApiDateFormat.DATE_PATTERN;
import static org.synyx.urlaubsverwaltung.api.SwaggerConfig.EXAMPLE_FIRST_DAY_OF_YEAR;
import static org.synyx.urlaubsverwaltung.api.SwaggerConfig.EXAMPLE_LAST_DAY_OF_YEAR;
import static org.synyx.urlaubsverwaltung.application.domain.ApplicationStatus.ALLOWED;

@RestControllerAdviceMarker
@Api("Vacations: Get all vacations for a certain period")
@RestController("restApiVacationController")
@RequestMapping("/api")
public class VacationApiController {

    private final PersonService personService;
    private final ApplicationService applicationService;
    private final DepartmentService departmentService;

    @Autowired
    VacationApiController(PersonService personService, ApplicationService applicationService, DepartmentService departmentService) {
        this.personService = personService;
        this.applicationService = applicationService;
        this.departmentService = departmentService;
    }

    @ApiOperation(
        value = "Get all allowed vacations for a certain period",
        notes = "Get all allowed vacations for a certain period. "
            + "If a person is specified, only the allowed vacations of the person are fetched. "
            + "If a person and the department members flag is specified, "
            + "then all the waiting and allowed vacations of the departments the person is assigned to, are fetched. "
            + "Information only reachable for users with role office."
    )
    @GetMapping("/vacations")
    @PreAuthorize(SecurityRules.IS_OFFICE + " or @userApiMethodSecurity.isSamePersonId(authentication, #personId)")
    public List<VacationResponse> vacations(
        @ApiParam(value = "Get vacations for department members of person")
        @RequestParam(value = "departmentMembers", required = false)
            Boolean departmentMembers,
        @ApiParam(value = "Start date with pattern yyyy-MM-dd", defaultValue = EXAMPLE_FIRST_DAY_OF_YEAR)
        @RequestParam(value = "from")
            String from,
        @ApiParam(value = "End date with pattern yyyy-MM-dd", defaultValue = EXAMPLE_LAST_DAY_OF_YEAR)
        @RequestParam(value = "to")
            String to,
        @ApiParam(value = "ID of the person")
        @RequestParam(value = "person", required = false)
            Integer personId) {

        final LocalDate startDate = parseDate(from);
        final LocalDate endDate = parseDate(to);
        if (startDate.isAfter(endDate)) {
            throw new ResponseStatusException(BAD_REQUEST, "Parameter 'from' must be before or equals to 'to' parameter");
        }

        List<Application> applications = new ArrayList<>();
        if (personId == null && departmentMembers == null) {
            applications = applicationService.getApplicationsForACertainPeriodAndState(startDate, endDate, ALLOWED);
        }

        if (personId != null) {
            final Optional<Person> person = personService.getPersonByID(personId);

            if (person.isPresent()) {
                if (departmentMembers == null || !departmentMembers) {
                    applications = applicationService.getApplicationsForACertainPeriodAndPersonAndState(startDate, endDate, person.get(), ALLOWED);
                } else {
                    applications = departmentService.getApplicationsForLeaveOfMembersInDepartmentsOfPerson(person.get(), startDate, endDate);
                }
            }
        }

        return applications.stream().map(VacationResponse::new).collect(toList());
    }

    private LocalDate parseDate(String date) {
        final LocalDate localDate;
        try {
            localDate = LocalDate.parse(date, ofPattern(DATE_PATTERN));
        } catch (DateTimeParseException exception) {
            throw new ResponseStatusException(BAD_REQUEST, "The value '" + date + "' has the wrong format");
        }
        return localDate;
    }
}

@@ -3240,17 +3240,17 @@
         
-B
+b
 oolean d
@@ -3395,24 +3395,16 @@
 stParam(
-value = 
 %22from%22)%0A
@@ -3553,24 +3553,16 @@
 stParam(
-value = 
 %22to%22)%0A  
@@ -4089,16 +4089,17 @@
 null && 
+!
 departme
@@ -4107,24 +4107,16 @@
 tMembers
- == null
 ) %7B%0A    
@@ -4389,24 +4389,24 @@
 resent()) %7B%0A
+
             
@@ -4417,37 +4417,8 @@
 if (
-departmentMembers == null %7C%7C 
 !dep

Change departmentMembers to primitive type