Passed
Pull Request — dev (#401)
by
unknown
06:28
created

QuestionsMapper()   C

Complexity

Conditions 6

Size

Total Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 72
rs 6.983
cc 6

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package easytests.api.v1.mappers;
2
3
import easytests.api.v1.models.AdminAnswer;
4
import easytests.api.v1.models.Identity;
5
import easytests.api.v1.models.Question;
6
import easytests.core.models.*;
7
import easytests.core.models.AnswerModel;
8
import easytests.core.models.QuestionModel;
9
import java.util.ArrayList;
10
import java.util.List;
11
import org.modelmapper.Converter;
12
import org.modelmapper.ModelMapper;
13
import org.modelmapper.PropertyMap;
14
import org.modelmapper.spi.MappingContext;
15
import org.springframework.stereotype.Service;
16
17
/**
18
 * @author RisaMagpie
19
 */
20
@Service("QuestionsMapperV1")
21
public class QuestionsMapper extends ModelMapper {
22
23
    private AdminAnswersMapper adminAnswersMapper = new AdminAnswersMapper();
24
25
    public QuestionsMapper() {
26
        super();
27
        this.createTypeMap(QuestionModel.class, Question.class)
28
                .addMappings(mapper -> {
29
                    mapper.<Integer>map(questionModel ->
30
                                    questionModel.getQuestionType().getId(), (question, id) -> question.setType(id));
31
32
                    mapper.<Integer>map(questionModel ->
33
                                    questionModel.getTopic().getId(), (question, id) -> question.getTopic().setId(id));
34
35
                    mapper.<List<AdminAnswer>>map(questionModel ->
36
                                    questionModel.getAnswers(), (question, list) -> question.setAnswers(list));
37
                }
38
            );
39
        this.createTypeMap(AnswerModel.class, AdminAnswer.class)
40
                .addMappings(mapper -> {
41
                    mapper.<String>map(answerModel ->
42
                                    answerModel.getTxt(), (answer, text) -> answer.setText(text));
43
44
                    mapper.<Boolean>map(answerModel ->
45
                                    answerModel.getRight(), (answer, right) -> answer.setIsRight(right));
46
                }
47
            );
48
49
        Converter<Identity, TopicModel> convertIdentityToTopicModel =
50
            new Converter<Identity, TopicModel>() {
51
                public TopicModel convert(MappingContext<Identity, TopicModel> context) {
52
                    final TopicModel topicModel = new TopicModel();
53
                    topicModel.setId(context.getSource().getId());
54
55
                    return topicModel;
56
                }
57
            };
58
59
        Converter<List<AdminAnswer>, List<AnswerModel>> convertAdminAnswerListToAnswerModelList
60
                = new Converter<List<AdminAnswer>, List<AnswerModel>>() {
61
                    public List<AnswerModel> convert(MappingContext<List<AdminAnswer>, List<AnswerModel>> context) {
62
                        final List<AnswerModel> answerModels = new ArrayList<>();
63
                        for (int i = 0; i < context.getSource().size(); ++i) {
64
                            final AdminAnswer adminAnswer = context.getSource().get(i);
65
66
                            final AnswerModel answerModel = new AnswerModel();
67
68
                            adminAnswersMapper.map(adminAnswer, answerModel);
69
70
                            answerModels.add(answerModel);
71
                        }
72
                        return answerModels;
73
                    }
74
                };
75
76
        Converter<Integer, QuestionTypeModel> convertIntegerToQuestionTypeModel
77
                = new Converter<Integer, QuestionTypeModel>() {
78
                    public QuestionTypeModel convert(MappingContext<Integer, QuestionTypeModel> context) {
79
                        final QuestionTypeModel questionTypeModel = new QuestionTypeModel();
80
                        questionTypeModel.setId(context.getSource());
81
82
                        return questionTypeModel;
83
                    }
84
                };
85
86
        final PropertyMap<Question, QuestionModel> mymap = new PropertyMap<Question, QuestionModel>() {
87
            protected void configure() {
88
                map(source.getId()).setId(null);
89
                map(source.getText()).setText(null);
90
                using(convertIntegerToQuestionTypeModel).map(source.getType()).setQuestionType(null);
91
                using(convertIdentityToTopicModel).map(source.getTopic()).setTopic(null);
92
                using(convertAdminAnswerListToAnswerModelList).map(source.getAnswers()).setAnswers(null);
93
            }
94
        };
95
96
        this.addMappings(mymap);
97
98
    }
99
}
100