2 Copyright 2009 senju@users.sourceforge.jp
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
17 package jp.sourceforge.rabbitBTS.services;
19 import java.util.Date;
20 import java.util.List;
22 import jp.sourceforge.rabbitBTS.Sht;
23 import jp.sourceforge.rabbitBTS.dao.AccountDao;
24 import jp.sourceforge.rabbitBTS.exceptions.HasNotValidIdException;
25 import jp.sourceforge.rabbitBTS.exceptions.RabbitBTSException;
26 import jp.sourceforge.rabbitBTS.models.Account;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.dao.DataRetrievalFailureException;
30 import org.springframework.web.context.request.RequestAttributes;
31 import org.springframework.web.context.request.RequestContextHolder;
33 import com.google.appengine.api.users.User;
40 public class AccountService {
43 * システムに最初管理者として登録されるユーザーのアドレス
45 private String firstSuperUser;
48 private AccountDao accountDao;
51 * 現在ログイン中のgoogleアカウントのアカウントをDBより取得する。
54 * また、"account"の名前でリクエストコンテクストにアカウントを保存する。
56 * @return 取得したアカウント。見つからない場合はnull
57 * @throws HasNotValidIdException
60 public Account fetchAccount() throws HasNotValidIdException {
61 final User gu = Sht.user();
63 throw new HasNotValidIdException();
65 final Account acc = this.accountDao.findAccountByEmail(gu.getEmail());
71 // getCurrentAccount用にリクエストコンテクストに保存しておく。
72 final RequestAttributes req = RequestContextHolder
73 .currentRequestAttributes();
74 req.setAttribute("account", acc, RequestAttributes.SCOPE_REQUEST);
81 * @return 現在ログイン中のアカウント。
83 public Account getCurrentAccount() {
84 final RequestAttributes req = RequestContextHolder
85 .currentRequestAttributes();
86 final Account account = (Account) req.getAttribute("account",
87 RequestAttributes.SCOPE_REQUEST);
92 * 指定されたアカウントを登録する。 TODO: トランザクション
96 * @throws RabbitBTSException
99 public void registAccount(Account account) throws RabbitBTSException {
101 if (this.accountDao.findAccountByEmail(account.getEmail()) != null) {
102 throw new RabbitBTSException("既に登録されています。");
106 if (account.getEmail().equals(this.firstSuperUser)) {
107 account.setAdmin(true);
108 Sht.log(this).warning(
109 "Admin Account Created. " + this.firstSuperUser);
113 if (this.accountDao.findAccountByNickName(account.getNickName()) != null) {
115 "nickName " + account.getNickName() + " is already used.");
116 throw new RabbitBTSException("nickNameが重複しています。");
120 account.setLastAccess(new Date());
121 this.accountDao.save(account);
128 * DBからニックネームでアカウントを検索する。
132 * @return 見つかったアカウント。無い場合null。
134 public Account fetchAccountByNickName(String nickName) {
135 final Account account = this.accountDao.findAccountByNickName(nickName);
144 * @return 見つかったアカウント。見つからない場合null。
146 public Account fetchAccountById(Long id) {
148 return this.accountDao.get(id);
149 } catch (final DataRetrievalFailureException e) {
155 * アカウントを取得する。 TODO: LastAccess順でソート、件数制限、ページング
157 * @return 取得したアカウントのリスト
159 public List<Account> fetchAccounts() {
160 return this.accountDao.getAll();
164 * 指定されたアカウントと関連情報を削除する。
169 public void deleteAccount(Account account) {
170 this.accountDao.remove(account.getAccountId());
174 * @return the firstSuperUser
176 public String getFirstSuperUser() {
177 return this.firstSuperUser;
181 * @param firstSuperUser
182 * the firstSuperUser to set
184 public void setFirstSuperUser(String firstSuperUser) {
185 this.firstSuperUser = firstSuperUser;