findBySubject(SubjectModelInterface)   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.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 topicsOptions.withRelations(this.map(this.topicsMapper.findBySubjectId(subjectModel.getId())));
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 void save(TopicModelInterface topicModel) {
64
        final TopicEntity topicEntity = this.map(topicModel);
65
        if (topicEntity.getId() == null) {
66
            this.topicsMapper.insert(topicEntity);
67
            topicModel.setId(topicEntity.getId());
68
            return;
69
        }
70
        this.topicsMapper.update(topicEntity);
71
    }
72
73
    @Override
74
    public void save(TopicModelInterface topicModel, TopicsOptionsInterface topicsOptions) {
75
        this.withServices(topicsOptions).saveWithRelations(topicModel);
76
    }
77
78
    @Override
79
    public void save(List<TopicModelInterface> topicsModels) {
80
        for (TopicModelInterface topicModel: topicsModels) {
81
            this.save(topicModel);
82
        }
83
    }
84
85
    @Override
86
    public void save(List<TopicModelInterface> topicsModels, TopicsOptionsInterface topicsOptions) {
87
        for (TopicModelInterface topicModel: topicsModels) {
88
            this.save(topicModel, topicsOptions);
89
        }
90
    }
91
92
    @Override
93
    public void delete(TopicModelInterface topicModel) {
94
        final TopicEntity topicEntity = this.map(topicModel);
95
        if (topicEntity.getId() == null) {
96
            throw new DeleteUnidentifiedModelException();
97
        }
98
        this.topicsMapper.delete(topicEntity);
99
    }
100
101
    @Override
102
    public void delete(TopicModelInterface topicModel, TopicsOptionsInterface topicsOptions) {
103
        this.withServices(topicsOptions).deleteWithRelations(topicModel);
104
    }
105
106
    @Override
107
    public void delete(List<TopicModelInterface> topicsModels) {
108
        for (TopicModelInterface topicModel: topicsModels) {
109
            this.delete(topicModel);
110
        }
111
    }
112
113
    @Override
114
    public void delete(List<TopicModelInterface> topicsModels, TopicsOptionsInterface topicsOptions) {
115
        for (TopicModelInterface topicModel: topicsModels) {
116
            this.delete(topicModel, topicsOptions);
117
        }
118
    }
119
120
    private TopicsOptionsInterface withServices(TopicsOptionsInterface topicsOptions) {
121
        topicsOptions.setTopicsService(this);
122
        topicsOptions.setSubjectsService(this.subjectsService);
123
        topicsOptions.setQuestionsService(this.questionsService);
124
        return topicsOptions;
125
    }
126
127
    private TopicModelInterface map(TopicEntity topicEntity) {
128
        if (topicEntity == null) {
129
            return null;
130
        }
131
        final TopicModelInterface topicModel = new TopicModel();
132
        topicModel.map(topicEntity);
133
        return topicModel;
134
    }
135
136
    private TopicEntity map(TopicModelInterface topicModel) {
137
        final TopicEntity topicEntity = new TopicEntity();
138
        topicEntity.map(topicModel);
139
        return topicEntity;
140
    }
141
142
    private List<TopicModelInterface> map(List<TopicEntity> topicsList) {
143
        final List<TopicModelInterface> resultUsersList = new ArrayList(topicsList.size());
144
        for (TopicEntity topic: topicsList) {
145
            resultUsersList.add(this.map(topic));
146
        }
147
        return resultUsersList;
148
    }
149
}
150