#68-save rule in db

This commit is contained in:
BarminaA 2022-10-15 14:26:53 +04:00
parent ff7c9ce665
commit 7dbe89169e
11 changed files with 247 additions and 59 deletions

View File

@ -17,6 +17,7 @@ public class Route {
public static final String FILTER_COMMITS = "filterCommits"; public static final String FILTER_COMMITS = "filterCommits";
public static final String STATISTIC = "statistic"; public static final String STATISTIC = "statistic";
public static final String LIST_RULE = "listRules"; public static final String LIST_RULE = "listRules";
public static final String ADD_RULE = "addRule";
public static String getLIST_INDEXED_REPOSITORIES() { public static String getLIST_INDEXED_REPOSITORIES() {
return LIST_INDEXED_REPOSITORIES; return LIST_INDEXED_REPOSITORIES;

View File

@ -3,18 +3,30 @@ package ru.ulstu.extractor.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.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.servlet.mvc.support.RedirectAttributes;
import ru.ulstu.extractor.model.mvc.AddRuleForm;
import ru.ulstu.extractor.service.AntecedentValueService;
import ru.ulstu.extractor.service.RuleService; import ru.ulstu.extractor.service.RuleService;
import ru.ulstu.extractor.service.TimeSeriesService;
import springfox.documentation.annotations.ApiIgnore; import springfox.documentation.annotations.ApiIgnore;
import static ru.ulstu.extractor.controller.Route.ADD_RULE;
import static ru.ulstu.extractor.controller.Route.LIST_RULE; import static ru.ulstu.extractor.controller.Route.LIST_RULE;
@Controller @Controller
@ApiIgnore @ApiIgnore
public class RuleController { public class RuleController {
private RuleService ruleService; private final RuleService ruleService;
private final AntecedentValueService antecedentValueService;
private final TimeSeriesService timeSeriesService;
public RuleController(RuleService ruleService) { public RuleController(RuleService ruleService, AntecedentValueService antecedentValueService, TimeSeriesService timeSeriesService) {
this.ruleService = ruleService; this.ruleService = ruleService;
this.antecedentValueService = antecedentValueService;
this.timeSeriesService = timeSeriesService;
} }
@GetMapping(LIST_RULE) @GetMapping(LIST_RULE)
@ -22,4 +34,18 @@ public class RuleController {
model.addAttribute("rules", ruleService.getList()); model.addAttribute("rules", ruleService.getList());
return LIST_RULE; return LIST_RULE;
} }
@GetMapping(ADD_RULE)
public String getAntecedent(Model model) {
model.addAttribute("antecedentValues", antecedentValueService.getList());
model.addAttribute("antecedents", timeSeriesService.getAllTimeSeries());
model.addAttribute("addRuleForm", new AddRuleForm());
return ADD_RULE;
}
@RequestMapping(value = ADD_RULE, method = RequestMethod.POST)
public String addRule(@ModelAttribute AddRuleForm ruleForm, Model model, RedirectAttributes redirectAttributes) {
ruleService.saveRule(ruleForm);
return "redirect:/" + LIST_RULE;
}
} }

View File

@ -22,7 +22,25 @@ public class Rule extends BaseEntity {
public Rule() { public Rule() {
} }
public Rule(Integer id, Integer version, AntecedentValue firstAntecedentValue, TimeSeries firstAntecedent, AntecedentValue secondAntecedentValue, TimeSeries secondAntecedent, String consequent) { public Rule(AntecedentValue firstAntecedentValue,
TimeSeries firstAntecedent,
AntecedentValue secondAntecedentValue,
TimeSeries secondAntecedent,
String consequent) {
this.firstAntecedentValue = firstAntecedentValue;
this.firstAntecedent = firstAntecedent;
this.secondAntecedentValue = secondAntecedentValue;
this.secondAntecedent = secondAntecedent;
this.consequent = consequent;
}
public Rule(Integer id,
Integer version,
AntecedentValue firstAntecedentValue,
TimeSeries firstAntecedent,
AntecedentValue secondAntecedentValue,
TimeSeries secondAntecedent,
String consequent) {
super(id, version); super(id, version);
this.firstAntecedentValue = firstAntecedentValue; this.firstAntecedentValue = firstAntecedentValue;
this.firstAntecedent = firstAntecedent; this.firstAntecedent = firstAntecedent;

View File

@ -0,0 +1,63 @@
package ru.ulstu.extractor.model.mvc;
public class AddRuleForm {
private Integer firstAntecedentValueId;
private Integer firstAntecedentId;
private Integer secondAntecedentValueId;
private Integer secondAntecedentId;
private String consequent;
public AddRuleForm() {
}
public Integer getFirstAntecedentValueId() {
return firstAntecedentValueId;
}
public void setFirstAntecedentValueId(Integer firstAntecedentValueId) {
this.firstAntecedentValueId = firstAntecedentValueId;
}
public Integer getFirstAntecedentId() {
return firstAntecedentId;
}
public void setFirstAntecedentId(Integer firstAntecedentId) {
this.firstAntecedentId = firstAntecedentId;
}
public Integer getSecondAntecedentValueId() {
return secondAntecedentValueId;
}
public void setSecondAntecedentValueId(Integer secondAntecedentValueId) {
this.secondAntecedentValueId = secondAntecedentValueId;
}
public Integer getSecondAntecedentId() {
return secondAntecedentId;
}
public void setSecondAntecedentId(Integer secondAntecedentId) {
this.secondAntecedentId = secondAntecedentId;
}
public String getConsequent() {
return consequent;
}
public void setConsequent(String consequent) {
this.consequent = consequent;
}
@Override
public String toString() {
return "AddRuleForm{" +
"firstAntecedentValueId=" + firstAntecedentValueId +
", firstAntecedentId=" + firstAntecedentId +
", secondAntecedentValueId=" + secondAntecedentValueId +
", secondAntecedentId=" + secondAntecedentId +
", consequent='" + consequent + '\'' +
'}';
}
}

View File

@ -0,0 +1,8 @@
package ru.ulstu.extractor.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.extractor.model.AntecedentValue;
public interface AntecedentValueRepository extends JpaRepository<AntecedentValue, Integer> {
}

View File

@ -0,0 +1,25 @@
package ru.ulstu.extractor.service;
import org.springframework.stereotype.Service;
import ru.ulstu.extractor.model.AntecedentValue;
import ru.ulstu.extractor.repository.AntecedentValueRepository;
import java.util.List;
@Service
public class AntecedentValueService {
private final AntecedentValueRepository antecedentValueRepository;
public AntecedentValueService(AntecedentValueRepository antecedentValueRepository) {
this.antecedentValueRepository = antecedentValueRepository;
}
public List<AntecedentValue> getList() {
return antecedentValueRepository.findAll();
}
public AntecedentValue getById(Integer antecedentValueId) {
return antecedentValueRepository.findById(antecedentValueId)
.orElseThrow(() -> new RuntimeException("Antecedent value not found by id " + antecedentValueId));
}
}

View File

@ -2,6 +2,7 @@ package ru.ulstu.extractor.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import ru.ulstu.extractor.model.Rule; import ru.ulstu.extractor.model.Rule;
import ru.ulstu.extractor.model.mvc.AddRuleForm;
import ru.ulstu.extractor.repository.RuleRepository; import ru.ulstu.extractor.repository.RuleRepository;
import java.util.List; import java.util.List;
@ -9,12 +10,26 @@ import java.util.List;
@Service @Service
public class RuleService { public class RuleService {
private final RuleRepository ruleRepository; private final RuleRepository ruleRepository;
private final TimeSeriesService timeSeriesService;
private final AntecedentValueService antecedentValueService;
public RuleService(RuleRepository ruleRepository) { public RuleService(RuleRepository ruleRepository,
TimeSeriesService timeSeriesService,
AntecedentValueService antecedentValueService) {
this.ruleRepository = ruleRepository; this.ruleRepository = ruleRepository;
this.timeSeriesService = timeSeriesService;
this.antecedentValueService = antecedentValueService;
} }
public List<Rule> getList() { public List<Rule> getList() {
return ruleRepository.findAll(); return ruleRepository.findAll();
} }
public void saveRule(AddRuleForm addRuleForm) {
ruleRepository.save(new Rule(antecedentValueService.getById(addRuleForm.getFirstAntecedentValueId()),
timeSeriesService.getById(addRuleForm.getFirstAntecedentId()),
antecedentValueService.getById(addRuleForm.getSecondAntecedentValueId()),
timeSeriesService.getById(addRuleForm.getSecondAntecedentId()),
addRuleForm.getConsequent()));
}
} }

View File

@ -18,11 +18,7 @@ import ru.ulstu.extractor.repository.TimeSeriesValueRepository;
import ru.ulstu.extractor.ts.TimeSeriesDateMapper; import ru.ulstu.extractor.ts.TimeSeriesDateMapper;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@ -113,4 +109,13 @@ public class TimeSeriesService {
} }
}).start(); }).start();
} }
public List<TimeSeries> getAllTimeSeries() {
return timeSeriesRepository.findAll();
}
public TimeSeries getById(Integer tsId) {
return timeSeriesRepository.findById(tsId)
.orElseThrow(() -> new RuntimeException("Time series not found by id " + tsId));
}
} }

View File

@ -0,0 +1,76 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}">
<head>
<title>Простая обработка формы на Spring MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<form action="/listRule" th:action="${@route.ADD_RULE}" th:object="${addRuleForm}" method="post">
<div class="row">
<div class="col-md-2 col-sm-12">
Если
</div>
<div class="col-md-2 col-sm-12">
<select id="select-measures" class="selectpicker" data-live-search="true"
th:field="*{firstAntecedentValueId}"
data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}"
th:value="${antecedentValue.id}"
th:utext="${antecedentValue.antecedentValue}">
</option>
</select>
</div>
<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"
th:field="*{firstAntecedentId}"
data-width="90%">
<option th:each="antecedent : ${antecedents}"
th:value="${antecedent.id}"
th:utext="${antecedent.name}">
</option>
</select>
</div>
<div class="col-md-2 col-sm-12">
и
</div>
<div class="col-md-2 col-sm-12">
<select id="select-second-measures" class="selectpicker" data-live-search="true"
th:field="*{secondAntecedentValueId}"
data-width="100%">
<option th:each="antecedentValue : ${antecedentValues}"
th:value="${antecedentValue.id}"
th:utext="${antecedentValue.antecedentValue}">
</option>
</select>
</div>
<div class="col-md-2 col-sm-12">
временной ряд
</div>
<div class="col-md-6 col-sm-12">
<select id="select-second-antecedent" class="selectpicker" data-live-search="true"
th:field="*{secondAntecedentId}"
data-width="90%">
<option th:each="antecedent : ${antecedents}"
th:value="${antecedent.id}"
th:utext="${antecedent.name}">
</option>
</select>
</div>
<div class="col-md-2 col-sm-12">
то:
</div>
<div class="col-md-2 col-sm-12">
<input type="text" class="form-control" th:field="*{consequent}">
</div>
<div class="col-md-8 col-sm-12">
<input type="submit" class="btn btn-outline-success" value="Создать правило"/>
</div>
</div>
</form>
</div>
</html>

View File

@ -27,7 +27,7 @@
</tbody> </tbody>
</table> </table>
<div class="col-md-4 col-sm-12"> <div class="col-md-4 col-sm-12">
<a href="/rule" class="btn btn-outline-success">Добавить правило</a> <a href="/addRule" class="btn btn-outline-success">Добавить правило</a>
</div> </div>
</div> </div>
</html> </html>

View File

@ -1,49 +0,0 @@
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-spring4-4.dtd">
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
<title>Простая обработка формы на Spring MVC</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<!-- <form action="#" th:action="${@route.STATISTIC}" th:object="${filterForm}" method="get">-->
<form>
<div class="row">
<div class="col-md-2 col-sm-12">
Если
</div>
<div class="col-md-2 col-sm-12">
<select id="select-measures" class="selectpicker" data-live-search="true"
data-width="90%">
<option value="">много</option>
<option value="">мало</option>
<option value="">средне</option>
</select>
</div>
<div class="col-md-1 col-sm-12">
времянной ряд
</div>
<div class="col-md-6 col-sm-12">
<select id="select-ts" class="selectpicker" data-live-search="true"
data-width="90%">
<option value="">авторов</option>
<option value="">коммитов</option>
<option value="">веток</option>
</select>
</div>
</div>
<div class="row">
<div class="col-md-2 col-sm-12">
то:
</div>
<div class="col-md-3 col-sm-12">
<input type="text" class="form-control" size="40">
</div>
<div class="col-md-4 col-sm-12">
<input type="submit" class="btn btn-outline-success w-50" value="Создать правило"/>
</div>
</div>
</form>
</div>
</html>