Passed
Pull Request — dev (#331)
by
unknown
05:24
created

findBySubject(SubjectModelInterface,TopicsOptionsInterface)   A

Complexity

Conditions 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
package easytests.core.services;
2
3
import easytests.core.entities.TopicEntity;
4
import easytests.core.mappers.TopicsMapper;
5
import easytests.core.models.SubjectModelInterface;
6
import easytests.core.models.TopicModel;
7
import easytests.core.models.TopicModelInterface;
8
import easytests.core.options.TopicsOptionsInterface;
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 malinink
18
 */
19
@Service
20
public class TopicsService implements TopicsServiceInterface {
21
    @Autowired
22
    private TopicsMapper topicsMapper;
23
24
    @Autowired
25
    private SubjectsService subjectsService;
26
27
    @Autowired
28
    private QuestionsService questionsService;
29
30
    @Override
31
    public List<TopicModelInterface> findAll() {
32
        return this.map(this.topicsMapper.findAll());
33
    }
34
35
    @Override
36
    public List<TopicModelInterface> findAll(TopicsOptionsInterface topicsOptions) {
37
        return this.withServices(topicsOptions).withRelations(this.findAll());
38
    }
39
40
    @Override
41
    public TopicModelInterface find(Integer id) {
42
        return this.map(this.topicsMapper.find(id));
43
    }
44
45
    @Override
46
    public TopicModelInterface find(Integer id, TopicsOptionsInterface usersOptions) {
47
        return this.withServices(usersOptions).withRelations(this.find(id));
48
    }
49
50
    @Override
51
    public List<TopicModelInterface> findBySubject(
52
            SubjectModelInterface subjectModel,
53
            TopicsOptionsInterface topicsOptions) {
54
        return this.withServices(topicsOptions).withRelations(this.findBySubject(subjectModel));
55
    }
56
57
    @Override
58
    public List<TopicModelInterface> findBySubject(SubjectModelInterface subjectModel) {
59
        return this.map(this.topicsMapper.findBySubjectId(subjectModel.getId()));
60
    }
61
62
    @Override
63
    public List<TopicModelInterface> findBySubject(Integer subjectId) {
64
        return this.map(this.topicsMapper.findBySubjectId(subjectId));
65
    }
66
67
    @Override
68
    public void save(TopicModelInterface topicModel) {
69
        final TopicEntity topicEntity = this.map(topicModel);
70
        if (topicEntity.getId() == null) {
71
            this.topicsMapper.insert(topicEntity);
72
            topicModel.setId(topicEntity.getId());
73
            return;
74
        }
75
        this.topicsMapper.update(topicEntity);
76
    }
77
78
    @Override
79
    public void save(TopicModelInterface topicModel, TopicsOptionsInterface topicsOptions) {
80
        this.withServices(topicsOptions).saveWithRelations(topicModel);
81
    }
82
83
    @Override
84
    public void save(List<TopicModelInterface> topicsModels) {
85
        for (TopicModelInterface topicModel: topicsModels) {
86
            this.save(topicModel);
87
        }
88
    }
89
90
    @Override
91
    public void save(List<TopicModelInterface> topicsModels, TopicsOptionsInterface topicsOptions) {
92
        for (TopicModelInterface topicModel: topicsModels) {
93
            this.save(topicModel, topicsOptions);
94
        }
95
    }
96
97
    @Override
98
    public void delete(TopicModelInterface topicModel) {
99
        final TopicEntity topicEntity = this.map(topicModel);
100
        if (topicEntity.getId() == null) {
101
            throw new DeleteUnidentifiedModelException();
102
        }
103
        this.topicsMapper.delete(topicEntity);
104
    }
105
106
    @Override
107
    public void delete(TopicModelInterface topicModel, TopicsOptionsInterface topicsOptions) {
108
        this.withServices(topicsOptions).deleteWithRelations(topicModel);
109
    }
110
111
    @Override
112
    public void delete(List<TopicModelInterface> topicsModels) {
113
        for (TopicModelInterface topicModel: topicsModels) {
114
            this.delete(topicModel);
115
        }
116
    }
117
118
    @Override
119
    public void delete(List<TopicModelInterface> topicsModels, TopicsOptionsInterface topicsOptions) {
120
        for (TopicModelInterface topicModel: topicsModels) {
121
            this.delete(topicModel, topicsOptions);
122
        }
123
    }
124
125
    private TopicsOptionsInterface withServices(TopicsOptionsInterface topicsOptions) {
126
        topicsOptions.setTopicsService(this);
127
        topicsOptions.setSubjectsService(this.subjectsService);
128
        topicsOptions.setQuestionsService(this.questionsService);
129
        return topicsOptions;
130
    }
131
132
    private TopicModelInterface map(TopicEntity topicEntity) {
133
        if (topicEntity == null) {
134
            return null;
135
        }
136
        final TopicModelInterface topicModel = new TopicModel();
137
        topicModel.map(topicEntity);
138
        return topicModel;
139
    }
140
141
    private TopicEntity map(TopicModelInterface topicModel) {
142
        final TopicEntity topicEntity = new TopicEntity();
143
        topicEntity.map(topicModel);
144
        return topicEntity;
145
    }
146
147
    private List<TopicModelInterface> map(List<TopicEntity> topicsList) {
148
        final List<TopicModelInterface> resultTopicsList = new ArrayList<>(topicsList.size());
149
        for (TopicEntity topic: topicsList) {
150
            resultTopicsList.add(this.map(topic));
151
        }
152
        return resultTopicsList;
153
    }
154
}
155