OSDN Git Service

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