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 javax.jdo.PersistenceManager;
23 import javax.jdo.Query;
24 import javax.jdo.Transaction;
26 import jp.sourceforge.rabbitBTS.PMF;
27 import jp.sourceforge.rabbitBTS.Sht;
28 import jp.sourceforge.rabbitBTS.exceptions.NotRegisteredException;
29 import jp.sourceforge.rabbitBTS.exceptions.RabbitBTSException;
30 import jp.sourceforge.rabbitBTS.models.Account;
32 import com.google.appengine.api.users.User;
39 public class AccountService {
42 * システムに最初管理者として登録されるユーザーのアドレス
44 private String firstSuperUser;
47 * 現在ログイン中のgoogleアカウントのアカウントを取得する。
49 * @return 取得したアカウント。ログインしていない場合はnull
50 * @throws NotRegisteredException
53 @SuppressWarnings("unchecked")
54 public Account fetchAccount() throws NotRegisteredException {
55 final User gu = Sht.user();
59 final Query q = PMF.get().getPersistenceManager().newQuery(
61 q.setFilter("email == e");
62 q.declareParameters("String e");
63 final List<Account> up = (List<Account>) q.execute(gu.getEmail());
65 throw new NotRegisteredException();
76 * @throws RabbitBTSException
79 public void registAccount(Account account) throws RabbitBTSException {
81 final PersistenceManager pm = PMF.get().getPersistenceManager();
82 final Transaction tx = pm.currentTransaction();
87 final Account up = this.fetchAccount();
89 throw new RabbitBTSException("既に登録されています。");
91 throw new RabbitBTSException("googleにログインしてください。");
93 } catch (final NotRegisteredException e) {
98 if (account.getEmail().equals(this.firstSuperUser)) {
99 account.setAdmin(true);
100 Sht.log(this).warning(
101 "Admin Account Created. " + this.firstSuperUser);
105 if (fetchAccountByNickName(account.getNickName()) != null) {
107 "nickName " + account.getNickName()
108 + " is already used.");
109 throw new RabbitBTSException("nickNameが重複しています。");
113 account.setLastAccess(new Date());
114 pm.makePersistent(account);
127 * DBからニックネームでアカウントを検索する。
131 * @return 見つかったアカウント。無い場合null。
133 @SuppressWarnings("unchecked")
134 public Account fetchAccountByNickName(String nickName) {
135 final PersistenceManager pm = PMF.get().getPersistenceManager();
136 final Query q = pm.newQuery(Account.class);
137 q.setFilter("nickName == n");
138 q.declareParameters("String n");
139 final List<Account> accList = (List<Account>) q.execute(nickName);
140 if (accList.size() == 0) {
143 return accList.get(0);
147 * @return the firstSuperUser
149 public String getFirstSuperUser() {
150 return this.firstSuperUser;
154 * @param firstSuperUser
155 * the firstSuperUser to set
157 public void setFirstSuperUser(String firstSuperUser) {
158 this.firstSuperUser = firstSuperUser;