-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProjectServiceImpl.java
158 lines (125 loc) · 5.67 KB
/
ProjectServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.netz00.hibernatesearch6example.services;
import com.netz00.hibernatesearch6example.dto.ProjectDTO;
import com.netz00.hibernatesearch6example.exceptions.ResourceNotFoundException;
import com.netz00.hibernatesearch6example.model.Project;
import com.netz00.hibernatesearch6example.model.enums.ProjectSort;
import com.netz00.hibernatesearch6example.model.mapper.ProjectMapper;
import com.netz00.hibernatesearch6example.repository.ProjectRepository;
import com.netz00.hibernatesearch6example.services.api.ProjectService;
import org.hibernate.search.engine.search.query.SearchResult;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.session.SearchSession;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
@Service
@Transactional
public class ProjectServiceImpl implements ProjectService {
private final ProjectRepository projectRepository;
private final ProjectMapper projectMapper;
EntityManager entityManager;
public ProjectServiceImpl(ProjectRepository projectRepository, ProjectMapper projectMapper, EntityManager entityManager) {
this.projectRepository = projectRepository;
this.projectMapper = projectMapper;
this.entityManager = entityManager;
}
@Override
public Page<ProjectDTO> findAll(Pageable pageable) {
return projectRepository.findAll(pageable).map(projectMapper::toDto);
}
/**
* Search method capable for search and sort.
* Uses projections and fetched results directly from Elasticsearch.
*
* @param query the string containing searched term, can be od part of project name
* @param sort should results be sorted
* @param ascending change the order if sorting used
* @param page
* @param size
* @return
*/
@Override
public Page<ProjectDTO> searchProjects(String query, ProjectSort sort, Boolean ascending, int page, int size) {
int length = query.length();
SearchSession searchSession = Search.session(entityManager);
SearchResult<List<?>> result = searchSession.search(Project.class)
.select(
f -> f.composite(f.id(Long.class),
f.field("name", String.class),
f.field("date_created", Date.class)
)
)
.where(f -> f.match()
.fields("name")
.matching(query)
.fuzzy(length > 3 ? 1 : 0)) // if query longer than 3 chars allow fuzzy for the edit distance of 1
.sort(sort == null ?
f -> f.score()
:
f -> ascending ?
f.field(sort.toString().toLowerCase())
:
f.field(sort.toString().toLowerCase()).desc()
)
.fetch(page * size, size); // fetch single page of results
List<ProjectDTO> projectDTOS = result.hits().stream().map(s -> {
ProjectDTO projectDTO = new ProjectDTO();
projectDTO.setId((Long) s.get(0));
projectDTO.setName((String) s.get(1));
projectDTO.setDateCreated((Date) s.get(2));
return projectDTO;
}).collect(Collectors.toList());
long totalHitCount = result.total().hitCount();
Pageable paging = PageRequest.of(page, size);
return new PageImpl<>(projectDTOS, paging, totalHitCount);
}
/**
* Same as previous but without using Projections
*
* @param query
* @param sort
* @param ascending
* @param page
* @param size
* @return
*/
@Override
public Page<ProjectDTO> searchProjectsEntities(String query, ProjectSort sort, Boolean ascending, int page, int size) {
int length = query.length();
SearchSession searchSession = Search.session(entityManager);
SearchResult<Project> result = searchSession.search(Project.class)
.where(f -> f.match()
.fields("name")
.matching(query)
.fuzzy(length > 3 ? 1 : 0)) // if query longer than 3 chars allow fuzzy for the edit distance of 1
.sort(sort == null ?
f -> f.score()
:
f -> ascending ?
f.field(sort.toString().toLowerCase())
:
f.field(sort.toString().toLowerCase()).desc()
)
.fetch(page * size, size); // fetch single page of results
long totalHitCount = result.total().hitCount();
Pageable paging = PageRequest.of(page, size);
return new PageImpl<>(projectMapper.toDto(result.hits()), paging, totalHitCount);
}
@Override
public ProjectDTO save(ProjectDTO project) {
return projectMapper.toDto(projectRepository.save(projectMapper.toEntity(project)));
}
@Override
public ProjectDTO delete(Long id) {
Project project = projectRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Project not found with id :" + id));
projectRepository.delete(project);
return projectMapper.toDto(project);
}
}