sort papers

This commit is contained in:
Anton Romanov 2018-09-05 14:50:49 +04:00
parent 294ed1838a
commit e9d81fef48
3 changed files with 16 additions and 2 deletions

View File

@ -12,7 +12,7 @@ import java.util.List;
@Entity
public class Paper extends BaseEntity {
public enum PaperStatus {
DRAFT("Черновик"), ON_PREPARATION("На подготовке"), COMPLETED("Завершена"), ATTENTION("Обратить внимание");
ATTENTION("Обратить внимание"), ON_PREPARATION("На подготовке"), DRAFT("Черновик"), COMPLETED("Завершена");
private String name;

View File

@ -3,5 +3,7 @@ package ru.ulstu.paper.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import ru.ulstu.paper.model.Paper;
import java.util.List;
public interface PaperRepository extends JpaRepository<Paper, Integer> {
}

View File

@ -8,7 +8,9 @@ import ru.ulstu.paper.model.PaperDto;
import ru.ulstu.paper.repository.PaperRepository;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import static ru.ulstu.core.util.StreamApiUtils.convert;
@ -25,7 +27,17 @@ public class PaperService {
}
public List<PaperDto> findAll() {
return convert(paperRepository.findAll(), PaperDto::new);
List<Paper> allPapers = paperRepository.findAll();
allPapers = allPapers.stream().sorted((paper1, paper2) -> {
int statusCompareResult =
Integer.valueOf(Arrays.asList(Paper.PaperStatus.values()).indexOf(paper1.getStatus()))
.compareTo(Integer.valueOf(Arrays.asList(Paper.PaperStatus.values()).indexOf(paper2.getStatus())));
if (statusCompareResult != 0) {
return statusCompareResult;
}
return paper1.getTitle().compareTo(paper2.getTitle());
}).collect(Collectors.toList());
return convert(allPapers, PaperDto::new);
}
@Transactional