OSDN Git Service

3a27dac60f569da7e9ea0220e9176313557ca8e9
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / services / AccountService.java
1 /*
2    Copyright 2009 senju@users.sourceforge.jp
3
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
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
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.
15  */
16
17 package jp.sourceforge.rabbitBTS.services;
18
19 import java.util.Date;
20 import java.util.List;
21
22 import javax.jdo.PersistenceManager;
23 import javax.jdo.Query;
24 import javax.jdo.Transaction;
25
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;
31
32 import com.google.appengine.api.users.User;
33
34 /**
35  * アカウント/アカウントまわりのサービス
36  * 
37  * @author senju
38  */
39 public class AccountService {
40
41         /**
42          * システムに最初管理者として登録されるユーザーのアドレス
43          */
44         private String firstSuperUser;
45
46         /**
47          * 現在ログイン中のgoogleアカウントのアカウントを取得する。
48          * 
49          * @return 取得したアカウント。ログインしていない場合はnull
50          * @throws NotRegisteredException
51          *             未登録ユーザーの場合
52          */
53         @SuppressWarnings("unchecked")
54         public Account fetchAccount() throws NotRegisteredException {
55                 final User gu = Sht.user();
56                 if (gu == null) {
57                         return null;
58                 }
59                 final Query q = PMF.get().getPersistenceManager().newQuery(
60                                 Account.class);
61                 q.setFilter("email == e");
62                 q.declareParameters("String e");
63                 final List<Account> up = (List<Account>) q.execute(gu.getEmail());
64                 if (up.size() == 0) {
65                         throw new NotRegisteredException();
66                 }
67
68                 return up.get(0);
69         }
70
71         /**
72          * 指定されたアカウントを登録する。
73          * 
74          * @param account
75          *            登録するアカウント
76          * @throws RabbitBTSException
77          *             既に登録済みの場合
78          */
79         public void registAccount(Account account) throws RabbitBTSException {
80
81                 final PersistenceManager pm = PMF.get().getPersistenceManager();
82                 final Transaction tx = pm.currentTransaction();
83                 try {
84                         tx.begin();
85                         try {
86                                 // 登録済みチェック
87                                 final Account up = this.fetchAccount();
88                                 if (up != null) {
89                                         throw new RabbitBTSException("既に登録されています。");
90                                 } else {
91                                         throw new RabbitBTSException("googleにログインしてください。");
92                                 }
93                         } catch (final NotRegisteredException e) {
94                                 // OK
95                         }
96
97                         // 最初のスーパーユーザーかチェック
98                         if (account.getEmail().equals(this.firstSuperUser)) {
99                                 account.setAdmin(true);
100                                 Sht.log(this).warning(
101                                                 "Admin Account Created. " + this.firstSuperUser);
102                         }
103
104                         // nickName重複チェック
105                         if (fetchAccountByNickName(account.getNickName()) != null) {
106                                 Sht.log(this).info(
107                                                 "nickName " + account.getNickName()
108                                                                 + " is already used.");
109                                 throw new RabbitBTSException("nickNameが重複しています。");
110                         }
111
112                         // 登録処理
113                         account.setLastAccess(new Date());
114                         pm.makePersistent(account);
115
116                         // TODO: メッセージ追加
117
118                         tx.commit();
119                 } finally {
120                         if (tx.isActive()) {
121                                 tx.rollback();
122                         }
123                 }
124         }
125
126         /**
127          * DBからニックネームでアカウントを検索する。
128          * 
129          * @param nickName
130          *            検索するアカウントのニックネーム
131          * @return 見つかったアカウント。無い場合null。
132          */
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) {
141                         return null;
142                 }
143                 return accList.get(0);
144         }
145
146         /**
147          * @return the firstSuperUser
148          */
149         public String getFirstSuperUser() {
150                 return this.firstSuperUser;
151         }
152
153         /**
154          * @param firstSuperUser
155          *            the firstSuperUser to set
156          */
157         public void setFirstSuperUser(String firstSuperUser) {
158                 this.firstSuperUser = firstSuperUser;
159         }
160 }