Java tutorial
/** * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package io.omatic.event.controller; import io.omatic.event.model.ApiIssue; import io.omatic.event.model.ApiIssue.IssueLevel; import io.omatic.event.model.Event; import io.omatic.event.repo.EventRepository; import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.PathVariable; 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; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/event") public class EventController { private static final Logger log = LoggerFactory.getLogger(EventController.class); @Autowired private EventRepository eventRepository; @Transactional @ApiOperation(value = "deleteEventsForCategory", nickname = "deleteEventsForCategory") @ApiImplicitParams({ @ApiImplicitParam(name = "category", value = "The domain category for the event", required = true, dataType = "string", paramType = "path", defaultValue = "") }) @RequestMapping(value = "/category/{category}", method = RequestMethod.DELETE) public void deleteEventsForCategory(@PathVariable String category) { eventRepository.deleteByCategory(category); } @Transactional @ApiOperation(value = "deleteAll", nickname = "deleteAll") @RequestMapping(value = "/", method = RequestMethod.DELETE) public void deleteAll() { eventRepository.deleteAll(); ; } @ApiOperation(value = "getEventsForCategory", nickname = "getEventsForCategory") @ApiImplicitParams({ @ApiImplicitParam(name = "category", value = "The domain category for the event", required = true, dataType = "string", paramType = "path", defaultValue = "") }) @RequestMapping(value = "/category/{category}", method = RequestMethod.GET) public List<Event> getEventsForCategory(@PathVariable String category) { return eventRepository.findByCategory(category); } @ApiOperation(value = "getEventsForCategoryAndBusinessKey", nickname = "getEventsForCategoryAndBusinessKey") @ApiImplicitParams({ @ApiImplicitParam(name = "category", value = "The domain category for the event", required = true, dataType = "string", paramType = "path", defaultValue = ""), @ApiImplicitParam(name = "businessKey", value = "The business identifier for the event", required = true, dataType = "string", paramType = "path", defaultValue = "") }) @RequestMapping(value = "/category/{category}/businesskey/{businessKey}", method = RequestMethod.GET) public List<Event> getEventsForCategoryAndBusinessKey(@PathVariable String category, @PathVariable String businessKey) { return eventRepository.findByCategoryAndBusinessKey(category, businessKey); } @ApiOperation(value = "getLatestEventForCategoryAndBusinessKey", nickname = "getLatestEventForCategoryAndBusinessKey") @ApiImplicitParams({ @ApiImplicitParam(name = "category", value = "The domain category for the event", required = true, dataType = "string", paramType = "path", defaultValue = ""), @ApiImplicitParam(name = "businessKey", value = "The business identifier for the event", required = true, dataType = "string", paramType = "path", defaultValue = "") }) @RequestMapping(value = "/category/{category}/businesskey/{businessKey}/latest", method = RequestMethod.GET) public Event getLatestEventForCategoryAndBusinessKey(@PathVariable String category, @PathVariable String businessKey) { return eventRepository.findFirstByCategoryAndBusinessKeyOrderByRecievedDateDesc(category, businessKey); } @ApiOperation(value = "getAllCategories", nickname = "getAllCategories") @RequestMapping(value = "/category", method = RequestMethod.GET) public List<String> getAllCategories() { return eventRepository.findCategoryDistinct(); } @ExceptionHandler @ResponseBody @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) public ApiIssue handleException(Exception exception) { ApiIssue error = new ApiIssue(); error.setLevel(IssueLevel.ERROR); error.setCode(500); error.setMessage("The request could not be processed due to an unexpected error."); log.error(exception.getMessage()); return error; } }