[STYLE] controllerの無駄な改行やスペースなどの削除

This commit is contained in:
s-choi
2026-09-10 15:49:34 +09:00
parent d2c78b12aa
commit 2e748dc1cb
8 changed files with 411 additions and 617 deletions

View File

@@ -1,5 +1,19 @@
package com.example.rhizome.app.presentation.controller;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.*;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.rhizome.aop.aspect.PermissionCheck;
import com.example.rhizome.aop.aspect.SessionCheck;
@@ -12,30 +26,12 @@ import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTa
import com.example.rhizome.app.presentation.form.DepartmentForm;
import com.example.rhizome.app.presentation.form.LoginUserForm;
import com.example.rhizome.app.presentation.form.MessageForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.DELETE_EMPLOYEE;
/**
* 削除処理のコントロータ
*/
@Controller
public class DeleteEmployeeController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(DeleteEmployeeController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
@@ -43,78 +39,52 @@ public class DeleteEmployeeController {
SearchDepartment searchDepartment;
@Autowired
public DeleteEmployeeController(DeleteEmployee deleteEmployee, SearchDepartment searchDepartment, HttpSession httpSession) {
public DeleteEmployeeController(DeleteEmployee deleteEmployee, SearchDepartment searchDepartment,
HttpSession httpSession) {
this.httpSession = httpSession;
this.deleteEmployee = deleteEmployee;
this.searchDepartment = searchDepartment;
}
/**
* 削除処理のコントローラ
*
* @param idEmployee 削除対象のemployeeId
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = DELETE_EMPLOYEE, method = RequestMethod.POST)
public String deleteEmployee(@RequestParam("idEmployee") Integer idEmployee, Model model) {
try {
// 部署一覧を取得し、画面に返す用のクラスに詰め替える
// 作成した部署一覧をセット
model.addAttribute(
PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(searchDepartment.selectAll())
);
DepartmentForm.convertFrom(searchDepartment.selectAll()));
// 削除処理実施
deleteExec(
model,
idEmployee,
LoginUserForm.convertTo((LoginUserForm) httpSession.getAttribute(SessionKeyword.LOGIN_USER)));
// 検索画面に遷移
return "forward:" + TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_INFO_FORM_EMPTY;
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラー画面遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
/**
* 削除処理実施
*
* @param model ビューにデータを渡すためのコンテナ
* @param idEmployee 削除対象のemployeeId
* @param loginUser ログインユーザー情報
*/
private void deleteExec(Model model, Integer idEmployee, LoginUser loginUser) {
final String DELETE_TARGET_ID_CAN_NOT_DELETE = "ログイン者自身は削除出来ません。";
final String DELETE_COMPLETE = "削除しました。";
// ログインしている人が、削除対象かチェック
if (Objects.equals(loginUser.getIdEmployee(), idEmployee)) {
// 削除対象だった場合
// エラーメッセージをセット
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DELETE_TARGET_ID_CAN_NOT_DELETE));
// 検索画面に遷移
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,
new MessageForm(DELETE_TARGET_ID_CAN_NOT_DELETE));
return;
}
// 削除を実施
deleteEmployee.deleteEmployee(idEmployee);
// 削除完了したメッセージをセット
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_INFO, new MessageForm(DELETE_COMPLETE));
}

View File

@@ -1,17 +1,18 @@
package com.example.rhizome.app.presentation.controller;
import com.example.rhizome.app.domain.service.impl.LoginImpl;
import com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword;
import com.example.rhizome.app.presentation.form.LoginUserForm;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.*;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.*;
import com.example.rhizome.app.domain.service.impl.LoginImpl;
import com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword;
import com.example.rhizome.app.presentation.form.LoginUserForm;
@Controller
public class IndexController {
@@ -28,23 +29,14 @@ public class IndexController {
return new LoginUserForm();
}
/**
* ログイン画面表示処理
*
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@RequestMapping(value = {INDEX_BLANK, INDEX_SLASH, INDEX_LOGIN})
@RequestMapping(value = { INDEX_BLANK, INDEX_SLASH, INDEX_LOGIN })
public String index(Model model) {
// セッション情報を破棄
if (httpSession != null) {
httpSession.invalidate();
}
// 空のFormをSessionに用意
model.addAttribute(PageReturnAttributeKeyword.LOGIN_INFO_FORM, loginUserForm());
// ログイン画面に遷移
return LOGIN_HTML;
}
}

View File

@@ -1,5 +1,20 @@
package com.example.rhizome.app.presentation.controller;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.*;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.rhizome.aop.aspect.PermissionCheck;
import com.example.rhizome.aop.aspect.SessionCheck;
import com.example.rhizome.app.domain.model.Employee;
@@ -12,30 +27,12 @@ import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTa
import com.example.rhizome.app.presentation.form.DepartmentForm;
import com.example.rhizome.app.presentation.form.InsertEmployeeForm;
import com.example.rhizome.app.presentation.form.MessageForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.INSERT_EMPLOYEE_CONFIRM;
/**
* 新規登録者情報チェック用のコントローラ
*/
@Controller
public class InsertEmployeeConfirmController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(InsertEmployeeConfirmController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
@@ -46,8 +43,7 @@ public class InsertEmployeeConfirmController {
public InsertEmployeeConfirmController(
HttpSession httpSession,
SearchDepartmentImpl searchDepartment,
SearchEmployeeImpl searchEmployee
) {
SearchEmployeeImpl searchEmployee) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
this.searchEmployee = searchEmployee;
@@ -58,76 +54,46 @@ public class InsertEmployeeConfirmController {
return new InsertEmployeeForm();
}
/**
* 登録情報のチェックを行う
*
* @param insertEmployeeForm 登録情報
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = INSERT_EMPLOYEE_CONFIRM, method = RequestMethod.POST)
public String insertEmployeeConfirm(InsertEmployeeForm insertEmployeeForm, Model model) {
try {
// 部署一覧を取得し、画面に返す用のクラスに詰め替える
// 作成した部署一覧をセット
model.addAttribute(
PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(
searchDepartment.selectAll()
));
searchDepartment.selectAll()));
// 登録用パラメータチェック
List<String> error = insertEmployeeForm.validateParameter();
// 入力内容に誤りがあるか
if (!error.isEmpty()) {
// 誤りがあった場合
// 誤りがあった箇所のエラーメッセージを表示
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(String.join(", ", error) + "が不正です。"));
// 画面に返す為の登録情報をセット
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,
new MessageForm(String.join(", ", error) + "が不正です。"));
model.addAttribute(PageReturnAttributeKeyword.INSERT_EMPLOYEE_FORM, insertEmployeeForm);
// 登録画面に遷移
return TransitionTargetPageNameKeyword.INSERT_EMPLOYEE_INPUT_HTML;
}
final String DUPLICATION = "既に登録済みのメールアドレスです。";
// 登録対象のメールアドレスを元に検索
List<Employee> employeeList = searchEmployee.searchEmployee(insertEmployeeForm.getMailAddress());
// 検索結果があるか
if (!employeeList.isEmpty()) {
// 検索した結果0件ではない場合
// 重複を許さないため、エラー処理
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION));
// 画面に返す為の登録情報をセット
model.addAttribute(PageReturnAttributeKeyword.INSERT_EMPLOYEE_FORM, insertEmployeeForm);
// 登録画面に遷移
return TransitionTargetPageNameKeyword.INSERT_EMPLOYEE_INPUT_HTML;
}
// 登録確認ページに遷移
model.addAttribute(PageReturnAttributeKeyword.INSERT_EMPLOYEE_FORM, insertEmployeeForm);
// 登録確認画面に遷移
return TransitionTargetPageNameKeyword.INSERT_EMPLOYEE_CONFIRM_HTML;
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラー画面遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
}

View File

@@ -1,5 +1,21 @@
package com.example.rhizome.app.presentation.controller;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.rhizome.aop.aspect.PermissionCheck;
import com.example.rhizome.aop.aspect.SessionCheck;
import com.example.rhizome.app.domain.service.RegisterEmployee;
@@ -11,31 +27,12 @@ import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTa
import com.example.rhizome.app.presentation.form.DepartmentForm;
import com.example.rhizome.app.presentation.form.InsertEmployeeForm;
import com.example.rhizome.app.presentation.form.MessageForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.INSERT_EMPLOYEE;
/**
* 新規登録用のコントローラ
*/
@Controller
public class InsertEmployeeController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(InsertEmployeeController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
@@ -43,7 +40,8 @@ public class InsertEmployeeController {
SearchDepartment searchDepartment;
@Autowired
public InsertEmployeeController(HttpSession httpSession, RegisterEmployeeImpl registerEmployee, SearchDepartmentImpl searchDepartment) {
public InsertEmployeeController(HttpSession httpSession, RegisterEmployeeImpl registerEmployee,
SearchDepartmentImpl searchDepartment) {
this.httpSession = httpSession;
this.registerEmployee = registerEmployee;
this.searchDepartment = searchDepartment;
@@ -54,80 +52,51 @@ public class InsertEmployeeController {
return new InsertEmployeeForm();
}
/**
* 新規登録画面表示処理
*
* @param insertEmployeeForm 登録情報
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = INSERT_EMPLOYEE, method = RequestMethod.POST)
public String insertEmployee(InsertEmployeeForm insertEmployeeForm, Model model) {
try {
// 部署一覧を取得し、画面に返す用のクラスに詰め替える
// 作成した部署一覧をセット
model.addAttribute(
PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(
searchDepartment.selectAll()
));
searchDepartment.selectAll()));
// Confirm画面から渡ってきた値をチェック
List<String> error = validateParameter(
insertEmployeeForm.getNmEmployee(),
insertEmployeeForm.getKnEmployee(),
insertEmployeeForm.getMailAddress(),
insertEmployeeForm.getPassword(),
insertEmployeeForm.getAdminAuth(),
insertEmployeeForm.getIdDepartment()
);
insertEmployeeForm.getIdDepartment());
// エラーが1件以上有る場合
if (!error.isEmpty()) {
// エラー内容を画面に表示
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(String.join(", ", error) + "が不正です。"));
// 登録画面に遷移
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,
new MessageForm(String.join(", ", error) + "が不正です。"));
return TransitionTargetPageNameKeyword.INSERT_EMPLOYEE_INPUT_HTML;
}
// 登録処理
registerEmployee.registerEmployee(InsertEmployeeForm.convertTo(insertEmployeeForm));
// 登録結果画面に遷移
return TransitionTargetPageNameKeyword.INSERT_EMPLOYEE_RESULT_HTML;
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラー画面遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
/**
* @param employeeName 登録対象の名前
* @param employeeKana 登録対象のカナ
* @param mailAddress 登録対象のメールアドレス
* @param password 登録対象のパスワード
* @param management システム権限
* @param departmentId 部署ID
* @return エラーが有った場合はエラー文言、無ければ空のリストが返る
*/
private List<String> validateParameter(
String employeeName,
String employeeKana,
String mailAddress,
String password,
Integer management,
Integer departmentId
) {
Integer departmentId) {
final String EMPLOYEE_NAME_NULL_BLANK = "社員名";
final String EMPLOYEE_KANA_NULL_BLANK = "社員名フリガナ";
@@ -138,39 +107,27 @@ public class InsertEmployeeController {
List<String> error = new ArrayList<>();
// null or 空文字判定
if (isNullOrBlank(employeeName)) {
// エラー内容を追加
error.add(EMPLOYEE_NAME_NULL_BLANK);
}
// null or 空文字判定
if (isNullOrBlank(employeeKana)) {
// エラー内容を追加
error.add(EMPLOYEE_KANA_NULL_BLANK);
}
// null or 空文字判定
if (isNullOrBlank(mailAddress)) {
// エラー内容を追加
error.add(MAIL_ADDRESS_NULL_BLANK);
}
// null or 空文字判定
if (isNullOrBlank(password)) {
// エラー内容を追加
error.add(PASSWORD_NULL_BLANK);
}
// 管理者権限の値が不正か
if (management == null) {
// エラー内容を追加
error.add(MANAGEMENT_ERROR);
}
// 所属部署の値が不正か
if (departmentId == null) {
// エラー内容を追加
error.add(DEPARTMENT_ID_NULL_BLANK);
}
@@ -178,12 +135,6 @@ public class InsertEmployeeController {
}
/**
* 仮引数を null or 空 判定する
*
* @param str 判定対象の文字列
* @return true:null or 空 false:null or 空 では無い
*/
private static boolean isNullOrBlank(String str) {
if (null == str) {
return true;
@@ -193,6 +144,3 @@ public class InsertEmployeeController {
}
}

View File

@@ -1,5 +1,19 @@
package com.example.rhizome.app.presentation.controller;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.rhizome.aop.aspect.PermissionCheck;
import com.example.rhizome.aop.aspect.SessionCheck;
import com.example.rhizome.app.domain.service.RegisterEmployee;
@@ -8,29 +22,12 @@ import com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAt
import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword;
import com.example.rhizome.app.presentation.form.DepartmentForm;
import com.example.rhizome.app.presentation.form.InsertEmployeeForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.INSERT_EMPLOYEE_INPUT;
/**
* 登録画面表示用コントローラ
*/
@Controller
public class InsertEmployeeInputController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(InsertEmployeeInputController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
@@ -38,7 +35,8 @@ public class InsertEmployeeInputController {
SearchDepartment searchDepartment;
@Autowired
public InsertEmployeeInputController(HttpSession httpSession, RegisterEmployee registerEmployee, SearchDepartment searchDepartment) {
public InsertEmployeeInputController(HttpSession httpSession, RegisterEmployee registerEmployee,
SearchDepartment searchDepartment) {
this.httpSession = httpSession;
this.registerEmployee = registerEmployee;
this.searchDepartment = searchDepartment;
@@ -49,42 +47,26 @@ public class InsertEmployeeInputController {
return new InsertEmployeeForm();
}
/**
* 登録画面表示処理
*
* @param insertEmployeeForm 登録画面のフォーム
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@SessionCheck
@PermissionCheck
@RequestMapping(value = INSERT_EMPLOYEE_INPUT, method = RequestMethod.POST)
public String insertInput(InsertEmployeeForm insertEmployeeForm, Model model) {
try {
// 部署コードリストを作成し、画面に返す値としてセット
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, DepartmentForm.convertFrom(searchDepartment.selectAll()));
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(searchDepartment.selectAll()));
// パラメータをセット
model.addAttribute(PageReturnAttributeKeyword.INSERT_EMPLOYEE_FORM, insertEmployeeForm);
// 登録画面に遷移
return TransitionTargetPageNameKeyword.INSERT_EMPLOYEE_INPUT_HTML;
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラー画面遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
}

View File

@@ -1,5 +1,18 @@
package com.example.rhizome.app.presentation.controller;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.example.rhizome.app.domain.model.LoginInfo;
import com.example.rhizome.app.domain.model.LoginUser;
@@ -11,29 +24,12 @@ import com.example.rhizome.app.presentation.form.LoginInfoForm;
import com.example.rhizome.app.presentation.form.LoginUserForm;
import com.example.rhizome.app.presentation.form.MessageForm;
import com.example.rhizome.app.presentation.form.UpdatePersonalUserForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.DO_LOGIN;
/**
* ログイン関連のコントローラ
*/
@Controller
public class LoginController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(LoginController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
@@ -50,99 +46,59 @@ public class LoginController {
return new LoginUserForm();
}
/**
* 画面から渡ってきたパラメータのチェックを行う
*
* @param loginInfoForm mailAddress, password を保持したインスタンス
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@RequestMapping(value = DO_LOGIN, method = RequestMethod.POST)
public String doLogin(LoginInfoForm loginInfoForm, Model model) {
// ログに出力する
final String MAIL_ADDRESS_PASSWORD_ERROR = "メールアドレス、パスワードの入力内容に誤りがあります";
// 発生しうるErrorに対するメッセージ
final String LOGIN_ERROR = "IDまたはパスワードが間違っています";
try {
// mailAddress, password の入力に誤りがあるかチェック
if (!LoginUserForm.checkParameter(loginInfoForm)) {
// mail,passwordが仕様を満たしていない場合
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(MAIL_ADDRESS_PASSWORD_ERROR));
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,
new MessageForm(MAIL_ADDRESS_PASSWORD_ERROR));
// エラーだった場合に login.html に処理を戻す。mailAddress, password を login.htmlに表示するために
// ここで addAttribute する
model.addAttribute(PageReturnAttributeKeyword.LOGIN_INFO_FORM, loginInfoForm);
// login.html に遷移する様に設定
return TransitionTargetPageNameKeyword.LOGIN_HTML;
}
// Login者情報インスタンスを作成
LoginInfo loginInfo = createLoginInfo(httpSession, loginInfoForm);
// login 可能かチェック
if (!login.canLogin(loginInfo)) {
// ログイン不可の場合、エラーメッセージをセットして入力された値とともに login.html を再表示
// 画面に表示するエラーメッセージをセット
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(LOGIN_ERROR));
// 画面に表示する mailAddress, password をセット
model.addAttribute(PageReturnAttributeKeyword.LOGIN_INFO_FORM, loginInfoForm);
// login.html に遷移
return TransitionTargetPageNameKeyword.LOGIN_HTML;
}
// ログイン(インスタンス作成)
LoginUser loginUser = login.doLogin(loginInfo);
// ログイン者の情報をセッション情報に格納
// セッションに loginUser 情報を保持し続ける = ログイン中として扱う
httpSession.setAttribute(SessionKeyword.LOGIN_USER, LoginUserForm.convertFrom(loginUser));
// 管理者かチェック
if (!loginUser.isAdminUser()) {
// 管理者ではない場合直で自分の情報を更新するページに遷移する
// 編集対象のログイン者の情報を画面に渡す値にセット
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM, UpdatePersonalUserForm.convertFrom(loginUser));
// 個人の情報更新ページに遷移
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM,
UpdatePersonalUserForm.convertFrom(loginUser));
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_EMPLOYEE_INPUT_HTML;
}
// メニュー画面に遷移
return TransitionTargetPageNameKeyword.MENU_HTML;
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラー画面に遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
/**
* ログイン者情報のインスタンスを作成
*
* @param httpSession セッション情報
* @param loginInfoForm mailAddress, password を保持したインスタンス
*/
private static LoginInfo createLoginInfo(HttpSession httpSession, LoginInfoForm loginInfoForm) {
// セッションからログイン者情報を取得
LoginUserForm loginUserForm = (LoginUserForm) httpSession.getAttribute(SessionKeyword.LOGIN_USER);
// セッションにログイン者情報があるか
if (loginUserForm != null) {
// セッションにログイン者情報がある場合
return new LoginInfo(loginUserForm.getMailAddress(), loginUserForm.getPassword());
}
// セッションにない場合は画面からの input 値 を取得
return new LoginInfo(
loginInfoForm.getMailAddress(),
loginInfoForm.getPassword()
);
loginInfoForm.getPassword());
}
}

View File

@@ -1,10 +1,12 @@
package com.example.rhizome.app.presentation.controller;
import com.example.rhizome.aop.aspect.PermissionCheck;
import com.example.rhizome.aop.aspect.SessionCheck;
import com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword;
import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword;
import com.example.rhizome.app.presentation.form.LoginUserForm;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@@ -12,21 +14,16 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.http.HttpSession;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.example.rhizome.aop.aspect.PermissionCheck;
import com.example.rhizome.aop.aspect.SessionCheck;
import com.example.rhizome.app.presentation.controller.pageproperty.PageReturnAttributeKeyword;
import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword;
import com.example.rhizome.app.presentation.form.LoginUserForm;
import static com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword.LOGOUT;
/**
* ログアウト処理のコントローラ
*/
@Controller
public class LogoutController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(LogoutController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
@@ -41,30 +38,19 @@ public class LogoutController {
return new LoginUserForm();
}
/**
* ログアウト処理
*
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = LOGOUT, method = RequestMethod.POST)
public String doLogout(Model model) {
try {
// セッション情報の破棄
httpSession.invalidate();
// ログイン画面に遷移するための情報を設定
model.addAttribute(PageReturnAttributeKeyword.LOGIN_INFO_FORM, loginUserForm());
// login.html に遷移
return TransitionTargetPageNameKeyword.LOGIN_HTML;
} catch (Exception e) {
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラーページに遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}

View File

@@ -1,16 +1,14 @@
package com.example.rhizome.app.presentation.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import com.example.rhizome.aop.aspect.PermissionCheck;
import com.example.rhizome.aop.aspect.SessionCheck;
import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword;
import com.example.rhizome.app.presentation.form.MessageForm;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* メニュー画面表示用コントローラ
*/
@Controller
public class MenuController {
@@ -19,14 +17,10 @@ public class MenuController {
return new MessageForm();
}
/**
* メニュー画面に遷移
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.SHOW_MENU)
public String showMenu() {
// メニュー画面遷移
return TransitionTargetPageNameKeyword.MENU_HTML;
}