|
1
|
|
|
package easytests.core.services; |
|
2
|
|
|
|
|
3
|
|
|
import easytests.core.entities.TesteeEntity; |
|
4
|
|
|
import easytests.core.mappers.TesteesMapper; |
|
5
|
|
|
import easytests.core.models.QuizModelInterface; |
|
6
|
|
|
import easytests.core.models.TesteeModel; |
|
7
|
|
|
import easytests.core.models.TesteeModelInterface; |
|
8
|
|
|
import easytests.core.options.TesteesOptionsInterface; |
|
9
|
|
|
import easytests.core.services.exceptions.DeleteUnidentifiedModelException; |
|
10
|
|
|
import java.util.ArrayList; |
|
11
|
|
|
import java.util.List; |
|
12
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
13
|
|
|
import org.springframework.stereotype.Service; |
|
14
|
|
|
|
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @author DoZor-80 |
|
18
|
|
|
*/ |
|
19
|
|
|
@Service |
|
20
|
|
|
public class TesteesService implements TesteesServiceInterface { |
|
21
|
|
|
@Autowired |
|
22
|
|
|
private TesteesMapper testeesMapper; |
|
23
|
|
|
|
|
24
|
|
|
@Autowired |
|
25
|
|
|
private QuizzesService quizzesService; |
|
26
|
|
|
|
|
27
|
|
|
@Override |
|
28
|
|
|
public TesteeModelInterface findByQuiz(QuizModelInterface quiz) { |
|
29
|
|
|
final TesteeEntity testeeEntity = this.testeesMapper.findByQuizId(quiz.getId()); |
|
30
|
|
|
if (testeeEntity == null) { |
|
31
|
|
|
return null; |
|
32
|
|
|
} |
|
33
|
|
|
return this.map(testeeEntity); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
@Override |
|
37
|
|
|
public TesteeModelInterface findByQuiz(QuizModelInterface quizModel, |
|
38
|
|
|
TesteesOptionsInterface testeesOptions) { |
|
39
|
|
|
return this.withServices(testeesOptions).withRelations(this.findByQuiz(quizModel)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
@Override |
|
43
|
|
|
public List<TesteeModelInterface> findAll() { |
|
44
|
|
|
return this.map(this.testeesMapper.findAll()); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
@Override |
|
48
|
|
|
public List<TesteeModelInterface> findAll(TesteesOptionsInterface testeesOptions) { |
|
49
|
|
|
return this.withServices(testeesOptions).withRelations(this.findAll()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
@Override |
|
53
|
|
|
public TesteeModelInterface find(Integer id) { |
|
54
|
|
|
return this.map(this.testeesMapper.find(id)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
@Override |
|
58
|
|
|
public TesteeModelInterface find(Integer id, TesteesOptionsInterface testeesOptions) { |
|
59
|
|
|
return this.withServices(testeesOptions).withRelations(this.find(id)); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
@Override |
|
63
|
|
|
public void save(TesteeModelInterface testeeModel) { |
|
64
|
|
|
final TesteeEntity testeeEntity = this.map(testeeModel); |
|
65
|
|
|
if (testeeEntity.getId() == null) { |
|
66
|
|
|
this.testeesMapper.insert(testeeEntity); |
|
67
|
|
|
testeeModel.setId(testeeEntity.getId()); |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
this.testeesMapper.update(testeeEntity); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
@Override |
|
74
|
|
|
public void save(TesteeModelInterface testeeModel, TesteesOptionsInterface testeesOptions) { |
|
75
|
|
|
this.withServices(testeesOptions).saveWithRelations(testeeModel); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
@Override |
|
79
|
|
|
public void delete(TesteeModelInterface testeeModel) { |
|
80
|
|
|
final TesteeEntity testeeEntity = this.map(testeeModel); |
|
81
|
|
|
if (testeeEntity.getId() == null) { |
|
82
|
|
|
throw new DeleteUnidentifiedModelException(); |
|
83
|
|
|
} |
|
84
|
|
|
this.testeesMapper.delete(testeeEntity); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
@Override |
|
88
|
|
|
public void delete(TesteeModelInterface testeeModel, TesteesOptionsInterface testeesOptions) { |
|
89
|
|
|
this.withServices(testeesOptions).deleteWithRelations(testeeModel); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
private TesteesOptionsInterface withServices(TesteesOptionsInterface testeesOptions) { |
|
93
|
|
|
testeesOptions.setTesteesService(this); |
|
94
|
|
|
testeesOptions.setQuizzesService(this.quizzesService); |
|
95
|
|
|
return testeesOptions; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private TesteeModelInterface map(TesteeEntity testeeEntity) { |
|
99
|
|
|
if (testeeEntity == null) { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
final TesteeModelInterface testeeModel = new TesteeModel(); |
|
103
|
|
|
testeeModel.map(testeeEntity); |
|
104
|
|
|
return testeeModel; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
private TesteeEntity map(TesteeModelInterface testeeModel) { |
|
108
|
|
|
final TesteeEntity testeeEntity = new TesteeEntity(); |
|
109
|
|
|
testeeEntity.map(testeeModel); |
|
110
|
|
|
return testeeEntity; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private List<TesteeModelInterface> map(List<TesteeEntity> testeesList) { |
|
114
|
|
|
final List<TesteeModelInterface> resultTesteeList = new ArrayList(testeesList.size()); |
|
115
|
|
|
for (TesteeEntity testee: testeesList) { |
|
116
|
|
|
resultTesteeList.add(this.map(testee)); |
|
117
|
|
|
} |
|
118
|
|
|
return resultTesteeList; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|