OSDN Git Service

パッケージ名変更その他
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / controllers / BbsController.java
1 /*
2    Copyright 2009 senju@users.sourceforge.jp
3
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
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17
18 package jp.sourceforge.rabbitBTS.controllers;
19
20 import java.util.Date;
21 import java.util.List;
22
23 import javax.jdo.PersistenceManager;
24 import javax.servlet.http.HttpServletRequest;
25
26 import jp.sourceforge.rabbitBTS.PMF;
27 import jp.sourceforge.rabbitBTS.Sht;
28 import jp.sourceforge.rabbitBTS.models.BbsPost;
29
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;
37
38
39 @Controller
40 public class BbsController implements IController{
41         @Autowired
42         private Validator validator;
43         
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());
53         }
54         
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.");
62                         return null;
63                 }
64                 validator.validate(g, result);
65                 if(result.hasErrors()){
66                         Sht.log(this).finest("content is empty.");
67                         return null;
68                 }
69                 PersistenceManager pm = PMF.get().getPersistenceManager();
70                 try{
71                         pm.makePersistent(g);
72                 }finally{
73                         pm.close();
74                 }
75                 
76                 return "redirect:index.html";
77         }
78         
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();
83         }
84         
85
86 }