From 7dbe89169e9b9d71481cfc8ec0d67274eaf9ac3a Mon Sep 17 00:00:00 2001 From: BarminaA Date: Sat, 15 Oct 2022 14:26:53 +0400 Subject: [PATCH] #68-save rule in db --- .../ru/ulstu/extractor/controller/Route.java | 1 + .../extractor/controller/RuleController.java | 30 +++++++- .../java/ru/ulstu/extractor/model/Rule.java | 20 ++++- .../extractor/model/mvc/AddRuleForm.java | 63 +++++++++++++++ .../repository/AntecedentValueRepository.java | 8 ++ .../service/AntecedentValueService.java | 25 ++++++ .../ulstu/extractor/service/RuleService.java | 17 ++++- .../extractor/service/TimeSeriesService.java | 15 ++-- src/main/resources/templates/addRule.html | 76 +++++++++++++++++++ src/main/resources/templates/listRules.html | 2 +- src/main/resources/templates/rule.html | 49 ------------ 11 files changed, 247 insertions(+), 59 deletions(-) create mode 100644 src/main/java/ru/ulstu/extractor/model/mvc/AddRuleForm.java create mode 100644 src/main/java/ru/ulstu/extractor/repository/AntecedentValueRepository.java create mode 100644 src/main/java/ru/ulstu/extractor/service/AntecedentValueService.java create mode 100644 src/main/resources/templates/addRule.html delete mode 100644 src/main/resources/templates/rule.html diff --git a/src/main/java/ru/ulstu/extractor/controller/Route.java b/src/main/java/ru/ulstu/extractor/controller/Route.java index 0d3f39e..77e1efe 100644 --- a/src/main/java/ru/ulstu/extractor/controller/Route.java +++ b/src/main/java/ru/ulstu/extractor/controller/Route.java @@ -17,6 +17,7 @@ public class Route { public static final String FILTER_COMMITS = "filterCommits"; public static final String STATISTIC = "statistic"; public static final String LIST_RULE = "listRules"; + public static final String ADD_RULE = "addRule"; public static String getLIST_INDEXED_REPOSITORIES() { return LIST_INDEXED_REPOSITORIES; diff --git a/src/main/java/ru/ulstu/extractor/controller/RuleController.java b/src/main/java/ru/ulstu/extractor/controller/RuleController.java index f9c4eab..038005b 100644 --- a/src/main/java/ru/ulstu/extractor/controller/RuleController.java +++ b/src/main/java/ru/ulstu/extractor/controller/RuleController.java @@ -3,18 +3,30 @@ package ru.ulstu.extractor.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.servlet.mvc.support.RedirectAttributes; +import ru.ulstu.extractor.model.mvc.AddRuleForm; +import ru.ulstu.extractor.service.AntecedentValueService; import ru.ulstu.extractor.service.RuleService; +import ru.ulstu.extractor.service.TimeSeriesService; import springfox.documentation.annotations.ApiIgnore; +import static ru.ulstu.extractor.controller.Route.ADD_RULE; import static ru.ulstu.extractor.controller.Route.LIST_RULE; @Controller @ApiIgnore public class RuleController { - private RuleService ruleService; + private final RuleService ruleService; + private final AntecedentValueService antecedentValueService; + private final TimeSeriesService timeSeriesService; - public RuleController(RuleService ruleService) { + public RuleController(RuleService ruleService, AntecedentValueService antecedentValueService, TimeSeriesService timeSeriesService) { this.ruleService = ruleService; + this.antecedentValueService = antecedentValueService; + this.timeSeriesService = timeSeriesService; } @GetMapping(LIST_RULE) @@ -22,4 +34,18 @@ public class RuleController { model.addAttribute("rules", ruleService.getList()); return LIST_RULE; } + + @GetMapping(ADD_RULE) + public String getAntecedent(Model model) { + model.addAttribute("antecedentValues", antecedentValueService.getList()); + model.addAttribute("antecedents", timeSeriesService.getAllTimeSeries()); + model.addAttribute("addRuleForm", new AddRuleForm()); + return ADD_RULE; + } + + @RequestMapping(value = ADD_RULE, method = RequestMethod.POST) + public String addRule(@ModelAttribute AddRuleForm ruleForm, Model model, RedirectAttributes redirectAttributes) { + ruleService.saveRule(ruleForm); + return "redirect:/" + LIST_RULE; + } } diff --git a/src/main/java/ru/ulstu/extractor/model/Rule.java b/src/main/java/ru/ulstu/extractor/model/Rule.java index 0075b92..baf8565 100644 --- a/src/main/java/ru/ulstu/extractor/model/Rule.java +++ b/src/main/java/ru/ulstu/extractor/model/Rule.java @@ -22,7 +22,25 @@ public class Rule extends BaseEntity { public Rule() { } - public Rule(Integer id, Integer version, AntecedentValue firstAntecedentValue, TimeSeries firstAntecedent, AntecedentValue secondAntecedentValue, TimeSeries secondAntecedent, String consequent) { + public Rule(AntecedentValue firstAntecedentValue, + TimeSeries firstAntecedent, + AntecedentValue secondAntecedentValue, + TimeSeries secondAntecedent, + String consequent) { + this.firstAntecedentValue = firstAntecedentValue; + this.firstAntecedent = firstAntecedent; + this.secondAntecedentValue = secondAntecedentValue; + this.secondAntecedent = secondAntecedent; + this.consequent = consequent; + } + + public Rule(Integer id, + Integer version, + AntecedentValue firstAntecedentValue, + TimeSeries firstAntecedent, + AntecedentValue secondAntecedentValue, + TimeSeries secondAntecedent, + String consequent) { super(id, version); this.firstAntecedentValue = firstAntecedentValue; this.firstAntecedent = firstAntecedent; diff --git a/src/main/java/ru/ulstu/extractor/model/mvc/AddRuleForm.java b/src/main/java/ru/ulstu/extractor/model/mvc/AddRuleForm.java new file mode 100644 index 0000000..cf164d8 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/model/mvc/AddRuleForm.java @@ -0,0 +1,63 @@ +package ru.ulstu.extractor.model.mvc; + +public class AddRuleForm { + private Integer firstAntecedentValueId; + private Integer firstAntecedentId; + private Integer secondAntecedentValueId; + private Integer secondAntecedentId; + private String consequent; + + public AddRuleForm() { + } + + public Integer getFirstAntecedentValueId() { + return firstAntecedentValueId; + } + + public void setFirstAntecedentValueId(Integer firstAntecedentValueId) { + this.firstAntecedentValueId = firstAntecedentValueId; + } + + public Integer getFirstAntecedentId() { + return firstAntecedentId; + } + + public void setFirstAntecedentId(Integer firstAntecedentId) { + this.firstAntecedentId = firstAntecedentId; + } + + public Integer getSecondAntecedentValueId() { + return secondAntecedentValueId; + } + + public void setSecondAntecedentValueId(Integer secondAntecedentValueId) { + this.secondAntecedentValueId = secondAntecedentValueId; + } + + public Integer getSecondAntecedentId() { + return secondAntecedentId; + } + + public void setSecondAntecedentId(Integer secondAntecedentId) { + this.secondAntecedentId = secondAntecedentId; + } + + public String getConsequent() { + return consequent; + } + + public void setConsequent(String consequent) { + this.consequent = consequent; + } + + @Override + public String toString() { + return "AddRuleForm{" + + "firstAntecedentValueId=" + firstAntecedentValueId + + ", firstAntecedentId=" + firstAntecedentId + + ", secondAntecedentValueId=" + secondAntecedentValueId + + ", secondAntecedentId=" + secondAntecedentId + + ", consequent='" + consequent + '\'' + + '}'; + } +} diff --git a/src/main/java/ru/ulstu/extractor/repository/AntecedentValueRepository.java b/src/main/java/ru/ulstu/extractor/repository/AntecedentValueRepository.java new file mode 100644 index 0000000..86ea561 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/repository/AntecedentValueRepository.java @@ -0,0 +1,8 @@ +package ru.ulstu.extractor.repository; + + +import org.springframework.data.jpa.repository.JpaRepository; +import ru.ulstu.extractor.model.AntecedentValue; + +public interface AntecedentValueRepository extends JpaRepository { +} diff --git a/src/main/java/ru/ulstu/extractor/service/AntecedentValueService.java b/src/main/java/ru/ulstu/extractor/service/AntecedentValueService.java new file mode 100644 index 0000000..e2c48e6 --- /dev/null +++ b/src/main/java/ru/ulstu/extractor/service/AntecedentValueService.java @@ -0,0 +1,25 @@ +package ru.ulstu.extractor.service; + +import org.springframework.stereotype.Service; +import ru.ulstu.extractor.model.AntecedentValue; +import ru.ulstu.extractor.repository.AntecedentValueRepository; + +import java.util.List; + +@Service +public class AntecedentValueService { + private final AntecedentValueRepository antecedentValueRepository; + + public AntecedentValueService(AntecedentValueRepository antecedentValueRepository) { + this.antecedentValueRepository = antecedentValueRepository; + } + + public List getList() { + return antecedentValueRepository.findAll(); + } + + public AntecedentValue getById(Integer antecedentValueId) { + return antecedentValueRepository.findById(antecedentValueId) + .orElseThrow(() -> new RuntimeException("Antecedent value not found by id " + antecedentValueId)); + } +} diff --git a/src/main/java/ru/ulstu/extractor/service/RuleService.java b/src/main/java/ru/ulstu/extractor/service/RuleService.java index 7b9f4e1..5c8acd7 100644 --- a/src/main/java/ru/ulstu/extractor/service/RuleService.java +++ b/src/main/java/ru/ulstu/extractor/service/RuleService.java @@ -2,6 +2,7 @@ package ru.ulstu.extractor.service; import org.springframework.stereotype.Service; import ru.ulstu.extractor.model.Rule; +import ru.ulstu.extractor.model.mvc.AddRuleForm; import ru.ulstu.extractor.repository.RuleRepository; import java.util.List; @@ -9,12 +10,26 @@ import java.util.List; @Service public class RuleService { private final RuleRepository ruleRepository; + private final TimeSeriesService timeSeriesService; + private final AntecedentValueService antecedentValueService; - public RuleService(RuleRepository ruleRepository) { + public RuleService(RuleRepository ruleRepository, + TimeSeriesService timeSeriesService, + AntecedentValueService antecedentValueService) { this.ruleRepository = ruleRepository; + this.timeSeriesService = timeSeriesService; + this.antecedentValueService = antecedentValueService; } public List getList() { return ruleRepository.findAll(); } + + public void saveRule(AddRuleForm addRuleForm) { + ruleRepository.save(new Rule(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()), + timeSeriesService.getById(addRuleForm.getFirstAntecedentId()), + antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()), + timeSeriesService.getById(addRuleForm.getSecondAntecedentId()), + addRuleForm.getConsequent())); + } } diff --git a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java index 706b52f..f978433 100644 --- a/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java +++ b/src/main/java/ru/ulstu/extractor/service/TimeSeriesService.java @@ -18,11 +18,7 @@ import ru.ulstu.extractor.repository.TimeSeriesValueRepository; import ru.ulstu.extractor.ts.TimeSeriesDateMapper; import javax.transaction.Transactional; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; import java.util.stream.Collectors; @Service @@ -113,4 +109,13 @@ public class TimeSeriesService { } }).start(); } + + public List getAllTimeSeries() { + return timeSeriesRepository.findAll(); + } + + public TimeSeries getById(Integer tsId) { + return timeSeriesRepository.findById(tsId) + .orElseThrow(() -> new RuntimeException("Time series not found by id " + tsId)); + } } diff --git a/src/main/resources/templates/addRule.html b/src/main/resources/templates/addRule.html new file mode 100644 index 0000000..8c709d0 --- /dev/null +++ b/src/main/resources/templates/addRule.html @@ -0,0 +1,76 @@ + + + + Простая обработка формы на Spring MVC + + +
+
+
+
+ Если +
+
+ +
+
+ временной ряд +
+
+ +
+
+ и +
+
+ +
+
+ временной ряд +
+
+ +
+
+ то: +
+
+ +
+
+ +
+
+
+
+ diff --git a/src/main/resources/templates/listRules.html b/src/main/resources/templates/listRules.html index c10aa15..93e139a 100644 --- a/src/main/resources/templates/listRules.html +++ b/src/main/resources/templates/listRules.html @@ -27,7 +27,7 @@ diff --git a/src/main/resources/templates/rule.html b/src/main/resources/templates/rule.html deleted file mode 100644 index 00be0ad..0000000 --- a/src/main/resources/templates/rule.html +++ /dev/null @@ -1,49 +0,0 @@ - - - - Простая обработка формы на Spring MVC - - -
- -
-
-
- Если -
-
- -
-
- времянной ряд -
-
- -
-
-
-
- то: -
-
- -
-
- -
-
-
-
-