OSDN Git Service

DAO化の初期フェーズ
authorsenju <senju@users.sourceforge.jp>
Sat, 22 Aug 2009 10:12:58 +0000 (19:12 +0900)
committersenju <senju@users.sourceforge.jp>
Sat, 22 Aug 2009 10:12:58 +0000 (19:12 +0900)
途中だけどいい感じ。

.classpath
src/jp/sourceforge/rabbitBTS/PMFSupport.java
src/jp/sourceforge/rabbitBTS/dao/AccountDao.java [new file with mode: 0644]
src/jp/sourceforge/rabbitBTS/dao/BbsDao.java [new file with mode: 0644]
src/jp/sourceforge/rabbitBTS/dao/GenericDao.java [new file with mode: 0644]
src/jp/sourceforge/rabbitBTS/dao/jdo/AccountDaoImpl.java [new file with mode: 0644]
src/jp/sourceforge/rabbitBTS/dao/jdo/BbsDaoImpl.java [new file with mode: 0644]
src/jp/sourceforge/rabbitBTS/dao/jdo/GenericDaoJdo.java [new file with mode: 0644]
src/jp/sourceforge/rabbitBTS/services/AccountService.java
src/jp/sourceforge/rabbitBTS/services/BbsService.java
war/WEB-INF/rabbitBTS-servlet.xml

index d7f892c..3b2a545 100644 (file)
@@ -17,5 +17,6 @@
        <classpathentry kind="lib" path="war/WEB-INF/lib/aopalliance.jar"/>
        <classpathentry kind="lib" path="war/WEB-INF/lib/commons-lang-2.4.jar"/>
        <classpathentry kind="lib" path="war/WEB-INF/lib/spring-orm.jar"/>
+       <classpathentry kind="lib" path="war/WEB-INF/lib/spring-tx.jar"/>
        <classpathentry kind="output" path="war/WEB-INF/classes"/>
 </classpath>
index ab0095a..81c76be 100644 (file)
@@ -32,6 +32,7 @@ public class PMFSupport {
         * @return the pmf
         */
        public static PersistenceManagerFactory getPmf() {
+               Sht.log(PMFSupport.class).warning("getPmf() called.");
                return pmf;
        }
 
diff --git a/src/jp/sourceforge/rabbitBTS/dao/AccountDao.java b/src/jp/sourceforge/rabbitBTS/dao/AccountDao.java
new file mode 100644 (file)
index 0000000..1d5d0ee
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+   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.dao;
+
+import java.util.Collection;
+
+import jp.sourceforge.rabbitBTS.models.Account;
+
+/**
+ *
+ */
+public interface AccountDao extends GenericDao<Account, Long> {
+
+       public Account findAccountByEmail(String email);
+
+}
diff --git a/src/jp/sourceforge/rabbitBTS/dao/BbsDao.java b/src/jp/sourceforge/rabbitBTS/dao/BbsDao.java
new file mode 100644 (file)
index 0000000..eec282b
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+   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.dao;
+
+import java.util.Collection;
+
+import jp.sourceforge.rabbitBTS.models.BbsPost;
+
+/**
+ * BBS用DAO
+ */
+public interface BbsDao {
+       public Collection<BbsPost> getAllPosts();
+
+       public void savePost(BbsPost post);
+}
diff --git a/src/jp/sourceforge/rabbitBTS/dao/GenericDao.java b/src/jp/sourceforge/rabbitBTS/dao/GenericDao.java
new file mode 100644 (file)
index 0000000..238a1d3
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+   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.dao;
+
+import java.util.List;
+
+import com.google.appengine.api.datastore.Key;
+
+/**
+ *
+ */
+public interface GenericDao<T, PK> {
+
+       public boolean exists(PK id);
+
+       public T get(PK id);
+
+       public T getByKey(Key id);
+
+       public List<T> getAll();
+
+       public List<T> getAllDistinct();
+
+       public void remove(PK id);
+
+       public T save(T object);
+
+}
diff --git a/src/jp/sourceforge/rabbitBTS/dao/jdo/AccountDaoImpl.java b/src/jp/sourceforge/rabbitBTS/dao/jdo/AccountDaoImpl.java
new file mode 100644 (file)
index 0000000..2854fc9
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+   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.dao.jdo;
+
+import java.util.Collection;
+
+import jp.sourceforge.rabbitBTS.dao.AccountDao;
+import jp.sourceforge.rabbitBTS.models.Account;
+
+/**
+ *
+ */
+public class AccountDaoImpl extends GenericDaoJdo<Account, Long> implements
+               AccountDao {
+
+       public AccountDaoImpl() {
+               super(Account.class);
+       }
+
+       public Account findAccountByEmail(String email) {
+               final Collection<Account> c = getJdoTemplate().find(Account.class,
+                               "email == e", "String e", new Object[] { email });
+               if (c.size() == 0) {
+                       return null;
+               } else {
+                       return c.iterator().next();
+               }
+       }
+
+}
diff --git a/src/jp/sourceforge/rabbitBTS/dao/jdo/BbsDaoImpl.java b/src/jp/sourceforge/rabbitBTS/dao/jdo/BbsDaoImpl.java
new file mode 100644 (file)
index 0000000..d9a388b
--- /dev/null
@@ -0,0 +1,42 @@
+/*
+   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.dao.jdo;
+
+import java.util.Collection;
+
+import jp.sourceforge.rabbitBTS.dao.BbsDao;
+import jp.sourceforge.rabbitBTS.models.BbsPost;
+
+import org.springframework.orm.jdo.support.JdoDaoSupport;
+
+/**
+ * BBS用DAO
+ */
+public class BbsDaoImpl extends JdoDaoSupport implements BbsDao {
+
+       @SuppressWarnings("unchecked")
+       @Override
+       public Collection<BbsPost> getAllPosts() {
+               return getJdoTemplate().find(BbsPost.class);
+       }
+
+       @Override
+       public void savePost(BbsPost post) {
+               getJdoTemplate().makePersistent(post);
+       }
+
+}
diff --git a/src/jp/sourceforge/rabbitBTS/dao/jdo/GenericDaoJdo.java b/src/jp/sourceforge/rabbitBTS/dao/jdo/GenericDaoJdo.java
new file mode 100644 (file)
index 0000000..1a7f5fb
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+   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.dao.jdo;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+
+import jp.sourceforge.rabbitBTS.dao.GenericDao;
+
+import org.springframework.orm.ObjectRetrievalFailureException;
+import org.springframework.orm.jdo.support.JdoDaoSupport;
+
+import com.google.appengine.api.datastore.Key;
+
+public class GenericDaoJdo<T, PK extends Serializable> extends JdoDaoSupport
+               implements GenericDao<T, PK> {
+       public final Class<T> persistentClass;
+
+       /**
+        * Constructor that takes in a class for easy creation of DAO
+        */
+       public GenericDaoJdo(Class<T> persistentClass) {
+               this.persistentClass = persistentClass;
+       }
+
+       public boolean exists(PK id) {
+               final T entity = (T) getJdoTemplate().getObjectById(
+                               this.persistentClass, id);
+               return entity != null;
+       }
+
+       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;
+       }
+
+       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;
+       }
+
+       public List<T> getAll() {
+               return new ArrayList<T>(getJdoTemplate().find(this.persistentClass));
+       }
+
+       public List<T> getAllDistinct() {
+               final Collection result = new LinkedHashSet(getAll());
+               return new ArrayList(result);
+       }
+
+       public void remove(PK id) {
+               getJdoTemplate().deletePersistent(this.get(id));
+       }
+
+       public T save(T object) {
+               return (T) getJdoTemplate().makePersistent(object);
+       }
+
+}
index 3265278..27f0aea 100644 (file)
@@ -26,10 +26,12 @@ 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;
 import jp.sourceforge.rabbitBTS.exceptions.RabbitBTSException;
 import jp.sourceforge.rabbitBTS.models.Account;
 
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.context.request.RequestAttributes;
 import org.springframework.web.context.request.RequestContextHolder;
 
@@ -47,6 +49,9 @@ public class AccountService {
         */
        private String firstSuperUser;
 
+       @Autowired
+       private AccountDao accountDao;
+
        /**
         * 現在ログイン中のgoogleアカウントのアカウントをDBより取得する。
         * 
@@ -57,18 +62,13 @@ public class AccountService {
         * @throws HasNotValidIdException
         *             ログインしていないユーザーの場合
         */
-       @SuppressWarnings("unchecked")
        public Account fetchAccount() throws HasNotValidIdException {
                final User gu = Sht.user();
                if (gu == 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) {
+               final Account acc = this.accountDao.findAccountByEmail(gu.getEmail());
+               if (acc == null) {
                        // 見つからない場合
                        return null;
                }
@@ -76,9 +76,8 @@ public class AccountService {
                // getCurrentAccount用にリクエストコンテクストに保存しておく。
                final RequestAttributes req = RequestContextHolder
                                .currentRequestAttributes();
-               req.setAttribute("account", accList.get(0),
-                               RequestAttributes.SCOPE_REQUEST);
-               return accList.get(0);
+               req.setAttribute("account", acc, RequestAttributes.SCOPE_REQUEST);
+               return acc;
        }
 
        /**
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);
        }
 
 }
index 6cb06aa..07d0860 100644 (file)
                /> <bean class="jp.sourceforge.rabbitBTS.TestAspect" />
        -->
 
-       <bean class="jp.sourceforge.rabbitBTS.services.AccountService">
-               <property name="firstSuperUser" value="test@example.com" />
-       </bean>
-
        <!-- validator -->
        <bean id="configurationLoader"
                class="org.springmodules.validation.bean.conf.loader.annotation.AnnotationBeanValidationConfigurationLoader" />
                id="authInterceptor" />
        <bean class="jp.sourceforge.rabbitBTS.interceptors.CSRFInterceptor"
                id="csrf" />
+       <bean
+               class="org.springframework.orm.jdo.support.OpenPersistenceManagerInViewInterceptor"
+               id="openInView">
+               <property name="persistenceManagerFactory" ref="pmf"></property>
+       </bean>
 
        <bean
                class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
                <property name="interceptors">
                        <list>
                                <ref bean="tracer" />
+                               <ref bean="openInView" />
                                <ref bean="authInterceptor" />
                                <ref bean="csrf" />
                        </list>
                class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping">
                <property name="interceptors">
                        <list>
-                               <ref bean="tracer"></ref>
+                               <ref bean="tracer" />
+                               <ref bean="openInView" />
                                <ref bean="authInterceptor" />
                                <ref bean="csrf" />
                        </list>
                </property>
        </bean>
 
-       <context:component-scan base-package="jp.sourceforge.rabbitBTS"
-               use-default-filters="true" />
-
        <!--  trace log -->
        <!--
                <bean id="dInterceptor"
        -->
 
        <!-- DAO -->
-       <bean id="persistenceManagerFactory"
+       <bean id="pmf"
                class="org.springframework.orm.jdo.LocalPersistenceManagerFactoryBean">
                <property name="persistenceManagerFactoryName" value="transactions-optional" />
        </bean>
-       
+
+       <!-- TODO:こきたないハック -->
        <bean class="jp.sourceforge.rabbitBTS.PMFSupport" />
 
+       <bean id="abstractDao" abstract="true">
+               <property name="persistenceManagerFactory" ref="pmf"></property>
+       </bean>
+
+       <bean id="accountDao" parent="abstractDao"
+               class="jp.sourceforge.rabbitBTS.dao.jdo.AccountDaoImpl">
+       </bean>
+
+       <bean id="bbsDao" parent="abstractDao"
+               class="jp.sourceforge.rabbitBTS.dao.jdo.BbsDaoImpl">
+       </bean>
+
+       <!-- サービス -->
+       <bean class="jp.sourceforge.rabbitBTS.services.BbsService">
+       </bean>
+       <bean class="jp.sourceforge.rabbitBTS.services.AccountService">
+               <property name="firstSuperUser" value="test@example.com" />
+       </bean>
+
+       <!-- アノテーションベースの設定 -->
+       <context:component-scan base-package="jp.sourceforge.rabbitBTS"
+               use-default-filters="true" />
+
 </beans>