From 7b601c744fb7b9802c3e053c7b0f5de124a117b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Mon, 18 Mar 2019 11:07:30 +0400 Subject: [PATCH 01/12] #13 change model, schema --- src/main/java/ru/ulstu/paper/model/Paper.java | 38 ++++++------------- .../java/ru/ulstu/paper/model/PaperDto.java | 6 +-- .../ru/ulstu/paper/service/PaperService.java | 30 ++++++--------- .../db/changelog-20190318_000000-schema.xml | 23 +++++++++++ src/main/resources/db/changelog-master.xml | 1 + 5 files changed, 49 insertions(+), 49 deletions(-) create mode 100644 src/main/resources/db/changelog-20190318_000000-schema.xml diff --git a/src/main/java/ru/ulstu/paper/model/Paper.java b/src/main/java/ru/ulstu/paper/model/Paper.java index c09fd28..f98b54b 100644 --- a/src/main/java/ru/ulstu/paper/model/Paper.java +++ b/src/main/java/ru/ulstu/paper/model/Paper.java @@ -9,26 +9,8 @@ import ru.ulstu.deadline.model.Deadline; import ru.ulstu.file.model.FileData; import ru.ulstu.user.model.User; -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.ManyToOne; -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; +import javax.persistence.*; +import java.util.*; @Entity public class Paper extends BaseEntity implements UserContainer { @@ -77,9 +59,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 files = new ArrayList<>(); @ManyToMany(fetch = FetchType.EAGER) private Set authors = new HashSet<>(); @@ -132,12 +116,12 @@ public class Paper extends BaseEntity implements UserContainer { this.locked = locked; } - public FileData getFileData() { - return fileData; + public List getFiles() { + return files; } - public void setFileData(FileData fileData) { - this.fileData = fileData; + public void setFiles(List files) { + this.files = files; } public String getTitle() { diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index 545b432..f870dc3 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -79,9 +79,9 @@ public class PaperDto { this.comment = paper.getComment(); 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.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.authorIds = convert(paper.getAuthors(), user -> user.getId()); this.authors = convert(paper.getAuthors(), UserDto::new); } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 9df8be2..39c2ace 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -14,20 +14,12 @@ import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; import java.io.IOException; -import java.util.Arrays; -import java.util.Date; -import java.util.HashSet; -import java.util.List; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import static org.springframework.util.ObjectUtils.isEmpty; import static ru.ulstu.core.util.StreamApiUtils.convert; -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; +import static ru.ulstu.paper.model.Paper.PaperStatus.*; @Service public class PaperService { @@ -88,9 +80,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())); - } +// if (paperDto.getTmpFileName() != null) { +// paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName())); +// } paper.getAuthors().clear(); if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); @@ -103,9 +95,9 @@ public class PaperService { Paper paper = paperRepository.findOne(paperDto.getId()); Paper.PaperStatus oldStatus = paper.getStatus(); Set oldAuthors = new HashSet<>(paper.getAuthors()); - if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { - fileService.deleteFile(paper.getFileData()); - } +// if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { +// fileService.deleteFile(paper.getFileData()); +// } paperRepository.save(copyFromDto(paper, paperDto)); paper.getAuthors().forEach(author -> { @@ -124,9 +116,9 @@ public class PaperService { @Transactional public void delete(Integer paperId) throws IOException { Paper paper = paperRepository.findOne(paperId); - if (paper.getFileData() != null) { - fileService.deleteFile(paper.getFileData()); - } +// if (paper.getFileData() != null) { +// fileService.deleteFile(paper.getFileData()); +// } paperRepository.delete(paper); } diff --git a/src/main/resources/db/changelog-20190318_000000-schema.xml b/src/main/resources/db/changelog-20190318_000000-schema.xml new file mode 100644 index 0000000..d1966b4 --- /dev/null +++ b/src/main/resources/db/changelog-20190318_000000-schema.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/db/changelog-master.xml b/src/main/resources/db/changelog-master.xml index b8654ae..dcba122 100644 --- a/src/main/resources/db/changelog-master.xml +++ b/src/main/resources/db/changelog-master.xml @@ -18,5 +18,6 @@ + \ No newline at end of file From 0764d961fac6aff165f262f534ecb05d5012e6b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Mon, 18 Mar 2019 13:00:34 +0400 Subject: [PATCH 02/12] #13 add fileDataDto --- .../java/ru/ulstu/file/model/FileDataDto.java | 102 ++++++++++++++++++ .../ru/ulstu/file/service/FileService.java | 32 ++++++ .../java/ru/ulstu/paper/model/PaperDto.java | 54 +++------- .../ru/ulstu/paper/service/PaperService.java | 10 +- src/main/resources/public/js/core.js | 2 +- 5 files changed, 149 insertions(+), 51 deletions(-) create mode 100644 src/main/java/ru/ulstu/file/model/FileDataDto.java diff --git a/src/main/java/ru/ulstu/file/model/FileDataDto.java b/src/main/java/ru/ulstu/file/model/FileDataDto.java new file mode 100644 index 0000000..d59f75b --- /dev/null +++ b/src/main/java/ru/ulstu/file/model/FileDataDto.java @@ -0,0 +1,102 @@ +package ru.ulstu.file.model; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.Date; + +public class FileDataDto { + private Integer id; + private String name; + private long size; + + private Date createDate; + private byte[] data; + private String fileName; + private String tmpFileName; + + public FileDataDto() { + } + + @JsonCreator + public FileDataDto(@JsonProperty("id") Integer id, + @JsonProperty("name") String name, + @JsonProperty("size") long size, + @JsonProperty("createDate") Date createDate, + @JsonProperty("data") byte[] data, + @JsonProperty("fileName") String fileName, + @JsonProperty("tmpFileName") String tmpFileName) { + this.id = id; + this.name = name; + this.size = size; + this.createDate = createDate; + this.data = data; + this.fileName = fileName; + this.tmpFileName = tmpFileName; + } + + public FileDataDto(FileData fileData) { + this.id = fileData.getId(); + this.name = fileData.getName(); + this.size = fileData.getSize(); + this.createDate = fileData.getCreateDate(); + this.data = fileData.getData(); + } + + 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 long getSize() { + return size; + } + + public void setSize(long size) { + this.size = size; + } + + public Date getCreateDate() { + return createDate; + } + + public void setCreateDate(Date createDate) { + this.createDate = createDate; + } + + public byte[] getData() { + return data; + } + + public void setData(byte[] data) { + this.data = data; + } + + 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; + } + +} diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 2f1fb6d..b15c342 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -1,8 +1,10 @@ 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; @@ -10,6 +12,8 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Date; +import java.util.List; +import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; @@ -78,4 +82,32 @@ public class FileService { public void deleteFile(FileData fileData) { fileRepository.delete(fileData); } + + public List saveOrCreate(List files) { + return files.stream().map(fileDataDto -> { + return fileDataDto.getId() != null ? update(fileDataDto) : create(fileDataDto); + }).collect(Collectors.toList()); + } + + @Transactional + public FileData update(FileDataDto fileDataDto) { + FileData file = fileRepository.findOne(fileDataDto.getId()); + fileRepository.save(copyFromDto(file, fileDataDto)); + return file; + } + + @Transactional + public FileData create(FileDataDto fileDataDto) { + FileData newFile = copyFromDto(new FileData(), fileDataDto); + newFile = fileRepository.save(newFile); + return newFile; + } + + private FileData copyFromDto(FileData fileData, FileDataDto fileDataDto) { + fileData.setCreateDate(fileDataDto.getCreateDate()); + fileData.setData(fileDataDto.getData()); + fileData.setName(fileDataDto.getName()); + fileData.setSize(fileDataDto.getSize()); + return fileData; + } } diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index f870dc3..96d7654 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import org.apache.commons.lang3.StringUtils; import org.hibernate.validator.constraints.NotEmpty; import ru.ulstu.deadline.model.DeadlineDto; +import ru.ulstu.file.model.FileDataDto; import ru.ulstu.user.model.UserDto; import javax.validation.constraints.Size; @@ -30,10 +31,9 @@ public class PaperDto { private List deadlines = new ArrayList<>(); private String comment; private Boolean locked; - private String tmpFileName; - private Integer fileId; - private String fileName; - private Date fileCreateDate; + + private List files = new ArrayList<>(); + private Set authorIds; private Set authors; private Integer filterAuthorId; @@ -51,7 +51,7 @@ public class PaperDto { @JsonProperty("deadlines") List deadlines, @JsonProperty("comment") String comment, @JsonProperty("locked") Boolean locked, - @JsonProperty("tmpFileName") String tmpFileName, + @JsonProperty("files") List files, @JsonProperty("authorIds") Set authorIds, @JsonProperty("authors") Set authors) { this.id = id; @@ -62,10 +62,7 @@ public class PaperDto { this.deadlines = deadlines; this.comment = comment; this.locked = locked; - this.tmpFileName = tmpFileName; - this.fileId = null; - this.fileName = null; - this.fileCreateDate = null; + this.files = files; this.authors = authors; } @@ -78,10 +75,9 @@ public class PaperDto { this.deadlines = convert(paper.getDeadlines(), DeadlineDto::new); this.comment = paper.getComment(); 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); } @@ -150,36 +146,12 @@ public class PaperDto { this.locked = locked; } - public String getTmpFileName() { - return tmpFileName; + public List 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 files) { + this.files = files; } public Set getAuthors() { diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 39c2ace..3f4028d 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -80,9 +80,7 @@ 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())); paper.getAuthors().clear(); if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); @@ -95,9 +93,6 @@ public class PaperService { Paper paper = paperRepository.findOne(paperDto.getId()); Paper.PaperStatus oldStatus = paper.getStatus(); Set oldAuthors = new HashSet<>(paper.getAuthors()); -// if (paperDto.getTmpFileName() != null && paper.getFileData() != null) { -// fileService.deleteFile(paper.getFileData()); -// } paperRepository.save(copyFromDto(paper, paperDto)); paper.getAuthors().forEach(author -> { @@ -116,9 +111,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); } diff --git a/src/main/resources/public/js/core.js b/src/main/resources/public/js/core.js index 5c9241c..31642f4 100644 --- a/src/main/resources/public/js/core.js +++ b/src/main/resources/public/js/core.js @@ -1,7 +1,7 @@ // from config.js /* global urlVersions */ -var urlFileUpload = "/api/1.0/papers/uploadTmpFile"; +var urlFileUpload = "/api/1.0/files/uploadTmpFile"; /* exported MessageTypesEnum */ var MessageTypesEnum = { From cb394edf907e66f35d70ce2103faae2e09b0f7ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Mon, 18 Mar 2019 16:02:55 +0400 Subject: [PATCH 03/12] #13 file list on paper page --- .../java/ru/ulstu/file/model/FileDataDto.java | 11 +++++++++- .../ru/ulstu/file/service/FileService.java | 6 +++++ .../ru/ulstu/paper/service/PaperService.java | 8 ++++++- .../db/changelog-20190318_000000-schema.xml | 11 ---------- .../resources/templates/papers/paper.html | 22 +++++++++++++++++++ 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/main/java/ru/ulstu/file/model/FileDataDto.java b/src/main/java/ru/ulstu/file/model/FileDataDto.java index d59f75b..fda3e81 100644 --- a/src/main/java/ru/ulstu/file/model/FileDataDto.java +++ b/src/main/java/ru/ulstu/file/model/FileDataDto.java @@ -9,12 +9,13 @@ public class FileDataDto { private Integer id; private String name; private long size; - private Date createDate; private byte[] data; private String fileName; private String tmpFileName; + private boolean deleteFlag; + public FileDataDto() { } @@ -99,4 +100,12 @@ public class FileDataDto { this.tmpFileName = tmpFileName; } + public boolean isDeleteFlag() { + return deleteFlag; + } + + public void setDeleteFlag(boolean deleteFlag) { + this.deleteFlag = deleteFlag; + } + } diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index b15c342..3f3dbca 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -110,4 +110,10 @@ public class FileService { fileData.setSize(fileDataDto.getSize()); return fileData; } + + @Transactional + public void delete(Integer fileId) throws IOException { + FileData file = fileRepository.findOne(fileId); + fileRepository.delete(file); + } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 3f4028d..851a21b 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -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; @@ -80,7 +81,7 @@ public class PaperService { paper.setTitle(paperDto.getTitle()); paper.setUpdateDate(new Date()); paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines())); - paper.setFiles(fileService.saveOrCreate(paperDto.getFiles())); + paper.setFiles(fileService.saveOrCreate(paperDto.getFiles().stream().filter(f -> f.isDeleteFlag() == false).collect(Collectors.toList()))); paper.getAuthors().clear(); if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); @@ -93,6 +94,11 @@ public class PaperService { Paper paper = paperRepository.findOne(paperDto.getId()); Paper.PaperStatus oldStatus = paper.getStatus(); Set oldAuthors = new HashSet<>(paper.getAuthors()); + + for (FileDataDto file : paperDto.getFiles().stream().filter(f -> f.isDeleteFlag() == true && f.getId() != null).collect(Collectors.toList())) { + fileService.delete(file.getId()); + } + paperRepository.save(copyFromDto(paper, paperDto)); paper.getAuthors().forEach(author -> { diff --git a/src/main/resources/db/changelog-20190318_000000-schema.xml b/src/main/resources/db/changelog-20190318_000000-schema.xml index d1966b4..a4f7ec1 100644 --- a/src/main/resources/db/changelog-20190318_000000-schema.xml +++ b/src/main/resources/db/changelog-20190318_000000-schema.xml @@ -8,16 +8,5 @@ - - - - - - - diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index a5ff6b5..0f1a467 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -91,6 +91,28 @@

Incorrect title

+ +
+ + +
+ + + +
+ + + +
+
+
+
+
+
+
+
From 4fb8061653b47634219161aedd3be7fa139d4a7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Mon, 18 Mar 2019 18:39:15 +0400 Subject: [PATCH 04/12] #13 addNewFile js function --- .../java/ru/ulstu/file/FileController.java | 11 +--- .../ru/ulstu/file/service/FileService.java | 1 + .../resources/templates/papers/paper.html | 63 ++++++++++++++++++- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/src/main/java/ru/ulstu/file/FileController.java b/src/main/java/ru/ulstu/file/FileController.java index e60626b..f68b820 100644 --- a/src/main/java/ru/ulstu/file/FileController.java +++ b/src/main/java/ru/ulstu/file/FileController.java @@ -3,12 +3,7 @@ package ru.ulstu.file; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -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 org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import ru.ulstu.configuration.Constants; import ru.ulstu.core.model.response.Response; @@ -51,7 +46,7 @@ public class FileController { } @PostMapping("/uploadTmpFile") - public Response upload(@RequestParam("file") MultipartFile multipartFile) throws IOException { - return new Response(fileService.uploadToTmpDir(multipartFile)); + public Response upload(@RequestParam("file") MultipartFile multipartFile) throws IOException { + return new Response(new Object[]{fileService.uploadToTmpDir(multipartFile), multipartFile.getOriginalFilename()}); } } diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 3f3dbca..e0d9813 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -33,6 +33,7 @@ public class FileService { public FileData createFileFromTmp(String tmpFileName) throws IOException { FileData fileData = new FileData(); fileData.setData(getTmpFile(tmpFileName)); + fileData.setSize(getTmpFileSize(tmpFileName)); fileData.setName(getTmpFileName(tmpFileName)); fileData.setCreateDate(new Date()); return fileRepository.save(fileData); diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index 0f1a467..d758bb9 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -92,7 +92,7 @@ class="alert alert-danger">Incorrect title

-
+
@@ -189,15 +191,72 @@ div: "loader", url: urlFileUpload, maxSize: 1.5, - extensions: ["doc", "docx", "xls", "jpg", "pdf", "txt", "png"], + extensions: [], callback: function (response) { showFeedbackMessage("Файл успешно загружен"); console.debug(response); + + addNewFile(response); } }); $('.selectpicker').selectpicker(); }); /*]]>*/ + function addNewFile(fileNames) { + var filesList = $("#files-list"); + var fileNumber = $("#files-list div.row").length; + + var newFileRow = $("
") + .attr("id", 'files' + fileNumber) + .addClass("row"); + + var idInput = $("") + .attr("type", "hidden") + .attr("id", "files" + fileNumber + ".id") + .attr("value", '') + .attr("name", "files[" + fileNumber + "].id"); + newFileRow.append(idInput); + + var flagInput = $("") + .attr("type", "hidden") + .attr("id", "files" + fileNumber + ".deleteFlag") + .attr("value", "false") + .attr("name", "files[" + fileNumber + "].deleteFlag"); + newFileRow.append(flagInput); + + var nameInput = $("") + .attr("type", "hidden") + .attr("id", "files" + fileNumber + ".name") + .attr("value", tmpFileName[1]) + .attr("name", "files[" + fileNumber + "].name"); + newFileRow.append(nameInput); + + var tmpFileNameInput = $("") + .attr("type", "hidden") + .attr("id", "files" + fileNumber + ".tmpFileName") + .attr("value", tmpFileName[0]) + .attr("name", "files[" + fileNumber + "].tmpFileName"); + newFileRow.append(tmpFileNameInput); + + var nextDiv = $("
") + .addClass("col-2"); + + var nextA = $("") + .addClass("btn btn-danger float-right") + .attr("onclick", "$('#files" + fileNumber + "\\\\.deleteFlag').val('true'); $('#files" + fileNumber + "').hide();") + .append(($("").attr("aria-hidden", "true")).append($("").addClass("fa fa-times"))) + ; + nextDiv.append(nextA) + newFileRow.append(nextDiv); + + var nameDiv = $("
") + .addClass("col-10") + .append($("
").text(tmpFileName[1])); + newFileRow.append(nameDiv); + + $("#files-list").append(newFileRow); + + }
From 8dc5eec35c36292374ca34c33def7660ed1852d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Mon, 18 Mar 2019 19:17:28 +0400 Subject: [PATCH 05/12] #13 add files to db --- .../ru/ulstu/file/service/FileService.java | 11 +++---- src/main/resources/public/js/file-loader.js | 29 ++++++++++--------- .../resources/templates/papers/paper.html | 8 ++--- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index e0d9813..3a68bb6 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -34,7 +34,6 @@ public class FileService { FileData fileData = new FileData(); fileData.setData(getTmpFile(tmpFileName)); fileData.setSize(getTmpFileSize(tmpFileName)); - fileData.setName(getTmpFileName(tmpFileName)); fileData.setCreateDate(new Date()); return fileRepository.save(fileData); } @@ -99,16 +98,18 @@ public class FileService { @Transactional public FileData create(FileDataDto fileDataDto) { - FileData newFile = copyFromDto(new FileData(), fileDataDto); + FileData newFile = new FileData(); + try { + newFile = createFileFromTmp(fileDataDto.getTmpFileName()); + } catch (IOException e) { + } + copyFromDto(newFile, fileDataDto); newFile = fileRepository.save(newFile); return newFile; } private FileData copyFromDto(FileData fileData, FileDataDto fileDataDto) { - fileData.setCreateDate(fileDataDto.getCreateDate()); - fileData.setData(fileDataDto.getData()); fileData.setName(fileDataDto.getName()); - fileData.setSize(fileDataDto.getSize()); return fileData; } diff --git a/src/main/resources/public/js/file-loader.js b/src/main/resources/public/js/file-loader.js index 9ee16a1..1ba467b 100644 --- a/src/main/resources/public/js/file-loader.js +++ b/src/main/resources/public/js/file-loader.js @@ -42,6 +42,7 @@ function FileLoader(args) { div.append(fileLabel); var fileInput = $("") .attr("type", "file") + .attr("multiple", '') .hide(); fileInput.change(function () { var files = $(this).prop("files"); @@ -75,20 +76,22 @@ function FileLoader(args) { showFeedbackMessage(ALERT_CHOOSE_FILE, MessageTypesEnum.DANGER); return; } - var currentFile = files[0]; - if (!isEmpty(fileExtensions) && fileExtensions.indexOf(getFileExt(currentFile)) === -1) { - showFeedbackMessage(ALERT_UNKNOWN_FILE_EXT, MessageTypesEnum.DANGER); - return; + 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; + } + if (currentFile.size === 0) { + showFeedbackMessage(ALERT_EMPTY_FILE, MessageTypesEnum.DANGER); + return; + } + 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); } - if (currentFile.size === 0) { - showFeedbackMessage(ALERT_EMPTY_FILE, MessageTypesEnum.DANGER); - return; - } - if (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); diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index d758bb9..996c991 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -190,7 +190,7 @@ new FileLoader({ div: "loader", url: urlFileUpload, - maxSize: 1.5, + maxSize: -1, extensions: [], callback: function (response) { showFeedbackMessage("Файл успешно загружен"); @@ -227,14 +227,14 @@ var nameInput = $("") .attr("type", "hidden") .attr("id", "files" + fileNumber + ".name") - .attr("value", tmpFileName[1]) + .attr("value", fileNames[1]) .attr("name", "files[" + fileNumber + "].name"); newFileRow.append(nameInput); var tmpFileNameInput = $("") .attr("type", "hidden") .attr("id", "files" + fileNumber + ".tmpFileName") - .attr("value", tmpFileName[0]) + .attr("value", fileNames[0]) .attr("name", "files[" + fileNumber + "].tmpFileName"); newFileRow.append(tmpFileNameInput); @@ -251,7 +251,7 @@ var nameDiv = $("
") .addClass("col-10") - .append($("
").text(tmpFileName[1])); + .append($("
").text(fileNames[1])); newFileRow.append(nameDiv); $("#files-list").append(newFileRow); From 1425cceb63189b4c4d6f28ae12654a51e08cd4c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Mon, 18 Mar 2019 21:02:26 +0400 Subject: [PATCH 06/12] #13 download files --- .../ru/ulstu/file/service/FileService.java | 3 +- src/main/resources/public/js/core.js | 2 + .../resources/templates/papers/paper.html | 46 +++++++++++++++++-- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 3a68bb6..68d0b35 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -13,6 +13,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.util.Date; import java.util.List; +import java.util.UUID; import java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; @@ -39,7 +40,7 @@ public class FileService { } 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)); diff --git a/src/main/resources/public/js/core.js b/src/main/resources/public/js/core.js index 31642f4..216f411 100644 --- a/src/main/resources/public/js/core.js +++ b/src/main/resources/public/js/core.js @@ -2,6 +2,8 @@ /* global urlVersions */ 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 = { diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index 996c991..cfba4bc 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -107,8 +107,17 @@
-
-
+
+ + +
+
+ +
@@ -251,12 +260,43 @@ var nameDiv = $("
") .addClass("col-10") - .append($("
").text(fileNames[1])); + .append($("").text(fileNames[1]) + .attr("href", 'javascript:void(0)') + .attr("onclick", "downloadFile('" + fileNames[0] + "',null,'" + fileNames[1] + "')")); 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 { + } + } + }
From 3c068df201d2c35b29ec12cf80e33106a1492c0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Tue, 19 Mar 2019 17:59:19 +0400 Subject: [PATCH 07/12] #13 some refactoring --- .../java/ru/ulstu/file/model/FileDataDto.java | 38 ------------------- src/main/resources/public/css/paper.css | 5 +++ .../resources/templates/papers/paper.html | 13 ++----- 3 files changed, 9 insertions(+), 47 deletions(-) create mode 100644 src/main/resources/public/css/paper.css diff --git a/src/main/java/ru/ulstu/file/model/FileDataDto.java b/src/main/java/ru/ulstu/file/model/FileDataDto.java index fda3e81..d117fe7 100644 --- a/src/main/java/ru/ulstu/file/model/FileDataDto.java +++ b/src/main/java/ru/ulstu/file/model/FileDataDto.java @@ -3,14 +3,9 @@ package ru.ulstu.file.model; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import java.util.Date; - public class FileDataDto { private Integer id; private String name; - private long size; - private Date createDate; - private byte[] data; private String fileName; private String tmpFileName; @@ -22,16 +17,10 @@ public class FileDataDto { @JsonCreator public FileDataDto(@JsonProperty("id") Integer id, @JsonProperty("name") String name, - @JsonProperty("size") long size, - @JsonProperty("createDate") Date createDate, - @JsonProperty("data") byte[] data, @JsonProperty("fileName") String fileName, @JsonProperty("tmpFileName") String tmpFileName) { this.id = id; this.name = name; - this.size = size; - this.createDate = createDate; - this.data = data; this.fileName = fileName; this.tmpFileName = tmpFileName; } @@ -39,9 +28,6 @@ public class FileDataDto { public FileDataDto(FileData fileData) { this.id = fileData.getId(); this.name = fileData.getName(); - this.size = fileData.getSize(); - this.createDate = fileData.getCreateDate(); - this.data = fileData.getData(); } public Integer getId() { @@ -60,30 +46,6 @@ public class FileDataDto { this.name = name; } - public long getSize() { - return size; - } - - public void setSize(long size) { - this.size = size; - } - - public Date getCreateDate() { - return createDate; - } - - public void setCreateDate(Date createDate) { - this.createDate = createDate; - } - - public byte[] getData() { - return data; - } - - public void setData(byte[] data) { - this.data = data; - } - public String getFileName() { return fileName; } diff --git a/src/main/resources/public/css/paper.css b/src/main/resources/public/css/paper.css new file mode 100644 index 0000000..6e61d0c --- /dev/null +++ b/src/main/resources/public/css/paper.css @@ -0,0 +1,5 @@ +#files-list .row > div:nth-child(6) { + display: flex; + justify-content: center; + flex-direction: column; +} \ No newline at end of file diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index cfba4bc..1daec46 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -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"> - + @@ -107,14 +107,9 @@
-
- - -
-
- + From c702e2abb49a9243f5f6b3f8209b8a94931b85da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Thu, 21 Mar 2019 18:26:28 +0400 Subject: [PATCH 08/12] #13 fileDataDto instead of Object[] --- .../java/ru/ulstu/file/FileController.java | 15 ++++++++++--- src/main/java/ru/ulstu/paper/model/Paper.java | 22 +++++++++++++++++-- .../ru/ulstu/paper/service/PaperService.java | 13 +++++++++-- .../resources/templates/papers/paper.html | 11 +++++----- 4 files changed, 48 insertions(+), 13 deletions(-) diff --git a/src/main/java/ru/ulstu/file/FileController.java b/src/main/java/ru/ulstu/file/FileController.java index f68b820..591c23d 100644 --- a/src/main/java/ru/ulstu/file/FileController.java +++ b/src/main/java/ru/ulstu/file/FileController.java @@ -3,11 +3,17 @@ package ru.ulstu.file; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; +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 org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; 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; @@ -46,7 +52,10 @@ public class FileController { } @PostMapping("/uploadTmpFile") - public Response upload(@RequestParam("file") MultipartFile multipartFile) throws IOException { - return new Response(new Object[]{fileService.uploadToTmpDir(multipartFile), multipartFile.getOriginalFilename()}); + public Response upload(@RequestParam("file") MultipartFile multipartFile) throws IOException { + FileDataDto fileDataDto = new FileDataDto(); + fileDataDto.setFileName(multipartFile.getOriginalFilename()); + fileDataDto.setTmpFileName(fileService.uploadToTmpDir(multipartFile)); + return new Response(fileDataDto); } } diff --git a/src/main/java/ru/ulstu/paper/model/Paper.java b/src/main/java/ru/ulstu/paper/model/Paper.java index f98b54b..b2f8327 100644 --- a/src/main/java/ru/ulstu/paper/model/Paper.java +++ b/src/main/java/ru/ulstu/paper/model/Paper.java @@ -9,8 +9,26 @@ 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 { diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 851a21b..932735d 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -15,12 +15,21 @@ import ru.ulstu.user.model.User; import ru.ulstu.user.service.UserService; import java.io.IOException; -import java.util.*; +import java.util.Arrays; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Set; import java.util.stream.Collectors; 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 { diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index 1daec46..3dafd90 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -206,8 +206,7 @@ $('.selectpicker').selectpicker(); }); /*]]>*/ - function addNewFile(fileNames) { - var filesList = $("#files-list"); + function addNewFile(fileDto) { var fileNumber = $("#files-list div.row").length; var newFileRow = $("
") @@ -231,14 +230,14 @@ var nameInput = $("") .attr("type", "hidden") .attr("id", "files" + fileNumber + ".name") - .attr("value", fileNames[1]) + .attr("value", fileDto.fileName) .attr("name", "files[" + fileNumber + "].name"); newFileRow.append(nameInput); var tmpFileNameInput = $("") .attr("type", "hidden") .attr("id", "files" + fileNumber + ".tmpFileName") - .attr("value", fileNames[0]) + .attr("value", fileDto.tmpFileName) .attr("name", "files[" + fileNumber + "].tmpFileName"); newFileRow.append(tmpFileNameInput); @@ -255,9 +254,9 @@ var nameDiv = $("
") .addClass("col-10") - .append($("").text(fileNames[1]) + .append($("").text(fileDto.fileName) .attr("href", 'javascript:void(0)') - .attr("onclick", "downloadFile('" + fileNames[0] + "',null,'" + fileNames[1] + "')")); + .attr("onclick", "downloadFile('" + fileDto.tmpFileName + "',null,'" + fileDto.fileName + "')")); newFileRow.append(nameDiv); $("#files-list").append(newFileRow); From 5da9de1bab4d05d448bf2a37993b29fa76615f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Fri, 22 Mar 2019 09:32:21 +0400 Subject: [PATCH 09/12] #13 rename 'deleted', move creating fileDto to service --- src/main/java/ru/ulstu/file/FileController.java | 5 +---- src/main/java/ru/ulstu/file/model/FileDataDto.java | 14 +++++++++----- .../java/ru/ulstu/file/service/FileService.java | 4 ++++ .../java/ru/ulstu/paper/service/PaperService.java | 4 ++-- src/main/resources/templates/papers/paper.html | 12 ++++++------ 5 files changed, 22 insertions(+), 17 deletions(-) diff --git a/src/main/java/ru/ulstu/file/FileController.java b/src/main/java/ru/ulstu/file/FileController.java index 591c23d..ac08ac1 100644 --- a/src/main/java/ru/ulstu/file/FileController.java +++ b/src/main/java/ru/ulstu/file/FileController.java @@ -53,9 +53,6 @@ public class FileController { @PostMapping("/uploadTmpFile") public Response upload(@RequestParam("file") MultipartFile multipartFile) throws IOException { - FileDataDto fileDataDto = new FileDataDto(); - fileDataDto.setFileName(multipartFile.getOriginalFilename()); - fileDataDto.setTmpFileName(fileService.uploadToTmpDir(multipartFile)); - return new Response(fileDataDto); + return new Response(fileService.createFromMultipartFile(multipartFile)); } } diff --git a/src/main/java/ru/ulstu/file/model/FileDataDto.java b/src/main/java/ru/ulstu/file/model/FileDataDto.java index d117fe7..f99656c 100644 --- a/src/main/java/ru/ulstu/file/model/FileDataDto.java +++ b/src/main/java/ru/ulstu/file/model/FileDataDto.java @@ -9,7 +9,7 @@ public class FileDataDto { private String fileName; private String tmpFileName; - private boolean deleteFlag; + private boolean deleted; public FileDataDto() { } @@ -30,6 +30,10 @@ public class FileDataDto { this.name = fileData.getName(); } + public FileDataDto(String fileName, String tmpFileName) { + this.fileName = fileName; + this.tmpFileName = tmpFileName; + } public Integer getId() { return id; } @@ -62,12 +66,12 @@ public class FileDataDto { this.tmpFileName = tmpFileName; } - public boolean isDeleteFlag() { - return deleteFlag; + public boolean isDeleted() { + return deleted; } - public void setDeleteFlag(boolean deleteFlag) { - this.deleteFlag = deleteFlag; + public void setDeleted(boolean deleted) { + this.deleted = deleted; } } diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 68d0b35..7c40b60 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -119,4 +119,8 @@ public class FileService { FileData file = fileRepository.findOne(fileId); fileRepository.delete(file); } + + public FileDataDto createFromMultipartFile(MultipartFile multipartFile) throws IOException { + return new FileDataDto(multipartFile.getOriginalFilename(), uploadToTmpDir(multipartFile)); + } } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 932735d..7c5df22 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -90,7 +90,7 @@ public class PaperService { paper.setTitle(paperDto.getTitle()); paper.setUpdateDate(new Date()); paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines())); - paper.setFiles(fileService.saveOrCreate(paperDto.getFiles().stream().filter(f -> f.isDeleteFlag() == false).collect(Collectors.toList()))); + paper.setFiles(fileService.saveOrCreate(paperDto.getFiles().stream().filter(f -> !f.isDeleted()).collect(Collectors.toList()))); paper.getAuthors().clear(); if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); @@ -104,7 +104,7 @@ public class PaperService { Paper.PaperStatus oldStatus = paper.getStatus(); Set oldAuthors = new HashSet<>(paper.getAuthors()); - for (FileDataDto file : paperDto.getFiles().stream().filter(f -> f.isDeleteFlag() == true && f.getId() != null).collect(Collectors.toList())) { + for (FileDataDto file : paperDto.getFiles().stream().filter(f -> f.isDeleted() && f.getId() != null).collect(Collectors.toList())) { fileService.delete(file.getId()); } diff --git a/src/main/resources/templates/papers/paper.html b/src/main/resources/templates/papers/paper.html index 3dafd90..a0b11f1 100644 --- a/src/main/resources/templates/papers/paper.html +++ b/src/main/resources/templates/papers/paper.html @@ -96,14 +96,14 @@
+ th:style="${file.deleted} ? 'display: none;' :''"> - + @@ -222,9 +222,9 @@ var flagInput = $("") .attr("type", "hidden") - .attr("id", "files" + fileNumber + ".deleteFlag") + .attr("id", "files" + fileNumber + ".deleted") .attr("value", "false") - .attr("name", "files[" + fileNumber + "].deleteFlag"); + .attr("name", "files[" + fileNumber + "].deleted"); newFileRow.append(flagInput); var nameInput = $("") @@ -246,7 +246,7 @@ var nextA = $("") .addClass("btn btn-danger float-right") - .attr("onclick", "$('#files" + fileNumber + "\\\\.deleteFlag').val('true'); $('#files" + fileNumber + "').hide();") + .attr("onclick", "$('#files" + fileNumber + "\\\\.deleted').val('true'); $('#files" + fileNumber + "').hide();") .append(($("").attr("aria-hidden", "true")).append($("").addClass("fa fa-times"))) ; nextDiv.append(nextA) From a0b65f341bca680ad048b98303ad6c86dfe591ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D0=B5=D0=BC=D0=B5=D0=BD=D0=BE=D0=B2=D0=B0=20=D0=9C?= =?UTF-8?q?=D0=B0=D1=80=D0=B8=D1=8F?= Date: Fri, 22 Mar 2019 13:21:32 +0400 Subject: [PATCH 10/12] #13 streams refactoring --- .../ru/ulstu/file/service/FileService.java | 27 +++++++++---------- .../ru/ulstu/paper/service/PaperService.java | 16 ++++++----- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 7c40b60..ceb4503 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -11,10 +11,10 @@ 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 java.util.stream.Collectors; import static java.nio.charset.StandardCharsets.UTF_8; @@ -84,10 +84,13 @@ public class FileService { fileRepository.delete(fileData); } - public List saveOrCreate(List files) { - return files.stream().map(fileDataDto -> { - return fileDataDto.getId() != null ? update(fileDataDto) : create(fileDataDto); - }).collect(Collectors.toList()); + public List saveOrCreate(List fileDtos) throws IOException { + List files = new ArrayList<>(); + for (FileDataDto file : fileDtos) { + if (file.getId() != null) files.add(update(file)); + else files.add(create(file)); + } + return files; } @Transactional @@ -98,12 +101,9 @@ public class FileService { } @Transactional - public FileData create(FileDataDto fileDataDto) { - FileData newFile = new FileData(); - try { - newFile = createFileFromTmp(fileDataDto.getTmpFileName()); - } catch (IOException e) { - } + public FileData create(FileDataDto fileDataDto) throws IOException { + FileData newFile = null; + newFile = createFileFromTmp(fileDataDto.getTmpFileName()); copyFromDto(newFile, fileDataDto); newFile = fileRepository.save(newFile); return newFile; @@ -115,9 +115,8 @@ public class FileService { } @Transactional - public void delete(Integer fileId) throws IOException { - FileData file = fileRepository.findOne(fileId); - fileRepository.delete(file); + public void delete(Integer fileId) { + fileRepository.delete(fileRepository.findOne(fileId)); } public FileDataDto createFromMultipartFile(MultipartFile multipartFile) throws IOException { diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 7c5df22..89a087d 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -20,8 +20,8 @@ import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; -import java.util.stream.Collectors; +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.ATTENTION; @@ -67,7 +67,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) { @@ -90,7 +90,9 @@ public class PaperService { paper.setTitle(paperDto.getTitle()); paper.setUpdateDate(new Date()); paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines())); - paper.setFiles(fileService.saveOrCreate(paperDto.getFiles().stream().filter(f -> !f.isDeleted()).collect(Collectors.toList()))); + 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))); @@ -104,7 +106,9 @@ public class PaperService { Paper.PaperStatus oldStatus = paper.getStatus(); Set oldAuthors = new HashSet<>(paper.getAuthors()); - for (FileDataDto file : paperDto.getFiles().stream().filter(f -> f.isDeleted() && f.getId() != null).collect(Collectors.toList())) { + for (FileDataDto file : paperDto.getFiles().stream() + .filter(f -> f.isDeleted() && f.getId() != null) + .collect(toList())) { fileService.delete(file.getId()); } @@ -164,7 +168,7 @@ public class PaperService { return statusCompareResult; } return paper1.getTitle().compareTo(paper2.getTitle()); - }).collect(Collectors.toList()); + }).collect(toList()); } public PaperDto findPaper(int id) { @@ -178,7 +182,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); From 7012c00c5fca738a99d45adc2ea4713196778203 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 22 Mar 2019 13:52:06 +0400 Subject: [PATCH 11/12] #13 fix condition --- src/main/java/ru/ulstu/file/service/FileService.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index ceb4503..810f853 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -87,8 +87,7 @@ public class FileService { public List saveOrCreate(List fileDtos) throws IOException { List files = new ArrayList<>(); for (FileDataDto file : fileDtos) { - if (file.getId() != null) files.add(update(file)); - else files.add(create(file)); + files.add(file.getId() != null ? update(file) : create(file)); } return files; } From 482e74e10e0f5c583b64b0a2203a0effc32edba2 Mon Sep 17 00:00:00 2001 From: Anton Romanov Date: Fri, 22 Mar 2019 13:58:03 +0400 Subject: [PATCH 12/12] #13 reduce of code --- src/main/java/ru/ulstu/file/model/FileDataDto.java | 1 - src/main/java/ru/ulstu/file/service/FileService.java | 9 +++------ src/main/java/ru/ulstu/paper/model/PaperDto.java | 4 ---- src/main/java/ru/ulstu/paper/service/PaperService.java | 1 - 4 files changed, 3 insertions(+), 12 deletions(-) diff --git a/src/main/java/ru/ulstu/file/model/FileDataDto.java b/src/main/java/ru/ulstu/file/model/FileDataDto.java index f99656c..919cd80 100644 --- a/src/main/java/ru/ulstu/file/model/FileDataDto.java +++ b/src/main/java/ru/ulstu/file/model/FileDataDto.java @@ -8,7 +8,6 @@ public class FileDataDto { private String name; private String fileName; private String tmpFileName; - private boolean deleted; public FileDataDto() { diff --git a/src/main/java/ru/ulstu/file/service/FileService.java b/src/main/java/ru/ulstu/file/service/FileService.java index 810f853..ad9f946 100644 --- a/src/main/java/ru/ulstu/file/service/FileService.java +++ b/src/main/java/ru/ulstu/file/service/FileService.java @@ -95,17 +95,14 @@ public class FileService { @Transactional public FileData update(FileDataDto fileDataDto) { FileData file = fileRepository.findOne(fileDataDto.getId()); - fileRepository.save(copyFromDto(file, fileDataDto)); - return file; + return fileRepository.save(copyFromDto(file, fileDataDto)); } @Transactional public FileData create(FileDataDto fileDataDto) throws IOException { - FileData newFile = null; - newFile = createFileFromTmp(fileDataDto.getTmpFileName()); + FileData newFile = createFileFromTmp(fileDataDto.getTmpFileName()); copyFromDto(newFile, fileDataDto); - newFile = fileRepository.save(newFile); - return newFile; + return fileRepository.save(newFile); } private FileData copyFromDto(FileData fileData, FileDataDto fileDataDto) { diff --git a/src/main/java/ru/ulstu/paper/model/PaperDto.java b/src/main/java/ru/ulstu/paper/model/PaperDto.java index c250a6b..18284f4 100644 --- a/src/main/java/ru/ulstu/paper/model/PaperDto.java +++ b/src/main/java/ru/ulstu/paper/model/PaperDto.java @@ -31,9 +31,7 @@ public class PaperDto { private List deadlines = new ArrayList<>(); private String comment; private Boolean locked; - private List files = new ArrayList<>(); - private Set authorIds; private Set authors; private Integer filterAuthorId; @@ -75,9 +73,7 @@ public class PaperDto { this.deadlines = paper.getDeadlines(); this.comment = paper.getComment(); this.locked = paper.getLocked(); - this.files = convert(paper.getFiles(), FileDataDto::new); - this.authorIds = convert(paper.getAuthors(), user -> user.getId()); this.authors = convert(paper.getAuthors(), UserDto::new); } diff --git a/src/main/java/ru/ulstu/paper/service/PaperService.java b/src/main/java/ru/ulstu/paper/service/PaperService.java index 89a087d..a664d00 100644 --- a/src/main/java/ru/ulstu/paper/service/PaperService.java +++ b/src/main/java/ru/ulstu/paper/service/PaperService.java @@ -30,7 +30,6 @@ 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 { private final static int MAX_DISPLAY_SIZE = 40;