1 // vim:set ts=4 sts=4 sw=4 noet fenc=utf-8:
3 Copyright 2009 senju@users.sourceforge.jp
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
9 http://www.apache.org/licenses/LICENSE-2.0
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.
18 package jp.sourceforge.rabbitBTS.controllers;
20 import java.util.Date;
21 import java.util.logging.Level;
23 import javax.servlet.http.HttpServletRequest;
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;
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;
40 public final class RegisterController extends BaseController implements
42 private static final String REDIRECT_HOME_HTML = "redirect:/home/";
45 private AccountService accountService;
48 private Validator validator;
53 * ユーザーがgoogleにログインしていない場合、googleのログイン画面にリダイレクトする。
54 * ユーザーがアカウントを持っている場合、ユーザーホームにリダイレクトする。
55 * ユーザーがgoogleにログイン済みでかつアカウントを持っていない時だけ、画面を表示する。
61 @RequestMapping(method = RequestMethod.GET)
62 public String index(HttpServletRequest req, ModelMap map) {
65 final Account account = this.accountService.fetchAccount();
66 if (account == null) {
68 map.addAttribute(new Account());
71 // すでに登録済みの場合ホームへリダイレクト
72 Sht.log(this).finer("既に登録済み");
73 ret = REDIRECT_HOME_HTML;
75 } catch (final HasNotValidIdException e) {
77 Sht.log(this).finer("ログインが必要");
78 ret = "redirect:" + Sht.loginUrl(req);
96 * <li>バリデートに失敗した場合、エラーメッセージ
97 * <li>Account登録に失敗した場合(二重登録等)、ロールバック、エラーメッセージ
104 @RequestMapping(method = RequestMethod.POST)
105 public String index(Account account, BindingResult result, ModelMap map) {
106 if (!this.isCsrfSafe(result)) {
110 account.setLastAccess(new Date());
112 account.setEmail(Sht.user().getEmail());
113 this.validator.validate(account, result);
114 if (result.hasErrors()) {
115 Sht.log(this).finer("ユーザ登録画面にてバリデートに失敗");
121 if (this.accountService.fetchAccountByNickName(account
122 .getNickName()) != null) {
123 result.rejectValue("nickName", "Account.nickName[duplicate]");
124 Sht.log(this).finer("nickName重複チェックエラー");
128 this.accountService.registAccount(account);
129 } catch (final RabbitBTSException e) {
130 Sht.log(this).log(Level.WARNING, "ユーザー登録で例外発生", e);
132 map.addAttribute("errorMessage", "登録に失敗しました。再度やりなおしてください。");
135 return REDIRECT_HOME_HTML;