#13 streams refactoring

This commit is contained in:
Семенова Мария 2019-03-22 13:21:32 +04:00
parent 5da9de1bab
commit a0b65f341b
2 changed files with 23 additions and 20 deletions

View File

@ -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<FileData> saveOrCreate(List<FileDataDto> files) {
return files.stream().map(fileDataDto -> {
return fileDataDto.getId() != null ? update(fileDataDto) : create(fileDataDto);
}).collect(Collectors.toList());
public List<FileData> saveOrCreate(List<FileDataDto> fileDtos) throws IOException {
List<FileData> 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 {
public FileData create(FileDataDto fileDataDto) throws IOException {
FileData newFile = null;
newFile = createFileFromTmp(fileDataDto.getTmpFileName());
} catch (IOException e) {
}
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 {

View File

@ -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<User> 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);