Package de.panikco.pmdb.web.controller

Source Code of de.panikco.pmdb.web.controller.MediaController

package de.panikco.pmdb.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
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.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import de.panikco.pmdb.model.Media;
import de.panikco.pmdb.service.MediaService;
import de.panikco.pmdb.web.validation.MediaValidator;

@Controller
public class MediaController {

  final static Logger logger = LoggerFactory.getLogger(MediaController.class);

  @Autowired
  private MediaService service;

  // TODO: is that the desired way to do it? no wiring, no annotation?
  private final MediaValidator validator = new MediaValidator();

  @RequestMapping(method = RequestMethod.GET, value = "/display/{id}")
  public String displayMedium(ModelMap modelMap, @PathVariable("id") String id) {
    logger.debug("processing request to display media with id {}", id);
    modelMap.put("media", service.getMedia(id));
    return "mediaDetail";
  }

  @RequestMapping(method = RequestMethod.GET, value = "/insert")
  public ModelAndView prepareInsertPage(ModelMap modelMap) {
    return new ModelAndView("insertMedium", "media", new Media());
  }

  @RequestMapping(method = RequestMethod.GET, value = "/list")
  public String list(ModelMap modelMap) {

    List<Media> mediae = service.getAllMedia();
    modelMap.addAttribute("mediae", mediae);

    return "listAllMedia";

  }
 
  @RequestMapping(method = RequestMethod.POST, value = "/insert")
  public String insert(HttpServletRequest request, ModelMap modelMap,
      @ModelAttribute("media") Media media,
      BindingResult result) {

    validator.validate(media, result);

    if (result.hasErrors()) {
      return "insertMedium";
    }
   
    service.addMedia(media);

    // TODO: I dislike the naming scheme, medium, media, mediae, refactor.
    List<Media> mediae = service.getAllMedia();
    modelMap.addAttribute("mediae", mediae);

    return "listAllMedia";

  }

}
TOP

Related Classes of de.panikco.pmdb.web.controller.MediaController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.