OSDN Git Service

全てのjavaファイルにモードライン追加。
[rabbit-bts/RabbitBTS.git] / src / jp / sourceforge / rabbitBTS / controllers / MembersController.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.ArrayList;
21 import java.util.List;
22
23 import jp.sourceforge.rabbitBTS.Sht;
24 import jp.sourceforge.rabbitBTS.forms.MemberSearchForm;
25 import jp.sourceforge.rabbitBTS.models.Account;
26 import jp.sourceforge.rabbitBTS.services.AccountService;
27
28 import org.apache.commons.lang.StringUtils;
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.web.bind.annotation.RequestMapping;
34 import org.springframework.web.bind.annotation.RequestMethod;
35 import org.springframework.web.bind.annotation.RequestParam;
36
37 /**
38  * メンバー検索/管理操作用コントローラ
39  */
40 @Controller
41 public class MembersController extends BaseController implements IController {
42         @Autowired
43         private AccountService accountService;
44
45         /**
46          * メンバー一覧画面
47          */
48         @RequestMapping(method = RequestMethod.GET)
49         public void index(MemberSearchForm form, BindingResult result, ModelMap map) {
50                 // フォームが空の場合
51                 if (StringUtils.isEmpty(form.getNickName())) {
52                         final List<Account> accList = this.accountService.fetchAccounts();
53                         map.addAttribute("accounts", accList);
54                         return;
55                 }
56                 // TODO:他の条件が入った場合修正
57                 final Account account = this.accountService.fetchAccountByNickName(form
58                                 .getNickName());
59                 final List<Account> accList = new ArrayList<Account>(1);
60                 map.addAttribute("accounts", accList);
61                 if (account == null) {
62                         result.reject("account.not.found");
63                 } else {
64                         accList.add(account);
65                 }
66         }
67
68         @RequestMapping(method = RequestMethod.GET)
69         public void delete(@RequestParam("id") Long accountId, ModelMap map) {
70                 // TODO: 管理者チェック
71                 Account account = null;
72                 if (accountId != null) {
73                         account = this.accountService.fetchAccountById(accountId);
74                 }
75
76                 if (account == null) {
77                         Sht.log(this).warning("存在しないアカウントの削除が試行されました。");
78                 }
79
80                 map.addAttribute("accForDel", account);
81         }
82
83         @RequestMapping(method = RequestMethod.POST)
84         public String delete(Long accountId) {
85                 if (!isCsrfSafe()) {
86                         // 再試行の必要なし。メンバー一覧に戻る。
87                         return "redirect:/members/";
88                 }
89                 // TODO: 管理者チェック
90                 if (accountId != null) {
91                         final Account accForDel = this.accountService
92                                         .fetchAccountById(accountId);
93                         // TODO: 取得できたかチェック
94                         this.accountService.deleteAccount(accForDel);
95                         // TODO: ランタイム例外の処理
96                 }
97                 return "redirect:/members/";
98         }
99 }