-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurationService.java
More file actions
217 lines (197 loc) · 8.79 KB
/
ConfigurationService.java
File metadata and controls
217 lines (197 loc) · 8.79 KB
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package de.unistuttgart.bugfinder.configuration;
import de.unistuttgart.bugfinder.code.Code;
import de.unistuttgart.bugfinder.code.CodeDTO;
import de.unistuttgart.bugfinder.code.CodeMapper;
import de.unistuttgart.bugfinder.code.CodeRepository;
import de.unistuttgart.bugfinder.code.word.Word;
import de.unistuttgart.bugfinder.code.word.WordRepository;
import de.unistuttgart.bugfinder.configuration.vm.CodeVM;
import de.unistuttgart.bugfinder.configuration.vm.ConfigurationVM;
import de.unistuttgart.bugfinder.configuration.vm.WordVM;
import de.unistuttgart.bugfinder.solution.Solution;
import de.unistuttgart.bugfinder.solution.SolutionRepository;
import de.unistuttgart.bugfinder.solution.bug.Bug;
import de.unistuttgart.bugfinder.solution.bug.BugRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
import java.util.*;
@Service
@Slf4j
public class ConfigurationService {
@Autowired
private ConfigurationRepository configurationRepository;
@Autowired
private ConfigurationMapper configurationMapper;
@Autowired
private CodeMapper codeMapper;
@Autowired
private CodeRepository codeRepository;
@Autowired
private SolutionRepository solutionRepository;
@Autowired
private WordRepository wordRepository;
@Autowired
private BugRepository bugRepository;
/**
* Get a configuration by its id.
*
* @param id the id of the configuration
* @return the found configuration
* @throws ResponseStatusException (404) when configuration with its id does not exist
*/
public Configuration getConfiguration(final UUID id) {
return configurationRepository
.findById(id)
.orElseThrow(() ->
new ResponseStatusException(HttpStatus.NOT_FOUND, String.format("Configuration with id %s not found.", id))
);
}
/**
* @return all configurations as list of DTOs
*/
public List<ConfigurationDTO> findAll() {
log.debug("get all configurations");
return configurationMapper.toDTO(configurationRepository.findAll());
}
/**
* Get the configuration by its id as DTO.
*
* @param id the id of the configuration
* @return the found configuration as DTO
* @throws ResponseStatusException (404) when configuration with its id does not exist
*/
public ConfigurationDTO find(final UUID id) {
log.debug("get configuration {}", id);
Configuration configuration = getConfiguration(id);
return configurationMapper.toDTO(configuration);
}
/**
* Save a configuration.
*
* @param configurationDTO the configuration to save
* @return the saved configuration as DTO
*/
public ConfigurationDTO save(final ConfigurationDTO configurationDTO) {
log.debug("create configuration {}", configurationDTO);
return configurationMapper.toDTO(configurationRepository.save(configurationMapper.fromDTO(configurationDTO)));
}
public ConfigurationDTO build(final ConfigurationVM configurationVM) {
Set<Code> codesToPersist = new HashSet<>(configurationVM.getCodes().size());
for (CodeVM codeVM : configurationVM.getCodes()) {
List<Word> wordsToPersistToCode = new ArrayList<>(codeVM.getWords().size());
Set<Bug> bugsToPersistToSolution = new HashSet<>();
bugsToPersistToSolution = new HashSet<>();
for (List<WordVM> row : codeVM.getWords()) {
// check if the row only contains blank strings
// the first row will never contain only blank strings since it comes from the lecture interface
if (row.stream().allMatch(wordVM -> wordVM.getCorrectValue().isBlank())) {
continue;
}
// if it is not the first row in the code we add a new line
if (codeVM.getWords().indexOf(row) != 0) {
wordsToPersistToCode.add(wordRepository.save(new Word(null, "\n")));
}
for (WordVM wordVM : row) {
// check if the word is blank
// the first word will never be blank since it comes from the lecture interface
if (wordVM.getCorrectValue().isBlank()) {
continue;
}
// if it is not the first word in the row we add a space
if (row.indexOf(wordVM) != 0) {
wordsToPersistToCode.add(wordRepository.save(new Word(null, " ")));
}
String displayValue = wordVM.getDisplayValue() != null ? wordVM.getDisplayValue() : wordVM.getCorrectValue();
Word word = new Word(null, displayValue);
word = wordRepository.save(word);
wordsToPersistToCode.add(word);
if (wordVM.getErrorType() != null) {
Bug bug = new Bug(word, wordVM.getErrorType(), wordVM.getCorrectValue());
bug = bugRepository.save(bug);
bugsToPersistToSolution.add(bug);
}
}
}
Code code = new Code(null, wordsToPersistToCode);
codesToPersist.add(code);
Solution solution = new Solution(null, bugsToPersistToSolution, code);
code = codeRepository.save(code);
solution = solutionRepository.save(solution);
}
Configuration configuration = new Configuration(null, codesToPersist);
return configurationMapper.toDTO(configurationRepository.save(configuration));
}
/**
* Deletes the configuration with the given ID, if present.
*
* @param id the ID of the configuration to delete
* @return the deleted configuration, if found
* @throws ResponseStatusException (204 - NO_CONTENT) if the configuration was not found
*/
public ConfigurationDTO delete(final UUID id) {
log.info("delete configuration {}", id);
Configuration configuration = configurationRepository
.findById(id)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NO_CONTENT));
configurationRepository.deleteById(id);
return configurationMapper.toDTO(configuration);
}
/**
* Get all codes of a configuration.
*
* @param id the configuration id
* @return the codes of the specified configuration
*/
public List<CodeDTO> getCodes(final UUID id) {
log.debug("get codes from configuration {}", id);
final Configuration configuration = getConfiguration(id);
return codeMapper.toDTO(configuration.getCodes());
}
public ConfigurationVM getViewModel(UUID id) {
log.debug("get configuration view model {}", id);
final Configuration configuration = getConfiguration(id);
final ConfigurationVM configurationVM = new ConfigurationVM(new ArrayList<>());
for (Code code : configuration.getCodes()) {
final CodeVM codeVM = new CodeVM(new ArrayList<>());
final Solution solution = solutionRepository
.findByCodeId(code.getId())
.orElseThrow(() ->
new ResponseStatusException(
HttpStatus.NOT_FOUND,
String.format("Solution with code id %s not found.", code.getId())
)
);
List<WordVM> row = new ArrayList<>();
for (Word word : code.getWords()) {
if (word.getWord().equals("\n")) {
codeVM.getWords().add(row);
row = new ArrayList<>();
continue;
}
Optional<Bug> bug = solution
.getBugs()
.stream()
.filter(b -> b.getWord().getId().equals(word.getId()))
.findFirst();
if (word.getWord().equals(" ") && bug.isEmpty()) {
continue;
}
final WordVM wordVM = new WordVM();
if (bug.isPresent()) {
wordVM.setErrorType(bug.get().getErrorType());
wordVM.setCorrectValue(bug.get().getCorrectValue());
wordVM.setDisplayValue(word.getWord());
} else {
wordVM.setCorrectValue(word.getWord());
}
row.add(wordVM);
}
codeVM.getWords().add(row);
configurationVM.getCodes().add(codeVM);
}
return configurationVM;
}
}