findByQuiz(QuizModelInterface,PointsOptionsInterface)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
c 1
b 0
f 0
cc 1
loc 3
rs 10
1
package easytests.core.services;
2
3
import easytests.core.entities.PointEntity;
4
import easytests.core.mappers.PointsMapper;
5
import easytests.core.models.PointModel;
6
import easytests.core.models.PointModelInterface;
7
import easytests.core.models.QuizModelInterface;
8
import easytests.core.options.PointsOptionsInterface;
9
import easytests.core.services.exceptions.DeleteUnidentifiedModelException;
10
import java.util.ArrayList;
11
import java.util.List;
12
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.stereotype.Service;
15
16
/**
17
 * @author nikitalpopov
18
 */
19
@Service
20
public class PointsService implements PointsServiceInterface {
21
22
    @Autowired
23
    private PointsMapper pointsMapper;
24
25
    @Autowired
26
    private QuizzesService quizzesService;
27
28
    @Autowired
29
    private QuestionsService questionsService;
30
31
    @Autowired
32
    private SolutionsService solutionsService;
33
34
    @Override
35
    public List<PointModelInterface> findAll() {
36
        return this.map(this.pointsMapper.findAll());
37
    }
38
39
    @Override
40
    public List<PointModelInterface> findAll(PointsOptionsInterface pointsOptions) {
41
        return this.withServices(pointsOptions).withRelations(this.findAll());
42
    }
43
44
    @Override
45
    public PointModelInterface find(Integer id) {
46
47
        final PointEntity pointEntity = this.pointsMapper.find(id);
48
        if (pointEntity == null) {
49
            return null;
50
        }
51
        return this.map(pointEntity);
52
53
    }
54
55
    @Override
56
    public PointModelInterface find(Integer id, PointsOptionsInterface pointsOptions) {
57
        return this.withServices(pointsOptions).withRelations(this.find(id));
58
    }
59
60
    @Override
61
    public List<PointModelInterface> findByQuiz(QuizModelInterface quizModel) {
62
        return this.map(this.pointsMapper.findByQuizId(quizModel.getId()));
63
    }
64
65
    @Override
66
    public List<PointModelInterface> findByQuiz(QuizModelInterface quizModel, PointsOptionsInterface pointsOptions) {
67
        return this.withServices(pointsOptions).withRelations(this.findByQuiz(quizModel));
68
    }
69
70
    @Override
71
    public void save(PointModelInterface pointModel) {
72
73
        final PointEntity pointEntity = this.map(pointModel);
74
        if (pointEntity.getId() == null) {
75
            this.pointsMapper.insert(pointEntity);
76
            pointModel.setId(pointEntity.getId());
77
            return;
78
        }
79
        this.pointsMapper.update(pointEntity);
80
81
    }
82
83
    @Override
84
    public void save(PointModelInterface pointModel, PointsOptionsInterface pointsOptions) {
85
        this.withServices(pointsOptions).saveWithRelations(pointModel);
86
    }
87
88
    @Override
89
    public void save(List<PointModelInterface> pointsModels) {
90
91
        for (PointModelInterface pointModel: pointsModels) {
92
            this.save(pointModel);
93
        }
94
95
    }
96
97
    @Override
98
    public void save(List<PointModelInterface> pointsModels, PointsOptionsInterface pointsOptions) {
99
100
        for (PointModelInterface pointModel: pointsModels) {
101
            this.save(pointModel, pointsOptions);
102
        }
103
104
    }
105
106
    @Override
107
    public void delete(PointModelInterface pointModel) {
108
109
        final PointEntity pointEntity = this.map(pointModel);
110
        if (pointEntity.getId() == null) {
111
            throw new DeleteUnidentifiedModelException();
112
        }
113
        this.pointsMapper.delete(pointEntity);
114
115
    }
116
117
    @Override
118
    public void delete(PointModelInterface pointModel, PointsOptionsInterface pointsOptions) {
119
        this.withServices(pointsOptions).deleteWithRelations(pointModel);
120
    }
121
122
    @Override
123
    public void delete(List<PointModelInterface> pointsModels) {
124
125
        for (PointModelInterface pointModel: pointsModels) {
126
            this.delete(pointModel);
127
        }
128
129
    }
130
131
    @Override
132
    public void delete(List<PointModelInterface> pointsModels, PointsOptionsInterface pointsOptions) {
133
134
        for (PointModelInterface pointModel: pointsModels) {
135
            this.delete(pointModel, pointsOptions);
136
        }
137
138
    }
139
140
    private PointEntity map(PointModelInterface pointModel) {
141
142
        final PointEntity pointEntity = new PointEntity();
143
        pointEntity.map(pointModel);
144
        return pointEntity;
145
146
    }
147
148
    private PointModel map(PointEntity pointEntity) {
149
150
        final PointModel pointModel = new PointModel();
151
        pointModel.map(pointEntity);
152
        return pointModel;
153
154
    }
155
156
    private List<PointModelInterface> map(List<PointEntity> pointsList) {
157
158
        final List<PointModelInterface> resultPointList = new ArrayList<>(pointsList.size());
159
        for (PointEntity point: pointsList) {
160
            resultPointList.add(this.map(point));
161
        }
162
        return resultPointList;
163
164
    }
165
166
    private PointsOptionsInterface withServices(PointsOptionsInterface pointsOptions) {
167
168
        pointsOptions.setPointsService(this);
169
        pointsOptions.setQuizzesService(this.quizzesService);
170
        pointsOptions.setQuestionsService(this.questionsService);
171
        pointsOptions.setSolutionsService(this.solutionsService);
172
173
        return pointsOptions;
174
175
    }
176
177
}
178