OSDN Git Service

DAO化の初期フェーズ
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / services / BbsService.java
index 8d0669f..33a44a7 100644 (file)
 
 package jp.sourceforge.rabbitBTS.services;
 
+import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Date;
 import java.util.List;
 
-import javax.jdo.JDOObjectNotFoundException;
-import javax.jdo.PersistenceManager;
-
-import jp.sourceforge.rabbitBTS.PMF;
+import jp.sourceforge.rabbitBTS.dao.AccountDao;
+import jp.sourceforge.rabbitBTS.dao.BbsDao;
 import jp.sourceforge.rabbitBTS.models.Account;
 import jp.sourceforge.rabbitBTS.models.BbsPost;
 
-import org.springframework.stereotype.Service;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.orm.ObjectRetrievalFailureException;
 
 /**
  * BBS用サービス
  */
-@Service
 public class BbsService {
+       @Autowired
+       private BbsDao bbsDao;
+       @Autowired
+       private AccountDao accountDao;
 
        /**
         * 登録されている投稿を返す。
@@ -41,23 +45,25 @@ public class BbsService {
         * 
         * @return 投稿のリスト。
         */
-       @SuppressWarnings("unchecked")
        public List<BbsPost> fetchPosts() {
-               final PersistenceManager pm = PMF.get().getPersistenceManager();
-               final List<BbsPost> posts = (List<BbsPost>) pm.newQuery(BbsPost.class)
-                               .execute();
+               final Collection<BbsPost> posts = this.bbsDao.getAllPosts();
                for (final BbsPost bbsPost : posts) {
                        try {
                                // 投稿者の検索
-                               final Account acc = pm.getObjectById(Account.class, bbsPost
-                                               .getAuthorId());
-                               bbsPost.setAuthor(acc);
-                       } catch (final JDOObjectNotFoundException e) {
+                               if (bbsPost.getAuthorId() != null) {
+                                       final Account acc = this.accountDao.get(bbsPost
+                                                       .getAuthorId());
+                                       bbsPost.setAuthor(acc);
+                               } else {
+                                       bbsPost.setAuthor(null);
+                                       bbsPost.setAuthorId(null);
+                               }
+                       } catch (final ObjectRetrievalFailureException e) {
                                bbsPost.setAuthor(null);
                                bbsPost.setAuthorId(null);
                        }
                }
-               return posts;
+               return new ArrayList<BbsPost>(posts);
        }
 
        /**
@@ -68,20 +74,15 @@ public class BbsService {
         * @param account
         *            投稿の作成者
         */
-       public void addNewPost(BbsPost bbsPost, final Account account) {
-               final PersistenceManager pm = PMF.get().getPersistenceManager();
-               try {
-                       // 投稿者の存在チェック(見つからない場合ランタイム例外)
-                       pm.getObjectById(Account.class, account.getAccountId());
+       public void addNewPost(BbsPost bbsPost, Account account) {
+               // 投稿者の存在チェック(見つからない場合ランタイム例外)
+               this.accountDao.get(account.getAccountId());
 
-                       // 保存
-                       bbsPost.setAuthorId(account.getAccountId());
-                       bbsPost.setDate(new Date());
+               // 保存
+               bbsPost.setAuthorId(account.getAccountId());
+               bbsPost.setDate(new Date());
 
-                       pm.makePersistent(bbsPost);
-               } finally {
-                       pm.close();
-               }
+               this.bbsDao.savePost(bbsPost);
        }
 
 }