OSDN Git Service

29150faf8250d798d2f3f06599358e44be8219d7
[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 package jp.sourceforge.rabbitBTS.controllers;
18
19 import java.util.Date;
20 import java.util.List;
21
22 import javax.jdo.PersistenceManager;
23 import javax.servlet.http.HttpServletRequest;
24
25 import jp.sourceforge.rabbitBTS.PMF;
26 import jp.sourceforge.rabbitBTS.Sht;
27 import jp.sourceforge.rabbitBTS.models.Account;
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 @Controller
39 public class BbsController implements IController {
40         @Autowired
41         private Validator validator;
42
43         @RequestMapping(method = RequestMethod.GET)
44         public void index(ModelMap model, HttpServletRequest req) {
45                 Sht.log(this).finest("index called");
46                 final BbsPost g = new BbsPost();
47                 model.addAttribute(g);
48                 model.addAttribute("loginurl", Sht.loginUrl(req));
49                 model.addAttribute("logouturl", Sht.logoutUrl(req));
50                 model.addAttribute("posts", fetchBbsPosts());
51         }
52
53         @RequestMapping(method = RequestMethod.POST)
54         public String postEdit(BbsPost g, BindingResult result,
55                         HttpServletRequest req) {
56                 Sht.log(this).finest("submit called");
57                 final Account acc = (Account) req.getAttribute("account");
58                 g.setAuthorId(acc.getAccountId());
59                 g.setDate(new Date());
60
61                 this.validator.validate(g, result);
62                 if (result.hasErrors()) {
63                         Sht.log(this).finest("content is empty.");
64                         return null;
65                 }
66                 final PersistenceManager pm = PMF.get().getPersistenceManager();
67                 try {
68                         pm.makePersistent(g);
69                 } finally {
70                         pm.close();
71                 }
72
73                 return "redirect:index.html";
74         }
75
76         @SuppressWarnings("unchecked")
77         private List<BbsPost> fetchBbsPosts() {
78                 final PersistenceManager pm = PMF.get().getPersistenceManager();
79                 final List<BbsPost> posts = (List<BbsPost>) pm.newQuery(BbsPost.class)
80                                 .execute();
81                 for (final BbsPost bbsPost : posts) {
82                         final Account acc = pm.getObjectById(Account.class, bbsPost
83                                         .getAuthorId());
84                         bbsPost.setAuthor(acc);
85                 }
86                 return posts;
87         }
88
89 }