diff --git a/src/main/java/com/example/rhizome/app/presentation/controller/SelectDepartmentController.java b/src/main/java/com/example/rhizome/app/presentation/controller/SelectDepartmentController.java new file mode 100644 index 0000000..754450b --- /dev/null +++ b/src/main/java/com/example/rhizome/app/presentation/controller/SelectDepartmentController.java @@ -0,0 +1,118 @@ +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; +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.SearchConditionDepartmentForm; + +@Controller +class SelectDepartmentController { + + // ログ出力用インスタンス + private final static Logger LOGGER = Logger.getLogger(SelectDepartmentController.class.getName()); + // エラーメッセージ用の定数フィールド + private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!"; + + HttpSession httpSession; + SearchDepartment searchDepartment; + + + @Autowired + public SelectDepartmentController(HttpSession httpSession, SearchDepartment searchDepartment) { + this.httpSession = httpSession; + this.searchDepartment = searchDepartment; + } + + @ModelAttribute + public SearchConditionDepartmentForm searchConditionDepartmentForm() { + return new SearchConditionDepartmentForm(); + } + + /** + * 部署検索画面表示処理コントローラ + * + * @param searchConditionDepartmentForm 検索画面のフォーム + * @param model ビューにデータを渡すためのコンテナ + * @return 遷移先の画面 + */ + @PermissionCheck + @SessionCheck + @RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_INFO, method = RequestMethod.POST) + public String select(SearchConditionDepartmentForm searchConditionDepartmentForm, Model model) { + + try { + + // 部署一覧を取得し、画面に返す用のクラスに詰め替える + // 作成した部署一覧をセット + model.addAttribute( + PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, + DepartmentForm.convertFrom(searchDepartment.selectAll()) + ); + + // 検索条件を元に検索を実施 + // 取得した DepartmentInfoの情報を画面に返す用の型に詰め替えてセット + model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, DepartmentForm.convertFrom(searchDepartment.searchByWithLike(SearchConditionDepartmentForm.convertTo(searchConditionDepartmentForm)))); + + return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML; + + + } catch (Exception e) { + // ログイン情報の破棄 + httpSession.invalidate(); + + e.printStackTrace(); + + // エラーログ出力 + LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e); + // エラー画面遷移 + return TransitionTargetPageNameKeyword.ERROR_HTML; + } + + } + + /** + * 部署検索画面表示処理コントローラ + * 検索条件無しの状態を作るため、空の検索条件で検索した結果を画面に返す + * + * @param model ビューにデータを渡すためのコンテナ + * @return 遷移先の画面 + */ + @PermissionCheck + @SessionCheck + @RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_INFO_FORM_EMPTY, method = RequestMethod.POST) + public String select(Model model) { + + // 部署一覧を取得し、画面に返す用のクラスに詰め替える + // 作成した部署一覧をセット + model.addAttribute( + PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, + DepartmentForm.convertFrom(searchDepartment.selectAll()) + ); + + // 検索条件を元に検索を実施 + model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, + DepartmentForm.convertFrom( + searchDepartment.searchBy( + SearchConditionDepartmentForm.convertTo(searchConditionDepartmentForm()) + ))); + + // 検索ページに遷移 + return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML; + } + +} diff --git a/src/main/java/com/example/rhizome/app/presentation/form/SearchConditionDepartmentForm.java b/src/main/java/com/example/rhizome/app/presentation/form/SearchConditionDepartmentForm.java new file mode 100644 index 0000000..1cd7876 --- /dev/null +++ b/src/main/java/com/example/rhizome/app/presentation/form/SearchConditionDepartmentForm.java @@ -0,0 +1,52 @@ +package com.example.rhizome.app.presentation.form; + +import com.example.rhizome.app.domain.model.SearchConditionDepartment; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * 画面から渡ってきた検索条件をまとめ、再度画面に表示する為のクラス + */ +@Data +@AllArgsConstructor +@NoArgsConstructor +public class SearchConditionDepartmentForm { + /** + * 部署ID + */ + private Integer idDepartment; + /** + * 部署名 + */ + private String nmDepartment; + + /** + * business 層の SearchConditionDepaerment クラスから画面用(presentation 層)の SearchConditionDepartmentForm に + * 変換します。 + * + * @param searchConditionDepartment business 層の SearchConditionDepartment インスタンス + * @return 画面用(presentation 層)の SearchConditionDepartmentForm インスタンス + */ + public static SearchConditionDepartmentForm convertFrom(SearchConditionDepartment searchConditionDepartment) { + return new SearchConditionDepartmentForm( + searchConditionDepartment.getIdDepartment(), + searchConditionDepartment.getNmDepartment() + ); + } + + /** + * 画面用(presentation 層)の SearchConditionDepartmentForm クラスから business 層の SearchConditionDepartment クラスに + * 変換します。 + * + * @param searchConditionDepartmentForm 画面用(presentation 層)の SearchConditionDepartmentForm インスタンス + * @return business 層の SearchConditionDepartment インスタンス + */ + public static SearchConditionDepartment convertTo(SearchConditionDepartmentForm searchConditionDepartmentForm) { + return new SearchConditionDepartment( + searchConditionDepartmentForm.getIdDepartment(), + searchConditionDepartmentForm.getNmDepartment() + ); + } +} diff --git a/src/main/resources/templates/selectDepartment.html b/src/main/resources/templates/selectDepartment.html new file mode 100644 index 0000000..9705881 --- /dev/null +++ b/src/main/resources/templates/selectDepartment.html @@ -0,0 +1,114 @@ + + + + + + + + + + + + 検索 + + + +

+ 検索条件入力    +
+ ログイン者名: + +
+

+
+ +
+ +
+ + + + + + + + + + + + + +
部署ID + +
部署名 + +
+ + + +
+
+
+メニューに戻る +
+

検索結果

+
+ + + +
+ + + + + + + + + + + + + + + + + +
部署ID部署名操作
+ +
+ + +
+ +
+ + +
+
+
+ + + 検索結果はありません。 + + + + + \ No newline at end of file