OSDN Git Service

DAO化完了。
authorsenju <senju@users.sourceforge.jp>
Sat, 22 Aug 2009 12:16:48 +0000 (21:16 +0900)
committersenju <senju@users.sourceforge.jp>
Sat, 22 Aug 2009 12:16:48 +0000 (21:16 +0900)
PMF/PMFSupport削除。
サービスのPMF依存をなくした。

src/jp/sourceforge/rabbitBTS/PMF.java [deleted file]
src/jp/sourceforge/rabbitBTS/PMFSupport.java [deleted file]
src/jp/sourceforge/rabbitBTS/dao/AccountDao.java
src/jp/sourceforge/rabbitBTS/dao/jdo/AccountDaoImpl.java
src/jp/sourceforge/rabbitBTS/dao/jdo/GenericDaoJdo.java
src/jp/sourceforge/rabbitBTS/services/AccountService.java
war/WEB-INF/views/members/delete.ftl

diff --git a/src/jp/sourceforge/rabbitBTS/PMF.java b/src/jp/sourceforge/rabbitBTS/PMF.java
deleted file mode 100644 (file)
index ddcf7fa..0000000
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
-   Copyright 2009 senju@users.sourceforge.jp
-
-   Licensed under the Apache License, Version 2.0 (the "License");
-   you may not use this file except in compliance with the License.
-   You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
- */
-
-package jp.sourceforge.rabbitBTS;
-
-import javax.jdo.PersistenceManagerFactory;
-
-public final class PMF {
-       // private static final PersistenceManagerFactory pmfInstance = JDOHelper
-       // .getPersistenceManagerFactory("transactions-optional");
-
-       private PMF() {
-       };
-
-       public static PersistenceManagerFactory get() {
-               return PMFSupport.getPmf();
-       }
-}
diff --git a/src/jp/sourceforge/rabbitBTS/PMFSupport.java b/src/jp/sourceforge/rabbitBTS/PMFSupport.java
deleted file mode 100644 (file)
index 81c76be..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
-   Copyright 2009 senju@users.sourceforge.jp
-
-   Licensed under the Apache License, Version 2.0 (the "License");
-   you may not use this file except in compliance with the License.
-   You may obtain a copy of the License at
-
-       http://www.apache.org/licenses/LICENSE-2.0
-
-   Unless required by applicable law or agreed to in writing, software
-   distributed under the License is distributed on an "AS IS" BASIS,
-   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-   See the License for the specific language governing permissions and
-   limitations under the License.
- */
-
-package jp.sourceforge.rabbitBTS;
-
-import javax.jdo.PersistenceManagerFactory;
-
-import org.springframework.beans.factory.annotation.Autowired;
-
-/**
- * 一時的に実装するクラス。
- * 
- * TODO:DAO化が完了したら削除されるべき
- */
-public class PMFSupport {
-       private static PersistenceManagerFactory pmf;
-
-       /**
-        * @return the pmf
-        */
-       public static PersistenceManagerFactory getPmf() {
-               Sht.log(PMFSupport.class).warning("getPmf() called.");
-               return pmf;
-       }
-
-       /**
-        * @param pmf
-        *            the pmf to set
-        */
-       @Autowired
-       public void setPmf(PersistenceManagerFactory pmf) {
-               PMFSupport.pmf = pmf;
-       }
-
-}
index 1d5d0ee..74d5f2c 100644 (file)
@@ -27,4 +27,6 @@ public interface AccountDao extends GenericDao<Account, Long> {
 
        public Account findAccountByEmail(String email);
 
+       public Account findAccountByNickName(String nickName);
+
 }
index 2854fc9..206e980 100644 (file)
@@ -34,7 +34,17 @@ public class AccountDaoImpl extends GenericDaoJdo<Account, Long> implements
        public Account findAccountByEmail(String email) {
                final Collection<Account> c = getJdoTemplate().find(Account.class,
                                "email == e", "String e", new Object[] { email });
-               if (c.size() == 0) {
+               if (c.isEmpty()) {
+                       return null;
+               } else {
+                       return c.iterator().next();
+               }
+       }
+
+       public Account findAccountByNickName(String nickName) {
+               final Collection<Account> c = getJdoTemplate().find(Account.class,
+                               "nickName == n", "String n", new Object[] { nickName });
+               if (c.isEmpty()) {
                        return null;
                } else {
                        return c.iterator().next();
index 1a7f5fb..4eabc3a 100644 (file)
@@ -41,30 +41,25 @@ public class GenericDaoJdo<T, PK extends Serializable> extends JdoDaoSupport
        }
 
        public boolean exists(PK id) {
-               final T entity = (T) getJdoTemplate().getObjectById(
-                               this.persistentClass, id);
-               return entity != null;
+               try {
+                       getJdoTemplate().getObjectById(this.persistentClass, id);
+                       return true;
+               } catch (final ObjectRetrievalFailureException e) {
+                       return false;
+               }
        }
 
+       @SuppressWarnings("unchecked")
        public T get(PK id) {
                final T entity = (T) getJdoTemplate().getObjectById(
                                this.persistentClass, id);
-
-               if (entity == null) {
-                       throw new ObjectRetrievalFailureException(this.persistentClass, id);
-               }
-
                return entity;
        }
 
+       @SuppressWarnings("unchecked")
        public T getByKey(Key id) {
                final T entity = (T) getJdoTemplate().getObjectById(
                                this.persistentClass, id);
-
-               if (entity == null) {
-                       throw new ObjectRetrievalFailureException(this.persistentClass, id);
-               }
-
                return entity;
        }
 
index 27f0aea..5d0b8fc 100644 (file)
@@ -19,12 +19,6 @@ package jp.sourceforge.rabbitBTS.services;
 import java.util.Date;
 import java.util.List;
 
-import javax.jdo.JDOObjectNotFoundException;
-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.dao.AccountDao;
 import jp.sourceforge.rabbitBTS.exceptions.HasNotValidIdException;
@@ -32,6 +26,7 @@ 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;
 
@@ -94,7 +89,7 @@ public class AccountService {
        }
 
        /**
-        * 指定されたアカウントを登録する。
+        * 指定されたアカウントを登録する。 TODO: トランザクション
         * 
         * @param account
         *            登録するアカウント
@@ -102,48 +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("既に登録されています。");
-                               }
-                       } catch (final HasNotValidIdException e) {
-                               throw new RabbitBTSException("googleにログインしてください。", e);
-                       }
-
-                       // 最初のスーパーユーザーかチェック
-                       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: メッセージ追加
+
        }
 
        /**
@@ -153,17 +131,9 @@ 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) {
-                       return null;
-               }
-               return accList.get(0);
+               final Account account = this.accountDao.findAccountByNickName(nickName);
+               return account;
        }
 
        /**
@@ -174,11 +144,9 @@ public class AccountService {
         * @return 見つかったアカウント。見つからない場合null。
         */
        public Account fetchAccountById(Long id) {
-               final PersistenceManager pm = PMF.get().getPersistenceManager();
                try {
-                       final Account account = pm.getObjectById(Account.class, id);
-                       return account;
-               } catch (final JDOObjectNotFoundException e) {
+                       return this.accountDao.get(id);
+               } catch (final DataRetrievalFailureException e) {
                        return null;
                }
        }
@@ -188,12 +156,8 @@ public class AccountService {
         * 
         * @return 取得したアカウントのリスト
         */
-       @SuppressWarnings("unchecked")
        public List<Account> fetchAccounts() {
-               final PersistenceManager pm = PMF.get().getPersistenceManager();
-               final List<Account> accList = (List<Account>) pm
-                               .newQuery(Account.class).execute();
-               return accList;
+               return this.accountDao.getAll();
        }
 
        /**
@@ -203,8 +167,7 @@ public class AccountService {
         *            削除するアカウント。
         */
        public void deleteAccount(Account account) {
-               final PersistenceManager pm = PMF.get().getPersistenceManager();
-               pm.deletePersistent(account);
+               this.accountDao.remove(account.getAccountId());
        }
 
        /**
index c6ff109..e3adb2b 100644 (file)
@@ -2,8 +2,8 @@
 [#assign title="アカウント削除"]
 [#include "/common/head.ftl"]
 <h1 class="admin">アカウント削除</h1>
-[@rbts.showGlobalErrors "accForDel" /]
 [#if accForDel??]
+[@rbts.showGlobalErrors "accForDel" /]
 <p>以下のアカウントを削除します。削除されたアカウントは復元できません。</p>
 <form method="post" action="">
 <table>