#21 -- Add files upload

This commit is contained in:
Anton Romanov 2025-04-15 00:26:35 +04:00
parent 8e74e8911c
commit dab6c030e5
5 changed files with 228 additions and 2 deletions

View File

@ -0,0 +1,39 @@
package ru.ulstu.report.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import ru.ulstu.aspirant.service.AspirantService;
import ru.ulstu.indicator.service.IndicatorService;
import ru.ulstu.report.model.dto.ReportValueDto;
import ru.ulstu.report.service.ReportPeriodService;
import ru.ulstu.report.service.ReportService;
@Controller
@RequestMapping("report-value")
public class ReportValueController {
private final IndicatorService indicatorService;
private final AspirantService aspirantService;
private final ReportService reportService;
private final ReportPeriodService reportPeriodService;
public ReportValueController(IndicatorService indicatorService,
AspirantService aspirantService,
ReportService reportService,
ReportPeriodService reportPeriodService) {
this.indicatorService = indicatorService;
this.aspirantService = aspirantService;
this.reportService = reportService;
this.reportPeriodService = reportPeriodService;
}
@GetMapping("edit-report-value/{reportId}/{indicatorId}")
public String getReportPeriods(@PathVariable("reportId") Integer reportId,
@PathVariable("indicatorId") Integer indicatorId,
Model model) {
model.addAttribute("reportValue", new ReportValueDto(reportId, indicatorService.getIndicatorById(indicatorId)));
return "report/editReportValue";
}
}

View File

@ -7,6 +7,8 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class ReportValueDto { public class ReportValueDto {
private Integer id = 0;
private Integer reportId;
private Indicator indicator; private Indicator indicator;
private int indicatorValue; private int indicatorValue;
private List<FileDataDto> files = new ArrayList<>(); private List<FileDataDto> files = new ArrayList<>();
@ -14,6 +16,11 @@ public class ReportValueDto {
public ReportValueDto() { public ReportValueDto() {
} }
public ReportValueDto(Integer reportId, Indicator indicator) {
this.reportId = reportId;
this.indicator = indicator;
}
public ReportValueDto(Indicator indicator) { public ReportValueDto(Indicator indicator) {
this.indicator = indicator; this.indicator = indicator;
} }
@ -41,4 +48,20 @@ public class ReportValueDto {
public void setFiles(List<FileDataDto> files) { public void setFiles(List<FileDataDto> files) {
this.files = files; this.files = files;
} }
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getReportId() {
return reportId;
}
public void setReportId(Integer reportId) {
this.reportId = reportId;
}
} }

View File

@ -12,7 +12,7 @@
<input type="hidden" th:field="*{status}"> <input type="hidden" th:field="*{status}">
<div class="form-group" th:each="rv, ind : *{reportValues}"> <div class="form-group" th:each="rv, ind : *{reportValues}">
<a th:href="${'/edit-indicator-value/' + rv.indicator.id}" <a th:href="${'/report-value/edit-report-value/'+report.id + '/' + rv.indicator.id}"
th:text="${rv.indicator.name}"></a> th:text="${rv.indicator.name}"></a>
<p th:text="${rv.indicator.proofDocuments}"></p> <p th:text="${rv.indicator.proofDocuments}"></p>
<p th:text="'Максимальное количество баллов за показатель: '+ ${rv.indicator.max}"></p> <p th:text="'Максимальное количество баллов за показатель: '+ ${rv.indicator.max}"></p>

View File

@ -0,0 +1,163 @@
<html
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.w3.org/1999/xhtml"
layout:decorate="~{default}">
<div class="container" layout:fragment="content">
<h3 th:text="${'Редактирование показателя '+ reportValue.indicator.name}"></h3>
<form action="#" th:action="@{/report/save-report-value}"
th:object="${reportValue}"
method="post">
<input type="hidden" th:field="*{id}">
<input type="hidden" th:field="*{indicator.id}">
<button name="save" type="submit" class="btn btn-outline-dark">Сохранить</button>
<button name="delete"
type="submit"
class="btn btn-outline-dark"
onclick="return confirm('Удалить запись?')">
Удалить
</button>
<a href="${'/report/edit-report/'+ reportValue.reportId}" class="btn btn-outline-dark">Отмена</a>
<div class="form-group files-list" id="files-list">
<label class="form-label">Загрузка подтверждающих документов</label>
<div th:each="file, rowStat : ${reportValue.files}">
<div class="row" th:id="|files${rowStat.index}|"
th:style="${file.deleted} ? 'display: none;' :''">
<input type="hidden" th:field="*{files[__${rowStat.index}__].id}"/>
<input type="hidden"
th:field="*{files[__${rowStat.index}__].deleted}"/>
<input type="hidden"
th:field="*{files[__${rowStat.index}__].name}"/>
<input type="hidden"
th:field="*{files[__${rowStat.index}__].tmpFileName}"/>
<div class="col-2">
<a class="btn btn-danger float-right"
th:onclick="|$('#files${rowStat.index}\\.deleted').val('true'); $('#files${rowStat.index}').hide(); |">
<span aria-hidden="true"><i class="fa fa-times"/></span>
</a>
</div>
<div class="col-10">
<a th:onclick="${file.id==null} ? 'downloadFile('+${file.tmpFileName}+',null,\''+${file.name}+'\')':
'downloadFile(null,'+${file.id}+',\''+${file.name}+'\')' "
href="javascript:void(0)"
th:text="*{files[__${rowStat.index}__].name}">
</a>
</div>
</div>
</div>
<div class="form-group">
<label for="loader">Загрузить файл:</label>
<div id="loader">
</div>
</div>
</div>
</form>
<script type="text/javascript" src="/js/file-loader.js"></script>
<script>
/*<![CDATA[*/
$(document).ready(function () {
new FileLoader({
div: "loader",
url: urlFileUpload,
maxSize: -1,
extensions: [],
callback: function (response) {
console.debug(response);
addNewFile(response, $("#files-list"));
}
});
$('.selectpicker').selectpicker();
});
/*]]>*/
function addNewFile(fileDto, listElement) {
var fileNumber = $('.files-list div.row').length;
var newFileRow = $("<div/>")
.attr("id", 'files' + fileNumber)
.addClass("row");
var idInput = $("<input/>")
.attr("type", "hidden")
.attr("id", "files" + fileNumber + ".id")
.attr("value", '')
.attr("name", "files[" + fileNumber + "].id");
newFileRow.append(idInput);
var flagInput = $("<input/>")
.attr("type", "hidden")
.attr("id", "files" + fileNumber + ".deleted")
.attr("value", "false")
.attr("name", "files[" + fileNumber + "].deleted");
newFileRow.append(flagInput);
var nameInput = $("<input/>")
.attr("type", "hidden")
.attr("id", "files" + fileNumber + ".name")
.attr("value", fileDto.fileName)
.attr("name", "files[" + fileNumber + "].name");
newFileRow.append(nameInput);
var tmpFileNameInput = $("<input/>")
.attr("type", "hidden")
.attr("id", "files" + fileNumber + ".tmpFileName")
.attr("value", fileDto.tmpFileName)
.attr("name", "files[" + fileNumber + "].tmpFileName");
newFileRow.append(tmpFileNameInput);
var nextDiv = $("<div/>")
.addClass("col-2");
var nextA = $("<a/>")
.addClass("btn btn-danger float-right")
.attr("onclick", "$('#files" + fileNumber + "\\\\.deleted').val('true'); $('#files" + fileNumber + "').hide();")
.append(($("<span/>").attr("aria-hidden", "true")).append($("<i/>").addClass("fa fa-times")))
;
nextDiv.append(nextA)
newFileRow.append(nextDiv);
var nameDiv = $("<div/>")
.addClass("col-10")
.append($("<a/>").text(fileDto.fileName)
.attr("href", 'javascript:void(0)')
.attr("onclick", "downloadFile('" + fileDto.tmpFileName + "',null,'" + fileDto.fileName + "')"));
newFileRow.append(nameDiv);
listElement.append(newFileRow);
}
function downloadFile(tmpName, fileId, downloadName) {
let xhr = new XMLHttpRequest();
if (fileId != null) xhr.open('GET', urlFileDownload + fileId);
if (tmpName != null) xhr.open('GET', urlFileDownloadTmp + tmpName);
xhr.responseType = 'blob';
var formData = new FormData();
if (fileId != null) formData.append("file-id", fileId);
if (tmpName != null) formData.append("tmp-file-name", tmpName);
xhr.send(formData);
xhr.onload = function () {
if (this.status == 200) {
console.debug(this.response);
var blob = new Blob([this.response], {type: '*'});
let a = document.createElement("a");
a.style = "display: none";
document.body.appendChild(a);
let url = window.URL.createObjectURL(blob);
a.href = url;
a.download = downloadName;
a.click();
window.URL.revokeObjectURL(url);
} else {
}
}
}
</script>
</div>
</html>

View File

@ -50,7 +50,8 @@
<td th:text="${#calendars.format(r.reportPeriod.startDate, 'dd.MM.yyyy') + ' - ' + #calendars.format(r.reportPeriod.endDate, 'dd.MM.yyyy')}"></td> <td th:text="${#calendars.format(r.reportPeriod.startDate, 'dd.MM.yyyy') + ' - ' + #calendars.format(r.reportPeriod.endDate, 'dd.MM.yyyy')}"></td>
<td th:text="${r.aspirant.surname + ' '+ r.aspirant.name + ' '+ r.aspirant.patronymic }"></td> <td th:text="${r.aspirant.surname + ' '+ r.aspirant.name + ' '+ r.aspirant.patronymic }"></td>
<td th:text="${#calendars.format(r.createDate, 'dd.MM.yyyy HH:mm')}"></td> <td th:text="${#calendars.format(r.createDate, 'dd.MM.yyyy HH:mm')}"></td>
<td th:text="${r.status.title}"></td> <td th:if="${r.status != null}" th:text="${r.status.title}"></td>
<td th:if="${r.status == null}">не определен</td>
<td> <td>
<!-- Ссылка на редактирование --> <!-- Ссылка на редактирование -->
<a th:href="@{'/report/edit-report/' + ${r.id}}" class="btn btn-sm btn-primary"> <a th:href="@{'/report/edit-report/' + ${r.id}}" class="btn btn-sm btn-primary">