OSDN Git Service

33a44a781c5961e69c047978b94ac834e564d167
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / services / BbsService.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.services;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Date;
22 import java.util.List;
23
24 import jp.sourceforge.rabbitBTS.dao.AccountDao;
25 import jp.sourceforge.rabbitBTS.dao.BbsDao;
26 import jp.sourceforge.rabbitBTS.models.Account;
27 import jp.sourceforge.rabbitBTS.models.BbsPost;
28
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.orm.ObjectRetrievalFailureException;
31
32 /**
33  * BBS用サービス
34  */
35 public class BbsService {
36         @Autowired
37         private BbsDao bbsDao;
38         @Autowired
39         private AccountDao accountDao;
40
41         /**
42          * 登録されている投稿を返す。
43          * 
44          * TODO: 日付順、件数制限、ページング
45          * 
46          * @return 投稿のリスト。
47          */
48         public List<BbsPost> fetchPosts() {
49                 final Collection<BbsPost> posts = this.bbsDao.getAllPosts();
50                 for (final BbsPost bbsPost : posts) {
51                         try {
52                                 // 投稿者の検索
53                                 if (bbsPost.getAuthorId() != null) {
54                                         final Account acc = this.accountDao.get(bbsPost
55                                                         .getAuthorId());
56                                         bbsPost.setAuthor(acc);
57                                 } else {
58                                         bbsPost.setAuthor(null);
59                                         bbsPost.setAuthorId(null);
60                                 }
61                         } catch (final ObjectRetrievalFailureException e) {
62                                 bbsPost.setAuthor(null);
63                                 bbsPost.setAuthorId(null);
64                         }
65                 }
66                 return new ArrayList<BbsPost>(posts);
67         }
68
69         /**
70          * 新規投稿を登録する。
71          * 
72          * @param bbsPost
73          *            登録する投稿
74          * @param account
75          *            投稿の作成者
76          */
77         public void addNewPost(BbsPost bbsPost, Account account) {
78                 // 投稿者の存在チェック(見つからない場合ランタイム例外)
79                 this.accountDao.get(account.getAccountId());
80
81                 // 保存
82                 bbsPost.setAuthorId(account.getAccountId());
83                 bbsPost.setDate(new Date());
84
85                 this.bbsDao.savePost(bbsPost);
86         }
87
88 }