easytests.core.options.PointsOptions   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
c 1
b 0
f 0
loc 101
rs 10
wmc 16

7 Methods

Rating   Name   Duplication   Size   Complexity  
B withRelations(PointModelInterface) 0 20 5
A deleteWithRelations(PointModelInterface) 0 10 3
A withSolutions(SolutionsOptionsInterface) 0 5 1
A withQuestion(QuestionsOptionsInterface) 0 5 1
A withRelations(List) 0 7 2
A saveWithRelations(PointModelInterface) 0 10 3
A withQuiz(QuizzesOptionsInterface) 0 5 1
1
package easytests.core.options;
2
3
import easytests.core.models.PointModelInterface;
4
import easytests.core.services.PointsServiceInterface;
5
import easytests.core.services.QuestionsServiceInterface;
6
import easytests.core.services.QuizzesServiceInterface;
7
import easytests.core.services.SolutionsServiceInterface;
8
import java.util.List;
9
import lombok.EqualsAndHashCode;
10
import lombok.Setter;
11
12
13
/**
14
 * @author nikitalpopov
15
 */
16
@EqualsAndHashCode
17
public class PointsOptions implements PointsOptionsInterface {
18
19
    @Setter
20
    private PointsServiceInterface pointsService;
21
22
    @Setter
23
    private QuizzesServiceInterface quizzesService;
24
25
    @Setter
26
    private QuestionsServiceInterface questionsService;
27
28
    @Setter
29
    private SolutionsServiceInterface solutionsService;
30
31
    private QuizzesOptionsInterface quizzesOptions;
32
33
    private QuestionsOptionsInterface questionsOptions;
34
35
    private SolutionsOptionsInterface solutionsOptions;
36
37
    public PointsOptionsInterface withQuiz(QuizzesOptionsInterface quizzesOptions) {
38
39
        this.quizzesOptions = quizzesOptions;
40
41
        return this;
42
43
    }
44
45
    public PointsOptionsInterface withQuestion(QuestionsOptionsInterface questionsOptions) {
46
47
        this.questionsOptions = questionsOptions;
48
49
        return this;
50
51
    }
52
53
    public PointsOptionsInterface withSolutions(SolutionsOptionsInterface solutionsOptions) {
54
55
        this.solutionsOptions = solutionsOptions;
56
57
        return this;
58
59
    }
60
61
    public PointModelInterface withRelations(PointModelInterface pointModel) {
62
63
        if (pointModel == null) {
64
            return pointModel;
65
        }
66
67
        if (this.quizzesOptions != null) {
68
            pointModel.setQuiz(this.quizzesService.find(pointModel.getQuiz().getId(), this.quizzesOptions));
69
        }
70
71
        if (this.questionsOptions != null) {
72
            pointModel.setQuestion(this.questionsService.find(pointModel.getQuestion().getId(),
73
                    this.questionsOptions));
74
        }
75
76
        if (this.solutionsOptions != null) {
77
            pointModel.setSolutions(this.solutionsService.findByPoint(pointModel, this.solutionsOptions));
78
        }
79
80
        return pointModel;
81
    }
82
83
    public List<PointModelInterface> withRelations(List<PointModelInterface> pointModels) {
84
85
        for (PointModelInterface pointModel: pointModels) {
86
            this.withRelations(pointModel);
87
        }
88
89
        return pointModels;
90
91
    }
92
93
    public void saveWithRelations(PointModelInterface pointModel) {
94
95
        if (this.quizzesOptions != null) {
96
            this.quizzesService.save(pointModel.getQuiz(), quizzesOptions);
97
        }
98
99
        this.pointsService.save(pointModel);
100
101
        if (this.solutionsOptions != null) {
102
            this.solutionsService.save(pointModel.getSolutions(), this.solutionsOptions);
103
        }
104
105
    }
106
107
    public void deleteWithRelations(PointModelInterface pointModel) {
108
109
        if (this.solutionsOptions != null) {
110
            this.solutionsService.delete(pointModel.getSolutions(), this.solutionsOptions);
111
        }
112
113
        this.pointsService.delete(pointModel);
114
115
        if (this.quizzesOptions != null) {
116
            this.quizzesService.delete(pointModel.getQuiz(), this.quizzesOptions);
117
        }
118
119
    }
120
}
121