OSDN Git Service

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