2 Copyright 2009 senju@users.sourceforge.jp
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
18 package jp.sourceforge.rabbitBTS.controllers;
20 import java.util.Date;
21 import java.util.List;
23 import javax.jdo.PersistenceManager;
24 import javax.servlet.http.HttpServletRequest;
26 import jp.sourceforge.rabbitBTS.PMF;
27 import jp.sourceforge.rabbitBTS.Sht;
28 import jp.sourceforge.rabbitBTS.models.BbsPost;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.stereotype.Controller;
32 import org.springframework.ui.ModelMap;
33 import org.springframework.validation.BindingResult;
34 import org.springframework.validation.Validator;
35 import org.springframework.web.bind.annotation.RequestMapping;
36 import org.springframework.web.bind.annotation.RequestMethod;
40 public class BbsController implements IController{
42 private Validator validator;
44 @RequestMapping(method = RequestMethod.GET)
45 public void index(ModelMap model, HttpServletRequest req) {
46 Sht.log(this).finest("index called");
47 BbsPost g = new BbsPost();
48 g.setAuthor(Sht.user());
49 model.addAttribute(g);
50 model.addAttribute("loginurl", Sht.loginUrl(req));
51 model.addAttribute("logouturl", Sht.logoutUrl(req));
52 model.addAttribute("posts",fetchBbsPosts());
55 @RequestMapping(method = RequestMethod.POST)
56 public String postEdit(BbsPost g, BindingResult result){
57 Sht.log(this).finest("submit called");
58 g.setAuthor(Sht.user());
59 g.setDate(new Date());
60 if(g.getAuthor() == null){
61 Sht.log(this).finest("author is null.");
64 validator.validate(g, result);
65 if(result.hasErrors()){
66 Sht.log(this).finest("content is empty.");
69 PersistenceManager pm = PMF.get().getPersistenceManager();
76 return "redirect:index.html";
79 @SuppressWarnings("unchecked")
80 private List<BbsPost> fetchBbsPosts(){
81 String q = "select from " + BbsPost.class.getName();
82 return (List<BbsPost>) PMF.get().getPersistenceManager().newQuery(q).execute();