All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.escalon.hypermedia.sample.event.EventController Maven / Gradle / Ivy

There is a newer version: 0.4.2
Show newest version
package de.escalon.hypermedia.sample.event;

import de.escalon.hypermedia.sample.beans.event.Event;
import de.escalon.hypermedia.sample.beans.event.Rating;
import de.escalon.hypermedia.sample.beans.event.Review;
import de.escalon.hypermedia.sample.model.event.CreativeWork;
import de.escalon.hypermedia.sample.model.event.EventModel;
import de.escalon.hypermedia.sample.model.event.EventStatusType;
import de.escalon.hypermedia.spring.AffordanceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.Resources;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Sample Event Controller. Created by dschulten on 28.12.2014.
 */
@Controller
@RequestMapping("/events")
public class EventController {

    @Autowired
    private EventBackend eventBackend;

    @Autowired
    private EventResourceAssembler assembler;

//    @RequestMapping(method = RequestMethod.GET)
//    @ResponseBody
//    public ResponseEntity> getEvents() {
//        List events = assembler.toResources(eventBackend.getEvents());
//        for (Event event : events) {
//            addAffordances(event);
//        }
//        Resources eventResources = new Resources(events);
//
//        eventResources.add(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(EventController.class).addEvent(null))
//                .withSelfRel());
//
//        return new ResponseEntity>(eventResources, HttpStatus.OK);
//    }

    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity> findEvents(@RequestParam(required = false) String name) {
        List events = assembler.toResources(eventBackend.getEvents());
        List matches = new ArrayList();
        for (Event event : events) {
            if (name == null || event.workPerformed.getContent().name.equals(name)) {
                addAffordances(event);
                matches.add(event);
            }
        }
        Resources eventResources = new Resources(matches);

        eventResources.add(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(EventController.class)
                .addEvent(new Event(null, new CreativeWork(null), null, EventStatusType.EVENT_SCHEDULED)))
                .withSelfRel());

        eventResources.add(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(EventController.class)
                .findEvents(null))
                .withRel("hydra:search"));

        return new ResponseEntity>(eventResources, HttpStatus.OK);
    }


    @RequestMapping(value = "/{eventId}", method = RequestMethod.GET)
    public ResponseEntity getEvent(@PathVariable Integer eventId) {
        Event event = assembler.toResource(eventBackend.getEvent(eventId));

        addAffordances(event);

        return new ResponseEntity(event, HttpStatus.OK);
    }

    private void addAffordances(Event event) {
        event.add(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(this.getClass())
                .getEvent(event.id))
                .and(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(EventController.class)
                        .updateEvent(event.id, event)))
                .and(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(EventController.class)
                        .deleteEvent(event.id)))
                .withSelfRel());
        event.workPerformed.add(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(ReviewController.class)
                .addReview(event.id, new Review(null, new Rating(3))))
                .withRel("review"));
    }

    @RequestMapping(value = "/{eventId}", method = RequestMethod.PUT)
    public ResponseEntity updateEvent(@PathVariable int eventId, @RequestBody Event event) {
        eventBackend.updateEvent(eventId, event.getEventStatus());
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @RequestMapping(value = "/{eventId}", method = RequestMethod.DELETE)
    public ResponseEntity deleteEvent(@PathVariable int eventId) {
        eventBackend.deleteEvent(eventId);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }


    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity addEvent(@RequestBody Event event) {
        EventModel eventModel = new EventModel(-1, event.performer, event.workPerformed.getContent(), event.location,
                event.getEventStatus());
        int eventId = eventBackend.addEvent(eventModel);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setLocation(AffordanceBuilder.linkTo(AffordanceBuilder.methodOn(this.getClass())
                .getEvent(eventId))
                .toUri());
        return new ResponseEntity(httpHeaders, HttpStatus.CREATED);
    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy