From bf87d34f2002869ee21f7e3b20258a1690ebd984 Mon Sep 17 00:00:00 2001 From: BarminaA Date: Mon, 24 Oct 2022 16:55:43 +0400 Subject: [PATCH] #76-add edit rule function --- .../rule/controller/RuleController.java | 32 ++----------------- .../extractor/rule/model/AddRuleForm.java | 10 ++++++ .../extractor/rule/service/RuleService.java | 28 +++++++++++++--- src/main/resources/templates/addRule.html | 6 +++- 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/rule/controller/RuleController.java b/src/main/java/ru/ulstu/extractor/rule/controller/RuleController.java index abd9b8f..a3bc397 100644 --- a/src/main/java/ru/ulstu/extractor/rule/controller/RuleController.java +++ b/src/main/java/ru/ulstu/extractor/rule/controller/RuleController.java @@ -2,11 +2,7 @@ package ru.ulstu.extractor.rule.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -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 org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.mvc.support.RedirectAttributes; import ru.ulstu.extractor.rule.model.AddRuleForm; import ru.ulstu.extractor.rule.repository.RuleRepository; @@ -15,9 +11,7 @@ import ru.ulstu.extractor.rule.service.RuleService; import ru.ulstu.extractor.ts.service.TimeSeriesService; import springfox.documentation.annotations.ApiIgnore; -import static ru.ulstu.extractor.core.Route.ADD_RULE; -import static ru.ulstu.extractor.core.Route.DELETE_RULE; -import static ru.ulstu.extractor.core.Route.LIST_RULE; +import static ru.ulstu.extractor.core.Route.*; @Controller @ApiIgnore @@ -46,12 +40,7 @@ public class RuleController { public String getAntecedent(Model model, @RequestParam(required = false) Integer ruleId) { model.addAttribute("antecedentValues", antecedentValueService.getList()); model.addAttribute("antecedents", timeSeriesService.getAllTimeSeriesTypes()); - model.addAttribute("addRuleForm", - ruleId != null - ? new AddRuleForm(ruleRepository - .findById(ruleId) - .orElseThrow(() -> new RuntimeException("Правило не найдено"))) - : new AddRuleForm()); + model.addAttribute("addRuleForm", ruleService.getAddRuleFormOrDefault(ruleId)); return ADD_RULE; } @@ -67,19 +56,4 @@ public class RuleController { model.addAttribute("rule", ruleRepository.findAll()); return "redirect:/" + LIST_RULE; } - -// @RequestMapping(value = FILTER_COMMITS, method = RequestMethod.GET) -// public String editRule(Model model, -// @RequestParam Optional firstAntecedentValueId, -// @RequestParam Optional firstAntecedentId, -// @RequestParam Optional secondAntecedentValueId, -// @RequestParam Optional secondAntecedentId, -// @RequestParam Optional consequent){ -// AddRuleForm addRuleForm = new AddRuleForm(); -// addRuleForm.setFirstAntecedentId(firstAntecedentValueId.); -// addRuleForm.setFirstAntecedentValueId(); -// addRuleForm.setSecondAntecedentId(); -// addRuleForm.setSecondAntecedentValueId(); -// addRuleForm.setConsequent(); -// } } diff --git a/src/main/java/ru/ulstu/extractor/rule/model/AddRuleForm.java b/src/main/java/ru/ulstu/extractor/rule/model/AddRuleForm.java index 4878b30..7834b45 100644 --- a/src/main/java/ru/ulstu/extractor/rule/model/AddRuleForm.java +++ b/src/main/java/ru/ulstu/extractor/rule/model/AddRuleForm.java @@ -1,6 +1,7 @@ package ru.ulstu.extractor.rule.model; public class AddRuleForm { + private Integer ruleId; private Integer firstAntecedentValueId; private String firstAntecedentId; private Integer secondAntecedentValueId; @@ -11,6 +12,7 @@ public class AddRuleForm { } public AddRuleForm(Rule rule) { + this.ruleId = rule.getId(); this.firstAntecedentId = rule.getFirstAntecedent().name(); this.secondAntecedentId = rule.getSecondAntecedent().name(); this.firstAntecedentValueId = rule.getFirstAntecedentValue().getId(); @@ -18,6 +20,14 @@ public class AddRuleForm { this.consequent = rule.getConsequent(); } + public Integer getRuleId() { + return ruleId; + } + + public void setRuleId(Integer ruleId) { + this.ruleId = ruleId; + } + public Integer getFirstAntecedentValueId() { return firstAntecedentValueId; } diff --git a/src/main/java/ru/ulstu/extractor/rule/service/RuleService.java b/src/main/java/ru/ulstu/extractor/rule/service/RuleService.java index 71fd09a..ae28f8f 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/RuleService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/RuleService.java @@ -28,14 +28,32 @@ public class RuleService { } public void saveRule(AddRuleForm addRuleForm) { - ruleRepository.save(new Rule(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()), - TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId()), - antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()), - TimeSeriesType.valueOf(addRuleForm.getSecondAntecedentId()), - addRuleForm.getConsequent())); + if (addRuleForm.getRuleId() != null) { + Rule rule = ruleRepository.getOne(addRuleForm.getRuleId()); + rule.setConsequent(addRuleForm.getConsequent()); + rule.setFirstAntecedent(TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId())); + rule.setFirstAntecedentValue(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId())); + rule.setSecondAntecedent(TimeSeriesType.valueOf(addRuleForm.getSecondAntecedentId())); + rule.setSecondAntecedentValue(antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId())); + ruleRepository.save(rule); + } else { + ruleRepository.save(new Rule(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()), + TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId()), + antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()), + TimeSeriesType.valueOf(addRuleForm.getSecondAntecedentId()), + addRuleForm.getConsequent())); + } } public Rule findById(Integer id) { return ruleRepository.getOne(id); } + + public AddRuleForm getAddRuleFormOrDefault(Integer id) { + return id != null + ? new AddRuleForm(ruleRepository + .findById(id) + .orElseThrow(() -> new RuntimeException("Правило не найдено"))) + : new AddRuleForm(); + } } diff --git a/src/main/resources/templates/addRule.html b/src/main/resources/templates/addRule.html index fd5b269..0abb1e8 100644 --- a/src/main/resources/templates/addRule.html +++ b/src/main/resources/templates/addRule.html @@ -8,6 +8,7 @@
+
Если @@ -71,7 +72,10 @@
- + +