段階3です
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
package com.example.rhizome.app.domain.service;
|
||||
|
||||
import com.example.rhizome.app.domain.model.Department;
|
||||
|
||||
public interface UpdateDepartment {
|
||||
void updateDepartment(Department department);
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
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.infra.entity.DepartmentEntity;
|
||||
import com.example.rhizome.app.infra.repository.DepartmentRepository;
|
||||
|
||||
@Service
|
||||
@Transactional
|
||||
public class UpdateDepartmentImpl {
|
||||
DepartmentRepository departmentRepository;
|
||||
|
||||
@Autowired
|
||||
public UpdateDepartmentImpl(DepartmentRepository departmentRepository) {
|
||||
this.departmentRepository = departmentRepository;
|
||||
}
|
||||
|
||||
public void updateDepartment(Department department) {
|
||||
// ★考える:Model を Entity に変換(DepartmentEntity.convertFrom)して、Repository の save に渡す
|
||||
departmentRepository.save(DepartmentEntity.convertFrom(department));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
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.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
import com.example.rhizome.app.domain.model.Department;
|
||||
import com.example.rhizome.app.domain.model.SearchConditionDepartment;
|
||||
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.MessageForm;
|
||||
import com.example.rhizome.app.presentation.form.UpdateDepartmentForm;
|
||||
|
||||
@Controller
|
||||
public class UpdateDepartmentController {
|
||||
|
||||
// ログ出力用インスタンス
|
||||
private final static Logger LOGGER = Logger.getLogger(UpdateEmployeeController.class.getName());
|
||||
// エラーメッセージ用の定数フィールド
|
||||
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
|
||||
|
||||
HttpSession httpSession;
|
||||
RegisterDepartment registerDepartment;
|
||||
SearchDepartment searchDepartment;
|
||||
|
||||
@Autowired
|
||||
public UpdateDepartmentController(HttpSession httpSession, RegisterDepartmentImpl registerDepartment,
|
||||
SearchDepartmentImpl searchDepartment) {
|
||||
this.httpSession = httpSession;
|
||||
this.registerDepartment = registerDepartment;
|
||||
this.searchDepartment = searchDepartment;
|
||||
}
|
||||
|
||||
@RequestMapping(value = TransitionTargetPageNameKeyword.SHOW_UPDATE_DEPARTMENT, method = RequestMethod.POST)
|
||||
public String showUpdateDepartment(@RequestParam("idDepartment") Integer idDepartment, Model model) {
|
||||
final String UPDATE_TARGET_ID_NOT_FOUND = "更新対象のデータが見つかりません。";
|
||||
try {
|
||||
// ★① searchDepartment.searchBy(new SearchConditionDepartment(idDepartment, null)) で対象を検索
|
||||
// ★② 結果が空なら MESSAGE_ERROR を載せて "forward:" + SELECT_DEPARTMENT_FORM_EMPTY
|
||||
// ★③ あれば UpdateDepartmentForm.convertFrom(結果の先頭) を
|
||||
// UPDATE_DEPARTMENT_FORM という名前で model に載せ、UPDATE_DEPARTMENT_INPUT_HTML を返す
|
||||
List<Department> DepartmentList = searchDepartment.searchBy(
|
||||
new SearchConditionDepartment(idDepartment, null));
|
||||
|
||||
// DBに更新対象のデータがあるか
|
||||
if (DepartmentList.isEmpty()) {
|
||||
// ない場合はエラーメッセージをセットして検索画面に戻る
|
||||
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,
|
||||
new MessageForm(UPDATE_TARGET_ID_NOT_FOUND));
|
||||
return "forward:" + TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_FORM_EMPTY;
|
||||
}
|
||||
// 更新画面に遷移
|
||||
model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_FORM,
|
||||
UpdateDepartmentForm.convertFrom(DepartmentList.get(0)));
|
||||
|
||||
// Update画面に遷移
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
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.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.DepartmentForm;
|
||||
import com.example.rhizome.app.presentation.form.MessageForm;
|
||||
import com.example.rhizome.app.presentation.form.UpdateDepartmentForm;
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@RequestMapping(value = TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_RESULT, method = RequestMethod.POST)
|
||||
public String departmentUpdateResult(@ModelAttribute UpdateDepartmentForm updateDepartmentForm, Model model) {
|
||||
|
||||
final String NOTHING = "項目が空欄です。";
|
||||
final String DUPLICATION = "既に同名の部署が存在するか、値が変更されていません。";
|
||||
final String NO_TARGET = "更新対象のデータが見つかりません。";
|
||||
final String UPDATE_COMPLETE = "更新が完了しました。";
|
||||
|
||||
try {
|
||||
|
||||
// 部署一覧取得
|
||||
model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, DepartmentForm.convertFrom(searchDepartment.selectAll()));
|
||||
|
||||
|
||||
updateDepartmentForm.hasRequiredValue();
|
||||
|
||||
if(updateDepartmentForm.hasRequiredValue() == false) {
|
||||
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NOTHING));
|
||||
model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_FORM, updateDepartmentForm);
|
||||
return TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_INPUT_HTML;
|
||||
}
|
||||
|
||||
if(searchDepartment.searchBy(null) != null){
|
||||
// 存在する場合はエラーメッセージを設定して更新入力画面に遷移
|
||||
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION));
|
||||
model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_FORM, updateDepartmentForm);
|
||||
return TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_INPUT_HTML;
|
||||
}
|
||||
|
||||
|
||||
if(searchDepartment.searchBy(null) != null) {
|
||||
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NO_TARGET));
|
||||
model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_FORM, updateDepartmentForm);
|
||||
return TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_INPUT_HTML;
|
||||
}
|
||||
|
||||
// 更新結果のメッセージを画面にセットし一覧画面に遷移
|
||||
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_INFO, new MessageForm(UPDATE_COMPLETE));
|
||||
|
||||
// forward 先で不用意な動作をしないために UpdateEmployeeInfoForm をクリア
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -9,6 +9,7 @@ public class PageReturnAttributeKeyword {
|
||||
public static final String MESSAGE_INFO = "messageInfo";
|
||||
|
||||
public static final String DEPARTMENT_FORM_LIST = "departmentFormList";
|
||||
public static final String UPDATE_DEPARTMENT_FORM = "updateDepartmentForm";
|
||||
public static final String UPDATE_EMPLOYEE_INFO_FORM = "updateEmployeeInfoForm";
|
||||
public static final String INSERT_EMPLOYEE_FORM = "insertEmployeeForm";
|
||||
public static final String LOGIN_INFO_FORM = "loginInfoForm";
|
||||
|
||||
@@ -48,6 +48,7 @@ public class TransitionTargetPageNameKeyword {
|
||||
// Department
|
||||
// HTML
|
||||
public static final String SELECT_DEPARTMENT_HTML = "selectDepartment";
|
||||
public static final String UPDATE_DEPARTMENT_INPUT_HTML = "updateDepartmentInput";
|
||||
public static final String INSERT_DEPARTMENT_INPUT_HTML = "insertDepartmentInput";
|
||||
public static final String INSERT_DEPARTMENT_CONFIRM_HTML = "insertDepartmentConfirm";
|
||||
public static final String INSERT_DEPARTMENT_RESULT_HTML = "insertDepartmentResult";
|
||||
@@ -55,6 +56,8 @@ public class TransitionTargetPageNameKeyword {
|
||||
// Controller
|
||||
public static final String SELECT_DEPARTMENT = "/selectDepartment";
|
||||
public static final String SELECT_DEPARTMENT_FORM_EMPTY = "/selectAll2";
|
||||
public static final String SHOW_UPDATE_DEPARTMENT = "/showUpdateDepartment";
|
||||
public static final String UPDATE_DEPARTMENT_RESULT = "/updateDepartmentResult";
|
||||
public static final String INSERT_DEPARTMENT_INPUT = "/insertDepartmentInput";
|
||||
public static final String DELETE_DEPARTMENT = "/deleteDepartment";
|
||||
|
||||
@@ -82,7 +85,10 @@ public class TransitionTargetPageNameKeyword {
|
||||
UPDATE_EMPLOYEE_RESULT,
|
||||
SELECT_DEPARTMENT,
|
||||
SELECT_DEPARTMENT_FORM_EMPTY,
|
||||
INSERT_DEPARTMENT_INPUT);
|
||||
INSERT_DEPARTMENT_INPUT,
|
||||
SHOW_UPDATE_DEPARTMENT,
|
||||
SHOW_UPDATE_DEPARTMENT);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.example.rhizome.app.presentation.form;
|
||||
|
||||
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;
|
||||
|
||||
/** Model(Department) → Form */
|
||||
public static UpdateDepartmentForm convertFrom(Department department) {
|
||||
return new UpdateDepartmentForm(department.getIdDepartment(), department.getNmDepartment());
|
||||
}
|
||||
|
||||
/** Form → Model(Department) */
|
||||
public static Department convertTo(UpdateDepartmentForm form) {
|
||||
return new Department(form.getIdDepartment(), form.getNmDepartment());
|
||||
}
|
||||
|
||||
/** 必須チェック。true:OK/false:部署ID か部署名が空 */
|
||||
public boolean hasRequiredValue() {
|
||||
// ★考える:idDepartment が null でなく、nmDepartment が null でも空白だけでもない
|
||||
if(idDepartment != null && nmDepartment.isBlank() && nmDepartment.isEmpty());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
59
src/main/resources/templates/updateDepartmentInput.html
Normal file
59
src/main/resources/templates/updateDepartmentInput.html
Normal file
@@ -0,0 +1,59 @@
|
||||
<!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>
|
||||
<!-- CSSの読み込み -->
|
||||
<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:text="${messageError.message}"></span>
|
||||
</div>
|
||||
<table>
|
||||
<tr>
|
||||
<th class="header">部署ID</th>
|
||||
<td>
|
||||
<input class="readOnly" readonly="readonly" th:field="*{idDepartment}" type="text" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th class="header">部署名</th>
|
||||
<td>
|
||||
<input maxlength="50" placeholder="部署名" required="required" th:field="*{nmDepartment}"
|
||||
type="text" />
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="btnArea" colspan="2"><input type="submit" value="更新" /></td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
<a href="javascript:selectDepartmentSubmit()">一覧に戻る</a>
|
||||
<script type="text/javascript">
|
||||
function selectDepartmentSubmit() {
|
||||
const form = document.createElement('form');
|
||||
form.setAttribute('method', 'post');
|
||||
form.setAttribute('action', 'selectDepartment');
|
||||
document.body.appendChild(form);
|
||||
form.submit();
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Reference in New Issue
Block a user