<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>
* @return the pmf
*/
public static PersistenceManagerFactory getPmf() {
+ Sht.log(PMFSupport.class).warning("getPmf() called.");
return pmf;
}
--- /dev/null
+/*
+ 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);
+
+}
--- /dev/null
+/*
+ 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);
+}
--- /dev/null
+/*
+ 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);
+
+}
--- /dev/null
+/*
+ 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();
+ }
+ }
+
+}
--- /dev/null
+/*
+ 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);
+ }
+
+}
--- /dev/null
+/*
+ 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);
+ }
+
+}
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;
*/
private String firstSuperUser;
+ @Autowired
+ private AccountDao accountDao;
+
/**
* 現在ログイン中のgoogleアカウントのアカウントをDBより取得する。
*
* @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;
}
// 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;
}
/**
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;
/**
* 登録されている投稿を返す。
*
* @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);
}
/**
* @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);
}
}
/> <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>