[ADD] 部署更新機能の実装

This commit is contained in:
s-choi
2026-09-10 16:05:05 +09:00
parent 4cdad5c24e
commit 6a60c5a85f
6 changed files with 380 additions and 0 deletions

View File

@@ -0,0 +1,8 @@
package com.example.rhizome.app.domain.service;
import com.example.rhizome.app.domain.model.Department;
public interface UpdateDepartment {
void updateDepartment(Department beforeDepartment, Department updateDepartment);
}

View File

@@ -0,0 +1,83 @@
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.Department;
import com.example.rhizome.app.domain.model.SearchConditionDepartment;
import com.example.rhizome.app.domain.service.SearchDepartment;
import com.example.rhizome.app.domain.service.SearchEmployeeInfo;
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.MessageForm;
import com.example.rhizome.app.presentation.form.SearchConditionDepartmentForm;
import com.example.rhizome.app.presentation.form.UpdateDepartmentForm;
@Controller
public class UpdateDepartmentController {
private final static Logger LOGGER = Logger.getLogger(UpdateDepartmentController.class.getName());
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
SearchDepartment searchDepartment;
SearchEmployeeInfo searchEmployeeInfo;
@Autowired
public UpdateDepartmentController(HttpSession httpSession, SearchDepartment searchDepartment) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
}
@ModelAttribute
public SearchConditionDepartmentForm searchConditionDepartmentForm() {
return new SearchConditionDepartmentForm();
}
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.SHOW_UPDATE_DEPARTMENT, method = RequestMethod.POST)
public String showUpdateDepartment(@RequestParam("idDepartment") Integer idDepartment, Model model) {
LOGGER.info("showUpdateDepartment 실행");
final String NO_TARGET = "更新対象のデータが見つかりません。";
try {
List<Department> departmentList = searchDepartment.searchByDepartmentId(
new SearchConditionDepartment(idDepartment,
null));
if (departmentList.isEmpty()) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NO_TARGET));
return "forward:" + TransitionTargetPageNameKeyword.SELECT_DEPARTMENT;
}
model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_FORM,
UpdateDepartmentForm.convertFrom(departmentList.get(0)));
return TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_INPUT_HTML;
} catch (Exception e) {
httpSession.invalidate();
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
}

View File

@@ -0,0 +1,116 @@
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;
import com.example.rhizome.app.domain.model.Department;
import com.example.rhizome.app.domain.model.SearchConditionDepartment;
import com.example.rhizome.app.domain.service.SearchDepartment;
import com.example.rhizome.app.domain.service.UpdateDepartment;
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.LoginUserForm;
import com.example.rhizome.app.presentation.form.MessageForm;
import com.example.rhizome.app.presentation.form.UpdateDepartmentForm;
import com.example.rhizome.app.presentation.form.UpdateEmployeeInfoForm;
@Controller
public class UpdateDepartmentResultController {
private final static Logger LOGGER = Logger.getLogger(UpdateEmployeeController.class.getName());
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
HttpSession httpSession;
SearchDepartment searchDepartment;
UpdateDepartment updateDepartment;
@Autowired
public UpdateDepartmentResultController(HttpSession httpSession, SearchDepartment searchDepartment,
UpdateDepartment updateDepartment) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
this.updateDepartment = updateDepartment;
}
private static boolean checkDuplicateDepartmentName(List<Department> departmentList,
UpdateDepartmentForm updateDepartmentForm) {
if (!departmentList.isEmpty()) {
return Objects.equals(departmentList.get(0).getNmDepartment(), updateDepartmentForm.getNmDepartment());
}
return true;
}
@PermissionCheck
@SessionCheck
@RequestMapping(value = TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_RESULT, method = RequestMethod.POST)
public String departmentUpdateResult(@ModelAttribute UpdateDepartmentForm updateDepartmentForm, Model model) {
final String DUPLICATION = "既に同名の部署が存在するか、値が変更されていません。";
final String NOTHING = "項目が空欄です。";
final String UPDATE_COMPLETE = "更新が完了しました。";
try {
List<Department> departmentList = searchDepartment
.searchByDepartmentId(new SearchConditionDepartment(updateDepartmentForm.getIdDepartment(), null));
if (checkDuplicateDepartmentName(departmentList, updateDepartmentForm)) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_FORM, updateDepartmentForm);
return TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_INPUT_HTML;
}
if (!UpdateDepartmentForm.hasErrorParameter(updateDepartmentForm)) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NOTHING));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_FORM, updateDepartmentForm);
return TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_INPUT_HTML;
}
List<Department> beforeDepartment = searchDepartment
.searchByDepartmentId(new SearchConditionDepartment(updateDepartmentForm.getIdDepartment(), null));
updateDepartment.updateDepartment(beforeDepartment.get(0),
UpdateDepartmentForm.convertTo(updateDepartmentForm));
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_INFO, new MessageForm(UPDATE_COMPLETE));
return "forward:" + TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_INFO_FORM_EMPTY;
} 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;
}
return updateEmployeeInfoForm.getAdminAuth().equals(0);
}
}

View File

@@ -0,0 +1,53 @@
package com.example.rhizome.app.presentation.form;
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 UpdateDepartmentForm {
private Integer idDepartment;
private String nmDepartment;
public static UpdateDepartmentForm convertFrom(Department department) {
return new UpdateDepartmentForm(
department.getIdDepartment(),
department.getNmDepartment());
}
public static List<UpdateDepartmentForm> convertFrom(List<Department> departmentList) {
return departmentList.stream().map(UpdateDepartmentForm::convertFrom).toList();
}
public static Department convertTo(UpdateDepartmentForm updateDepartmentForm) {
return new Department(
updateDepartmentForm.getIdDepartment(),
updateDepartmentForm.getNmDepartment());
}
public static boolean hasErrorParameter(UpdateDepartmentForm updateDepartmentForm) {
boolean errorFlag = !isNullOrBlank(updateDepartmentForm.getNmDepartment());
if (!isNullOrBlank(updateDepartmentForm.getNmDepartment())) {
errorFlag = true;
}
return errorFlag;
}
private static boolean isNullOrBlank(String str) {
if (null == str) {
return true;
}
return str.isBlank();
}
}

View File

@@ -0,0 +1,83 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<script th:src="@{/js/common.js}"></script>
<link rel="stylesheet" th:href="@{/css/all.css}">
<link rel=stylesheet th:href="@{/css/style.css}" type="text/css">
<title>部署情報更新</title>
</head>
<body>
<h1>
部署情報更新   
<div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
</div>
</h1>
<form action="/updateDepartmentResult"
method="post"
onsubmit="return confirm('本当によろしいですか?');"
th:object="${updateDepartmentForm}"
>
<div class="msgBox error" th:if="${messageError != null}">
<span class="error" th:each="message : ${messageError.message}" th:text="${message}"></span>
</div>
<table>
<tr>
<th class="header">部署ID</th>
<td>
<label>
<input class="readOnly"
readonly="readonly"
th:field="*{idDepartment}"
type="text"
/>
</label>
</td>
</tr>
<tr>
<th class="header">部署名</th>
<td>
<label>
<input maxlength="50"
placeholder="部署名"
required="required"
th:field="*{nmDepartment}"
type="text"
/>
</label>
</td>
</tr>
<tr>
<td class="btnArea" colspan="2">
<input type="submit" value="更新"/>
</td>
</tr>
</table>
</form>
<a href="javascript:selectSubmit()">一覧に戻る</a>
</body>
<script>
function selectSubmit() {
const form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', 'selectDepartmentAll');
const inputField = document.createElement("input");
inputField.setAttribute("type", "hidden");
inputField.setAttribute("value", "selectDepartmentAll");
form.appendChild(inputField);
document.body.appendChild(form);
form.submit();
}
</script>
</html>

View File

@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<script th:src="@{/js/common.js}"></script>
<link rel="stylesheet" th:href="@{/css/all.css}">
<link rel=stylesheet th:href="@{/css/style.css}" type="text/css">
<title>更新完了</title>
</head>
<body>
<h2>更新が完了しました</h2>
<br/>
<a href="javascript:backSubmit()">戻る</a>
</body>
<script type="text/javascript">
function backSubmit() {
const form = document.createElement('form');
form.setAttribute('method', 'post');
form.setAttribute('action', 'updatePersonalDepartmentResult');
const inputField = document.createElement("input");
inputField.setAttribute("type", "hidden");
inputField.setAttribute("value", "updatePersonalDepartmentResult");
form.appendChild(inputField);
document.body.appendChild(form);
form.submit();
}
</script>
</html>