OSDN Git Service

全てのjavaファイルにモードライン追加。
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / controllers / RegisterController.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.controllers;
19
20 import java.util.Date;
21 import java.util.logging.Level;
22
23 import javax.servlet.http.HttpServletRequest;
24
25 import jp.sourceforge.rabbitBTS.Sht;
26 import jp.sourceforge.rabbitBTS.exceptions.HasNotValidIdException;
27 import jp.sourceforge.rabbitBTS.exceptions.RabbitBTSException;
28 import jp.sourceforge.rabbitBTS.models.Account;
29 import jp.sourceforge.rabbitBTS.services.AccountService;
30
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.stereotype.Controller;
33 import org.springframework.ui.ModelMap;
34 import org.springframework.validation.BindingResult;
35 import org.springframework.validation.Validator;
36 import org.springframework.web.bind.annotation.RequestMapping;
37 import org.springframework.web.bind.annotation.RequestMethod;
38
39 @Controller
40 public final class RegisterController extends BaseController implements
41                 IPublicController {
42         private static final String REDIRECT_HOME_HTML = "redirect:/home/";
43
44         @Autowired
45         private AccountService accountService;
46
47         @Autowired
48         private Validator validator;
49
50         /**
51          * 新規登録画面を表示する。
52          * <p>
53          * ユーザーがgoogleにログインしていない場合、googleのログイン画面にリダイレクトする。
54          * ユーザーがアカウントを持っている場合、ユーザーホームにリダイレクトする。
55          * ユーザーがgoogleにログイン済みでかつアカウントを持っていない時だけ、画面を表示する。
56          * 
57          * @param req
58          * @param map
59          * @return
60          */
61         @RequestMapping(method = RequestMethod.GET)
62         public String index(HttpServletRequest req, ModelMap map) {
63                 String ret = null;
64                 try {
65                         final Account account = this.accountService.fetchAccount();
66                         if (account == null) {
67                                 // 正常系
68                                 map.addAttribute(new Account());
69                                 ret = null;
70                         } else {
71                                 // すでに登録済みの場合ホームへリダイレクト
72                                 Sht.log(this).finer("既に登録済み");
73                                 ret = REDIRECT_HOME_HTML;
74                         }
75                 } catch (final HasNotValidIdException e) {
76                         // googleにログインしてない場合
77                         Sht.log(this).finer("ログインが必要");
78                         ret = "redirect:" + Sht.loginUrl(req);
79                 }
80                 return ret;
81         }
82
83         /**
84          * 新規登録を行う。
85          * 
86          * <p>
87          * 正常系
88          * <ol>
89          * <li>バリデート
90          * <li>Accountを登録
91          * <li>ユーザーホームにリダイレクト
92          * <ol>
93          * <p>
94          * 異常系
95          * <ol>
96          * <li>バリデートに失敗した場合、エラーメッセージ
97          * <li>Account登録に失敗した場合(二重登録等)、ロールバック、エラーメッセージ
98          * </ol>
99          * 
100          * @param account
101          * @param result
102          * @return
103          */
104         @RequestMapping(method = RequestMethod.POST)
105         public String index(Account account, BindingResult result, ModelMap map) {
106                 if (!this.isCsrfSafe(result)) {
107                         return null;
108                 }
109
110                 account.setLastAccess(new Date());
111
112                 account.setEmail(Sht.user().getEmail());
113                 this.validator.validate(account, result);
114                 if (result.hasErrors()) {
115                         Sht.log(this).finer("ユーザ登録画面にてバリデートに失敗");
116                         return null;
117                 }
118
119                 try {
120                         // nickName重複チェック
121                         if (this.accountService.fetchAccountByNickName(account
122                                         .getNickName()) != null) {
123                                 result.rejectValue("nickName", "Account.nickName[duplicate]");
124                                 Sht.log(this).finer("nickName重複チェックエラー");
125                                 return null;
126                         }
127
128                         this.accountService.registAccount(account);
129                 } catch (final RabbitBTSException e) {
130                         Sht.log(this).log(Level.WARNING, "ユーザー登録で例外発生", e);
131                         // TODO:resultにセット
132                         map.addAttribute("errorMessage", "登録に失敗しました。再度やりなおしてください。");
133                         return null;
134                 }
135                 return REDIRECT_HOME_HTML;
136         }
137 }