Merge branch 'dev' into 33-paper-url

# Conflicts:
#	src/main/java/ru/ulstu/paper/model/Paper.java
#	src/main/java/ru/ulstu/paper/service/PaperService.java
#	src/main/resources/db/changelog-master.xml
This commit is contained in:
Семенова Мария 2019-03-23 12:04:51 +04:00
commit c228f5aee0
13 changed files with 340 additions and 86 deletions

View File

@ -38,3 +38,6 @@ deploy:
paths:
- build
- .gradle
environment:
name: staging
url: http://193.110.3.124:8080

View File

@ -13,6 +13,7 @@ import org.springframework.web.multipart.MultipartFile;
import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.Response;
import ru.ulstu.file.model.FileData;
import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.file.service.FileService;
import java.io.IOException;
@ -51,7 +52,7 @@ public class FileController {
}
@PostMapping("/uploadTmpFile")
public Response<String> upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
return new Response(fileService.uploadToTmpDir(multipartFile));
public Response<FileDataDto> upload(@RequestParam("file") MultipartFile multipartFile) throws IOException {
return new Response(fileService.createFromMultipartFile(multipartFile));
}
}

View File

@ -0,0 +1,76 @@
package ru.ulstu.file.model;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class FileDataDto {
private Integer id;
private String name;
private String fileName;
private String tmpFileName;
private boolean deleted;
public FileDataDto() {
}
@JsonCreator
public FileDataDto(@JsonProperty("id") Integer id,
@JsonProperty("name") String name,
@JsonProperty("fileName") String fileName,
@JsonProperty("tmpFileName") String tmpFileName) {
this.id = id;
this.name = name;
this.fileName = fileName;
this.tmpFileName = tmpFileName;
}
public FileDataDto(FileData fileData) {
this.id = fileData.getId();
this.name = fileData.getName();
}
public FileDataDto(String fileName, String tmpFileName) {
this.fileName = fileName;
this.tmpFileName = tmpFileName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getTmpFileName() {
return tmpFileName;
}
public void setTmpFileName(String tmpFileName) {
this.tmpFileName = tmpFileName;
}
public boolean isDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
}

View File

@ -1,15 +1,20 @@
package ru.ulstu.file.service;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import ru.ulstu.file.model.FileData;
import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.file.repostory.FileRepository;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import static java.nio.charset.StandardCharsets.UTF_8;
@ -29,13 +34,13 @@ public class FileService {
public FileData createFileFromTmp(String tmpFileName) throws IOException {
FileData fileData = new FileData();
fileData.setData(getTmpFile(tmpFileName));
fileData.setName(getTmpFileName(tmpFileName));
fileData.setSize(getTmpFileSize(tmpFileName));
fileData.setCreateDate(new Date());
return fileRepository.save(fileData);
}
public String uploadToTmpDir(MultipartFile multipartFile) throws IOException {
String tmpFileName = String.valueOf(System.currentTimeMillis());
String tmpFileName = String.valueOf(System.currentTimeMillis()) + UUID.randomUUID();
Files.write(getTmpFilePath(tmpFileName), multipartFile.getBytes());
String meta = multipartFile.getOriginalFilename() + "\n" + multipartFile.getSize();
Files.write(getTmpFileMetaPath(tmpFileName), meta.getBytes(UTF_8));
@ -78,4 +83,39 @@ public class FileService {
public void deleteFile(FileData fileData) {
fileRepository.delete(fileData);
}
public List<FileData> saveOrCreate(List<FileDataDto> fileDtos) throws IOException {
List<FileData> files = new ArrayList<>();
for (FileDataDto file : fileDtos) {
files.add(file.getId() != null ? update(file) : create(file));
}
return files;
}
@Transactional
public FileData update(FileDataDto fileDataDto) {
FileData file = fileRepository.findOne(fileDataDto.getId());
return fileRepository.save(copyFromDto(file, fileDataDto));
}
@Transactional
public FileData create(FileDataDto fileDataDto) throws IOException {
FileData newFile = createFileFromTmp(fileDataDto.getTmpFileName());
copyFromDto(newFile, fileDataDto);
return fileRepository.save(newFile);
}
private FileData copyFromDto(FileData fileData, FileDataDto fileDataDto) {
fileData.setName(fileDataDto.getName());
return fileData;
}
@Transactional
public void delete(Integer fileId) {
fileRepository.delete(fileRepository.findOne(fileId));
}
public FileDataDto createFromMultipartFile(MultipartFile multipartFile) throws IOException {
return new FileDataDto(multipartFile.getOriginalFilename(), uploadToTmpDir(multipartFile));
}
}

View File

@ -9,8 +9,25 @@ import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.file.model.FileData;
import ru.ulstu.user.model.User;
import javax.persistence.*;
import java.util.*;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;
@Entity
public class Paper extends BaseEntity implements UserContainer {
@ -61,9 +78,11 @@ public class Paper extends BaseEntity implements UserContainer {
private Boolean locked = false;
@ManyToOne
@JoinColumn(name = "file_id")
private FileData fileData;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "paper_id", unique = true)
@Fetch(FetchMode.SUBSELECT)
private List<FileData> files = new ArrayList<>();
@ManyToMany(fetch = FetchType.EAGER)
private Set<User> authors = new HashSet<>();
@ -116,12 +135,12 @@ public class Paper extends BaseEntity implements UserContainer {
this.locked = locked;
}
public FileData getFileData() {
return fileData;
public List<FileData> getFiles() {
return files;
}
public void setFileData(FileData fileData) {
this.fileData = fileData;
public void setFiles(List<FileData> files) {
this.files = files;
}
public String getTitle() {

View File

@ -4,6 +4,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotEmpty;
import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.user.model.UserDto;
@ -31,10 +32,7 @@ public class PaperDto {
private String comment;
private String url;
private Boolean locked;
private String tmpFileName;
private Integer fileId;
private String fileName;
private Date fileCreateDate;
private List<FileDataDto> files = new ArrayList<>();
private Set<Integer> authorIds;
private Set<UserDto> authors;
private Integer filterAuthorId;
@ -53,7 +51,7 @@ public class PaperDto {
@JsonProperty("comment") String comment,
@JsonProperty("url") String url,
@JsonProperty("locked") Boolean locked,
@JsonProperty("tmpFileName") String tmpFileName,
@JsonProperty("files") List<FileDataDto> files,
@JsonProperty("authorIds") Set<Integer> authorIds,
@JsonProperty("authors") Set<UserDto> authors) {
this.id = id;
@ -65,10 +63,7 @@ public class PaperDto {
this.comment = comment;
this.url = url;
this.locked = locked;
this.tmpFileName = tmpFileName;
this.fileId = null;
this.fileName = null;
this.fileCreateDate = null;
this.files = files;
this.authors = authors;
}
@ -82,10 +77,7 @@ public class PaperDto {
this.comment = paper.getComment();
this.url = paper.getUrl();
this.locked = paper.getLocked();
this.tmpFileName = null;
this.fileId = paper.getFileData() == null ? null : paper.getFileData().getId();
this.fileName = paper.getFileData() == null ? null : paper.getFileData().getName();
this.fileCreateDate = paper.getFileData() == null ? null : paper.getFileData().getCreateDate();
this.files = convert(paper.getFiles(), FileDataDto::new);
this.authorIds = convert(paper.getAuthors(), user -> user.getId());
this.authors = convert(paper.getAuthors(), UserDto::new);
}
@ -154,36 +146,12 @@ public class PaperDto {
this.locked = locked;
}
public String getTmpFileName() {
return tmpFileName;
public List<FileDataDto> getFiles() {
return files;
}
public void setTmpFileName(String tmpFileName) {
this.tmpFileName = tmpFileName;
}
public Integer getFileId() {
return fileId;
}
public void setFileId(Integer fileId) {
this.fileId = fileId;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public Date getFileCreateDate() {
return fileCreateDate;
}
public void setFileCreateDate(Date fileCreateDate) {
this.fileCreateDate = fileCreateDate;
public void setFiles(List<FileDataDto> files) {
this.files = files;
}
public Set<UserDto> getAuthors() {

View File

@ -5,6 +5,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.ulstu.deadline.model.Deadline;
import ru.ulstu.deadline.service.DeadlineService;
import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.file.service.FileService;
import ru.ulstu.paper.model.Paper;
import ru.ulstu.paper.model.PaperDto;
@ -14,12 +15,20 @@ import ru.ulstu.user.model.User;
import ru.ulstu.user.service.UserService;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static java.util.stream.Collectors.toList;
import static org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert;
import static ru.ulstu.paper.model.Paper.PaperStatus.*;
import static ru.ulstu.paper.model.Paper.PaperStatus.ATTENTION;
import static ru.ulstu.paper.model.Paper.PaperStatus.COMPLETED;
import static ru.ulstu.paper.model.Paper.PaperStatus.DRAFT;
import static ru.ulstu.paper.model.Paper.PaperStatus.FAILED;
import static ru.ulstu.paper.model.Paper.PaperStatus.ON_PREPARATION;
@Service
public class PaperService {
@ -57,7 +66,7 @@ public class PaperService {
return findAllDto()
.stream()
.filter(paper -> paper.getStatus() != COMPLETED && paper.getStatus() != FAILED)
.collect(Collectors.toList());
.collect(toList());
}
public PaperDto findOneDto(Integer id) {
@ -81,9 +90,9 @@ public class PaperService {
paper.setTitle(paperDto.getTitle());
paper.setUpdateDate(new Date());
paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines()));
if (paperDto.getTmpFileName() != null) {
paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName()));
}
paper.setFiles(fileService.saveOrCreate(paperDto.getFiles().stream()
.filter(f -> !f.isDeleted())
.collect(toList())));
paper.getAuthors().clear();
if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) {
paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds)));
@ -96,8 +105,11 @@ public class PaperService {
Paper paper = paperRepository.findOne(paperDto.getId());
Paper.PaperStatus oldStatus = paper.getStatus();
Set<User> oldAuthors = new HashSet<>(paper.getAuthors());
if (paperDto.getTmpFileName() != null && paper.getFileData() != null) {
fileService.deleteFile(paper.getFileData());
for (FileDataDto file : paperDto.getFiles().stream()
.filter(f -> f.isDeleted() && f.getId() != null)
.collect(toList())) {
fileService.delete(file.getId());
}
paperRepository.save(copyFromDto(paper, paperDto));
@ -117,9 +129,6 @@ public class PaperService {
@Transactional
public void delete(Integer paperId) throws IOException {
Paper paper = paperRepository.findOne(paperId);
if (paper.getFileData() != null) {
fileService.deleteFile(paper.getFileData());
}
paperRepository.delete(paper);
}
@ -158,7 +167,7 @@ public class PaperService {
return statusCompareResult;
}
return paper1.getTitle().compareTo(paper2.getTitle());
}).collect(Collectors.toList());
}).collect(toList());
}
public PaperDto findPaper(int id) {
@ -172,7 +181,7 @@ public class PaperService {
&& (paper.getStatus() == ON_PREPARATION
|| paper.getStatus() == DRAFT
|| paper.getStatus() == ATTENTION))
.collect(Collectors.toList());
.collect(toList());
papers.forEach(paper -> {
Paper.PaperStatus oldStatus = paper.getStatus();
paper.setStatus(Paper.PaperStatus.FAILED);

View File

@ -0,0 +1,12 @@
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet author="masha" id="20190318_000000-1">
<dropColumn columnName="file_id" tableName="paper"/>
<addColumn tableName="file">
<column name="paper_id" type="integer"/>
</addColumn>
</changeSet>
</databaseChangeLog>

View File

@ -18,6 +18,7 @@
<include file="db/changelog-20181111_000000-schema.xml"/>
<include file="db/changelog-20181208_000000-schema.xml"/>
<include file="db/changelog-20181224_000000-schema.xml"/>
<include file="db/changelog-20190318_000000-schema.xml"/>
<include file="db/changelog-20190318_000001-schema.xml"/>
<include file="db/common/changelog-20190312_130000-schema.xml"/>
</databaseChangeLog>

View File

@ -0,0 +1,5 @@
#files-list .row > div:nth-child(6) {
display: flex;
justify-content: center;
flex-direction: column;
}

View File

@ -1,7 +1,9 @@
// from config.js
/* global urlVersions */
var urlFileUpload = "/api/1.0/papers/uploadTmpFile";
var urlFileUpload = "/api/1.0/files/uploadTmpFile";
var urlFileDownload = "/api/1.0/files/download/";
var urlFileDownloadTmp = "/api/1.0/files/download-tmp/";
/* exported MessageTypesEnum */
var MessageTypesEnum = {

View File

@ -42,6 +42,7 @@ function FileLoader(args) {
div.append(fileLabel);
var fileInput = $("<input>")
.attr("type", "file")
.attr("multiple", '')
.hide();
fileInput.change(function () {
var files = $(this).prop("files");
@ -75,7 +76,8 @@ function FileLoader(args) {
showFeedbackMessage(ALERT_CHOOSE_FILE, MessageTypesEnum.DANGER);
return;
}
var currentFile = files[0];
for (var i = 0; i < files.length; i++) {
var currentFile = files[i];
if (!isEmpty(fileExtensions) && fileExtensions.indexOf(getFileExt(currentFile)) === -1) {
showFeedbackMessage(ALERT_UNKNOWN_FILE_EXT, MessageTypesEnum.DANGER);
return;
@ -84,11 +86,12 @@ function FileLoader(args) {
showFeedbackMessage(ALERT_EMPTY_FILE, MessageTypesEnum.DANGER);
return;
}
if (currentFile.size / SIZE_TO_MB > MAX_FILE_SIZE_MB) {
if (MAX_FILE_SIZE_MB != -1 && currentFile.size / SIZE_TO_MB > MAX_FILE_SIZE_MB) {
showFeedbackMessage(ALERT_MAX_FILE + " " + MAX_FILE_SIZE_MB + "Mb", MessageTypesEnum.DANGER);
return;
}
upload(currentFile);
}
});
buttonGroup.append(uploadButton);

View File

@ -3,7 +3,7 @@
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorator="default" xmlns:th="http://www.w3.org/1999/xhtml" xmlns="http://www.w3.org/1999/html">
<head>
<link rel="stylesheet" href="../css/paper.css"/>
</head>
<body>
@ -98,6 +98,34 @@
<p th:if="${#fields.hasErrors('authorIds')}" th:errors="*{authorIds}"
class="alert alert-danger">Incorrect title</p>
</div>
<div class="form-group" id="files-list">
<label>Файлы:</label>
<th:block th:each="file, rowStat : *{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>
</th:block>
</div>
<div class="form-check">
<input type="checkbox" class="form-check-input" id="locked"
th:field="*{locked}"/>
@ -173,16 +201,103 @@
new FileLoader({
div: "loader",
url: urlFileUpload,
maxSize: 1.5,
extensions: ["doc", "docx", "xls", "jpg", "pdf", "txt", "png"],
maxSize: -1,
extensions: [],
callback: function (response) {
showFeedbackMessage("Файл успешно загружен");
console.debug(response);
addNewFile(response);
}
});
$('.selectpicker').selectpicker();
});
/*]]>*/
function addNewFile(fileDto) {
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);
$("#files-list").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>