Merge pull request '76-edit-and-delete-rule' (#77) from 76-edit-and-delete-rule into master

Reviewed-on: #77
This commit is contained in:
romanov73 2022-10-24 17:05:15 +04:00
commit 5e9afaaddd
7 changed files with 95 additions and 29 deletions

View File

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

View File

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

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

View File

@ -28,10 +28,36 @@ public class RuleService {
}
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()),
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);
}
}

View File

@ -8,12 +8,13 @@
</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">
Если
</div>
<div class="col-md-6 col-sm-12">
<select id="select-antecedent" class="selectpicker" data-live-search="true"
<select id="select-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{firstAntecedentId}"
data-width="90%">
<option th:each="antecedent : ${antecedents}"
@ -26,7 +27,7 @@
имеет тенденцию
</div>
<div class="col-md-2 col-sm-12">
<select id="select-measures" class="selectpicker" data-live-search="true"
<select id="select-measures" class="selectpicker m-2" data-live-search="true"
th:field="*{firstAntecedentValueId}"
data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}"
@ -39,7 +40,7 @@
и
</div>
<div class="col-md-6 col-sm-12">
<select id="select-second-antecedent" class="selectpicker" data-live-search="true"
<select id="select-second-antecedent" class="selectpicker m-2" data-live-search="true"
th:field="*{secondAntecedentId}"
data-width="90%">
<option th:each="antecedent : ${antecedents}"
@ -52,7 +53,7 @@
имеет тенденцию
</div>
<div class="col-md-2 col-sm-12">
<select id="select-second-measures" class="selectpicker" data-live-search="true"
<select id="select-second-measures" class="selectpicker m-2" data-live-search="true"
th:field="*{secondAntecedentValueId}"
data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}"
@ -65,13 +66,16 @@
то:
</div>
<div class="col-md-6 col-sm-12">
<input type="text" class="form-control" th:field="*{consequent}">
<input type="text" class="form-control m-1" th:field="*{consequent}">
</div>
</div>
<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" 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>

View File

@ -10,7 +10,7 @@
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<th scope="col" colspan="8">Правила</th>
<th scope="col" colspan="10">Правила</th>
</tr>
</thead>
<tbody>
@ -23,11 +23,20 @@
<td><span class="badge badge-success" th:text="${rule.secondAntecedentValue.antecedentValue}"></span></td>
<td><span class="badge badge-info">то</span></td>
<td><span class="badge badge-warning" th:text="${rule.consequent}"></span></td>
<td>
<a role="button" class="btn btn-info" th:href="@{${@route.ADD_RULE} + '?ruleId=' + ${rule.id}}">
<i class="fa fa-pencil-square-o" aria-hidden="true"></i>
</a>
</td>
<td>
<a role="button" class="btn btn-danger" th:href="@{'deleteRule?id=' + ${rule.id}}"
onclick="return confirm('Удалить правило?')">
<i class="fa fa-times" aria-hidden="true"></i>
</a>
</td>
</tr>
</tbody>
</table>
<div class="col-md-4 col-sm-12">
<a href="/addRule" class="btn btn-outline-success">Добавить правило</a>
</div>
</div>
</html>

View File

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