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

This commit is contained in:
s-choi
2026-09-10 15:50:51 +09:00
parent d21e3eee9f
commit e6de486548
4 changed files with 300 additions and 440 deletions

View File

@@ -1,5 +1,17 @@
package com.example.rhizome.app.presentation.controller;
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.SearchDepartment;
@@ -9,113 +21,66 @@ import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTa
import com.example.rhizome.app.presentation.form.DepartmentForm;
import com.example.rhizome.app.presentation.form.EmployeeInfoForm;
import com.example.rhizome.app.presentation.form.SearchConditionEmployeeForm;
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;
/**
* 社員検索画面用コントローラ リスト全件表示、検索表示、更新、削除が可能
*/
@Controller
public class SelectEmployeeController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(SelectEmployeeController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
private final static Logger LOGGER = Logger.getLogger(SelectEmployeeController.class.getName());
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
SearchDepartment searchDepartment;
SearchEmployeeInfo searchEmployeeInfo;
HttpSession httpSession;
SearchDepartment searchDepartment;
SearchEmployeeInfo searchEmployeeInfo;
@Autowired
public SelectEmployeeController(HttpSession httpSession, SearchDepartment searchDepartment, SearchEmployeeInfo searchEmployeeInfo) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
this.searchEmployeeInfo = searchEmployeeInfo;
}
@Autowired
public SelectEmployeeController(HttpSession httpSession, SearchDepartment searchDepartment,
SearchEmployeeInfo searchEmployeeInfo) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
this.searchEmployeeInfo = searchEmployeeInfo;
}
@ModelAttribute
public SearchConditionEmployeeForm searchConditionEmployeeForm() {
return new SearchConditionEmployeeForm();
}
@ModelAttribute
public SearchConditionEmployeeForm searchConditionEmployeeForm() {
return new SearchConditionEmployeeForm();
}
/**
* 社員検索画面表示処理コントローラ
*
* @param searchConditionEmployeeForm 検索画面のフォーム
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_INFO, method = RequestMethod.POST)
public String select(SearchConditionEmployeeForm searchConditionEmployeeForm, Model model) {
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_INFO, method = RequestMethod.POST)
public String select(SearchConditionEmployeeForm searchConditionEmployeeForm, Model model) {
try {
try {
model.addAttribute(
PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(searchDepartment.selectAll()));
// 部署一覧を取得し、画面に返す用のクラスに詰め替える
// 作成した部署一覧をセット
model.addAttribute(
PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(searchDepartment.selectAll())
);
model.addAttribute(PageReturnAttributeKeyword.EMPLOYEE_INFO_FORM_LIST, EmployeeInfoForm.convertFrom(
searchEmployeeInfo.searchBy(SearchConditionEmployeeForm.convertTo(searchConditionEmployeeForm))));
return TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_HTML;
// 検索条件を元に検索を実施
// 取得した EmployeeInfo( employee + department )の情報を画面に返す用の型に詰め替えてセット
model.addAttribute(PageReturnAttributeKeyword.EMPLOYEE_INFO_FORM_LIST, EmployeeInfoForm.convertFrom(searchEmployeeInfo.searchBy(SearchConditionEmployeeForm.convertTo(searchConditionEmployeeForm))));
// 検索ページに遷移
return TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_HTML;
} catch (Exception e) {
httpSession.invalidate();
e.printStackTrace();
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
e.printStackTrace();
}
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラー画面遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
@RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_INFO_FORM_EMPTY, method = RequestMethod.POST)
public String select(Model model) {
model.addAttribute(
PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(searchDepartment.selectAll()));
}
model.addAttribute(PageReturnAttributeKeyword.EMPLOYEE_INFO_FORM_LIST,
EmployeeInfoForm.convertFrom(
searchEmployeeInfo.searchBy(
SearchConditionEmployeeForm.convertTo(searchConditionEmployeeForm()))));
/**
* 社員検索画面表示処理コントローラ
* 検索条件無しの状態を作るため、空の検索条件で検索した結果を画面に返す
*
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_INFO_FORM_EMPTY, method = RequestMethod.POST)
public String select(Model model) {
// 部署一覧を取得し、画面に返す用のクラスに詰め替える
// 作成した部署一覧をセット
model.addAttribute(
PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(searchDepartment.selectAll())
);
// 検索条件を元に検索を実施
model.addAttribute(PageReturnAttributeKeyword.EMPLOYEE_INFO_FORM_LIST,
EmployeeInfoForm.convertFrom(
searchEmployeeInfo.searchBy(
SearchConditionEmployeeForm.convertTo(searchConditionEmployeeForm())
)));
// 検索ページに遷移
return TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_HTML;
}
return TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_HTML;
}
}

View File

@@ -1,5 +1,19 @@
package com.example.rhizome.app.presentation.controller;
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 org.springframework.web.bind.annotation.RequestParam;
import com.example.rhizome.aop.aspect.PermissionCheck;
import com.example.rhizome.aop.aspect.SessionCheck;
import com.example.rhizome.app.domain.model.EmployeeInfo;
@@ -12,96 +26,66 @@ import com.example.rhizome.app.presentation.form.DepartmentForm;
import com.example.rhizome.app.presentation.form.MessageForm;
import com.example.rhizome.app.presentation.form.SearchConditionEmployeeForm;
import com.example.rhizome.app.presentation.form.UpdateEmployeeInfoForm;
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 org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* 更新対象の employee が存在するか確認する為のコントローラ
*/
@Controller
public class UpdateEmployeeController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(UpdateEmployeeController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
private final static Logger LOGGER = Logger.getLogger(UpdateEmployeeController.class.getName());
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
SearchDepartment searchDepartment;
SearchEmployeeInfo searchEmployeeInfo;
HttpSession httpSession;
SearchDepartment searchDepartment;
SearchEmployeeInfo searchEmployeeInfo;
@Autowired
public UpdateEmployeeController(HttpSession httpSession, SearchDepartment searchDepartment, SearchEmployeeInfo searchEmployeeInfo) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
this.searchEmployeeInfo = searchEmployeeInfo;
}
@Autowired
public UpdateEmployeeController(HttpSession httpSession, SearchDepartment searchDepartment,
SearchEmployeeInfo searchEmployeeInfo) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
this.searchEmployeeInfo = searchEmployeeInfo;
}
@ModelAttribute
public SearchConditionEmployeeForm searchConditionEmployeeForm() {
return new SearchConditionEmployeeForm();
}
@ModelAttribute
public SearchConditionEmployeeForm searchConditionEmployeeForm() {
return new SearchConditionEmployeeForm();
}
/**
* 更新対象の employee が存在するか確認する為のコントローラ
*
* @param idEmployee 更新対象の employee の id
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.SHOW_UPDATE_EMPLOYEE, method = RequestMethod.POST)
public String showUpdateEmployee(@RequestParam("idEmployee") Integer idEmployee, Model model) {
final String UPDATE_TARGET_ID_NOT_FOUND = "更新対象のデータが見つかりません。";
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.SHOW_UPDATE_EMPLOYEE, method = RequestMethod.POST)
public String showUpdateEmployee(@RequestParam("idEmployee") Integer idEmployee, Model model) {
final String UPDATE_TARGET_ID_NOT_FOUND = "更新対象のデータが見つかりません。";
try {
// 部署一覧を取得し画面に返す値としてセット
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, DepartmentForm.convertFrom(searchDepartment.selectAll()));
try {
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(searchDepartment.selectAll()));
// id を条件に更新対象のレコードを検索、取得する
List<EmployeeInfo> employeeInfoList = searchEmployeeInfo.searchBy(
new SearchConditionEmployee(idEmployee,
null,
null,
null,
null));
List<EmployeeInfo> employeeInfoList = searchEmployeeInfo.searchBy(
new SearchConditionEmployee(idEmployee,
null,
null,
null,
null));
// DBに更新対象のデータがあるか
if (employeeInfoList.isEmpty()) {
// ない場合はエラーメッセージをセットして検索画面に戻る
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(UPDATE_TARGET_ID_NOT_FOUND));
return "forward:" + TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_INFO;
}
if (employeeInfoList.isEmpty()) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,
new MessageForm(UPDATE_TARGET_ID_NOT_FOUND));
return "forward:" + TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_INFO;
}
// 更新画面に遷移
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, UpdateEmployeeInfoForm.convertFrom(employeeInfoList.get(0)));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM,
UpdateEmployeeInfoForm.convertFrom(employeeInfoList.get(0)));
// Update画面に遷移
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
} catch (Exception e) {
httpSession.invalidate();
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
e.printStackTrace();
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
return TransitionTargetPageNameKeyword.ERROR_HTML;
// エラーログ出力
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 java.util.List;
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.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;
@@ -15,192 +28,126 @@ 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 com.example.rhizome.app.presentation.form.UpdateEmployeeInfoForm;
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.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* employee を更新して結果を返す為のコントローラ
*/
@Controller
public class UpdateEmployeeResultController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(UpdateEmployeeController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
private final static Logger LOGGER = Logger.getLogger(UpdateEmployeeController.class.getName());
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
SearchDepartment searchDepartment;
SearchEmployee searchEmployee;
UpdateEmployee updateEmployee;
HttpSession httpSession;
SearchDepartment searchDepartment;
SearchEmployee searchEmployee;
UpdateEmployee updateEmployee;
@Autowired
public UpdateEmployeeResultController(HttpSession httpSession, SearchDepartment searchDepartment, SearchEmployee searchEmployee, UpdateEmployee updateEmployee) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
this.searchEmployee = searchEmployee;
this.updateEmployee = updateEmployee;
}
@Autowired
public UpdateEmployeeResultController(HttpSession httpSession, SearchDepartment searchDepartment,
SearchEmployee searchEmployee, UpdateEmployee updateEmployee) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
this.searchEmployee = searchEmployee;
this.updateEmployee = updateEmployee;
}
/**
* メールアドレスの重複チェック
*
* @param employeeList 元の employeeList
* @param updateEmployeeInfoForm 更新対象のデータ
* @return true:重複無し false:重複あり
*/
private static boolean checkDuplicateMailAddress(List<Employee> employeeList, UpdateEmployeeInfoForm updateEmployeeInfoForm) {
// メールアドレスが既に使われていた場合
if (!employeeList.isEmpty()) {
// 重複が有った場合に、同一人物のものであれば上書きOKとする
return Objects.equals(employeeList.get(0).getIdEmployee(), updateEmployeeInfoForm.getIdEmployee());
}
return true;
}
private static boolean checkDuplicateMailAddress(List<Employee> employeeList,
UpdateEmployeeInfoForm updateEmployeeInfoForm) {
if (!employeeList.isEmpty()) {
return Objects.equals(employeeList.get(0).getIdEmployee(), updateEmployeeInfoForm.getIdEmployee());
}
return true;
}
/**
* 更新結果画面表示処理
*
* @param updateEmployeeInfoForm 更新画面のフォーム
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_RESULT, method = RequestMethod.POST)
public String employeeUpdateResult(@ModelAttribute UpdateEmployeeInfoForm updateEmployeeInfoForm, Model model) {
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_RESULT, method = RequestMethod.POST)
public String employeeUpdateResult(@ModelAttribute UpdateEmployeeInfoForm updateEmployeeInfoForm, Model model) {
final String DUPLICATION = "既に登録済みのメールアドレスです。";
final String NOTHING = "項目が空欄です。";
final String ADMIN_USER = "更新対象者がログイン者のため、管理者権限を変更できません。";
final String NO_TARGET = "更新対象のデータが見つかりません。";
final String DUPLICATION = "既に登録済みのメールアドレスです。";
final String NOTHING = "項目が空欄です。";
final String ADMIN_USER = "更新対象者がログイン者のため、管理者権限を変更できません。";
final String NO_TARGET = "更新対象のデータが見つかりません。";
final String UPDATE_COMPLETE = "更新が完了しました。";
final String UPDATE_COMPLETE = "更新が完了しました。";
try {
try {
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(searchDepartment.selectAll()));
// 部署一覧取得
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, DepartmentForm.convertFrom(searchDepartment.selectAll()));
String error = UpdateEmployeeInfoForm.validatePassword(
updateEmployeeInfoForm.getPassword(),
updateEmployeeInfoForm.getPasswordConfirm());
// パスワードが仕様を満たしているか確認
String error = UpdateEmployeeInfoForm.validatePassword(
updateEmployeeInfoForm.getPassword(),
updateEmployeeInfoForm.getPasswordConfirm()
);
if (!error.isBlank()) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(error));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
// エラーが存在するか
if (!error.isBlank()) {
// エラーが存在する場合、更新画面に遷移
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(error));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
List<Employee> employeeList = searchEmployee.searchEmployee(updateEmployeeInfoForm.getMailAddress());
// メールアドレスを元に検索
List<Employee> employeeList = searchEmployee.searchEmployee(updateEmployeeInfoForm.getMailAddress());
if (!checkDuplicateMailAddress(employeeList, updateEmployeeInfoForm)) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
// 既に登録済みのメールアドレスが存在するか
if (!checkDuplicateMailAddress(employeeList, updateEmployeeInfoForm)) {
// 存在する場合はエラーメッセージを設定して更新入力画面に遷移
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
LoginUserForm loginUserForm = (LoginUserForm) httpSession.getAttribute(SessionKeyword.LOGIN_USER);
// session 情報からログイン者の情報を取得
LoginUserForm loginUserForm = (LoginUserForm) httpSession.getAttribute(SessionKeyword.LOGIN_USER);
if (isUpdateTargetLoginUserAdminToNormal(updateEmployeeInfoForm, loginUserForm)) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(ADMIN_USER));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
// ログイン者が更新対象で、権限を管理者権限から一般権限に変更しようとしているかチェック
if (isUpdateTargetLoginUserAdminToNormal(updateEmployeeInfoForm, loginUserForm)) {
// 存在する場合はエラーメッセージを設定して更新入力画面に遷移
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(ADMIN_USER));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
if (!UpdateEmployeeInfoForm.hasErrorParameter(updateEmployeeInfoForm)) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NOTHING));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
// 画面から渡ってきた値が必須を満たしているか
if (!UpdateEmployeeInfoForm.hasErrorParameter(updateEmployeeInfoForm)) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NOTHING));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
Employee beforeEmployee = searchEmployee.searchEmployeeByIdEmployee(updateEmployeeInfoForm.getIdEmployee());
if (beforeEmployee.getIdEmployee() == null) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NO_TARGET));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
// 更新前のデータを取得
Employee beforeEmployee = searchEmployee.searchEmployeeByIdEmployee(updateEmployeeInfoForm.getIdEmployee());
if (beforeEmployee.getIdEmployee() == null) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NO_TARGET));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_EMPLOYEE_INFO_FORM, updateEmployeeInfoForm);
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
return TransitionTargetPageNameKeyword.UPDATE_EMPLOYEE_INPUT_HTML;
}
updateEmployee.updateEmployee(beforeEmployee, UpdateEmployeeInfoForm.convertTo(updateEmployeeInfoForm));
// 画面用の型からビジネスロジック用のモデルに変換し、Updateを実行
updateEmployee.updateEmployee(beforeEmployee, UpdateEmployeeInfoForm.convertTo(updateEmployeeInfoForm));
LoginUser updatedTargetEmployee = searchEmployee.searchLoginUser(updateEmployeeInfoForm.getMailAddress());
// Update後の情報を再取得
LoginUser updatedTargetEmployee = searchEmployee.searchLoginUser(updateEmployeeInfoForm.getMailAddress());
if (Objects.equals(updatedTargetEmployee.getIdEmployee(), loginUserForm.getIdEmployee())) {
httpSession.setAttribute(SessionKeyword.LOGIN_USER, LoginUserForm.convertFrom(updatedTargetEmployee));
}
// ログイン者と更新した対象が同じアカウントだった場合、session情報を更新
if (Objects.equals(updatedTargetEmployee.getIdEmployee(), loginUserForm.getIdEmployee())) {
// ログイン者の情報をセッション情報に格納
httpSession.setAttribute(SessionKeyword.LOGIN_USER, LoginUserForm.convertFrom(updatedTargetEmployee));
}
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_INFO, new MessageForm(UPDATE_COMPLETE));
// 更新結果のメッセージを画面にセットし一覧画面に遷移
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_INFO, new MessageForm(UPDATE_COMPLETE));
return "forward:" + TransitionTargetPageNameKeyword.SELECT_EMPLOYEE_INFO_FORM_EMPTY;
// forward 先で不用意な動作をしないために UpdateEmployeeInfoForm をクリア
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;
}
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
}
e.printStackTrace();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラー画面遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
private boolean isUpdateTargetLoginUserAdminToNormal(UpdateEmployeeInfoForm updateEmployeeInfoForm,
LoginUserForm loginUserForm) {
}
if (!Objects.equals(updateEmployeeInfoForm.getIdEmployee(), loginUserForm.getIdEmployee())) {
return false;
}
/**
* ログイン者がログイン者自身を更新する際
* 管理者権限から一般権限に変更する内容も含まれているかチェックする
*
* @param updateEmployeeInfoForm 更新対象データ
* @param loginUserForm ログイン者情報
* @return true:ログイン者自身のデータをUpdate管理者→一般に権限変更しようとしている。
* false:Update対象者がログイン者自身ではない or 管理者権限 → 一般権限 への更新は含まれていない
*/
private boolean isUpdateTargetLoginUserAdminToNormal(UpdateEmployeeInfoForm updateEmployeeInfoForm, LoginUserForm loginUserForm) {
// ログイン者が今回の更新対象か
if (!Objects.equals(updateEmployeeInfoForm.getIdEmployee(), loginUserForm.getIdEmployee())) {
// 今回の対象者はログイン者ではない
return false;
}
// 管理者から一般権限への変更があるか
return updateEmployeeInfoForm.getAdminAuth().equals(0);
}
return updateEmployeeInfoForm.getAdminAuth().equals(0);
}
}

View File

@@ -1,5 +1,18 @@
package com.example.rhizome.app.presentation.controller;
import java.util.List;
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.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;
@@ -12,152 +25,103 @@ import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTa
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.List;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* 個人情報更新用コントローラ
*/
@Controller
public class UpdatePersonalEmployeeResultController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(UpdatePersonalEmployeeResultController.class.getName());
private final static Logger LOGGER = Logger.getLogger(UpdatePersonalEmployeeResultController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
SearchEmployee searchEmployee;
UpdateEmployee updateEmployee;
HttpSession httpSession;
SearchEmployee searchEmployee;
UpdateEmployee updateEmployee;
@Autowired
public UpdatePersonalEmployeeResultController(HttpSession httpSession, SearchEmployee searchEmployee, UpdateEmployee updateEmployee) {
this.httpSession = httpSession;
this.searchEmployee = searchEmployee;
this.updateEmployee = updateEmployee;
}
@Autowired
public UpdatePersonalEmployeeResultController(HttpSession httpSession, SearchEmployee searchEmployee,
UpdateEmployee updateEmployee) {
this.httpSession = httpSession;
this.searchEmployee = searchEmployee;
this.updateEmployee = updateEmployee;
}
/**
* メールアドレスの重複チェック
*
* @param employeeList 元の employeeList
* @param updatePersonalUserForm 更新対象のデータ
* @return true:重複無し false:重複あり
*/
private static boolean checkDuplicateMailAddress(List<Employee> employeeList, UpdatePersonalUserForm updatePersonalUserForm) {
// メールアドレスが既に使われていた場合
if (!employeeList.isEmpty()) {
// 更新対象のデータと同じIDの場合は重複していないとみなす
return Objects.equals(employeeList.get(0).getIdEmployee(), updatePersonalUserForm.getIdEmployee());
}
return true;
}
private static boolean checkDuplicateMailAddress(List<Employee> employeeList,
UpdatePersonalUserForm updatePersonalUserForm) {
if (!employeeList.isEmpty()) {
return Objects.equals(employeeList.get(0).getIdEmployee(), updatePersonalUserForm.getIdEmployee());
}
return true;
}
@ModelAttribute
public UpdatePersonalUserForm personalUpdateUserForm() {
return new UpdatePersonalUserForm();
}
@ModelAttribute
public UpdatePersonalUserForm personalUpdateUserForm() {
return new UpdatePersonalUserForm();
}
/**
* 個人情報更新結果画面表示処理
*
* @param updatePersonalUserForm 個人情報更新画面のフォーム
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.PERSONAL_UPDATE_RESULT, method = RequestMethod.POST)
public String personalEmployeeUpdateResult(UpdatePersonalUserForm updatePersonalUserForm, Model model) {
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.PERSONAL_UPDATE_RESULT, method = RequestMethod.POST)
public String personalEmployeeUpdateResult(UpdatePersonalUserForm updatePersonalUserForm, Model model) {
final String NOTHING = "項目が空欄です。";
final String DUPLICATION = "既に登録済みのメールアドレスです。";
final String NOTHING = "項目が空欄です。";
final String DUPLICATION = "既に登録済みのメールアドレスです。";
try {
try {
// 更新完了画面からの遷移の場合
if (updatePersonalUserForm.getIdEmployee() == null) {
// ログイン者の情報を取得
LoginUserForm loginUserForm = (LoginUserForm) httpSession.getAttribute(SessionKeyword.LOGIN_USER);
// ログイン者の情報を元に個人情報更新画面の初期表示用のインスタンスを作成
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM, UpdatePersonalUserForm.convertFrom(searchEmployee.searchLoginUser(loginUserForm.getMailAddress())));
if (updatePersonalUserForm.getIdEmployee() == null) {
LoginUserForm loginUserForm = (LoginUserForm) httpSession.getAttribute(SessionKeyword.LOGIN_USER);
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM, UpdatePersonalUserForm
.convertFrom(searchEmployee.searchLoginUser(loginUserForm.getMailAddress())));
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_EMPLOYEE_INPUT_HTML;
}
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_EMPLOYEE_INPUT_HTML;
}
// パスワードが仕様を満たしているかチェック
String error = UpdatePersonalUserForm.validatePassword(
updatePersonalUserForm.getPassword(),
updatePersonalUserForm.getPasswordConfirm()
);
String error = UpdatePersonalUserForm.validatePassword(
updatePersonalUserForm.getPassword(),
updatePersonalUserForm.getPasswordConfirm());
// エラーがあるか
if (!error.isBlank()) {
// エラーが有る場合メッセージをセットして個人情報更新入力画面に遷移
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(error));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM, updatePersonalUserForm);
if (!error.isBlank()) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(error));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM,
updatePersonalUserForm);
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_EMPLOYEE_INPUT_HTML;
}
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_EMPLOYEE_INPUT_HTML;
}
// 入力必須を満たしているか
if (!UpdatePersonalUserForm.hasErrorParameter(updatePersonalUserForm)) {
// 満たしていない場合、メッセージをセットして個人情報更新入力画面に遷移
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NOTHING));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM, updatePersonalUserForm);
if (!UpdatePersonalUserForm.hasErrorParameter(updatePersonalUserForm)) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NOTHING));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM,
updatePersonalUserForm);
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_EMPLOYEE_INPUT_HTML;
}
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_EMPLOYEE_INPUT_HTML;
}
// メールアドレス重複チェック
if (!checkDuplicateMailAddress(
searchEmployee.searchEmployee(updatePersonalUserForm.getMailAddress()),
updatePersonalUserForm)
) {
// メールアドレスが既に使われていた場合
// 個人情報更新ページに遷移
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM, updatePersonalUserForm);
if (!checkDuplicateMailAddress(
searchEmployee.searchEmployee(updatePersonalUserForm.getMailAddress()),
updatePersonalUserForm)) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_PERSONAL_EMPLOYEE_USER_FORM,
updatePersonalUserForm);
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_EMPLOYEE_INPUT_HTML;
}
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_EMPLOYEE_INPUT_HTML;
}
//更新前のEmployeeの情報を取得
// 画面用のインスタンスから更新用のインスタンスに変換し、Update実施
updateEmployee.updateEmployee(
searchEmployee.searchEmployeeByIdEmployee(updatePersonalUserForm.getIdEmployee()),
UpdatePersonalUserForm.convertTo(updatePersonalUserForm)
);
updateEmployee.updateEmployee(
searchEmployee.searchEmployeeByIdEmployee(updatePersonalUserForm.getIdEmployee()),
UpdatePersonalUserForm.convertTo(updatePersonalUserForm));
// ログイン者の情報をセッション情報に格納
httpSession.setAttribute(
SessionKeyword.LOGIN_USER,
LoginUserForm.convertFrom(
searchEmployee.searchLoginUser(updatePersonalUserForm.getMailAddress()))
);
httpSession.setAttribute(
SessionKeyword.LOGIN_USER,
LoginUserForm.convertFrom(
searchEmployee.searchLoginUser(updatePersonalUserForm.getMailAddress())));
// 更新結果画面に遷移
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_RESULT_HTML;
return TransitionTargetPageNameKeyword.UPDATE_PERSONAL_RESULT_HTML;
} catch (Exception e) {
// ログイン情報の破棄
httpSession.invalidate();
} catch (Exception e) {
httpSession.invalidate();
// エラーログ出力
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
// エラー画面遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
}