OSDN Git Service

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