withRelations(List)   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
dl 0
loc 7
rs 10
1
package easytests.core.options;
2
3
import easytests.core.models.SolutionModelInterface;
4
import easytests.core.services.AnswersServiceInterface;
5
import easytests.core.services.PointsServiceInterface;
6
import easytests.core.services.SolutionsServiceInterface;
7
import groovy.transform.EqualsAndHashCode;
8
import java.util.List;
9
import lombok.Setter;
10
11
/**
12
 * @author Loriens
13
 */
14
@EqualsAndHashCode
15
public class SolutionsOptions implements SolutionsOptionsInterface {
16
    @Setter
17
    private SolutionsServiceInterface solutionsService;
18
19
    @Setter
20
    private PointsServiceInterface pointsService;
21
22
    @Setter
23
    private AnswersServiceInterface answersService;
24
25
    private PointsOptionsInterface pointsOptions;
26
27
    private AnswersOptionsInterface answersOptions;
28
29
    public SolutionsOptionsInterface withPoint(PointsOptionsInterface pointsOptions) {
30
        this.pointsOptions = pointsOptions;
31
        return this;
32
    }
33
34
    public SolutionsOptionsInterface withAnswer(AnswersOptionsInterface answersOptions) {
35
        this.answersOptions = answersOptions;
36
        return this;
37
    }
38
39
    @Override
40
    public SolutionModelInterface withRelations(SolutionModelInterface solutionModel) {
41
        if (solutionModel == null) {
42
            return solutionModel;
43
        }
44
45
        if (this.pointsOptions != null) {
46
            solutionModel.setPoint(this.pointsService.find(solutionModel.getPoint().getId(), this.pointsOptions));
47
        }
48
49
        if (this.answersOptions != null) {
50
            solutionModel.setAnswer(this.answersService.find(solutionModel.getAnswer().getId(), this.answersOptions));
51
        }
52
53
        return solutionModel;
54
    }
55
56
    @Override
57
    public List<SolutionModelInterface> withRelations(List<SolutionModelInterface> solutionModels) {
58
59
        for (SolutionModelInterface solutionModel: solutionModels) {
60
            this.withRelations(solutionModel);
61
        }
62
        return solutionModels;
63
64
    }
65
66
    public void saveWithRelations(SolutionModelInterface solutionModel) {
67
        this.solutionsService.save(solutionModel);
68
69
        if (this.pointsOptions != null) {
70
            this.pointsService.save(solutionModel.getPoint(), this.pointsOptions);
71
        }
72
73
        if (this.answersOptions != null) {
74
            this.answersService.save(solutionModel.getAnswer(), this.answersOptions);
75
        }
76
    }
77
78
    public void deleteWithRelations(SolutionModelInterface solutionModel) {
79
        if (this.answersOptions != null) {
80
            this.answersService.delete(solutionModel.getAnswer(), this.answersOptions);
81
            this.answersOptions.deleteWithRelations(solutionModel.getAnswer());
82
        }
83
84
        if (this.pointsOptions != null) {
85
            this.pointsService.delete(solutionModel.getPoint(), this.pointsOptions);
86
            this.pointsOptions.deleteWithRelations(solutionModel.getPoint());
87
        }
88
89
        this.solutionsService.delete(solutionModel);
90
    }
91
92
}
93