OSDN Git Service

リファクタリング
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / services / AccountService.java
index 203ee41..e3723d8 100644 (file)
@@ -19,16 +19,17 @@ package jp.sourceforge.rabbitBTS.services;
 import java.util.Date;
 import java.util.List;
 
-import javax.jdo.PersistenceManager;
-import javax.jdo.Query;
-import javax.jdo.Transaction;
-
-import jp.sourceforge.rabbitBTS.PMF;
 import jp.sourceforge.rabbitBTS.Sht;
-import jp.sourceforge.rabbitBTS.exceptions.NotRegisteredException;
+import jp.sourceforge.rabbitBTS.dao.AccountDao;
+import jp.sourceforge.rabbitBTS.exceptions.HasNotValidIdException;
 import jp.sourceforge.rabbitBTS.exceptions.RabbitBTSException;
 import jp.sourceforge.rabbitBTS.models.Account;
 
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.dao.DataRetrievalFailureException;
+import org.springframework.web.context.request.RequestAttributes;
+import org.springframework.web.context.request.RequestContextHolder;
+
 import com.google.appengine.api.users.User;
 
 /**
@@ -43,33 +44,52 @@ public class AccountService {
         */
        private String firstSuperUser;
 
+       @Autowired
+       private AccountDao accountDao;
+
        /**
-        * 現在ログイン中のgoogleアカウントのアカウントを取得する。
+        * 現在ログイン中のgoogleアカウントのアカウントをDBより取得する。
+        * 
+        * <p>
+        * また、"account"の名前でリクエストコンテクストにアカウントを保存する。
         * 
-        * @return 取得したアカウント。ログインしていない場合はnull
-        * @throws NotRegisteredException
-        *             未登録ユーザーの場合
+        * @return 取得したアカウント。見つからない場合はnull
+        * @throws HasNotValidIdException
+        *             ログインしていないユーザーの場合
         */
-       @SuppressWarnings("unchecked")
-       public Account fetchAccount() throws NotRegisteredException {
+       public Account fetchAccount() throws HasNotValidIdException {
                final User gu = Sht.user();
                if (gu == null) {
-                       return null;
+                       throw new HasNotValidIdException();
                }
-               final Query q = PMF.get().getPersistenceManager().newQuery(
-                               Account.class);
-               q.setFilter("email == e");
-               q.declareParameters("String e");
-               final List<Account> accList = (List<Account>) q.execute(gu.getEmail());
-               if (accList.size() == 0) {
-                       throw new NotRegisteredException();
+               final Account acc = this.accountDao.findAccountByEmail(gu.getEmail());
+               if (acc == null) {
+                       // 見つからない場合
+                       return null;
                }
 
-               return accList.get(0);
+               // getCurrentAccount用にリクエストコンテクストに保存しておく。
+               final RequestAttributes req = RequestContextHolder
+                               .currentRequestAttributes();
+               req.setAttribute("account", acc, RequestAttributes.SCOPE_REQUEST);
+               return acc;
+       }
+
+       /**
+        * 現在ログイン中のアカウントを返す。
+        * 
+        * @return 現在ログイン中のアカウント。
+        */
+       public Account getCurrentAccount() {
+               final RequestAttributes req = RequestContextHolder
+                               .currentRequestAttributes();
+               final Account account = (Account) req.getAttribute("account",
+                               RequestAttributes.SCOPE_REQUEST);
+               return account;
        }
 
        /**
-        * 指定されたアカウントを登録する。
+        * 指定されたアカウントを登録する。 TODO: トランザクション
         * 
         * @param account
         *            登録するアカウント
@@ -77,50 +97,31 @@ public class AccountService {
         *             既に登録済みの場合
         */
        public void registAccount(Account account) throws RabbitBTSException {
+               // 登録済みチェック
+               if (this.accountDao.findAccountByEmail(account.getEmail()) != null) {
+                       throw new RabbitBTSException("既に登録されています。");
+               }
 
-               final PersistenceManager pm = PMF.get().getPersistenceManager();
-               final Transaction tx = pm.currentTransaction();
-               try {
-                       tx.begin();
-                       try {
-                               // 登録済みチェック
-                               final Account up = this.fetchAccount();
-                               if (up != null) {
-                                       throw new RabbitBTSException("既に登録されています。");
-                               } else {
-                                       throw new RabbitBTSException("googleにログインしてください。");
-                               }
-                       } catch (final NotRegisteredException e) {
-                               // OK
-                       }
-
-                       // 最初のスーパーユーザーかチェック
-                       if (account.getEmail().equals(this.firstSuperUser)) {
-                               account.setAdmin(true);
-                               Sht.log(this).warning(
-                                               "Admin Account Created. " + this.firstSuperUser);
-                       }
-
-                       // nickName重複チェック
-                       if (fetchAccountByNickName(account.getNickName()) != null) {
-                               Sht.log(this).info(
-                                               "nickName " + account.getNickName()
-                                                               + " is already used.");
-                               throw new RabbitBTSException("nickNameが重複しています。");
-                       }
-
-                       // 登録処理
-                       account.setLastAccess(new Date());
-                       pm.makePersistent(account);
-
-                       // TODO: メッセージ追加
-
-                       tx.commit();
-               } finally {
-                       if (tx.isActive()) {
-                               tx.rollback();
-                       }
+               // 最初のスーパーユーザーかチェック
+               if (account.getEmail().equals(this.firstSuperUser)) {
+                       account.setAdmin(true);
+                       Sht.log(this).warning(
+                                       "Admin Account Created. " + this.firstSuperUser);
+               }
+
+               // nickName重複チェック
+               if (this.accountDao.findAccountByNickName(account.getNickName()) != null) {
+                       Sht.log(this).info(
+                                       "nickName " + account.getNickName() + " is already used.");
+                       throw new RabbitBTSException("nickNameが重複しています。");
                }
+
+               // 登録処理
+               account.setLastAccess(new Date());
+               this.accountDao.save(account);
+
+               // TODO: メッセージ追加
+
        }
 
        /**
@@ -130,24 +131,43 @@ public class AccountService {
         *            検索するアカウントのニックネーム
         * @return 見つかったアカウント。無い場合null。
         */
-       @SuppressWarnings("unchecked")
-       public Account fetchAccountByNickName(String nickName) {
-               final PersistenceManager pm = PMF.get().getPersistenceManager();
-               final Query q = pm.newQuery(Account.class);
-               q.setFilter("nickName == n");
-               q.declareParameters("String n");
-               final List<Account> accList = (List<Account>) q.execute(nickName);
-               if (accList.size() == 0) {
+       public Account getAccountByNickName(String nickName) {
+               final Account account = this.accountDao.findAccountByNickName(nickName);
+               return account;
+       }
+
+       /**
+        * アカウントをIDで検索する。
+        * 
+        * @param id
+        *            検索対象のID
+        * @return 見つかったアカウント。見つからない場合null。
+        */
+       public Account getAccountById(Long id) {
+               try {
+                       return this.accountDao.get(id);
+               } catch (final DataRetrievalFailureException e) {
                        return null;
                }
-               return accList.get(0);
        }
 
        /**
-        * @return the firstSuperUser
+        * アカウントを取得する。 TODO: LastAccess順でソート、件数制限、ページング
+        * 
+        * @return 取得したアカウントのリスト
+        */
+       public List<Account> fetchAccounts() {
+               return this.accountDao.getAll();
+       }
+
+       /**
+        * 指定されたアカウントと関連情報を削除する。
+        * 
+        * @param account
+        *            削除するアカウント。
         */
-       public String getFirstSuperUser() {
-               return this.firstSuperUser;
+       public void deleteAccount(Account account) {
+               this.accountDao.remove(account.getAccountId());
        }
 
        /**