withAnswers(AnswersOptionsInterface)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 4

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 4
rs 10
1
package easytests.core.options;
2
3
import easytests.core.models.QuestionModelInterface;
4
import easytests.core.services.AnswersServiceInterface;
5
import easytests.core.services.QuestionTypesServiceInterface;
6
import easytests.core.services.QuestionsServiceInterface;
7
import easytests.core.services.TopicsServiceInterface;
8
import java.util.List;
9
import lombok.EqualsAndHashCode;
10
import lombok.Setter;
11
12
13
/**
14
 * @author firkhraag
15
 */
16
@EqualsAndHashCode
17
public class QuestionsOptions implements QuestionsOptionsInterface {
18
19
    @Setter
20
    private QuestionsServiceInterface questionsService;
21
22
    @Setter
23
    private TopicsServiceInterface topicsService;
24
25
    @Setter
26
    private AnswersServiceInterface answersService;
27
28
    @Setter
29
    private QuestionTypesServiceInterface questionTypesService;
30
31
    private TopicsOptionsInterface topicsOptions;
32
33
    private AnswersOptionsInterface answersOptions;
34
35
    private QuestionTypesOptionsInterface questionTypesOptions;
36
37
    @Override
38
    public QuestionsOptionsInterface withAnswers(AnswersOptionsInterface answerOptions) {
39
        this.answersOptions = answerOptions;
40
        return this;
41
    }
42
43
    @Override
44
    public QuestionsOptionsInterface withTopic(TopicsOptionsInterface topicOptions) {
45
        this.topicsOptions = topicOptions;
46
        return this;
47
    }
48
49
    @Override
50
    public QuestionsOptionsInterface withQuestionType(QuestionTypesOptionsInterface questionTypesOptions) {
51
        this.questionTypesOptions = questionTypesOptions;
52
        return this;
53
    }
54
55
    @Override
56
    public QuestionModelInterface withRelations(QuestionModelInterface questionModel) {
57
        if (questionModel == null) {
58
            return questionModel;
59
        }
60
        if (this.answersOptions != null) {
61
            questionModel.setAnswers(this.answersService.findByQuestion(questionModel, this.answersOptions));
62
        }
63
        if (this.topicsOptions != null) {
64
            questionModel.setTopic(this.topicsService.find(questionModel.getTopic().getId(), this.topicsOptions));
65
        }
66
        if (this.questionTypesOptions != null) {
67
            questionModel.setQuestionType(
68
                    this.questionTypesService.find(questionModel.getQuestionType().getId(), questionTypesOptions));
69
        }
70
        return questionModel;
71
    }
72
73
    @Override
74
    public List<QuestionModelInterface> withRelations(List<QuestionModelInterface> questionsModels) {
75
        for (QuestionModelInterface questionModel: questionsModels) {
76
            this.withRelations(questionModel);
77
        }
78
        return questionsModels;
79
    }
80
81
    @Override
82
    public void saveWithRelations(QuestionModelInterface questionModel) {
83
        if (this.topicsOptions != null) {
84
            this.topicsService.save(questionModel.getTopic(), this.topicsOptions);
85
        }
86
        this.questionsService.save(questionModel);
87
        if (this.answersOptions != null) {
88
            this.answersService.save(questionModel.getAnswers(), this.answersOptions);
89
        }
90
    }
91
92
    @Override
93
    public void deleteWithRelations(QuestionModelInterface questionModel) {
94
        if (this.answersOptions != null) {
95
            this.answersService.delete(questionModel.getAnswers(), this.answersOptions);
96
        }
97
        this.questionsService.delete(questionModel);
98
        if (this.topicsOptions != null) {
99
            this.topicsService.delete(questionModel.getTopic(), this.topicsOptions);
100
        }
101
    }
102
}
103