3段階目終了

This commit is contained in:
2026-10-14 15:04:07 +09:00
parent df39974a57
commit d80ed7ad78
10 changed files with 321 additions and 157 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 updateDepertment);
}

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.model.Department;
import com.example.rhizome.app.domain.service.UpdateDepartment;
import com.example.rhizome.app.infra.entity.DepartmentEntity;
import com.example.rhizome.app.infra.repository.DepartmentRepository;
@Service
@Transactional
public class UpdateDepartmentImpl implements UpdateDepartment {
DepartmentRepository departmentRepository;
@Autowired
public UpdateDepartmentImpl(DepartmentRepository departmentRepository) {
this.departmentRepository = departmentRepository;
}
public void updateDepartment(Department beforeDepartment, Department updateDepertment) {
departmentRepository.save(DepartmentEntity.convertFrom(mergeDepartment(beforeDepartment,updateDepertment)));
}
private Department mergeDepartment(Department beforeDepartment, Department updateDepartment) {
return new Department(updateDepartment.getIdDepartment(),updateDepartment.getNmDepartment());
}
}

View File

@@ -1,73 +1,82 @@
//package com.example.rhizome.app.presentation.controller; package com.example.rhizome.app.presentation.controller;
//
//import java.util.List; import java.util.List;
//import java.util.logging.Logger; import java.util.logging.Level;
// import java.util.logging.Logger;
//import javax.servlet.http.HttpSession;
// import javax.servlet.http.HttpSession;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Controller; import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.ui.Model; import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.ui.Model;
//import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ModelAttribute;
//import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam; 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.aop.aspect.PermissionCheck;
//import com.example.rhizome.app.domain.model.Department; import com.example.rhizome.aop.aspect.SessionCheck;
//import com.example.rhizome.app.domain.model.SearchConditionDepartment; import com.example.rhizome.app.domain.model.Department;
//import com.example.rhizome.app.domain.service.SearchDepartment; import com.example.rhizome.app.domain.model.SearchConditionDepartment;
//import com.example.rhizome.app.domain.service.SearchDepartmentInfo; 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.PageReturnAttributeKeyword;
//import com.example.rhizome.app.presentation.controller.pageproperty.TransitionTargetPageNameKeyword; 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.MessageForm; import com.example.rhizome.app.presentation.form.SearchConditionDepartmentForm;
//import com.example.rhizome.app.presentation.form.SearchConditionDepartmentForm; import com.example.rhizome.app.presentation.form.UpdateDepartmentInfoForm;
//import com.example.rhizome.app.presentation.form.UpdateEmployeeInfoForm;
// @Controller
//@Controller public class UpdateDepartmentController {
//public class UpdateDepartmentController {
// // ログ出力用インスタンス
// // ログ出力用インスタンス private final static Logger LOGGER = Logger.getLogger(UpdateDepartmentController.class.getName());
// private final static Logger LOGGER = Logger.getLogger(UpdateDepartmentController.class.getName()); // エラーメッセージ用の定数フィールド
// // エラーメッセージ用の定数フィールド private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
// private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
// private final HttpSession httpSession;
// HttpSession httpSession; private final SearchDepartment searchDepartment;
// SearchDepartment searchDepartment;
// @Autowired
// @Autowired public UpdateDepartmentController(HttpSession httpSession, SearchDepartment searchDepartment) {
// public UpdateDepartmentController(HttpSession httpSession, SearchDepartment searchDepartment) { this.httpSession = httpSession;
// this.httpSession = httpSession; this.searchDepartment = searchDepartment;
// this.searchDepartment = searchDepartment;
// } }
//
// @ModelAttribute @ModelAttribute
// public SearchConditionDepartmentForm searchConditionDepartmentForm() { public SearchConditionDepartmentForm searchConditionDepartmentForm() {
// return new SearchConditionDepartmentForm(); return new SearchConditionDepartmentForm();
// } }
//
// @PermissionCheck @PermissionCheck
// @SessionCheck @SessionCheck
// @RequestMapping(value = TransitionTargetPageNameKeyword.SHOW_UPDATE_DEPARTMENT, method = RequestMethod.POST)) @RequestMapping(value = "/showUpdateDepartment", method = RequestMethod.POST)
// public String showUpdateDepartment(@RequestParam("idDepartment") Integer idDepartment, Model model) { public String showUpdateDepartment(@RequestParam("idDepartment") Integer idDepartment, Model model) {
// final String UPDATE_TARGET_ID_NOT_FOUND = "更新対象のデータが見つかりません。"; final String UPDATE_TARGET_ID_NOT_FOUND = "更新対象のデータが見つかりません。";
//
// try { try {
//
// model.addAttribute(PageReturnAttributeKeyword.DEPARTMENT_FORM_LIST, DepartmentForm.convertFrom(searchDepartment.selectAll())); List<Department> departmentInfoList = searchDepartment.searchBy(
// new SearchConditionDepartment(idDepartment, null));
// List<Department> departmentInfoList = SearchDepartmentInfo.seachBy(
// new SearchConditionDepartment(idDepartment, if (departmentInfoList == null || departmentInfoList.isEmpty()) {
// null)); model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR,
// new MessageForm(UPDATE_TARGET_ID_NOT_FOUND));
// if (departmentInfoList.isEmpty()) {
// model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(UPDATE_TARGET_ID_NOT_FOUND)); return "forward:" + TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_INFO_FORM_EMPTY;
// return "forward:" + TransitionTargetPageNameKeyword.SELECT_DEPARTMENT_INFO; }
//
// model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_INFO_FORM, UpdateDepartmentInfoForm.convertFrom(departmentInfoList.get(0))); model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_FORM,
// } UpdateDepartmentInfoForm.convertForm(departmentInfoList.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.MessageForm;
import com.example.rhizome.app.presentation.form.SearchConditionDepartmentForm;
import com.example.rhizome.app.presentation.form.UpdateDepartmentInfoForm;
@Controller
public class UpdateDepartmentResultController {
// ログ出力用インスタンス
private final static Logger LOGGER = Logger.getLogger(UpdateDepartmentController.class.getName());
// エラーメッセージ用の定数フィールド
private final static String SEVERE_ERROR_MESSAGE = "エラーが発生しました!";
private final HttpSession httpSession;
private final SearchDepartment searchDepartment;
private final UpdateDepartment updateDepartment;
@Autowired
public UpdateDepartmentResultController(HttpSession httpSession, SearchDepartment searchDepartment,
UpdateDepartment updateDepartment) {
this.httpSession = httpSession;
this.searchDepartment = searchDepartment;
this.updateDepartment = updateDepartment;
}
@ModelAttribute
public SearchConditionDepartmentForm searchConditionDepartmentForm() {
return new SearchConditionDepartmentForm();
}
@PermissionCheck
@SessionCheck
@RequestMapping(value = "/updateDepartmentResult", method = RequestMethod.POST)
public String departmentUpdateResult(
@ModelAttribute("updateDepartmentInfoForm") UpdateDepartmentInfoForm updateDepartmentInfoForm,
Model model) {
final String NOTHING = "項目が空欄です。";
final String DUPLICATION = "既に同名の部署が存在するか、値が変更されていません。";
final String NO_TARGET = "更新対象のデータが見つかりません。";
final String UPDATE_COMPLETE = "更新が完了しました。";
try {
// 必須入力チェック
if (UpdateDepartmentInfoForm.hasErrorParameter(updateDepartmentInfoForm)) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NOTHING));
return TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_INPUT_HTML;
}
List<Department> beforeList = searchDepartment.searchBy(
new SearchConditionDepartment(updateDepartmentInfoForm.getIdDepartment(), null));
if (beforeList == null || beforeList.isEmpty()) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(NO_TARGET));
return "forward:/selectDepartment";
}
Department beforeDepartment = beforeList.get(0);
Department updateDepartmentData = UpdateDepartmentInfoForm.convertTo(updateDepartmentInfoForm);
if (Objects.equals(beforeDepartment.getNmDepartment(), updateDepartmentData.getNmDepartment())) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION));
return TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_INPUT_HTML;
}
List<Department> duplicationList = searchDepartment.searchBy(
new SearchConditionDepartment(null, updateDepartmentData.getNmDepartment()));
if (duplicationList != null && !duplicationList.isEmpty()) {
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_ERROR, new MessageForm(DUPLICATION));
model.addAttribute(PageReturnAttributeKeyword.UPDATE_DEPARTMENT_INFO_FORM, updateDepartmentInfoForm);
return TransitionTargetPageNameKeyword.UPDATE_DEPARTMENT_INPUT_HTML;
}
// 更新を実行 ➔ forward
updateDepartment.updateDepartment(beforeDepartment, updateDepartmentData);
model.addAttribute(PageReturnAttributeKeyword.MESSAGE_INFO, new MessageForm(UPDATE_COMPLETE));
return "forward:/selectDepartment";
} catch (
Exception e) {
httpSession.invalidate();
e.printStackTrace();
LOGGER.log(Level.SEVERE, SEVERE_ERROR_MESSAGE, e);
return TransitionTargetPageNameKeyword.ERROR_HTML;
}
}
}

View File

@@ -15,5 +15,6 @@ public class PageReturnAttributeKeyword {
public static final String LOGIN_INFO_FORM = "loginInfoForm"; public static final String LOGIN_INFO_FORM = "loginInfoForm";
public static final String UPDATE_PERSONAL_EMPLOYEE_USER_FORM = "updatePersonalEmployeeUserForm"; public static final String UPDATE_PERSONAL_EMPLOYEE_USER_FORM = "updatePersonalEmployeeUserForm";
public static final String EMPLOYEE_INFO_FORM_LIST = "employeeInfoFormList"; public static final String EMPLOYEE_INFO_FORM_LIST = "employeeInfoFormList";
public static final String UPDATE_DEPARTMENT_FORM = "updateDepartmentForm";
} }

View File

@@ -25,6 +25,7 @@ public class TransitionTargetPageNameKeyword {
public static final String INSERT_EMPLOYEE_RESULT_HTML = "insertEmployeeResult"; public static final String INSERT_EMPLOYEE_RESULT_HTML = "insertEmployeeResult";
public static final String INSERT_DEPARTMENT_RESULT_HTML = "insertDepartmentResult"; public static final String INSERT_DEPARTMENT_RESULT_HTML = "insertDepartmentResult";
public static final String UPDATE_EMPLOYEE_INPUT_HTML = "updateEmployeeInput"; public static final String UPDATE_EMPLOYEE_INPUT_HTML = "updateEmployeeInput";
public static final String UPDATE_DEPARTMENT_INPUT_HTML = "updateDepartmentInput";
public static final String ERROR_HTML = "error"; public static final String ERROR_HTML = "error";
@@ -55,9 +56,10 @@ public class TransitionTargetPageNameKeyword {
public static final String INSERT_DEPARTMENT_CONFIRM = "/insertDepartmentConfirm"; public static final String INSERT_DEPARTMENT_CONFIRM = "/insertDepartmentConfirm";
public static final String INSERT_DEPARTMENT = "/insertDepartment"; public static final String INSERT_DEPARTMENT = "/insertDepartment";
public static final String SELECT_DEPARTMENT_INFO = "/selectDepartment"; public static final String SELECT_DEPARTMENT_INFO = "/selectDepartment";
public static final String SELECT_DEPARTMENT_INFO_FORM_EMPTY = "/selectAll";
public static final String SHOW_UPDATE_DEPARTMENT = "/showUpdateDepartment"; public static final String SHOW_UPDATE_DEPARTMENT = "/showUpdateDepartment";
public static final String UPDATE_DEPARTMENT_RESULT = "/updateDepartmentResult"; public static final String UPDATE_DEPARTMENT_RESULT = "/updateDepartmentResult";
public static final String SELECT_DEPARTMENT_INFO_FORM_EMPTY = "/selectDepartmentAll";
// HTML // HTML
// Controller // Controller
@@ -90,6 +92,7 @@ public class TransitionTargetPageNameKeyword {
INSERT_DEPARTMENT, INSERT_DEPARTMENT,
INSERT_DEPARTMENT_CONFIRM, INSERT_DEPARTMENT_CONFIRM,
SELECT_DEPARTMENT_INFO, SELECT_DEPARTMENT_INFO,
SELECT_DEPARTMENT_INFO_FORM_EMPTY,
SHOW_UPDATE_DEPARTMENT, SHOW_UPDATE_DEPARTMENT,
UPDATE_DEPARTMENT_RESULT UPDATE_DEPARTMENT_RESULT
); );

View File

@@ -1,54 +1,48 @@
//package com.example.rhizome.app.presentation.form; package com.example.rhizome.app.presentation.form;
//
//import java.util.List; import java.util.List;
//
//import com.example.rhizome.app.domain.model.Department; import com.example.rhizome.app.domain.model.Department;
//
//import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
//import lombok.Data; import lombok.Data;
//import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
//
//@Data @Data
//@AllArgsConstructor @AllArgsConstructor
//@NoArgsConstructor @NoArgsConstructor
//public class UpdateDepartmentInfoForm { public class UpdateDepartmentInfoForm {
//
// private Integer idDepartment; private Integer idDepartment;
//
// private String nmDepartment; private String nmDepartment;
//
// public static UpdateDepartmentInfoForm convertForm(Department department) { public static UpdateDepartmentInfoForm convertForm(Department department) {
// return new UpdateDepartmentInfoForm( return new UpdateDepartmentInfoForm(
// department.getIdDepartment(), department.getIdDepartment(),
// department.getNmDepartment()); department.getNmDepartment());
//
// } }
//
// public static List<UpdateDepartmentInfoForm> convertForm(List<Department> departmentList) { public static List<UpdateDepartmentInfoForm> convertForm(List<Department> departmentList) {
// return departmentList.stream().map(UpdateDepartmentInfoForm::convertFrom).toList(); return departmentList.stream().map(UpdateDepartmentInfoForm::convertForm).toList();
// } }
//
// public static Department convertTo(UpdateDepartmentInfoForm updateDepartmentInfoForm) { public static Department convertTo(UpdateDepartmentInfoForm updateDepartmentInfoForm) {
// return new Department( return new Department(
// updateDepartmentInfoForm.getIdDepartment(), updateDepartmentInfoForm.getIdDepartment(),
// updateDepartmentInfoForm.getNmDepartment()); updateDepartmentInfoForm.getNmDepartment());
// } }
//
// public static boolean hasErrorParameter(UpdateDepartmentInfoForm updateDepartmentInfoForm) { public static boolean hasErrorParameter(UpdateDepartmentInfoForm updateDepartmentInfoForm) {
//
// boolean errorFlag = !isNullOrBlank(updateDepartmentInfoForm.getNmDepartment()); return isNullOrBlank(updateDepartmentInfoForm.getNmDepartment());
// }
// if (!isNullOrBlank(updateDepartmentInfoForm.getNmDepartment())) {
// errorFlag = true; private static boolean isNullOrBlank(String str) {
// } if (null == str) {
// return true;
// return errorFlag; }
// } return str.isBlank();
// }
// private static boolean isNullOrBlank(String str) { }
// if (null == str) {
// return true;
// }
// return str.isBlank();
// }
//}

View File

@@ -78,8 +78,7 @@
<td th:text="${departmentForm.nmDepartment}"></td> <td th:text="${departmentForm.nmDepartment}"></td>
<td> <td>
<!-- 更新フォーム --> <!-- 更新フォーム -->
<form action="showUpdateDepartment" method="post" style="display: inline;"> <form action="/updateDepartmentResult" method="post" style="display: inline;">
<input name="idDepartment" th:value="${departmentForm.idDepartment}" type="hidden"> <input name="idDepartment" th:value="${departmentForm.idDepartment}" type="hidden">
<input type="submit" value="更新"> <input type="submit" value="更新">
</form> </form>
@@ -89,6 +88,7 @@
<input name="idDepartment" th:value="${departmentForm.idDepartment}" type="hidden"> <input name="idDepartment" th:value="${departmentForm.idDepartment}" type="hidden">
<input type="submit" value="削除"> <input type="submit" value="削除">
</form> </form>
</td> </td>
</tr> </tr>
</tbody> </tbody>

View File

@@ -15,7 +15,7 @@
<h1> <h1>
部署情報更新    部署情報更新   
<div class="rightSection"> <div class="rightSection">
<span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmDepartment}"></span></span> <span class="loginInfo">ログイン者名:<span th:text="${session.loginUser.nmEmployee}"></span></span>
<button class="logoutButton" onclick="logoutSubmit()">ログアウト</button> <button class="logoutButton" onclick="logoutSubmit()">ログアウト</button>
</div> </div>
</h1> </h1>
@@ -49,13 +49,14 @@
</tr> </tr>
</table> </table>
</form> </form>
<a href="javascript:selectSubmit()">一覧に戻る</a>
</body> </body>
<script> <script>
function selectSubmit() { function selectSubmit() {
const form = document.createElement('form'); const form = document.createElement('form');
form.setAttribute('method', 'post'); form.setAttribute('method', 'post');
form.setAttribute('action', 'selectAll'); form.setAttribute('action', 'selectDepartment');
// 動的送信用のHidden // 動的送信用のHidden
// 動的にFormを作成する場合は何かしら送信しないと、正しく動作しない // 動的にFormを作成する場合は何かしら送信しないと、正しく動作しない

View File

@@ -13,9 +13,9 @@
</head> </head>
<body> <body>
<h2>更新が完了しました</h2> <h2>更新が完了しました</h2>
<br/> <br />
<a href="javascript:backSubmit()">戻る</a> <a href="javascript:backSubmit()">戻る</a>
</body> </body>
<script type="text/javascript"> <script type="text/javascript">