段階2です

This commit is contained in:
2026-10-14 13:56:38 +09:00
parent 0a8dc404c0
commit 2da7e4cf61
7 changed files with 181 additions and 19 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,32 @@
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.service.DeleteDepartment;
import com.example.rhizome.app.infra.repository.DepartmentRepository;
/**
* Department レコード削除関連の機能を持つサービス
*/
@Service
@Transactional
public class DeleteDepartmentImpl implements DeleteDepartment {
DepartmentRepository departmentRepository;
@Autowired
public DeleteDepartmentImpl(DepartmentRepository departmentRepository) {
this.departmentRepository = departmentRepository;
}
/**
* idDepartment を元に department データを削除
*
* @param idDepartment 削除対象のidDepartmnet
*/
public void deleteDepartment(Integer idDepartment) {
departmentRepository.deleteById(idDepartment);
}
}

View File

@@ -0,0 +1,120 @@
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.model.SearchConditionDepartment;
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.MessageForm;
/**
* 削除処理のコントロータ
*/
@Controller
public class DeleteDepartmentController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(DeleteEmployeeController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
DeleteDepartment deleteDepartment;
SearchDepartment searchDepartment;
SearchEmployee searchEmployee; // 所属社員の確認に使う(既存)
@Autowired
public DeleteDepartmentController(DeleteDepartment deleteDepartment, SearchDepartment searchDepartment,
HttpSession httpSession, SearchEmployee searchEmployee) {
this.httpSession = httpSession;
this.deleteDepartment = deleteDepartment;
this.searchDepartment = searchDepartment;
this.searchEmployee = searchEmployee;
}
/**
* 削除処理のコントローラ
*
* @param idDepartment 削除対象のdepartmentId
* @param model ビューにデータを渡すためのコンテナ
* @return 遷移先の画面
*/
@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が不明です。";
try {
// 部署一覧を取得し、画面に返す用のクラスに詰め替える
// 作成した部署一覧をセット
model.addAttribute(
PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
DepartmentForm.convertFrom(searchDepartment.selectAll()));
if (idDepartment == null
|| searchDepartment.searchBy(new SearchConditionDepartment(idDepartment, null)) == null) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(UNKNOWN_ID));
}
// 削除処理実施
deleteExec(model, idDepartment);
// 検索画面に遷移
return "forward:" + TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_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 削除対象のDepartmentId
*/
private void deleteExec(Model model, Integer idDepartment) {
final String DELETE_DPARTMENT_TARGET_ID_CAN_NOT_DELETE = "1名以上が所属している部署は削除出来ません。";
final String DELETE_COMPLETE = "削除しました。";
// 削除対象部署が1名以上所属しているかチェック
if (!searchEmployee.searchEmployeeByIdDepartment(idDepartment).isEmpty()) {
// 削除対象だった場合
// エラーメッセージをセット
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,
new MessageForm(DELETE_DPARTMENT_TARGET_ID_CAN_NOT_DELETE));
// 検索画面に遷移
return;
}
// 削除を実施
deleteDepartment.deleteDepartment(idDepartment);
// 削除完了したメッセージをセット
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_INFO, new MessageForm(DELETE_COMPLETE));
}
}

View File

@@ -1,6 +1,21 @@
package com.example.rhizome.app.presentation.controller; 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.PermissionCheck;
import com.example.rhizome.aop.aspect.SessionCheck; import com.example.rhizome.aop.aspect.SessionCheck;
import com.example.rhizome.app.domain.model.LoginUser; import com.example.rhizome.app.domain.model.LoginUser;
@@ -12,22 +27,9 @@ import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTa
import com.example.rhizome.app.presentation.form.DepartmentForm; import com.example.rhizome.app.presentation.form.DepartmentForm;
import com.example.rhizome.app.presentation.form.LoginUserForm; import com.example.rhizome.app.presentation.form.LoginUserForm;
import com.example.rhizome.app.presentation.form.MessageForm; 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 @Controller
public class DeleteEmployeeController { public class DeleteEmployeeController {

View File

@@ -106,10 +106,10 @@ public class SelectDepartmentController {
DepartmentForm.convertFrom(searchDepartment.selectAll())); DepartmentForm.convertFrom(searchDepartment.selectAll()));
// 検索条件を元に検索を実施 // 検索条件を元に検索を実施
// model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, // model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,
// DepartmentForm.convertFrom( // DepartmentForm.convertFrom(
// searchDepartment.searchBy( // searchDepartment.searchBy(
// SearchConditionDepartmentForm.convertTo(searchConditionDepartmentForm())))); // SearchConditionDepartmentForm.convertTo(searchConditionDepartmentForm()))));
//部分一致検索 //部分一致検索
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST,

View File

@@ -56,6 +56,7 @@ public class TransitionTargetPageNameKeyword {
public static final String SELECT_DEPARTMENT = "/selectDepartment"; public static final String SELECT_DEPARTMENT = "/selectDepartment";
public static final String SELECT_DEPARTMENT_FORM_EMPTY = "/selectAll2"; public static final String SELECT_DEPARTMENT_FORM_EMPTY = "/selectAll2";
public static final String INSERT_DEPARTMENT_INPUT = "/insertDepartmentInput"; public static final String INSERT_DEPARTMENT_INPUT = "/insertDepartmentInput";
public static final String DELETE_DEPARTMENT = "/deleteDepartment";
/** /**
* 管理者向けのページリストを返す * 管理者向けのページリストを返す

View File

@@ -57,6 +57,7 @@
alert([[${messageInfo.message}]]); alert([[${messageInfo.message}]]);
</script> </script>
</th:block> </th:block>
<div class="header-spacing"></div> <div class="header-spacing"></div>
<th:block th:if="${departmentFormList != null and departmentFormList.size() > 0}"> <th:block th:if="${departmentFormList != null and departmentFormList.size() > 0}">
<!-- 検索結果が1件以上ある場合 --> <!-- 検索結果が1件以上ある場合 -->
@@ -93,6 +94,7 @@
<!-- 検索結果が0件の場合 --> <!-- 検索結果が0件の場合 -->
検索結果はありません。 検索結果はありません。
</th:block> </th:block>
</body> </body>
<script type="text/javascript"> <script type="text/javascript">