diff --git a/src/main/java/ru/ulstu/extractor/core/Route.java b/src/main/java/ru/ulstu/extractor/core/Route.java index 9627848..0ea2caa 100644 --- a/src/main/java/ru/ulstu/extractor/core/Route.java +++ b/src/main/java/ru/ulstu/extractor/core/Route.java @@ -18,6 +18,7 @@ public class Route { public static final String STATISTIC = "statistic"; public static final String LIST_RULE = "listRules"; public static final String ADD_RULE = "addRule"; + public static final String DELETE_RULE = "deleteRule"; public static String getLIST_INDEXED_REPOSITORIES() { return LIST_INDEXED_REPOSITORIES; 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 7aeeff1..7893dea 100644 --- a/src/main/java/ru/ulstu/extractor/rule/controller/RuleController.java +++ b/src/main/java/ru/ulstu/extractor/rule/controller/RuleController.java @@ -6,6 +6,7 @@ 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.servlet.mvc.support.RedirectAttributes; import ru.ulstu.extractor.rule.model.AddRuleForm; import ru.ulstu.extractor.rule.service.AntecedentValueService; @@ -14,6 +15,7 @@ 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; @Controller @@ -38,10 +40,10 @@ public class RuleController { } @GetMapping(ADD_RULE) - public String getAntecedent(Model model) { + public String getAntecedent(Model model, @RequestParam(required = false) Integer ruleId) { model.addAttribute("antecedentValues", antecedentValueService.getList()); model.addAttribute("antecedents", timeSeriesService.getAllTimeSeriesTypes()); - model.addAttribute("addRuleForm", new AddRuleForm()); + model.addAttribute("addRuleForm", ruleService.getAddRuleFormOrDefault(ruleId)); return ADD_RULE; } @@ -50,4 +52,10 @@ public class RuleController { ruleService.saveRule(ruleForm); return "redirect:/" + LIST_RULE; } + + @GetMapping(DELETE_RULE) + public String deleteRule(Model model, @RequestParam Integer id) { + ruleService.deleteById(id); + return "redirect:/" + LIST_RULE; + } } 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 3b64bdd..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; @@ -10,6 +11,23 @@ public class AddRuleForm { public AddRuleForm() { } + public AddRuleForm(Rule rule) { + this.ruleId = rule.getId(); + this.firstAntecedentId = rule.getFirstAntecedent().name(); + this.secondAntecedentId = rule.getSecondAntecedent().name(); + this.firstAntecedentValueId = rule.getFirstAntecedentValue().getId(); + this.secondAntecedentValueId = rule.getSecondAntecedentValue().getId(); + 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 63413b9..bb2e2fe 100644 --- a/src/main/java/ru/ulstu/extractor/rule/service/RuleService.java +++ b/src/main/java/ru/ulstu/extractor/rule/service/RuleService.java @@ -28,10 +28,36 @@ 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(); + } + + public void deleteById(Integer id) { + ruleRepository.deleteById(id); } } diff --git a/src/main/resources/templates/addRule.html b/src/main/resources/templates/addRule.html index 191e07a..0abb1e8 100644 --- a/src/main/resources/templates/addRule.html +++ b/src/main/resources/templates/addRule.html @@ -8,12 +8,13 @@
+
Если
- diff --git a/src/main/resources/templates/listRules.html b/src/main/resources/templates/listRules.html index 1f71b46..d6f3ea1 100644 --- a/src/main/resources/templates/listRules.html +++ b/src/main/resources/templates/listRules.html @@ -10,7 +10,7 @@ - + @@ -23,11 +23,20 @@ + +
ПравилаПравила
то + + + + + + + +
- + Добавить правило
diff --git a/src/test/java/ru/ulstu/TimeSeriesMapperTest.java b/src/test/java/ru/ulstu/TimeSeriesMapperTest.java index c1cae8d..d799a2b 100644 --- a/src/test/java/ru/ulstu/TimeSeriesMapperTest.java +++ b/src/test/java/ru/ulstu/TimeSeriesMapperTest.java @@ -18,8 +18,8 @@ public class TimeSeriesMapperTest { Calendar c2 = GregorianCalendar.getInstance(); c2.set(2020, 5, 2, 2, 1, 1); TimeSeries timeSeries = new TimeSeries("Тестовый", - Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), - new TimeSeriesValue(c2.getTime(), 10))); + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0), + new TimeSeriesValue(c2.getTime(), 10.0))); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); Assert.assertEquals(1, timeSeries.getValues().size()); @@ -33,8 +33,8 @@ public class TimeSeriesMapperTest { Calendar c2 = GregorianCalendar.getInstance(); c2.set(2020, 5, 2, 1, 1, 1); TimeSeries timeSeries = new TimeSeries("Тестовый", - Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), - new TimeSeriesValue(c2.getTime(), 10))); + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0), + new TimeSeriesValue(c2.getTime(), 10.0))); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); Assert.assertEquals(1, timeSeries.getValues().size()); @@ -48,8 +48,8 @@ public class TimeSeriesMapperTest { Calendar c2 = GregorianCalendar.getInstance(); c2.set(2020, 5, 2, 1, 1, 1); TimeSeries timeSeries = new TimeSeries("Тестовый", - Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), - new TimeSeriesValue(c2.getTime(), 10))); + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0), + new TimeSeriesValue(c2.getTime(), 10.0))); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); Assert.assertEquals(1, timeSeries.getValues().size()); @@ -63,8 +63,8 @@ public class TimeSeriesMapperTest { Calendar c2 = GregorianCalendar.getInstance(); c2.set(2020, 6, 2, 1, 1, 1); TimeSeries timeSeries = new TimeSeries("Тестовый", - Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), - new TimeSeriesValue(c2.getTime(), 10))); + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0), + new TimeSeriesValue(c2.getTime(), 10.0))); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); Assert.assertEquals(2, timeSeries.getValues().size()); @@ -78,8 +78,8 @@ public class TimeSeriesMapperTest { Calendar c2 = GregorianCalendar.getInstance(); c2.set(2020, 5, 2, 1, 1, 1); TimeSeries timeSeries = new TimeSeries("Тестовый", - Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), - new TimeSeriesValue(c2.getTime(), 10))); + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0), + new TimeSeriesValue(c2.getTime(), 10.0))); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries); Assert.assertEquals(1, timeSeries.getValues().size()); @@ -93,8 +93,8 @@ public class TimeSeriesMapperTest { Calendar c2 = GregorianCalendar.getInstance(); c2.set(2021, 5, 2, 1, 1, 1); TimeSeries timeSeries = new TimeSeries("Тестовый", - Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), - new TimeSeriesValue(c2.getTime(), 10))); + Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0), + new TimeSeriesValue(c2.getTime(), 10.0))); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries); Assert.assertEquals(2, timeSeries.getValues().size());