diff --git a/src/main/java/com/example/rhizome/app/domain/service/UpdateDepartment.java b/src/main/java/com/example/rhizome/app/domain/service/UpdateDepartment.java new file mode 100644 index 0000000..33ae428 --- /dev/null +++ b/src/main/java/com/example/rhizome/app/domain/service/UpdateDepartment.java @@ -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); +} diff --git a/src/main/java/com/example/rhizome/app/domain/service/impl/UpdateDepartmentImpl.java b/src/main/java/com/example/rhizome/app/domain/service/impl/UpdateDepartmentImpl.java new file mode 100644 index 0000000..d047764 --- /dev/null +++ b/src/main/java/com/example/rhizome/app/domain/service/impl/UpdateDepartmentImpl.java @@ -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)); + } + +} diff --git a/src/main/java/com/example/rhizome/app/presentation/controller/UpdateDepartmentController.java b/src/main/java/com/example/rhizome/app/presentation/controller/UpdateDepartmentController.java new file mode 100644 index 0000000..6837f8a --- /dev/null +++ b/src/main/java/com/example/rhizome/app/presentation/controller/UpdateDepartmentController.java @@ -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 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; + + } + } +} diff --git a/src/main/java/com/example/rhizome/app/presentation/controller/UpdateDepartmentResultController.java b/src/main/java/com/example/rhizome/app/presentation/controller/UpdateDepartmentResultController.java new file mode 100644 index 0000000..15aa629 --- /dev/null +++ b/src/main/java/com/example/rhizome/app/presentation/controller/UpdateDepartmentResultController.java @@ -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; + } + } +} diff --git a/src/main/java/com/example/rhizome/app/presentation/controller/pageproperty/PageReturnAttributeKeyword.java b/src/main/java/com/example/rhizome/app/presentation/controller/pageproperty/PageReturnAttributeKeyword.java index 5e9ec04..3a5e9f1 100644 --- a/src/main/java/com/example/rhizome/app/presentation/controller/pageproperty/PageReturnAttributeKeyword.java +++ b/src/main/java/com/example/rhizome/app/presentation/controller/pageproperty/PageReturnAttributeKeyword.java @@ -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"; diff --git a/src/main/java/com/example/rhizome/app/presentation/controller/pageproperty/TransitionTargetPageNameKeyword.java b/src/main/java/com/example/rhizome/app/presentation/controller/pageproperty/TransitionTargetPageNameKeyword.java index d191c4b..2a673e2 100644 --- a/src/main/java/com/example/rhizome/app/presentation/controller/pageproperty/TransitionTargetPageNameKeyword.java +++ b/src/main/java/com/example/rhizome/app/presentation/controller/pageproperty/TransitionTargetPageNameKeyword.java @@ -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); + } /** diff --git a/src/main/java/com/example/rhizome/app/presentation/form/UpdateDepartmentForm.java b/src/main/java/com/example/rhizome/app/presentation/form/UpdateDepartmentForm.java new file mode 100644 index 0000000..47455f1 --- /dev/null +++ b/src/main/java/com/example/rhizome/app/presentation/form/UpdateDepartmentForm.java @@ -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; + } +} diff --git a/src/main/resources/templates/updateDepartmentInput.html b/src/main/resources/templates/updateDepartmentInput.html new file mode 100644 index 0000000..b6ccbf0 --- /dev/null +++ b/src/main/resources/templates/updateDepartmentInput.html @@ -0,0 +1,59 @@ + + + + + + + + + + + 部署登録更新 + + + +

+ 社員情報更新    +
+ ログイン者名: + +
+

+
+ +
+ +
+ + + + + + + + + + + + +
部署ID + +
部署名 + +
+
+ 一覧に戻る + + + + \ No newline at end of file