1 /* vim:set ts=4 sts=4 sw=4 noet fenc=utf-8:
3 Copyright 2009 senju@users.sourceforge.jp
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
18 package jp.sourceforge.rabbitBTS.services;
20 import java.util.Date;
21 import java.util.List;
23 import jp.sourceforge.rabbitBTS.Sht;
24 import jp.sourceforge.rabbitBTS.dao.AccountDao;
25 import jp.sourceforge.rabbitBTS.exceptions.HasNotValidIdException;
26 import jp.sourceforge.rabbitBTS.exceptions.RabbitBTSException;
27 import jp.sourceforge.rabbitBTS.models.Account;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.dao.DataRetrievalFailureException;
31 import org.springframework.web.context.request.RequestAttributes;
32 import org.springframework.web.context.request.RequestContextHolder;
34 import com.google.appengine.api.users.User;
41 public class AccountService {
44 * システムに最初管理者として登録されるユーザーのアドレス
46 private String firstSuperUser;
49 private AccountDao accountDao;
52 * 現在ログイン中のgoogleアカウントのアカウントをDBより取得する。
55 * また、"account"の名前でリクエストコンテクストにアカウントを保存する。
57 * @return 取得したアカウント。見つからない場合はnull
58 * @throws HasNotValidIdException
61 public Account fetchAccount() throws HasNotValidIdException {
62 final User gu = Sht.user();
64 throw new HasNotValidIdException();
66 final Account acc = this.accountDao.findAccountByEmail(gu.getEmail());
72 // getCurrentAccount用にリクエストコンテクストに保存しておく。
73 final RequestAttributes req = RequestContextHolder
74 .currentRequestAttributes();
75 req.setAttribute("account", acc, RequestAttributes.SCOPE_REQUEST);
82 * @return 現在ログイン中のアカウント。
84 public Account getCurrentAccount() {
85 final RequestAttributes req = RequestContextHolder
86 .currentRequestAttributes();
87 final Account account = (Account) req.getAttribute("account",
88 RequestAttributes.SCOPE_REQUEST);
93 * 指定されたアカウントを登録する。 TODO: トランザクション
97 * @throws RabbitBTSException
100 public void registAccount(Account account) throws RabbitBTSException {
102 if (this.accountDao.findAccountByEmail(account.getEmail()) != null) {
103 throw new RabbitBTSException("既に登録されています。");
107 if (account.getEmail().equals(this.firstSuperUser)) {
108 account.setAdmin(true);
109 Sht.log(this).warning(
110 "Admin Account Created. " + this.firstSuperUser);
114 if (this.accountDao.findAccountByNickName(account.getNickName()) != null) {
116 "nickName " + account.getNickName() + " is already used.");
117 throw new RabbitBTSException("nickNameが重複しています。");
121 account.setLastAccess(new Date());
122 this.accountDao.save(account);
129 * DBからニックネームでアカウントを検索する。
133 * @return 見つかったアカウント。無い場合null。
135 public Account getAccountByNickName(String nickName) {
136 final Account account = this.accountDao.findAccountByNickName(nickName);
145 * @return 見つかったアカウント。見つからない場合null。
147 public Account getAccountById(Long id) {
149 return this.accountDao.get(id);
150 } catch (final DataRetrievalFailureException e) {
156 * アカウントを取得する。 TODO: LastAccess順でソート、件数制限、ページング
158 * @return 取得したアカウントのリスト
160 public List<Account> fetchAccounts() {
161 return this.accountDao.getAll();
165 * 指定されたアカウントと関連情報を削除する。
170 public void deleteAccount(Account account) {
171 this.accountDao.remove(account.getAccountId());
175 * @param firstSuperUser
176 * the firstSuperUser to set
178 public void setFirstSuperUser(String firstSuperUser) {
179 this.firstSuperUser = firstSuperUser;