#13 add fileDataDto

This commit is contained in:
Семенова Мария 2019-03-18 13:00:34 +04:00
parent 7b601c744f
commit 0764d961fa
5 changed files with 149 additions and 51 deletions

View File

@ -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;
}
}

View File

@ -1,8 +1,10 @@
package ru.ulstu.file.service; package ru.ulstu.file.service;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import ru.ulstu.file.model.FileData; import ru.ulstu.file.model.FileData;
import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.file.repostory.FileRepository; import ru.ulstu.file.repostory.FileRepository;
import java.io.IOException; import java.io.IOException;
@ -10,6 +12,8 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@ -78,4 +82,32 @@ public class FileService {
public void deleteFile(FileData fileData) { public void deleteFile(FileData fileData) {
fileRepository.delete(fileData); fileRepository.delete(fileData);
} }
public List<FileData> saveOrCreate(List<FileDataDto> 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;
}
} }

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.NotEmpty; import org.hibernate.validator.constraints.NotEmpty;
import ru.ulstu.deadline.model.DeadlineDto; import ru.ulstu.deadline.model.DeadlineDto;
import ru.ulstu.file.model.FileDataDto;
import ru.ulstu.user.model.UserDto; import ru.ulstu.user.model.UserDto;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
@ -30,10 +31,9 @@ public class PaperDto {
private List<DeadlineDto> deadlines = new ArrayList<>(); private List<DeadlineDto> deadlines = new ArrayList<>();
private String comment; private String comment;
private Boolean locked; private Boolean locked;
private String tmpFileName;
private Integer fileId; private List<FileDataDto> files = new ArrayList<>();
private String fileName;
private Date fileCreateDate;
private Set<Integer> authorIds; private Set<Integer> authorIds;
private Set<UserDto> authors; private Set<UserDto> authors;
private Integer filterAuthorId; private Integer filterAuthorId;
@ -51,7 +51,7 @@ public class PaperDto {
@JsonProperty("deadlines") List<DeadlineDto> deadlines, @JsonProperty("deadlines") List<DeadlineDto> deadlines,
@JsonProperty("comment") String comment, @JsonProperty("comment") String comment,
@JsonProperty("locked") Boolean locked, @JsonProperty("locked") Boolean locked,
@JsonProperty("tmpFileName") String tmpFileName, @JsonProperty("files") List<FileDataDto> files,
@JsonProperty("authorIds") Set<Integer> authorIds, @JsonProperty("authorIds") Set<Integer> authorIds,
@JsonProperty("authors") Set<UserDto> authors) { @JsonProperty("authors") Set<UserDto> authors) {
this.id = id; this.id = id;
@ -62,10 +62,7 @@ public class PaperDto {
this.deadlines = deadlines; this.deadlines = deadlines;
this.comment = comment; this.comment = comment;
this.locked = locked; this.locked = locked;
this.tmpFileName = tmpFileName; this.files = files;
this.fileId = null;
this.fileName = null;
this.fileCreateDate = null;
this.authors = authors; this.authors = authors;
} }
@ -78,10 +75,9 @@ public class PaperDto {
this.deadlines = convert(paper.getDeadlines(), DeadlineDto::new); this.deadlines = convert(paper.getDeadlines(), DeadlineDto::new);
this.comment = paper.getComment(); this.comment = paper.getComment();
this.locked = paper.getLocked(); this.locked = paper.getLocked();
this.tmpFileName = null;
// this.fileId = paper.getFileData() == null ? null : paper.getFileData().getId(); this.files = convert(paper.getFiles(), FileDataDto::new);
// 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.authorIds = convert(paper.getAuthors(), user -> user.getId());
this.authors = convert(paper.getAuthors(), UserDto::new); this.authors = convert(paper.getAuthors(), UserDto::new);
} }
@ -150,36 +146,12 @@ public class PaperDto {
this.locked = locked; this.locked = locked;
} }
public String getTmpFileName() { public List<FileDataDto> getFiles() {
return tmpFileName; return files;
} }
public void setTmpFileName(String tmpFileName) { public void setFiles(List<FileDataDto> files) {
this.tmpFileName = tmpFileName; this.files = files;
}
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 Set<UserDto> getAuthors() { public Set<UserDto> getAuthors() {

View File

@ -80,9 +80,7 @@ public class PaperService {
paper.setTitle(paperDto.getTitle()); paper.setTitle(paperDto.getTitle());
paper.setUpdateDate(new Date()); paper.setUpdateDate(new Date());
paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines())); paper.setDeadlines(deadlineService.saveOrCreate(paperDto.getDeadlines()));
// if (paperDto.getTmpFileName() != null) { paper.setFiles(fileService.saveOrCreate(paperDto.getFiles()));
// paper.setFileData(fileService.createFileFromTmp(paperDto.getTmpFileName()));
// }
paper.getAuthors().clear(); paper.getAuthors().clear();
if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) { if (paperDto.getAuthorIds() != null && !paperDto.getAuthorIds().isEmpty()) {
paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds))); paperDto.getAuthorIds().forEach(authorIds -> paper.getAuthors().add(userService.findById(authorIds)));
@ -95,9 +93,6 @@ public class PaperService {
Paper paper = paperRepository.findOne(paperDto.getId()); Paper paper = paperRepository.findOne(paperDto.getId());
Paper.PaperStatus oldStatus = paper.getStatus(); Paper.PaperStatus oldStatus = paper.getStatus();
Set<User> oldAuthors = new HashSet<>(paper.getAuthors()); Set<User> oldAuthors = new HashSet<>(paper.getAuthors());
// if (paperDto.getTmpFileName() != null && paper.getFileData() != null) {
// fileService.deleteFile(paper.getFileData());
// }
paperRepository.save(copyFromDto(paper, paperDto)); paperRepository.save(copyFromDto(paper, paperDto));
paper.getAuthors().forEach(author -> { paper.getAuthors().forEach(author -> {
@ -116,9 +111,6 @@ public class PaperService {
@Transactional @Transactional
public void delete(Integer paperId) throws IOException { public void delete(Integer paperId) throws IOException {
Paper paper = paperRepository.findOne(paperId); Paper paper = paperRepository.findOne(paperId);
// if (paper.getFileData() != null) {
// fileService.deleteFile(paper.getFileData());
// }
paperRepository.delete(paper); paperRepository.delete(paper);
} }

View File

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