-/*
+/* vim:set ts=4 sts=4 sw=4 noet fenc=utf-8:
+
Copyright 2009 senju@users.sourceforge.jp
Licensed under the Apache License, Version 2.0 (the "License");
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.stereotype.Service;
+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;
*
* @author senju
*/
-@Service()
public class AccountService {
/**
- * 現在ログイン中のgoogleアカウントのアカウントを取得する。
+ * システムに最初管理者として登録されるユーザーのアドレス
+ */
+ private String firstSuperUser;
+
+ @Autowired
+ private AccountDao accountDao;
+
+ /**
+ * 現在ログイン中の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("gu == u");
- q.declareImports("import com.google.appengine.api.users.User;");
- q.declareParameters("User u");
- final List<Account> up = (List<Account>) q.execute(gu);
- if (up.size() == 0) {
- throw new NotRegisteredException();
+ final Account acc = this.accountDao.findAccountByEmail(gu.getEmail());
+ if (acc == null) {
+ // 見つからない場合
+ return null;
}
- return up.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
* 登録するアカウント
* @throws RabbitBTSException
* 既に登録済みの場合
*/
- public void registAccount(Account account)
- throws RabbitBTSException {
+ public void registAccount(Account account) throws RabbitBTSException {
+ // 登録済みチェック
+ if (this.accountDao.findAccountByEmail(account.getEmail()) != null) {
+ throw new RabbitBTSException("既に登録されています。");
+ }
+
+ // 最初のスーパーユーザーかチェック
+ if (account.getEmail().equals(this.firstSuperUser)) {
+ account.setAdmin(true);
+ Sht.log(this).warn(
+ "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);
- final PersistenceManager pm = PMF.get().getPersistenceManager();
- final Transaction tx = pm.currentTransaction();
+ // TODO: メッセージ追加
+
+ }
+
+ /**
+ * DBからニックネームでアカウントを検索する。
+ *
+ * @param nickName
+ * 検索するアカウントのニックネーム
+ * @return 見つかったアカウント。無い場合null。
+ */
+ public Account fetchAccountByNickName(String nickName) {
+ final Account account = this.accountDao.findAccountByNickName(nickName);
+ return account;
+ }
+
+ /**
+ * アカウントをIDで検索する。
+ *
+ * @param id
+ * 検索対象のID
+ * @return 見つかったアカウント。見つからない場合null。
+ */
+ public Account fetchAccountById(Long id) {
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
- }
-
- // 登録処理
- account.setGu(Sht.user());
- account.setLastAccess(new Date());
- pm.makePersistent(account);
-
- // TODO: メッセージ追加
-
- tx.commit();
- } finally {
- if (tx.isActive()) {
- tx.rollback();
- }
+ return this.accountDao.get(id);
+ } catch (final DataRetrievalFailureException e) {
+ return null;
}
}
+
+ /**
+ * アカウントを取得する。 TODO: LastAccess順でソート、件数制限、ページング
+ *
+ * @return 取得したアカウントのリスト
+ */
+ public List<Account> fetchAccounts() {
+ return this.accountDao.getAll();
+ }
+
+ /**
+ * 指定されたアカウントと関連情報を削除する。
+ *
+ * @param account
+ * 削除するアカウント。
+ */
+ public void deleteAccount(Account account) {
+ this.accountDao.remove(account.getAccountId());
+ }
+
+ /**
+ * @return the firstSuperUser
+ */
+ public String getFirstSuperUser() {
+ return this.firstSuperUser;
+ }
+
+ /**
+ * @param firstSuperUser
+ * the firstSuperUser to set
+ */
+ public void setFirstSuperUser(String firstSuperUser) {
+ this.firstSuperUser = firstSuperUser;
+ }
}