#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.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@ -84,10 +84,13 @@ public class FileService {
fileRepository.delete(fileData); fileRepository.delete(fileData);
} }
public List<FileData> saveOrCreate(List<FileDataDto> files) { public List<FileData> saveOrCreate(List<FileDataDto> fileDtos) throws IOException {
return files.stream().map(fileDataDto -> { List<FileData> files = new ArrayList<>();
return fileDataDto.getId() != null ? update(fileDataDto) : create(fileDataDto); for (FileDataDto file : fileDtos) {
}).collect(Collectors.toList()); if (file.getId() != null) files.add(update(file));
else files.add(create(file));
}
return files;
} }
@Transactional @Transactional
@ -98,12 +101,9 @@ public class FileService {
} }
@Transactional @Transactional
public FileData create(FileDataDto fileDataDto) { public FileData create(FileDataDto fileDataDto) throws IOException {
FileData newFile = new FileData(); FileData newFile = null;
try { newFile = createFileFromTmp(fileDataDto.getTmpFileName());
newFile = createFileFromTmp(fileDataDto.getTmpFileName());
} catch (IOException e) {
}
copyFromDto(newFile, fileDataDto); copyFromDto(newFile, fileDataDto);
newFile = fileRepository.save(newFile); newFile = fileRepository.save(newFile);
return newFile; return newFile;
@ -115,9 +115,8 @@ public class FileService {
} }
@Transactional @Transactional
public void delete(Integer fileId) throws IOException { public void delete(Integer fileId) {
FileData file = fileRepository.findOne(fileId); fileRepository.delete(fileRepository.findOne(fileId));
fileRepository.delete(file);
} }
public FileDataDto createFromMultipartFile(MultipartFile multipartFile) throws IOException { public FileDataDto createFromMultipartFile(MultipartFile multipartFile) throws IOException {

View File

@ -20,8 +20,8 @@ import java.util.Date;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; 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 org.springframework.util.ObjectUtils.isEmpty;
import static ru.ulstu.core.util.StreamApiUtils.convert; 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.ATTENTION;
@ -67,7 +67,7 @@ public class PaperService {
return findAllDto() return findAllDto()
.stream() .stream()
.filter(paper -> paper.getStatus() != COMPLETED && paper.getStatus() != FAILED) .filter(paper -> paper.getStatus() != COMPLETED && paper.getStatus() != FAILED)
.collect(Collectors.toList()); .collect(toList());
} }
public PaperDto findOneDto(Integer id) { public PaperDto findOneDto(Integer id) {
@ -90,7 +90,9 @@ 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()));
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(); 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)));
@ -104,7 +106,9 @@ public class PaperService {
Paper.PaperStatus oldStatus = paper.getStatus(); Paper.PaperStatus oldStatus = paper.getStatus();
Set<User> oldAuthors = new HashSet<>(paper.getAuthors()); 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()); fileService.delete(file.getId());
} }
@ -164,7 +168,7 @@ public class PaperService {
return statusCompareResult; return statusCompareResult;
} }
return paper1.getTitle().compareTo(paper2.getTitle()); return paper1.getTitle().compareTo(paper2.getTitle());
}).collect(Collectors.toList()); }).collect(toList());
} }
public PaperDto findPaper(int id) { public PaperDto findPaper(int id) {
@ -178,7 +182,7 @@ public class PaperService {
&& (paper.getStatus() == ON_PREPARATION && (paper.getStatus() == ON_PREPARATION
|| paper.getStatus() == DRAFT || paper.getStatus() == DRAFT
|| paper.getStatus() == ATTENTION)) || paper.getStatus() == ATTENTION))
.collect(Collectors.toList()); .collect(toList());
papers.forEach(paper -> { papers.forEach(paper -> {
Paper.PaperStatus oldStatus = paper.getStatus(); Paper.PaperStatus oldStatus = paper.getStatus();
paper.setStatus(Paper.PaperStatus.FAILED); paper.setStatus(Paper.PaperStatus.FAILED);