|
1
|
|
|
package com.osomapps.pt.admin.goal; |
|
2
|
|
|
|
|
3
|
|
|
import com.osomapps.pt.ResourceNotFoundException; |
|
4
|
|
|
import com.osomapps.pt.dictionary.DictionaryName; |
|
5
|
|
|
import com.osomapps.pt.dictionary.DictionaryService; |
|
6
|
|
|
import com.osomapps.pt.goals.Goal; |
|
7
|
|
|
import com.osomapps.pt.goals.GoalParameterRepository; |
|
8
|
|
|
import com.osomapps.pt.goals.GoalRepository; |
|
9
|
|
|
import com.osomapps.pt.goals.GoalType; |
|
10
|
|
|
import com.osomapps.pt.goals.GoalTypeRepository; |
|
11
|
|
|
import java.util.List; |
|
12
|
|
|
import java.util.stream.Collectors; |
|
13
|
|
|
import org.springframework.data.domain.Sort; |
|
14
|
|
|
import org.springframework.stereotype.Service; |
|
15
|
|
|
|
|
16
|
|
|
@Service |
|
17
|
|
|
class AdminGoalService { |
|
18
|
|
|
private final GoalRepository goalRepository; |
|
19
|
|
|
private final GoalParameterRepository goalParameterRepository; |
|
20
|
|
|
private final DictionaryService dictionaryService; |
|
21
|
|
|
private final GoalTypeRepository goalTypeRepository; |
|
22
|
|
|
|
|
23
|
|
|
AdminGoalService( |
|
24
|
|
|
GoalRepository goalRepository, |
|
25
|
|
|
GoalParameterRepository goalParameterRepository, |
|
26
|
|
|
DictionaryService dictionaryService, |
|
27
|
|
|
GoalTypeRepository goalTypeRepository) { |
|
28
|
|
|
this.goalRepository = goalRepository; |
|
29
|
|
|
this.goalParameterRepository = goalParameterRepository; |
|
30
|
|
|
this.dictionaryService = dictionaryService; |
|
31
|
|
|
this.goalTypeRepository = goalTypeRepository; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
List<GoalResponseDTO> findAll() { |
|
35
|
|
|
return goalRepository.findAll(sortByIdAsc()).stream() |
|
36
|
|
|
.map(goal -> goalToDto(goal)) |
|
37
|
|
|
.collect(Collectors.toList()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private Sort sortByIdAsc() { |
|
41
|
|
|
return Sort.by(Sort.Direction.ASC, "id"); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
private GoalResponseDTO goalToDto(Goal goal) { |
|
45
|
|
|
return GoalResponseDTO.builder() |
|
46
|
|
|
.id(goal.getId()) |
|
47
|
|
|
.titleEn( |
|
48
|
|
|
dictionaryService.getEnValue( |
|
49
|
|
|
DictionaryName.goal_title, goal.getDGoalTitle(), "")) |
|
50
|
|
|
.titleNo( |
|
51
|
|
|
dictionaryService.getNoValue( |
|
52
|
|
|
DictionaryName.goal_title, goal.getDGoalTitle(), "")) |
|
53
|
|
|
.title2En( |
|
54
|
|
|
dictionaryService.getEnValue( |
|
55
|
|
|
DictionaryName.goal_title_2, goal.getDGoalTitle2(), null)) |
|
56
|
|
|
.title2No( |
|
57
|
|
|
dictionaryService.getNoValue( |
|
58
|
|
|
DictionaryName.goal_title_2, goal.getDGoalTitle2(), null)) |
|
59
|
|
|
.parameters( |
|
60
|
|
|
goal.getGoalParameters().stream() |
|
61
|
|
|
.map( |
|
62
|
|
|
parameter -> |
|
63
|
|
|
GoalParameterResponseDTO.builder() |
|
64
|
|
|
.id(parameter.getId()) |
|
65
|
|
|
.name(parameter.getName()) |
|
66
|
|
|
.build()) |
|
67
|
|
|
.collect(Collectors.toList())) |
|
68
|
|
|
.type( |
|
69
|
|
|
goal.getGoalType() == null |
|
70
|
|
|
? null |
|
71
|
|
|
: GoalTypeResponseDTO.builder() |
|
72
|
|
|
.id(goal.getGoalType().getId()) |
|
73
|
|
|
.name(goal.getGoalType().getName()) |
|
74
|
|
|
.build()) |
|
75
|
|
|
.build(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
GoalResponseDTO findOne(Long id) { |
|
79
|
|
|
final Goal goal = goalRepository.findById(id).orElse(null); |
|
80
|
|
|
if (goal == null) { |
|
81
|
|
|
throw new ResourceNotFoundException("Goal with id " + id + " not found."); |
|
82
|
|
|
} |
|
83
|
|
|
return goalToDto(goal); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
GoalResponseDTO create(GoalRequestDTO goalRequestDTO) { |
|
87
|
|
|
final GoalType goalTypeDb = |
|
88
|
|
|
goalRequestDTO.getType().getId() == null |
|
89
|
|
|
? null |
|
90
|
|
|
: goalTypeRepository |
|
91
|
|
|
.findById(goalRequestDTO.getType().getId()) |
|
92
|
|
|
.orElse(null); |
|
93
|
|
|
final String dataKey = dictionaryService.getNewDictionaryDataKey(DictionaryName.goal_title); |
|
94
|
|
|
dictionaryService.createDictionaryDataKey( |
|
95
|
|
|
DictionaryName.goal_title, |
|
96
|
|
|
dataKey, |
|
97
|
|
|
goalRequestDTO.getTitleEn(), |
|
98
|
|
|
goalRequestDTO.getTitleNo()); |
|
99
|
|
|
final String data2Key = |
|
100
|
|
|
dictionaryService.getNewDictionaryDataKey(DictionaryName.goal_title_2); |
|
101
|
|
|
dictionaryService.createDictionaryDataKey( |
|
102
|
|
|
DictionaryName.goal_title_2, |
|
103
|
|
|
dataKey, |
|
104
|
|
|
goalRequestDTO.getTitle2En(), |
|
105
|
|
|
goalRequestDTO.getTitle2No()); |
|
106
|
|
|
final Goal goal = new Goal(); |
|
107
|
|
|
goal.setDGoalTitle(dataKey); |
|
108
|
|
|
goal.setDGoalTitle2(data2Key); |
|
109
|
|
|
goal.setGoalParameters( |
|
110
|
|
|
goalParameterRepository.findAllById( |
|
111
|
|
|
goalRequestDTO.getParameters().stream() |
|
112
|
|
|
.map(type -> type.getId()) |
|
113
|
|
|
.collect(Collectors.toList()))); |
|
114
|
|
|
goal.setGoalType(goalTypeDb); |
|
115
|
|
|
return goalToDto(goalRepository.save(goal)); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
GoalResponseDTO update(Long id, GoalRequestDTO goalRequestDTO) { |
|
119
|
|
|
final Goal existedGoal = goalRepository.findById(id).orElse(null); |
|
120
|
|
|
if (existedGoal == null) { |
|
121
|
|
|
throw new ResourceNotFoundException("Goal with id not found: " + id); |
|
122
|
|
|
} |
|
123
|
|
|
final GoalType goalTypeDb = |
|
124
|
|
|
goalRequestDTO.getType().getId() == null |
|
125
|
|
|
? null |
|
126
|
|
|
: goalTypeRepository |
|
127
|
|
|
.findById(goalRequestDTO.getType().getId()) |
|
128
|
|
|
.orElse(null); |
|
129
|
|
|
final String dataKey = existedGoal.getDGoalTitle(); |
|
130
|
|
|
dictionaryService.createDictionaryDataKey( |
|
131
|
|
|
DictionaryName.goal_title, |
|
132
|
|
|
dataKey, |
|
133
|
|
|
goalRequestDTO.getTitleEn(), |
|
134
|
|
|
goalRequestDTO.getTitleNo()); |
|
135
|
|
|
final String data2Key = |
|
136
|
|
|
dictionaryService.createDictionaryDataKey( |
|
137
|
|
|
DictionaryName.goal_title_2, |
|
138
|
|
|
existedGoal.getDGoalTitle2(), |
|
139
|
|
|
goalRequestDTO.getTitle2En(), |
|
140
|
|
|
goalRequestDTO.getTitle2No()); |
|
141
|
|
|
existedGoal.setDGoalTitle(dataKey); |
|
142
|
|
|
existedGoal.setDGoalTitle2(data2Key); |
|
143
|
|
|
existedGoal.setGoalParameters( |
|
144
|
|
|
goalParameterRepository.findAllById( |
|
145
|
|
|
goalRequestDTO.getParameters().stream() |
|
146
|
|
|
.map(type -> type.getId()) |
|
147
|
|
|
.collect(Collectors.toList()))); |
|
148
|
|
|
existedGoal.setGoalType(goalTypeDb); |
|
149
|
|
|
final Goal savedGoal = goalRepository.save(existedGoal); |
|
150
|
|
|
return goalToDto(savedGoal); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
GoalResponseDTO delete(Long id) { |
|
154
|
|
|
final Goal goal = goalRepository.findById(id).orElse(null); |
|
155
|
|
|
if (goal == null) { |
|
156
|
|
|
throw new ResourceNotFoundException("Goal with id " + id + " not found."); |
|
157
|
|
|
} |
|
158
|
|
|
dictionaryService.deleteDatas(DictionaryName.goal_title, goal.getDGoalTitle()); |
|
159
|
|
|
dictionaryService.deleteDatas(DictionaryName.goal_title_2, goal.getDGoalTitle2()); |
|
160
|
|
|
final GoalResponseDTO goalResponseDTO = goalToDto(goal); |
|
161
|
|
|
goalRepository.deleteById(id); |
|
162
|
|
|
return goalResponseDTO; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|