#76-add edit rule function

This commit is contained in:
BarminaA 2022-10-24 16:55:43 +04:00
parent 281d24bde2
commit bf87d34f20
4 changed files with 41 additions and 35 deletions

View File

@ -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<Integer> firstAntecedentValueId,
// @RequestParam Optional<String > firstAntecedentId,
// @RequestParam Optional<Integer> secondAntecedentValueId,
// @RequestParam Optional<String> secondAntecedentId,
// @RequestParam Optional<String> consequent){
// AddRuleForm addRuleForm = new AddRuleForm();
// addRuleForm.setFirstAntecedentId(firstAntecedentValueId.);
// addRuleForm.setFirstAntecedentValueId();
// addRuleForm.setSecondAntecedentId();
// addRuleForm.setSecondAntecedentValueId();
// addRuleForm.setConsequent();
// }
}

View File

@ -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;
}

View File

@ -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();
}
}

View File

@ -8,6 +8,7 @@
</head>
<div class="container" layout:fragment="content">
<form action="/listRule" th:action="${@route.ADD_RULE}" th:object="${addRuleForm}" method="post">
<input type="hidden" th:field="*{ruleId}">
<div class="row">
<div class="col-md-2 col-sm-12">
Если
@ -71,7 +72,10 @@
<div class="row">
<div class="col-md-4 col-sm-12"></div>
<div class="col-md-4 col-sm-12">
<input type="submit" class="btn btn-outline-success m-2" value="Создать правило"/>
<input type="submit" class="btn btn-outline-success m-2" th:if="*{ruleId == null}"
value="Создать правило"/>
<input type="submit" class="btn btn-outline-success m-2" th:if="*{ruleId != null}"
value="Сохранить правило"/>
</div>
</div>
</form>