From 3b06b23703e63d519780bbfef3d2e37b7f96994d Mon Sep 17 00:00:00 2001 From: n-kawahito Date: Thu, 10 Sep 2026 15:23:00 +0900 Subject: [PATCH] =?UTF-8?q?[ADD]=E9=83=A8=E7=BD=B2=E6=96=B0=E8=A6=8F?= =?UTF-8?q?=E7=99=BB=E9=8C=B2=E6=A9=9F=E8=83=BD=E3=81=AE=E5=AE=9F=E6=96=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/RegisterDepartmentImpl.java | 35 ++++ .../InsertDepartmentConfirmController.java | 131 +++++++++++++++ .../InsertDepartmentController.java | 149 ++++++++++++++++++ .../InsertDepartmentInputController.java | 93 +++++++++++ .../form/InsertDepartmentForm.java | 69 ++++++++ .../templates/insertDepartmentConfirm.html | 87 ++++++++++ .../templates/insertDepartmentInput.html | 68 ++++++++ .../templates/insertDepartmentResult.html | 40 +++++ 8 files changed, 672 insertions(+) create mode 100644 src/main/java/com/example/rhizome/app/domain/service/impl/RegisterDepartmentImpl.java create mode 100644 src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentConfirmController.java create mode 100644 src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentController.java create mode 100644 src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentInputController.java create mode 100644 src/main/java/com/example/rhizome/app/presentation/form/InsertDepartmentForm.java create mode 100644 src/main/resources/templates/insertDepartmentConfirm.html create mode 100644 src/main/resources/templates/insertDepartmentInput.html create mode 100644 src/main/resources/templates/insertDepartmentResult.html diff --git a/src/main/java/com/example/rhizome/app/domain/service/impl/RegisterDepartmentImpl.java b/src/main/java/com/example/rhizome/app/domain/service/impl/RegisterDepartmentImpl.java new file mode 100644 index 0000000..3911f7c --- /dev/null +++ b/src/main/java/com/example/rhizome/app/domain/service/impl/RegisterDepartmentImpl.java @@ -0,0 +1,35 @@ +package com.example.rhizome.app.domain.service.impl; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import com.example.rhizome.app.domain.model.Department; +import com.example.rhizome.app.domain.service.RegisterDepartment; +import com.example.rhizome.app.infra.entity.DepartmentEntity; +import com.example.rhizome.app.infra.repository.DepartmentRepository; + +/** + * Employee テーブルの登録系の機能を集約させるサービスクラスです。 + */ +@Service +@Transactional +public class RegisterDepartmentImpl implements RegisterDepartment { + + DepartmentRepository departmentRepository; + + @Autowired + public RegisterDepartmentImpl(DepartmentRepository departmentRepository) { + this.departmentRepository = departmentRepository; + } + + /** + * employee インスタンスの内容を登録する + * + * @param employee 登録対象のデータ + */ + public void registerDepartment(Department department) { + departmentRepository.save(DepartmentEntity.convertFrom(department)); + } +} diff --git a/src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentConfirmController.java b/src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentConfirmController.java new file mode 100644 index 0000000..18f6e26 --- /dev/null +++ b/src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentConfirmController.java @@ -0,0 +1,131 @@ +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.Department; +import com.example.rhizome.app.domain.service.SearchDepartment; +import com.example.rhizome.app.domain.service.SearchEmployee; +import com.example.rhizome.app.domain.service.impl.SearchDepartmentImpl; +import com.example.rhizome.app.domain.service.impl.SearchEmployeeImpl; +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.DepartmentForm; +import com.example.rhizome.app.presentation.form.InsertDepartmentForm; +import com.example.rhizome.app.presentation.form.MessageForm; + +/** + * 新規登録者情報チェック用のコントローラ + */ +@Controller +public class InsertDepartmentConfirmController { + + // ログ出力用インスタンス + private final static Logger LOGGER = Logger.getLogger(InsertEmployeeConfirmController.class.getName()); + + // エラーメッセージ用の定数フィールド + private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; + + HttpSession httpSession; + SearchDepartment searchDepartment; + SearchEmployee searchEmployee; + + @Autowired + public InsertDepartmentConfirmController( + HttpSession httpSession, + SearchDepartmentImpl searchDepartment, + SearchEmployeeImpl searchEmployee) { + this.httpSession = httpSession; + this.searchDepartment = searchDepartment; + this.searchEmployee = searchEmployee; + } + + @ModelAttribute + public InsertDepartmentForm insertDepartmentForm() { + return new InsertDepartmentForm(); + } + + /** + * 登録情報のチェックを行う + * + * @param insertEmployeeForm 登録情報 + * @param model ビューにデータを渡すためのコンテナ + * @return 遷移先の画面 + */ + @PermissionCheck + @SessionCheck + @RequestMapping(value = INSERT_DEPARTMENT_CONFIRM, method = RequestMethod.POST) + public String insertDepartmentConfirm(InsertDepartmentForm insertDepartmentForm, Model model) { + + try { + // 部署一覧を取得し、画面に返す用のクラスに詰め替える + // 作成した部署一覧をセット + model.addAttribute( + PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, + DepartmentForm.convertFrom( + searchDepartment.selectAll())); + + // 登録用パラメータチェック + List error = insertDepartmentForm.validateParameter(); + + // 入力内容に誤りがあるか + if (!error.isEmpty()) { + // 誤りがあった場合 + // 誤りがあった箇所のエラーメッセージを表示 + model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, + new MessageForm(String.join(", ", error) + "が不正です。")); + // 画面に返す為の登録情報をセット + model.addAttribute(PageReturnAttributeKeyword.INSERT_DEPARTMENT_FORM, insertDepartmentForm); + // 登録画面に遷移 + return TransitionTargetPageNameKeyword.INSERT_DEPARTMENT_INPUT_HTML; + } + + final String DUPLICATION = "既に同名の部署が存在します。"; + + // 登録対象のメールアドレスを元に検索 + List departmentList = searchDepartment + .searchByNmDepartment(insertDepartmentForm.getNmDepartment()); + + // 検索結果があるか + if (!departmentList.isEmpty()) { + // 検索した結果0件ではない場合 + // 重複を許さないため、エラー処理 + model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION)); + // 画面に返す為の登録情報をセット + model.addAttribute(PageReturnAttributeKeyword.INSERT_DEPARTMENT_FORM, insertDepartmentForm); + // 登録画面に遷移 + return TransitionTargetPageNameKeyword.INSERT_DEPARTMENT_INPUT_HTML; + } + + // 登録確認ページに遷移 + model.addAttribute(PageReturnAttributeKeyword.INSERT_EMPLOYEE_FORM, insertDepartmentForm); + // 登録確認画面に遷移 + return TransitionTargetPageNameKeyword.INSERT_DEPARTMENT_CONFIRM_HTML; + + } catch (Exception e) { + // ログイン情報の破棄 + httpSession.invalidate(); + + e.printStackTrace(); + + // エラーログ出力 + LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e); + // エラー画面遷移 + return TransitionTargetPageNameKeyword.ERROR_HTML; + } + } +} diff --git a/src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentController.java b/src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentController.java new file mode 100644 index 0000000..822f3aa --- /dev/null +++ b/src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentController.java @@ -0,0 +1,149 @@ +package com.example.rhizome.app.presentation.controller; + +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.RegisterDepartment; +import com.example.rhizome.app.domain.service.SearchDepartment; +import com.example.rhizome.app.domain.service.impl.RegisterDepartmentImpl; +import com.example.rhizome.app.domain.service.impl.SearchDepartmentImpl; +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.DepartmentForm; +import com.example.rhizome.app.presentation.form.InsertDepartmentForm; +import com.example.rhizome.app.presentation.form.MessageForm; + +/** + * 新規登録用のコントローラ + */ +@Controller +public class InsertDepartmentController { + + // ログ出力用インスタンス + private final static Logger LOGGER = Logger.getLogger(InsertEmployeeController.class.getName()); + + // エラーメッセージ用の定数フィールド + private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; + + HttpSession httpSession; + RegisterDepartment registerDepartment; + SearchDepartment searchDepartment; + + @Autowired + public InsertDepartmentController(HttpSession httpSession, RegisterDepartmentImpl registerDepartment, SearchDepartmentImpl searchDepartment) { + this.httpSession = httpSession; + this.registerDepartment = registerDepartment; + this.searchDepartment = searchDepartment; + } + + @ModelAttribute + public InsertDepartmentForm insertDepartmentForm() { + return new InsertDepartmentForm(); + } + + /** + * 新規登録画面表示処理 + * + * @param insertEmployeeForm 登録情報 + * @param model ビューにデータを渡すためのコンテナ + * @return 遷移先の画面 + */ + @PermissionCheck + @SessionCheck + @RequestMapping(value = TransitionTargetPageNameKeyword.INSERT_DEPARTMENT, method = RequestMethod.POST) + public String insertDepartment(InsertDepartmentForm insertDepartmentForm, Model model) { + + try { + // 部署一覧を取得し、画面に返す用のクラスに詰め替える + // 作成した部署一覧をセット + model.addAttribute( + PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, + DepartmentForm.convertFrom( + searchDepartment.selectAll() + )); + + // Confirm画面から渡ってきた値をチェック + List error = validateParameter( + insertDepartmentForm.getNmDepartment() + ); + + // エラーが1件以上有る場合 + if (!error.isEmpty()) { + // エラー内容を画面に表示 + model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(String.join(", ", error) + "が不正です。")); + // 登録画面に遷移 + return TransitionTargetPageNameKeyword.INSERT_DEPARTMENT_INPUT_HTML; + } + + // 登録処理 + registerDepartment.registerDepartment(InsertDepartmentForm.convertTo(insertDepartmentForm)); + // 登録結果画面に遷移 + return TransitionTargetPageNameKeyword.INSERT_DEPARTMENT_RESULT_HTML; + + } catch (Exception e) { + // ログイン情報の破棄 + httpSession.invalidate(); + + e.printStackTrace(); + + // エラーログ出力 + LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e); + // エラー画面遷移 + return TransitionTargetPageNameKeyword.ERROR_HTML; + } + } + + /** + * @param departmentName 登録対象の名前 + * @return エラーが有った場合はエラー文言、無ければ空のリストが返る + */ + private List validateParameter( + String departmentName + + ) { + + final String EMPLOYEE_NAME_NULL_BLANK = "部署名"; + + List error = new ArrayList<>(); + + // null or 空文字判定 + if (isNullOrBlank(departmentName)) { + // エラー内容を追加 + error.add(EMPLOYEE_NAME_NULL_BLANK); + } + + return error; + + } + + /** + * 仮引数を null or 空 判定する + * + * @param str 判定対象の文字列 + * @return true:null or 空 false:null or 空 では無い + */ + private static boolean isNullOrBlank(String str) { + if (null == str) { + return true; + } + + return str.isBlank(); + } + +} + + + diff --git a/src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentInputController.java b/src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentInputController.java new file mode 100644 index 0000000..4c581dc --- /dev/null +++ b/src/main/java/com/example/rhizome/app/presentation/controller/InsertDepartmentInputController.java @@ -0,0 +1,93 @@ +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; +import com.example.rhizome.app.domain.service.SearchDepartment; +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.DepartmentForm; +import com.example.rhizome.app.presentation.form.InsertDepartmentForm; +import com.example.rhizome.app.presentation.form.InsertEmployeeForm; + +/** + * 登録画面表示用コントローラ + */ +@Controller +public class InsertDepartmentInputController { + + // ログ出力用インスタンス + private final static Logger LOGGER = Logger.getLogger(InsertDepartmentInputController.class.getName()); + + // エラーメッセージ用の定数フィールド + private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; + + HttpSession httpSession; + RegisterEmployee registerEmployee; + SearchDepartment searchDepartment; + + @Autowired + public InsertDepartmentInputController(HttpSession httpSession, RegisterEmployee registerEmployee, SearchDepartment searchDepartment) { + this.httpSession = httpSession; + this.registerEmployee = registerEmployee; + this.searchDepartment = searchDepartment; + } + + @ModelAttribute + public InsertEmployeeForm insertEmployeeForm() { + return new InsertEmployeeForm(); + } + + + /** + * 登録画面表示処理 + * + * @param insertEmployeeForm 登録画面のフォーム + * @param model ビューにデータを渡すためのコンテナ + * @return 遷移先の画面 + */ + @SessionCheck + @PermissionCheck + @RequestMapping(value = INSERT_DEPARTMENT_INPUT, method = RequestMethod.POST) + public String insertInput(InsertDepartmentForm insertDepartmentForm, Model model) { + + try { + // 部署コードリストを作成し、画面に返す値としてセット + model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, DepartmentForm.convertFrom(searchDepartment.selectAll())); + + // パラメータをセット + model.addAttribute(PageReturnAttributeKeyword.INSERT_DEPARTMENT_FORM, insertDepartmentForm); + + // 登録画面に遷移 + return TransitionTargetPageNameKeyword.INSERT_DEPARTMENT_INPUT_HTML; + + } catch (Exception e) { + // ログイン情報の破棄 + httpSession.invalidate(); + + e.printStackTrace(); + + // エラーログ出力 + LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e); + // エラー画面遷移 + return TransitionTargetPageNameKeyword.ERROR_HTML; + } + } +} + + + diff --git a/src/main/java/com/example/rhizome/app/presentation/form/InsertDepartmentForm.java b/src/main/java/com/example/rhizome/app/presentation/form/InsertDepartmentForm.java new file mode 100644 index 0000000..e784660 --- /dev/null +++ b/src/main/java/com/example/rhizome/app/presentation/form/InsertDepartmentForm.java @@ -0,0 +1,69 @@ +package com.example.rhizome.app.presentation.form; + +import java.util.ArrayList; +import java.util.List; + +import com.example.rhizome.app.domain.model.Department; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 画面から渡ってきた登録内容をまとめ、再度画面に表示する為のクラス + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class InsertDepartmentForm { + + /** + * 部署名 + */ + private String nmDepartment; + + /** + * 仮引数を null or 空 判定する + * + * @param str 判定対象の文字列 + * @return true:null or 空 false:null or 空 では無い + */ + private static boolean isNullOrBlank(String str) { + if (null == str) { + return true; + } + + return str.isBlank(); + } + + /** + * 画面から渡ってきた登録内容をまとめ、ビジネスモデルに変換 + * + * @param insertEmployeeForm 画面から渡ってきた登録内容 + * @return ビジネスモデルのインスタンス + */ + public static Department convertTo(InsertDepartmentForm insertDepartmentForm) { + return new Department( + null, + insertDepartmentForm.getNmDepartment()); + } + + /** + * 登録データのValidationを行う + * + * @return エラーが有った場合はエラー文言、無ければ空のリストが返る + */ + public List validateParameter() { + final String DEPARTMENT_NAME_NULL_BLANK = "部署名"; + + List error = new ArrayList<>(); + + if (isNullOrBlank(nmDepartment)) { + error.add(DEPARTMENT_NAME_NULL_BLANK); + + } + + return error; + } + +} diff --git a/src/main/resources/templates/insertDepartmentConfirm.html b/src/main/resources/templates/insertDepartmentConfirm.html new file mode 100644 index 0000000..911ee2d --- /dev/null +++ b/src/main/resources/templates/insertDepartmentConfirm.html @@ -0,0 +1,87 @@ + + + + + + + + + + + + 登録内容確認 + + + +

登録内容確認    +
+ ログイン者名: + +
+

+ +
+ +
+ +
+ + + + + + + + + + + + +
部署ID + +
部署名 + +
+ +
+ 登録内容の編集 | メニューに戻る +
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/insertDepartmentInput.html b/src/main/resources/templates/insertDepartmentInput.html new file mode 100644 index 0000000..b4288c4 --- /dev/null +++ b/src/main/resources/templates/insertDepartmentInput.html @@ -0,0 +1,68 @@ + + + + + + + + + + +部署登録 + + + + +

+ 部署登録    +
+ ログイン者名: + +
+

+ +
+ +
+ +
+ + + + + + + + + + + + + +
部署ID + +
部署名 + +
+ +
+
+メニューに戻る + + + \ No newline at end of file diff --git a/src/main/resources/templates/insertDepartmentResult.html b/src/main/resources/templates/insertDepartmentResult.html new file mode 100644 index 0000000..c33da9c --- /dev/null +++ b/src/main/resources/templates/insertDepartmentResult.html @@ -0,0 +1,40 @@ + + + + + + + + + + + + 部署登録完了 + + + +

部署登録が完了しました

+
+続けて登録する | メニューに戻る + + + \ No newline at end of file