#76 -- fix edit

This commit is contained in:
Anton Romanov 2022-10-21 23:20:48 +04:00
parent 616bb3385e
commit 281d24bde2
4 changed files with 42 additions and 24 deletions

View File

@ -2,7 +2,11 @@ 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.*; 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 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;
@ -11,7 +15,9 @@ 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.*; 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 @Controller
@ApiIgnore @ApiIgnore
@ -37,10 +43,15 @@ public class RuleController {
} }
@GetMapping(ADD_RULE) @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("antecedentValues", antecedentValueService.getList());
model.addAttribute("antecedents", timeSeriesService.getAllTimeSeriesTypes()); model.addAttribute("antecedents", timeSeriesService.getAllTimeSeriesTypes());
model.addAttribute("addRuleForm", new AddRuleForm()); model.addAttribute("addRuleForm",
ruleId != null
? new AddRuleForm(ruleRepository
.findById(ruleId)
.orElseThrow(() -> new RuntimeException("Правило не найдено")))
: new AddRuleForm());
return ADD_RULE; return ADD_RULE;
} }
@ -51,8 +62,7 @@ public class RuleController {
} }
@GetMapping(DELETE_RULE) @GetMapping(DELETE_RULE)
public String deleteRule(Model model, public String deleteRule(Model model, @RequestParam Integer id) {
@RequestParam Integer id) {
ruleRepository.deleteById(id); ruleRepository.deleteById(id);
model.addAttribute("rule", ruleRepository.findAll()); model.addAttribute("rule", ruleRepository.findAll());
return "redirect:/" + LIST_RULE; return "redirect:/" + LIST_RULE;

View File

@ -10,6 +10,14 @@ public class AddRuleForm {
public AddRuleForm() { public AddRuleForm() {
} }
public AddRuleForm(Rule rule) {
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 getFirstAntecedentValueId() { public Integer getFirstAntecedentValueId() {
return firstAntecedentValueId; return firstAntecedentValueId;
} }

View File

@ -13,7 +13,7 @@
Если Если
</div> </div>
<div class="col-md-6 col-sm-12"> <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}" th:field="*{firstAntecedentId}"
data-width="90%"> data-width="90%">
<option th:each="antecedent : ${antecedents}" <option th:each="antecedent : ${antecedents}"
@ -26,7 +26,7 @@
имеет тенденцию имеет тенденцию
</div> </div>
<div class="col-md-2 col-sm-12"> <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}" th:field="*{firstAntecedentValueId}"
data-width="100%"> data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}" <option th:each="antecedentValue : ${antecedentValues}"
@ -39,7 +39,7 @@
и и
</div> </div>
<div class="col-md-6 col-sm-12"> <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}" th:field="*{secondAntecedentId}"
data-width="90%"> data-width="90%">
<option th:each="antecedent : ${antecedents}" <option th:each="antecedent : ${antecedents}"
@ -52,7 +52,7 @@
имеет тенденцию имеет тенденцию
</div> </div>
<div class="col-md-2 col-sm-12"> <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}" th:field="*{secondAntecedentValueId}"
data-width="100%"> data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}" <option th:each="antecedentValue : ${antecedentValues}"
@ -65,13 +65,13 @@
то: то:
</div> </div>
<div class="col-md-6 col-sm-12"> <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> </div>
<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" value="Создать правило"/> <input type="submit" class="btn btn-outline-success m-2" value="Создать правило"/>
</div> </div>
</div> </div>
</form> </form>

View File

@ -18,8 +18,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 2, 1, 1); c2.set(2020, 5, 2, 2, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size()); Assert.assertEquals(1, timeSeries.getValues().size());
@ -33,8 +33,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 1, 1, 1); c2.set(2020, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size()); Assert.assertEquals(1, timeSeries.getValues().size());
@ -48,8 +48,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 1, 1, 1); c2.set(2020, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size()); Assert.assertEquals(1, timeSeries.getValues().size());
@ -63,8 +63,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 6, 2, 1, 1, 1); c2.set(2020, 6, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.MONTH, timeSeries);
Assert.assertEquals(2, timeSeries.getValues().size()); Assert.assertEquals(2, timeSeries.getValues().size());
@ -78,8 +78,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2020, 5, 2, 1, 1, 1); c2.set(2020, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
Assert.assertEquals(1, timeSeries.getValues().size()); Assert.assertEquals(1, timeSeries.getValues().size());
@ -93,8 +93,8 @@ public class TimeSeriesMapperTest {
Calendar c2 = GregorianCalendar.getInstance(); Calendar c2 = GregorianCalendar.getInstance();
c2.set(2021, 5, 2, 1, 1, 1); c2.set(2021, 5, 2, 1, 1, 1);
TimeSeries timeSeries = new TimeSeries("Тестовый", TimeSeries timeSeries = new TimeSeries("Тестовый",
Arrays.asList(new TimeSeriesValue(c1.getTime(), 10), Arrays.asList(new TimeSeriesValue(c1.getTime(), 10.0),
new TimeSeriesValue(c2.getTime(), 10))); new TimeSeriesValue(c2.getTime(), 10.0)));
TimeSeriesDateMapper mapper = new TimeSeriesDateMapper(); TimeSeriesDateMapper mapper = new TimeSeriesDateMapper();
timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries); timeSeries = mapper.mapTimeSeriesToInterval(TimeSeriesDateMapper.TimeSeriesInterval.YEAR, timeSeries);
Assert.assertEquals(2, timeSeries.getValues().size()); Assert.assertEquals(2, timeSeries.getValues().size());