Java tutorial
/* * Copyright 2011-2016 Frank W. Rahn and the project authors. * * Licensed 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 de.rahn.finances.server.web.ui; import static de.rahn.finances.domains.entities.SecurityType.getKeyValueEntries; import static org.slf4j.LoggerFactory.getLogger; import static org.springframework.http.HttpStatus.NO_CONTENT; import static org.springframework.web.bind.annotation.RequestMethod.DELETE; import static org.springframework.web.bind.annotation.RequestMethod.GET; import static org.springframework.web.bind.annotation.RequestMethod.POST; import java.util.List; import java.util.Map.Entry; import javax.validation.Valid; import org.slf4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Description; import org.springframework.data.domain.Pageable; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import de.rahn.finances.domains.entities.Security; import de.rahn.finances.domains.entities.SecurityType; import de.rahn.finances.services.SecuritiesService; import de.rahn.finances.services.SecurityNotFoundException; /** * Der Controller fr die Verwaltung der Wertpapiere. * * @author Frank W. Rahn */ @Controller @Description("Der Controller fr die Verwaltung der Wertpapiere") public class SecuritiesController { private static final Logger LOGGER = getLogger(SecuritiesController.class); @Autowired private SecuritiesService service; /** * @return die Liste der {@link SecurityType}s fr die Auswahllisten */ @ModelAttribute("securityTypeList") public List<Entry<String, String>> securityTypeList() { return getKeyValueEntries(); } /** * @return die Liste der anzuzeigenden Wertpapiere */ @RequestMapping(value = "/securities", method = GET) public String securities(@RequestParam(name = "inventory", defaultValue = "true") boolean inventory, Pageable pageable, Model model) { LOGGER.info("Methode aufgerufen: securities({}, {})", inventory, pageable); model.addAttribute("inventory", inventory).addAttribute("page", service.getSecurities(inventory, pageable)); return "securities"; } /** * @return die Liste der anzuzeigenden Wertpapiere */ @RequestMapping(value = "/securities/{type}", method = GET) public String securities(@PathVariable("type") SecurityType type, @RequestParam(name = "inventory", required = false) boolean inventory, Pageable pageable, Model model) { LOGGER.info("Methode aufgerufen: securities({}, {}, {})", type, inventory, pageable); model.addAttribute("inventory", inventory).addAttribute("type", type).addAttribute("page", service.getSecurities(inventory, type, pageable)); return "securities"; } /** * Zeige die Maske zum erfassen eines Wertpapieres an. * * @return das Model mit dem leeren Wertpapier */ @RequestMapping(value = "/security", method = GET) public String security(Model model) { LOGGER.info("Methode aufgerufen: security()"); model.addAttribute("security", new Security()); return "security"; } /** * Ermittle das anzuzeigene Wertpapier. * * @param id die Id des Wertpapiers * @return das Model mit dem Wertpapier */ @RequestMapping(value = "/security/{id}", method = GET) public String security(@PathVariable("id") String id, Model model) { LOGGER.info("Methode aufgerufen: security({})", id); model.addAttribute("security", service.getSecurity(id)); return "security"; } /** * Speichere das Wertpapier. * * @param security das genderte Wertpapier * @return die nchste anzuzeigende View */ @RequestMapping(value = "/security", method = POST) public String security(@Valid @ModelAttribute("security") Security security, BindingResult bindingResult) { LOGGER.info("Methode aufgerufen: security({})", security); if (bindingResult.hasErrors()) { return "security"; } try { security = service.save(security); } catch (Exception exception) { LOGGER.error("Fehler beim Speichern eines Wertpapiers", exception); bindingResult.addError(new ObjectError("security", exception.toString())); return "security"; } return "redirect:/securities"; } /** * Lsche das Wertpapier. * * @param id die Id des Wertpapiers * @return der Status */ @RequestMapping(value = "/security/{id}", method = DELETE) public ResponseEntity<Void> securityDelete(@PathVariable("id") String id) { LOGGER.info("Methode aufgerufen: securityDelete({})", id); try { service.delete(service.getSecurity(id)); } catch (SecurityNotFoundException exception) { // Alles Gut, da Wertpapier schon gelscht ist } catch (Exception exception) { throw new IllegalArgumentException("Das Wertpapier zur ID '" + id + "' konnte nicht gelscht werden.", exception); } return new ResponseEntity<>(NO_CONTENT); } }