76-edit-and-delete-rule #77

Merged
romanov73 merged 4 commits from 76-edit-and-delete-rule into master 2022-10-24 17:05:16 +04:00
4 changed files with 41 additions and 35 deletions
Showing only changes of commit bf87d34f20 - Show all commits

View File

@ -2,11 +2,7 @@ package ru.ulstu.extractor.rule.controller;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.*;
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 org.springframework.web.servlet.mvc.support.RedirectAttributes;
import ru.ulstu.extractor.rule.model.AddRuleForm; import ru.ulstu.extractor.rule.model.AddRuleForm;
import ru.ulstu.extractor.rule.repository.RuleRepository; 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 ru.ulstu.extractor.ts.service.TimeSeriesService;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import static ru.ulstu.extractor.core.Route.ADD_RULE; import static ru.ulstu.extractor.core.Route.*;
import static ru.ulstu.extractor.core.Route.DELETE_RULE;
import static ru.ulstu.extractor.core.Route.LIST_RULE;
@Controller @Controller
@ApiIgnore @ApiIgnore
@ -46,12 +40,7 @@ public class RuleController {
public String getAntecedent(Model model, @RequestParam(required = false) Integer ruleId) { public String getAntecedent(Model model, @RequestParam(required = false) Integer ruleId) {
model.addAttribute("antecedentValues", antecedentValueService.getList()); model.addAttribute("antecedentValues", antecedentValueService.getList());
model.addAttribute("antecedents", timeSeriesService.getAllTimeSeriesTypes()); model.addAttribute("antecedents", timeSeriesService.getAllTimeSeriesTypes());
model.addAttribute("addRuleForm", model.addAttribute("addRuleForm", ruleService.getAddRuleFormOrDefault(ruleId));
ruleId != null
? new AddRuleForm(ruleRepository
.findById(ruleId)
.orElseThrow(() -> new RuntimeException("Правило не найдено")))
: new AddRuleForm());
return ADD_RULE; return ADD_RULE;
} }
@ -67,19 +56,4 @@ public class RuleController {
model.addAttribute("rule", ruleRepository.findAll()); model.addAttribute("rule", ruleRepository.findAll());
return "redirect:/" + LIST_RULE; 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; package ru.ulstu.extractor.rule.model;
public class AddRuleForm { public class AddRuleForm {
private Integer ruleId;
private Integer firstAntecedentValueId; private Integer firstAntecedentValueId;
private String firstAntecedentId; private String firstAntecedentId;
private Integer secondAntecedentValueId; private Integer secondAntecedentValueId;
@ -11,6 +12,7 @@ public class AddRuleForm {
} }
public AddRuleForm(Rule rule) { public AddRuleForm(Rule rule) {
this.ruleId = rule.getId();
this.firstAntecedentId = rule.getFirstAntecedent().name(); this.firstAntecedentId = rule.getFirstAntecedent().name();
this.secondAntecedentId = rule.getSecondAntecedent().name(); this.secondAntecedentId = rule.getSecondAntecedent().name();
this.firstAntecedentValueId = rule.getFirstAntecedentValue().getId(); this.firstAntecedentValueId = rule.getFirstAntecedentValue().getId();
@ -18,6 +20,14 @@ public class AddRuleForm {
this.consequent = rule.getConsequent(); this.consequent = rule.getConsequent();
} }
public Integer getRuleId() {
return ruleId;
}
public void setRuleId(Integer ruleId) {
this.ruleId = ruleId;
}
public Integer getFirstAntecedentValueId() { public Integer getFirstAntecedentValueId() {
return firstAntecedentValueId; return firstAntecedentValueId;
} }

View File

@ -28,14 +28,32 @@ public class RuleService {
} }
public void saveRule(AddRuleForm addRuleForm) { public void saveRule(AddRuleForm addRuleForm) {
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()), ruleRepository.save(new Rule(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()),
TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId()), TimeSeriesType.valueOf(addRuleForm.getFirstAntecedentId()),
antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()), antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()),
TimeSeriesType.valueOf(addRuleForm.getSecondAntecedentId()), TimeSeriesType.valueOf(addRuleForm.getSecondAntecedentId()),
addRuleForm.getConsequent())); addRuleForm.getConsequent()));
} }
}
public Rule findById(Integer id) { public Rule findById(Integer id) {
return ruleRepository.getOne(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> </head>
<div class="container" layout:fragment="content"> <div class="container" layout:fragment="content">
<form action="/listRule" th:action="${@route.ADD_RULE}" th:object="${addRuleForm}" method="post"> <form action="/listRule" th:action="${@route.ADD_RULE}" th:object="${addRuleForm}" method="post">
<input type="hidden" th:field="*{ruleId}">
<div class="row"> <div class="row">
<div class="col-md-2 col-sm-12"> <div class="col-md-2 col-sm-12">
Если Если
@ -71,7 +72,10 @@
<div class="row"> <div class="row">
<div class="col-md-4 col-sm-12"></div> <div class="col-md-4 col-sm-12"></div>
<div class="col-md-4 col-sm-12"> <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>
</div> </div>
</form> </form>