tv.arte.resteventapi.web.controllers.EventsController.java Source code

Java tutorial

Introduction

Here is the source code for tv.arte.resteventapi.web.controllers.EventsController.java

Source

package tv.arte.resteventapi.web.controllers;

/*
 * #%L
 * RestEventAPI
 * %%
 * Copyright (C) 2014 ARTE G.E.I.E
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of The MIT License (MIT) as published by the Open Source 
 * Initiative.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See The 
 * MIT License (MIT) for more details.
 * 
 * You should have received a copy of The MIT License (MIT) 
 * along with this program.  If not, see <http://opensource.org/licenses/MIT>
 * #L%
 */

import java.util.Arrays;
import java.util.List;

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

import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import tv.arte.resteventapi.core.domain.entities.RestEvent;
import tv.arte.resteventapi.core.domain.repositories.RestEventRepository;
import tv.arte.resteventapi.core.exceptions.NoResultInApiException;
import tv.arte.resteventapi.core.http.RestEventApiMediaType;
import tv.arte.resteventapi.core.presentation.decoration.Hrefed;
import tv.arte.resteventapi.core.presentation.decoration.HrefedLocationHeader;
import tv.arte.resteventapi.core.services.EventService;
import tv.arte.resteventapi.core.transferobjects.BaseRestEventRequestParam;
import tv.arte.resteventapi.core.transferobjects.EventResourceResponse;
import tv.arte.resteventapi.core.transferobjects.RestEventPostRequestParam;
import tv.arte.resteventapi.core.transferobjects.RestEventSearchRequestParam;
import tv.arte.resteventapi.core.validation.RestEventApiValidationException;

/**
 * The event controller
 * 
 * @author Simeon Petev
 * @since 0.1
 */
@RestController
@RequestMapping("/events")
public class EventsController {

    @SuppressWarnings("unused")
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private EventService eventService;

    @Autowired
    private RestEventRepository restEventRepository;

    /**
     * Retrieves an event form the API
     * 
     * @param baseEventRequestParam A set of params
     * @return
     */
    @RequestMapping(value = { "/{id}" }, produces = {
            RestEventApiMediaType.APPLICATION_VND_API_JSON_VALUE }, method = { RequestMethod.GET })
    @Hrefed(value = RestEvent.class)
    @HrefedLocationHeader
    public EventResourceResponse retrieveEvent(BaseRestEventRequestParam baseEventRequestParam,
            HttpServletResponse response) {

        RestEvent restEvent = eventService.retrieveRestEventById(baseEventRequestParam.getId());

        if (restEvent == null) {
            throw new NoResultInApiException(Arrays.asList(baseEventRequestParam));
        }

        return new EventResourceResponse(restEvent);
    }

    /**
     * Retrieves events form the API
     * 
     * @param baseEventRequestParam A set of params
     * @return
     */
    @RequestMapping(produces = { RestEventApiMediaType.APPLICATION_VND_API_JSON_VALUE }, method = {
            RequestMethod.GET })
    @HrefedLocationHeader
    public EventResourceResponse retrieveEvents(@Valid RestEventSearchRequestParam restEventSearchRequestParams) {

        List<RestEvent> restEvents = eventService.retrieveRestEventForSearchParams(restEventSearchRequestParams);

        if (CollectionUtils.isEmpty(restEvents)) {
            throw new NoResultInApiException(Arrays.asList(restEventSearchRequestParams));
        }

        return new EventResourceResponse(restEvents);
    }

    /**
     * Create an event and returns it (additional infos included)
     * 
     * @param baseEventRequestParam A set of params
     * @return The created evet
     * @throws RestEventApiValidationException 
     */
    @RequestMapping(produces = { RestEventApiMediaType.APPLICATION_VND_API_JSON_VALUE }, consumes = {
            RestEventApiMediaType.APPLICATION_VND_API_JSON_VALUE,
            MediaType.APPLICATION_JSON_VALUE }, method = { RequestMethod.POST })
    @ResponseStatus(HttpStatus.CREATED)
    @HrefedLocationHeader
    public EventResourceResponse createEvent(
            @RequestBody @Valid RestEventPostRequestParam restEventPostRequestParam, HttpServletResponse response)
            throws RestEventApiValidationException {

        RestEvent restEvent = eventService.createRestEvent(restEventPostRequestParam);

        return new EventResourceResponse(restEvent);
    }

    /**
     * Deletes an event
     * 
     * @param baseEventRequestParam A set of params
     * @return The created evet
     * @throws RestEventApiValidationException 
     */
    @RequestMapping(value = { "/{id}" }, produces = {
            RestEventApiMediaType.APPLICATION_VND_API_JSON_VALUE }, method = { RequestMethod.DELETE })
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public void deleteEvent(BaseRestEventRequestParam baseEventRequestParam)
            throws RestEventApiValidationException {
        eventService.deleteRestEventForId(baseEventRequestParam.getId());
    }
}