OSDN Git Service

5d0b8fc2a0395ee81caaa0b0c441d9179ec8e7be
[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 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;
27
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;
32
33 import com.google.appengine.api.users.User;
34
35 /**
36  * アカウント/アカウントまわりのサービス
37  * 
38  * @author senju
39  */
40 public class AccountService {
41
42         /**
43          * システムに最初管理者として登録されるユーザーのアドレス
44          */
45         private String firstSuperUser;
46
47         @Autowired
48         private AccountDao accountDao;
49
50         /**
51          * 現在ログイン中のgoogleアカウントのアカウントをDBより取得する。
52          * 
53          * <p>
54          * また、"account"の名前でリクエストコンテクストにアカウントを保存する。
55          * 
56          * @return 取得したアカウント。見つからない場合はnull
57          * @throws HasNotValidIdException
58          *             ログインしていないユーザーの場合
59          */
60         public Account fetchAccount() throws HasNotValidIdException {
61                 final User gu = Sht.user();
62                 if (gu == null) {
63                         throw new HasNotValidIdException();
64                 }
65                 final Account acc = this.accountDao.findAccountByEmail(gu.getEmail());
66                 if (acc == null) {
67                         // 見つからない場合
68                         return null;
69                 }
70
71                 // getCurrentAccount用にリクエストコンテクストに保存しておく。
72                 final RequestAttributes req = RequestContextHolder
73                                 .currentRequestAttributes();
74                 req.setAttribute("account", acc, RequestAttributes.SCOPE_REQUEST);
75                 return acc;
76         }
77
78         /**
79          * 現在ログイン中のアカウントを返す。
80          * 
81          * @return 現在ログイン中のアカウント。
82          */
83         public Account getCurrentAccount() {
84                 final RequestAttributes req = RequestContextHolder
85                                 .currentRequestAttributes();
86                 final Account account = (Account) req.getAttribute("account",
87                                 RequestAttributes.SCOPE_REQUEST);
88                 return account;
89         }
90
91         /**
92          * 指定されたアカウントを登録する。 TODO: トランザクション
93          * 
94          * @param account
95          *            登録するアカウント
96          * @throws RabbitBTSException
97          *             既に登録済みの場合
98          */
99         public void registAccount(Account account) throws RabbitBTSException {
100                 // 登録済みチェック
101                 if (this.accountDao.findAccountByEmail(account.getEmail()) != null) {
102                         throw new RabbitBTSException("既に登録されています。");
103                 }
104
105                 // 最初のスーパーユーザーかチェック
106                 if (account.getEmail().equals(this.firstSuperUser)) {
107                         account.setAdmin(true);
108                         Sht.log(this).warning(
109                                         "Admin Account Created. " + this.firstSuperUser);
110                 }
111
112                 // nickName重複チェック
113                 if (this.accountDao.findAccountByNickName(account.getNickName()) != null) {
114                         Sht.log(this).info(
115                                         "nickName " + account.getNickName() + " is already used.");
116                         throw new RabbitBTSException("nickNameが重複しています。");
117                 }
118
119                 // 登録処理
120                 account.setLastAccess(new Date());
121                 this.accountDao.save(account);
122
123                 // TODO: メッセージ追加
124
125         }
126
127         /**
128          * DBからニックネームでアカウントを検索する。
129          * 
130          * @param nickName
131          *            検索するアカウントのニックネーム
132          * @return 見つかったアカウント。無い場合null。
133          */
134         public Account fetchAccountByNickName(String nickName) {
135                 final Account account = this.accountDao.findAccountByNickName(nickName);
136                 return account;
137         }
138
139         /**
140          * アカウントをIDで検索する。
141          * 
142          * @param id
143          *            検索対象のID
144          * @return 見つかったアカウント。見つからない場合null。
145          */
146         public Account fetchAccountById(Long id) {
147                 try {
148                         return this.accountDao.get(id);
149                 } catch (final DataRetrievalFailureException e) {
150                         return null;
151                 }
152         }
153
154         /**
155          * アカウントを取得する。 TODO: LastAccess順でソート、件数制限、ページング
156          * 
157          * @return 取得したアカウントのリスト
158          */
159         public List<Account> fetchAccounts() {
160                 return this.accountDao.getAll();
161         }
162
163         /**
164          * 指定されたアカウントと関連情報を削除する。
165          * 
166          * @param account
167          *            削除するアカウント。
168          */
169         public void deleteAccount(Account account) {
170                 this.accountDao.remove(account.getAccountId());
171         }
172
173         /**
174          * @return the firstSuperUser
175          */
176         public String getFirstSuperUser() {
177                 return this.firstSuperUser;
178         }
179
180         /**
181          * @param firstSuperUser
182          *            the firstSuperUser to set
183          */
184         public void setFirstSuperUser(String firstSuperUser) {
185                 this.firstSuperUser = firstSuperUser;
186         }
187 }