org.opentestsystem.delivery.testadmin.rest.ScheduleController.java Source code

Java tutorial

Introduction

Here is the source code for org.opentestsystem.delivery.testadmin.rest.ScheduleController.java

Source

/*******************************************************************************
 * Educational Online Test Delivery System
 * Copyright (c) 2013 American Institutes for Research
 * 
 * Distributed under the AIR Open Source License, Version 1.0
 * See accompanying file AIR-License-1_0.txt or at
 * http://www.smarterapp.org/documents/American_Institutes_for_Research_Open_Source_Software_License.pdf
 ******************************************************************************/
package org.opentestsystem.delivery.testadmin.rest;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.opentestsystem.delivery.testadmin.domain.TestAdminBase;
import org.opentestsystem.delivery.testadmin.domain.schedule.Schedule;
import org.opentestsystem.delivery.testadmin.domain.schedule.ScheduledDay;
import org.opentestsystem.delivery.testadmin.domain.search.ScheduleSearchRequest;
import org.opentestsystem.delivery.testadmin.persistence.validator.ScheduleValidator;
import org.opentestsystem.delivery.testadmin.service.ScheduleService;
import org.opentestsystem.shared.exception.LocalizedException;
import org.opentestsystem.shared.exception.RestException;
import org.opentestsystem.shared.search.domain.SearchResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@Controller
public class ScheduleController extends TestAdminBaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ScheduleController.class);

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private ScheduleValidator scheduleValidator;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        if (binder.getTarget() != null && TestAdminBase.class.isAssignableFrom(binder.getTarget().getClass())) {
            binder.setValidator(this.scheduleValidator);
        }
    }

    /**
     * Gets schedule by scheduleId.
     * @param schedule id.
     * @return Schedule object.
     */
    @RequestMapping(value = "/schedule/{scheduleId}", method = RequestMethod.GET, produces = {
            MediaType.APPLICATION_JSON_VALUE })
    @Secured({ "ROLE_Schedule Read" })
    @ResponseBody
    public Schedule findScheduleById(@PathVariable final String scheduleId) {
        LOGGER.debug("Finding schedule for Id: " + scheduleId);
        return scheduleService.getSchedule(scheduleId);
    }

    /**
     * Creates Schedule.
     * @param schedule to be saved.
     * @param response HttpServletResponse.
     * @return Schedule newly created schedule object.
     */
    @ResponseStatus(HttpStatus.CREATED)
    @RequestMapping(value = "/schedule", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = {
            MediaType.APPLICATION_JSON_VALUE })
    @Secured({ "ROLE_Schedule Modify" })
    @ResponseBody
    public Schedule saveSchedule(@RequestBody @Valid final Schedule schedule, final HttpServletResponse response) {
        LOGGER.debug("Saving Schedule");
        Schedule savedSchedule = scheduleService.saveSchedule(schedule);
        response.setHeader("Location", savedSchedule.getUrl());
        return savedSchedule;
    }

    /**
     * Updates Schedule.
     * @param schedule to be saved.
     * @param response HttpServletResponse.
     * @return Schedule updated schedule object.
     */
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/schedule/{scheduleId}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE, produces = {
            MediaType.APPLICATION_JSON_VALUE })
    @Secured({ "ROLE_Schedule Modify" })
    @ResponseBody
    public Schedule updateSchedule(@PathVariable final String scheduleId,
            @RequestBody @Valid final Schedule schedule, final HttpServletResponse response) {
        if (schedule == null || StringUtils.isEmpty(schedule.getId()) || !scheduleId.equals(schedule.getId())) {
            throw new LocalizedException("schedule.invalid.id");
        } else {
            Schedule savedSchedule = scheduleService.saveSchedule(schedule);
            response.setHeader("Location", savedSchedule.getUrl());
            return savedSchedule;
        }
    }

    /**
     * Removes Schedule.
     * @param scheduleId schedule Id.
     */
    @ResponseStatus(HttpStatus.NO_CONTENT)
    @RequestMapping(value = "/schedule/{scheduleId}", method = RequestMethod.DELETE)
    @Secured({ "ROLE_Schedule Modify" })
    public void removeScheduleById(@PathVariable final String scheduleId) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("Removing schedule with Id:" + scheduleId);
        }
        scheduleService.removeSchedule(scheduleId);
    }

    /**
     * Can query the /schedule endpoint with query parameters in a querystring.
     * or with JSON Valid parameters are: state, district
     * @param request HttpServletRequest
     * @param response HttpServletResponse
     * @return SearchResponse<Schedule>
     */
    @RequestMapping(value = "/schedule", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @Secured({ "ROLE_Schedule Read" })
    @ResponseBody
    public SearchResponse<Schedule> searchSchedule(final HttpServletRequest request,
            final HttpServletResponse response) {

        ScheduleSearchRequest searchRequest = new ScheduleSearchRequest(request.getParameterMap());
        SearchResponse<Schedule> searchResponse;
        if (searchRequest.isValid()) {
            searchResponse = scheduleService.searchSchedules(searchRequest);
        } else {
            throw new RestException("schedule.search.invalidSearchCriteria");
        }
        return searchResponse;
    }

    @RequestMapping(value = "/schedule/{institutionId}/{facilityName}/{scheduleDay}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    @Secured({ "ROLE_Schedule Read" })
    @ResponseBody
    public Schedule getScheduleResults(@PathVariable(value = "institutionId") String institutionId,
            @PathVariable(value = "facilityName") String facilityName,
            @PathVariable(value = "scheduleDay") @DateTimeFormat(pattern = "yyyy-MM-dd") Date scheduleDay) {
        return scheduleService.findSchedule(institutionId, facilityName, new DateTime(scheduleDay));
    }

    /**
     * Patches Schedule with new ScheduleDay object containing updated TimeSlot.
     * 
     * @param schedule
     *            to be saved.
     * @param response
     *            HttpServletResponse.
     * @return Schedule updated schedule object.
     */
    @ResponseStatus(HttpStatus.OK)
    @RequestMapping(value = "/schedule/{scheduleId}", method = RequestMethod.PATCH, consumes = MediaType.APPLICATION_JSON_VALUE, produces = {
            MediaType.APPLICATION_JSON_VALUE })
    @Secured({ "ROLE_Schedule Modify" })
    @ResponseBody
    public Schedule patchSchedule(@PathVariable final String scheduleId,
            @RequestBody @Valid final ScheduledDay scheduleDay, final HttpServletResponse response) {
        return scheduleService.updateScheduleDay(scheduleId, scheduleDay);
    }
}