2段階_途中まで

This commit is contained in:
2026-10-14 16:50:59 +09:00
parent 1a09ef83be
commit b08d51d187
8 changed files with 193 additions and 64 deletions

View File

@@ -0,0 +1,5 @@
package com.example.rhizome.app.domain.service;
public interface DeleteDepartment {
void deleteDepartment(Integer idDepartment);
}

View File

@@ -0,0 +1,22 @@
package com.example.rhizome.app.domain.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.rhizome.app.domain.service.DeleteDepartment;
import com.example.rhizome.app.infra.repository.DepartmentRepository;
public class DeleteDepartmentImpl implements DeleteDepartment {
DepartmentRepository departmentRepository;
@Autowired
public DeleteDepartmentImpl(DepartmentRepository departmentRepository) {
this.departmentRepository = departmentRepository;
}
public void deleteDepartment(Integer idDepartment) {
// ★考える:Repository のどのメソッドで「IDを指定して削除」できる?
// (お手本の DeleteEmployeeImpl を見る)
departmentRepository.deleteById(idDepartment);
}
}

View File

@@ -1,11 +1,13 @@
package com.example.rhizome.app.domain.service.impl;
import com.example.rhizome.app.domain.service.DeleteEmployee;
import com.example.rhizome.app.infra.repository.EmployeeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.example.rhizome.app.domain.service.DeleteEmployee;
import com.example.rhizome.app.infra.repository.EmployeeRepository;
/**
* Employee レコード削除関連の機能を持つサービス
*/

View File

@@ -0,0 +1,72 @@
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.rhizome.app.domain.service.DeleteDepartment;
import com.example.rhizome.app.domain.service.SearchDepartment;
import com.example.rhizome.app.domain.service.SearchEmployee;
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
public class DeleteDepartmentController {
private final static Logger LOGGER = Logger.getLogger(DeleteDepartmentController.class.getName());
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
DeleteDepartment deleteDepartment;
SearchDepartment searchDepartment;
SearchEmployee searchEmployee; // 所属社員の確認に使う(既存)
//コンストラクタ
@Autowired
public DeleteDepartmentController(HttpSession httpSession, DeleteDepartment deleteDepartment,
SearchDepartment searchDepartment, SearchEmployee searchEmployee) {
this.httpSession = httpSession;
this.deleteDepartment = deleteDepartment;
this.searchDepartment = searchDepartment;
this.searchEmployee = searchEmployee;
}
@RequestMapping(value = TransitionTargetPageNameKeyword.DELETE_DEPARTMENT, method = RequestMethod.POST)
public String deleteDepartment(@RequestParam(value = "idDepartment", required = false) Integer idDepartment, Model model) {
final String UNKNOWN_ID = "削除対象のIDが不明です。";
final String HAS_EMPLOYEE = "1名以上が所属している部署は削除出来ません。";
final String DELETE_COMPLETE = "削除しました。";
try {
// ★① idDepartment が null、または searchDepartment.searchBy(new SearchConditionDepartment(idDepartment, null)) が空
// → MESSAGE_ERROR に UNKNOWN_ID を載せて一覧へ
deleteDepartment.deleteDepartment(idDepartment);
// ★② searchEmployee.searchEmployeeByIdDepartment(idDepartment) が空でない
// → MESSAGE_ERROR に HAS_EMPLOYEE を載せて一覧へ
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,DepartmentForm.convertFrom(searchDepartment.selectAll()));
// ★③ deleteDepartment.deleteDepartment(idDepartment) → MESSAGE_INFO に DELETE_COMPLETE を載せて一覧へ
model.addAttribute("searchConditinDepartmentForm", new SearchConditionDepartmentForm());
// return "一覧へ戻る(ステップ 2-5 で決める)";
return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML;
} catch (Exception e) {
httpSession.invalidate();
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,"部署を削除できませんでした");
// return TransitionTargetPageNameKeyword.ERROR_HTML;
return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML;
}
}
}

View File

@@ -1,6 +1,5 @@
package com.example.rhizome.app.presentation.controller;
import java.util.logging.Level;
import java.util.logging.Logger;
//import java.util.List;
@@ -28,68 +27,79 @@ import com.example.rhizome.app.presentation.form.SearchConditionDepartmentForm;
@Controller
public class SelectDepartmentController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(SelectDepartmentController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(SelectDepartmentController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
// private static final String DEPARTMENT_HTML = "/department";
// public static final String SELECT_DEPARTMENT_FORM_EMPTY = "/selectDepartmentAll";
HttpSession httpSession;
SearchDepartment searchDepartment;
// private static final String DEPARTMENT_HTML = "/department";
HttpSession httpSession;
SearchDepartment searchDepartment;
@Autowired
public SelectDepartmentController (HttpSession httpSession, SearchDepartment serchDepartment) {
this.httpSession = httpSession;
this.searchDepartment = serchDepartment;
}
@Autowired
public SelectDepartmentController(HttpSession httpSession, SearchDepartment serchDepartment) {
this.httpSession = httpSession;
this.searchDepartment = serchDepartment;
}
//空の検索条件FormをこのControllerの画面用に用意する
@ModelAttribute
public SearchConditionDepartmentForm searchConditionDepartmentForm() {
return new SearchConditionDepartmentForm();
}
//空の検索条件FormをこのControllerの画面用に用意する
@ModelAttribute
public SearchConditionDepartmentForm searchConditionDepartmentForm() {
return new SearchConditionDepartmentForm();
}
@RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_DEPARTMENT, method = RequestMethod.POST)
public String select(SearchConditionDepartmentForm searchConditionDepartmentForm, Model model) {
try {
// ★考える:①検索サービスを呼ぶ(Form→Model変換 → 部分一致のメソッド)
// ②結果(List<Department>)を DepartmentForm.convertFrom(...) で画面用に変換
// ③ model に PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST という名前で載せる
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,DepartmentForm
.convertFrom(searchDepartment.searchByWithLike(SearchConditionDepartmentForm
.convertTo(searchConditionDepartmentForm)
)
)
);
@RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_DEPARTMENT, method = RequestMethod.POST)
public String select(SearchConditionDepartmentForm searchConditionDepartmentForm, Model model) {
try {
// ★考える:①検索サービスを呼ぶ(Form→Model変換 → 部分一致のメソッド)
// ②結果(List<Department>)を DepartmentForm.convertFrom(...) で画面用に変換
// ③ model に PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST という名前で載せる
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, DepartmentForm
.convertFrom(searchDepartment.searchByWithLike(SearchConditionDepartmentForm
.convertTo(searchConditionDepartmentForm))));
return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML;
}catch(Exception e) {
//httpSessionを破棄
httpSession.invalidate();
return TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_HTML;
} catch (Exception e) {
//httpSessionを破棄
httpSession.invalidate();
//エラー内容を出力
e.printStackTrace();
//エラー内容を出力
e.printStackTrace();
//ログにエラーを記録
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
//ログにエラーを記録
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
//エラー画面へ遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
//エラー画面へ遷移
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
@RequestMapping(value = TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_FORM_EMPTY, method = RequestMethod.POST)
public String select(Model model) {
try {
// ★全件取得 → DepartmentForm に変換 → DEPARTMENT_FORM_LIST で model へ
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 searchConditionEmployeeForm 検索画面のフォーム
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
/**
* 社員検索画面表示処理コントローラ
*
* @param searchConditionEmployeeForm 検索画面のフォーム
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
// @PermissionCheck
// @SessionCheck
@@ -109,12 +119,9 @@ public class SelectDepartmentController {
// SearchConditionEmployeeForm.convertTo(searchConditionEmployeeForm())
// );
// /**
// * 社員検索画面表示処理コントローラ
// * 検索条件無しの状態を作るため、空の検索条件で検索した結果を画面に返す
// *
// * @param model ビューにデータを渡すためのコンテナ
// * @return 遷移先の画面

View File

@@ -33,6 +33,8 @@ public class SelectEmployeeController {
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
public static final String SELECT_DEPARTMENT_FORM_EMPTY = "/selectDepartmentAll";
HttpSession httpSession;
SearchDepartment searchDepartment;
SearchEmployeeInfo searchEmployeeInfo;

View File

@@ -56,9 +56,15 @@ public class TransitionTargetPageNameKeyword {
// Department
// HTML 実装手順書ステップ1-2通りの定数追加
public static final String SELECT_DEPARTMENT_HTML = "selectDepartment";
// Controller 実装手順書ステップ1-2通りの定数追加 -> 管理者向けと重複、エラーでたため一旦コメアウト
public static final String SELECT_DEPARTMENT = "/selectDepartment";
// ステップ2-3*TransitionTargetPageNameKeyword の Department(Controller) の枠に追記
public static final String DELETE_DEPARTMENT = "/deleteDepartment";
public static final String SELECT_DEPARTMENT_FORM_EMPTY = "/selectDepartmentAll";
/**
* 管理者向けのページリストを返す
*

View File

@@ -1,4 +1,17 @@
<!DOCTYPE html>
<!--<html> HTML文書の開始タグ-->
<!--<head> ページの設定情報を含む部分-->
<!--<title> ページのタイトルを指定するタグ-->
<!--<body> 実際に表示される内容を記述する部分-->
<!--<h1> 見出し最大サイズ・<p>段落の基本タグ-->
<!--<div> 汎用のブロック要素 複数の要素をまとめてレイアウトするためのコンテナ-->
<!--<table> 表全体を囲む-->
<!--<tr> 行全体を囲む-->
<!--<th> 行見出しセルを囲む-->
<!--<td> 行データセルを囲む-->
<!--<a href ="URL"> 飛び先のURLを指定"> -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">