package com.dtikhonov.controller;
/**
* Created by dtikhonov on 08/05/14.
*/
import com.dtikhonov.MessageManager;
import com.dtikhonov.listeners.MyMessageListener;
import com.dtikhonov.model.Message;
import com.dtikhonov.publishers.MyPublisherInterface;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.beans.factory.annotation.Autowired;
@Controller
public class GreetingController {
private final static Logger logger = Logger.getLogger(MyMessageListener.class);
private String result = "result";
@Autowired
private MessageManager messageManager;
@Autowired
private MyPublisherInterface meesagePublisher;
public void setResult(String result){
this.result = result;
}
@RequestMapping(value="/greeting", method=RequestMethod.GET)
public ModelAndView greeting() {
return new ModelAndView("greeting", "command", new Message());
}
@RequestMapping(value="/addGreeting", method=RequestMethod.POST)
public String addGreeting(@ModelAttribute("SpringWeb")Message message, Model model) {
messageManager.addMessage(message);
model.addAttribute("message", message);
logger.info("add message");
meesagePublisher.publish(message.getContent());
return result;
}
@RequestMapping(value="/messageList", method=RequestMethod.GET)
public ModelAndView listMessage(){
ModelAndView modelAndView = new ModelAndView("messages");
modelAndView.addObject("messageListSize", messageManager.getMessageList().size());
modelAndView.addObject("messageList", messageManager.getMessageList());
return modelAndView;
}
}