#5 -- Edit terms
All checks were successful
CI fuzzy controller / container-test-job (push) Successful in 1m28s

This commit is contained in:
Anton Romanov 2025-02-24 15:00:11 +04:00
parent 746da497de
commit 9f3af7033e
12 changed files with 324 additions and 60 deletions

View File

@ -0,0 +1,55 @@
package ru.ulstu.fc.rule.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.validation.Valid;
import ru.ulstu.fc.rule.model.FuzzyTermForm;
import ru.ulstu.fc.rule.service.FuzzyTermService;
@Controller
@RequestMapping("fuzzyTerm")
public class FuzzyTermController {
private final FuzzyTermService fuzzyTermService;
public FuzzyTermController(FuzzyTermService fuzzyTermService) {
this.fuzzyTermService = fuzzyTermService;
}
@GetMapping("/edit/{projectId}/{variableId}/{fuzzyTermId}")
public String edit(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "variableId") Integer variableId,
@PathVariable(value = "fuzzyTermId") Integer fuzzyTermId, Model model) {
model.addAttribute("projectId", projectId);
model.addAttribute("variableId", variableId);
model.addAttribute("fuzzyTermForm",
(fuzzyTermId == null || fuzzyTermId == 0)
? new FuzzyTermForm(fuzzyTermId, projectId, variableId)
: new FuzzyTermForm(fuzzyTermId, projectId, variableId, fuzzyTermService.getById(fuzzyTermId)));
model.addAttribute("fuzzyTerms", fuzzyTermService.getByVariableId(variableId));
return "fuzzyTerm/edit";
}
@PostMapping(value = "save", params = "save")
public String save(@Valid FuzzyTermForm fuzzyTermForm, BindingResult result, Model model) {
if (result.hasErrors()) {
model.addAttribute("projectId", fuzzyTermForm.getProjectId());
return "fuzzyTerm/edit";
}
fuzzyTermService.save(fuzzyTermForm);
return "redirect:/variable/edit/" + fuzzyTermForm.getProjectId() + "/" + fuzzyTermForm.getVariableId();
}
@PostMapping(value = "save", params = "delete")
public String delete(FuzzyTermForm fuzzyTermForm) {
if (fuzzyTermForm != null && fuzzyTermForm.getId() != null) {
fuzzyTermService.delete(fuzzyTermForm);
}
return "redirect:/variable/edit/" + fuzzyTermForm.getProjectId() + "/" + fuzzyTermForm.getVariableId();
}
}

View File

@ -10,33 +10,38 @@ import org.springframework.web.bind.annotation.RequestMapping;
import jakarta.validation.Valid;
import ru.ulstu.fc.rule.model.VariableForm;
import ru.ulstu.fc.rule.service.FuzzyTermService;
import ru.ulstu.fc.rule.service.VariableService;
@Controller
@RequestMapping("var")
@RequestMapping("variable")
public class VariableController {
private final VariableService variableService;
private final FuzzyTermService termService;
public VariableController(VariableService variableService) {
public VariableController(VariableService variableService,
FuzzyTermService termService) {
this.variableService = variableService;
this.termService = termService;
}
@GetMapping("/edit/{projectId}/{varId}")
@GetMapping("/edit/{projectId}/{variableId}")
public String edit(@PathVariable(value = "projectId") Integer projectId,
@PathVariable(value = "varId") Integer id, Model model) {
@PathVariable(value = "variableId") Integer variableId, Model model) {
model.addAttribute("projectId", projectId);
model.addAttribute("variableForm",
(id != null && id != 0)
? new VariableForm(id, variableService.getById(id))
: new VariableForm(id, projectId));
return "var/edit";
(variableId == null || variableId == 0)
? new VariableForm(variableId, projectId)
: new VariableForm(variableId, variableService.getById(variableId)));
model.addAttribute("fuzzyTerms", termService.getByVariableId(variableId));
return "variable/edit";
}
@PostMapping(value = "save", params = "save")
public String save(@Valid VariableForm variableForm, BindingResult result, Model model) {
if (result.hasErrors()) {
model.addAttribute("projectId", variableForm.getProjectId());
return "var/edit";
return "variable/edit";
}
variableService.save(variableForm);
return "redirect:/project/edit/" + variableForm.getProjectId();

View File

@ -8,7 +8,7 @@ public class FuzzyTerm extends BaseEntity {
private String description;
private Double crispValue;
public FuzzyTerm() {
public FuzzyTerm() {
}
public FuzzyTerm(String description, Double value) {

View File

@ -0,0 +1,71 @@
package ru.ulstu.fc.rule.model;
public class FuzzyTermForm {
private Integer projectId;
private Integer variableId;
private Integer id;
private String description;
private Double crispValue;
public FuzzyTermForm() {
}
public FuzzyTermForm(String description, Double value) {
this.description = description;
this.crispValue = value;
}
public FuzzyTermForm(Integer id, Integer projectId, Integer variableId) {
this.projectId = projectId;
this.variableId = variableId;
this.id = id;
}
public FuzzyTermForm(Integer id, Integer projectId, Integer variableId, FuzzyTerm fuzzyTerm) {
this.id = id;
this.projectId = projectId;
this.variableId = variableId;
this.crispValue = fuzzyTerm.getCrispValue();
this.description = fuzzyTerm.getDescription();
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Double getCrispValue() {
return crispValue;
}
public void setCrispValue(Double crispValue) {
this.crispValue = crispValue;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getProjectId() {
return projectId;
}
public void setProjectId(Integer projectId) {
this.projectId = projectId;
}
public Integer getVariableId() {
return variableId;
}
public void setVariableId(Integer variableId) {
this.variableId = variableId;
}
}

View File

@ -0,0 +1,9 @@
package ru.ulstu.fc.rule.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.fc.rule.model.FuzzyTerm;
public interface FuzzyTermRepository extends JpaRepository<FuzzyTerm, Integer> {
}

View File

@ -0,0 +1,55 @@
package ru.ulstu.fc.rule.service;
import java.util.Collections;
import java.util.List;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.rule.model.FuzzyTerm;
import ru.ulstu.fc.rule.model.FuzzyTermForm;
import ru.ulstu.fc.rule.repository.FuzzyTermRepository;
@Service
public class FuzzyTermService {
private final FuzzyTermRepository fuzzyTermRepository;
private final VariableService variableService;
public FuzzyTermService(FuzzyTermRepository fuzzyTermRepository,
VariableService variableService) {
this.fuzzyTermRepository = fuzzyTermRepository;
this.variableService = variableService;
}
public FuzzyTerm getById(Integer id) {
return fuzzyTermRepository
.findById(id)
.orElseThrow(() -> new RuntimeException("Term not found by id"));
}
public FuzzyTerm save(FuzzyTermForm fuzzyTermForm) {
FuzzyTerm term;
if (fuzzyTermForm.getId() == null || fuzzyTermForm.getId() == 0) {
term = new FuzzyTerm();
} else {
term = getById(fuzzyTermForm.getId());
}
term.setDescription(fuzzyTermForm.getDescription());
term.setCrispValue(fuzzyTermForm.getCrispValue());
FuzzyTerm ft = fuzzyTermRepository.save(term);
variableService.addFuzzyTerm(fuzzyTermForm.getVariableId(), ft);
return ft;
}
public void delete(FuzzyTermForm fuzzyTermForm) {
getById(fuzzyTermForm.getId());
fuzzyTermRepository.deleteById(fuzzyTermForm.getId());
}
public List<FuzzyTerm> getByVariableId(Integer variableId) {
if (variableId == null || variableId == 0) {
return Collections.emptyList();
}
return variableService.getById(variableId).getFuzzyTerms();
}
}

View File

@ -3,6 +3,7 @@ package ru.ulstu.fc.rule.service;
import org.springframework.stereotype.Service;
import ru.ulstu.fc.project.service.ProjectService;
import ru.ulstu.fc.rule.model.FuzzyTerm;
import ru.ulstu.fc.rule.model.Variable;
import ru.ulstu.fc.rule.model.VariableForm;
import ru.ulstu.fc.rule.repository.VariableRepository;
@ -40,4 +41,10 @@ public class VariableService {
getById(ruleForm.getId());
variableRepository.deleteById(ruleForm.getId());
}
public void addFuzzyTerm(Integer variableId, FuzzyTerm ft) {
Variable var = getById(variableId);
var.getFuzzyTerms().add(ft);
variableRepository.save(var);
}
}

View File

@ -0,0 +1,54 @@
<!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>Редактирование терма</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<h3>Редактирование терма:</h3>
<form th:action="@{/fuzzyTerm/save}" th:object="${fuzzyTermForm}" method="post">
<input type="hidden" th:field="*{projectId}">
<input type="hidden" th:field="*{variableId}">
<input type="hidden" th:field="*{id}">
<div class="form-group">
<label for="name">Терм</label>
<input th:field="*{description}"
id="description"
type="text"
required
class="form-control"
placeholder="Терм">
<p th:if="${#fields.hasErrors('description')}"
th:class="${#fields.hasErrors('description')} ? error">
Не может быть пустым
</p>
</div>
<div class="form-group">
<label for="name">Значение</label>
<input th:field="*{crispValue}"
id="crispValue"
type="number"
required
class="form-control"
placeholder="Терм">
<p th:if="${#fields.hasErrors('crispValue')}"
th:class="${#fields.hasErrors('crispValue')} ? error">
Не может быть пустым
</p>
</div>
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
<button name="delete"
type="submit"
class="btn btn-outline-dark"
th:if="*{id != 0}"
onclick="return confirm('Удалить запись?')">
Удалить
</button>
<a th:href="@{'/variable/edit/' + ${projectId}+'/' + ${variableId}}" class="btn btn-outline-dark">Отмена</a>
</form>
</div>
</html>

View File

@ -28,7 +28,7 @@
th:field="*{ageValue}"
data-width="90%">
<option th:each="ageValue : ${ageValues}"
th:value="${ageValue.value}"
th:value="${ageValue.crispValue}"
th:utext="${ageValue.description}">
</option>
</select>
@ -43,7 +43,7 @@
th:field="*{incomeValue}"
data-width="90%">
<option th:each="incomeValue : ${incomeValues}"
th:value="${incomeValue.value}"
th:value="${incomeValue.crispValue}"
th:utext="${incomeValue.description}">
</option>
</select>

View File

@ -34,13 +34,13 @@
<div class="form-group">
<div class="row" th:each="v, iter : ${variables}">
<div class="col col-md-12">
<a th:href="@{'/var/edit/' + ${projectId}+'/'+${v.id}}">
<a th:href="@{'/variable/edit/' + ${projectId}+'/'+${v.id}}">
<span class="badge badge-light" th:text="${iter.index+1} + '. ' + ${v.name}"></span>
</a>
</div>
</div>
</div>
<a th:href="@{'/var/edit/' + ${projectId}+'/0'}" class="btn btn-outline-dark">Добавить преременную</a>
<a th:href="@{'/variable/edit/' + ${projectId}+'/0'}" class="btn btn-outline-dark">Добавить преременную</a>
</div>
<div class="col col-md-6">
<h4> Список правил</h4>

View File

@ -1,46 +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" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}">
<head>
<title>Редактирование переменной</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<div class="container" layout:fragment="content">
<h3>Редактирование переменной:</h3>
<form th:action="@{/var/save}" th:object="${variableForm}" method="post">
<input type="hidden" th:field="*{projectId}">
<input type="hidden" th:field="*{id}">
<div class="form-group">
<label for="name">Переменная</label>
<input th:field="*{name}"
id="name"
type="text"
required
class="form-control"
placeholder="Переменная">
<p th:if="${#fields.hasErrors('name')}"
th:class="${#fields.hasErrors('name')} ? error">
Не может быть пустым
</p>
</div>
<div class="form-group">
<input th:field="*{input}"
id="input"
type="checkbox">
<label for="input">Является входной переменной</label>
</div>
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
<button name="delete"
type="submit"
class="btn btn-outline-dark"
th:if="*{id != 0}"
onclick="return confirm('Удалить запись?')">
Удалить
</button>
<a th:href="@{'/project/edit/' + ${projectId}}" class="btn btn-outline-dark">Отмена</a>
</form>
</div>
</html>

View File

@ -0,0 +1,54 @@
<!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>Редактирование переменной</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<div class="container" layout:fragment="content">
<h3>Редактирование переменной:</h3>
<form th:action="@{/variable/save}" th:object="${variableForm}" method="post">
<input type="hidden" th:field="*{projectId}">
<input type="hidden" th:field="*{id}">
<div class="form-group">
<label for="name">Переменная</label>
<input th:field="*{name}" id="name" type="text" required class="form-control" placeholder="Переменная">
<p th:if="${#fields.hasErrors('name')}" th:class="${#fields.hasErrors('name')} ? error">
Не может быть пустым
</p>
</div>
<div class="form-group">
<input th:field="*{input}" id="input" type="checkbox">
<label for="input">Является входной переменной</label>
</div>
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
<button name="delete" type="submit" class="btn btn-outline-dark" th:if="*{id != 0}"
onclick="return confirm('Удалить запись?')">
Удалить
</button>
<a th:href="@{'/project/edit/' + ${projectId}}" class="btn btn-outline-dark">Отмена</a>
<hr />
<div class="row">
<div class="col col-md-6">
<h4> Список термов</h4>
<div class="form-group">
<div class="row" th:each="t, iter : ${fuzzyTerms}">
<div class="col col-md-12">
<a th:href="@{'/fuzzyTerm/edit/' + ${projectId} + '/' + ${variableId}+'/'+${t.id}}">
<span class="badge badge-light"
th:text="${iter.index+1} + '. ' + ${t.description}"></span>
</a>
</div>
</div>
</div>
<a th:href="@{'/fuzzyTerm/edit/' + ${projectId} + '/' + ${variableId}+'/0'}" class="btn btn-outline-dark">Добавить терм</a>
</div>
</div>
</form>
</div>
</html>