X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=src%2Fjp%2Fsourceforge%2FrabbitBTS%2Fservices%2FAccountService.java;h=22e6ed1a753bb928ed64d4062dc279d66152241e;hb=a9dc58700f58dfd87cd59337344e7b1de79eb665;hp=a69fdd83a98a035f602da49abeea0f9b3731755d;hpb=673a963ba7e1b1713e4869caf1ec43f59d451cc2;p=rabbit-bts%2FRabbitBTS.git
diff --git a/src/jp/sourceforge/rabbitBTS/services/AccountService.java b/src/jp/sourceforge/rabbitBTS/services/AccountService.java
index a69fdd8..22e6ed1 100644
--- a/src/jp/sourceforge/rabbitBTS/services/AccountService.java
+++ b/src/jp/sourceforge/rabbitBTS/services/AccountService.java
@@ -1,4 +1,5 @@
-/*
+/* 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");
@@ -19,17 +20,16 @@ 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.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;
@@ -38,74 +38,151 @@ import com.google.appengine.api.users.User;
*
* @author senju
*/
-@Service()
public class AccountService {
/**
- * ç¾å¨ãã°ã¤ã³ä¸ã®googleã¢ã«ã¦ã³ãã®ã¢ã«ã¦ã³ããåå¾ããã
+ * ã·ã¹ãã ã«æå管çè
ã¨ãã¦ç»é²ãããã¦ã¼ã¶ã¼ã®ã¢ãã¬ã¹
+ */
+ private String firstSuperUser;
+
+ @Autowired
+ private AccountDao accountDao;
+
+ /**
+ * ç¾å¨ãã°ã¤ã³ä¸ã®googleã¢ã«ã¦ã³ãã®ã¢ã«ã¦ã³ããDBããåå¾ããã
+ *
+ *
+ * ã¾ãã"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 up = (List) 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 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;
+ }
}