-
Notifications
You must be signed in to change notification settings - Fork 5
/
QuizContextAction.java
156 lines (115 loc) · 4.39 KB
/
QuizContextAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package org.genericsystem.quiz.utils;
import org.genericsystem.common.Generic;
import org.genericsystem.quiz.model.Quiz;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.context.ContextAction;
import org.genericsystem.reactor.contextproperties.SelectionDefaults;
import org.genericsystem.security.model.User;
import javafx.beans.property.Property;
public class QuizContextAction {
public final static String SELECTED_QUIZ = "selectedQuiz";
public final static String SELECTED_USER = "selectedUser";
public static class CLEAR_QUIZ implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Property<Generic> selectedQuiz = tag.getProperty(SELECTED_QUIZ, context);
if (selectedQuiz == null) {
tag.getRootTag().createNewProperty(SELECTED_QUIZ);
return;
}
if (selectedQuiz.getValue() == null)
return;
selectedQuiz.setValue(null);
}
}
public static class CLEAR_USER implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Property<Generic> selectedUser = tag.getProperty(SELECTED_USER, context);
if (selectedUser == null) {
tag.getRootTag().createNewProperty(SELECTED_USER);
return;
}
if (selectedUser.getValue() == null)
return;
selectedUser.setValue(null);
}
}
public static class CLEAR_QUIZCONTEXT_PROPERTIES implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
new CLEAR_USER().accept(context, tag);
new CLEAR_QUIZ().accept(context, tag);
}
}
public static class SELECT_QUIZ implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Generic quiz = context.getGeneric();
Property<Generic> selectedQuiz = tag.getProperty(SELECTED_QUIZ, context);
// Compare le type du tag avec la classe "Quiz" du modèle
if (!quiz.getRoot().find(Quiz.class).equals(quiz.getMeta()))
return;
if (selectedQuiz == null)
tag.getRootTag().createNewInitializedProperty(SELECTED_QUIZ, context.getRootContext(), c -> quiz);
else if (!quiz.equals(selectedQuiz.getValue()))
selectedQuiz.setValue(quiz);
}
}
public static class SELECT_LOGGEDUSER implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
tag.getProperty(SELECTED_USER, context).setValue(tag.getLoggedUserProperty(context).getName());
}
}
public static class SAVE_QUIZ_RESULT implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Generic quiz = ((SelectionDefaults) tag).getSelectionProperty(context).getValue().getGeneric();
Generic sUser = context.find(User.class).getInstance("Anti-Seche");
Generic loggedUser = tag.getLoggedUserProperty(context).getValue();
ScoreUtils.setResult(context, quiz, sUser, loggedUser);
ScoreUtils.getResult(context, quiz, loggedUser);
}
}
// NAVIGATION ENTRE LES PAGES
public static class CLEAR_PAGES implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
if (tag.getProperty(QuizTagSwitcher.PAGE, context) != null)
tag.getProperty(QuizTagSwitcher.PAGE, context).setValue(null);
}
}
// Possibilité de factoriser
public static class CALL_HOME_PAGE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Property<String> pageProperty = tag.getProperty(QuizTagSwitcher.PAGE, context);
if (pageProperty == null)
tag.createNewInitializedProperty(QuizTagSwitcher.PAGE, context, c -> QuizTagSwitcher.HOME_PAGE);
else
pageProperty.setValue(QuizTagSwitcher.HOME_PAGE);
}
}
public static class CALL_RESULT_PAGE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Property<String> pageProperty = tag.getProperty(QuizTagSwitcher.PAGE, context);
if (pageProperty == null)
tag.createNewInitializedProperty(QuizTagSwitcher.PAGE, context, c -> QuizTagSwitcher.RESULT_PAGE);
else
pageProperty.setValue(QuizTagSwitcher.RESULT_PAGE);
}
}
public static class CALL_QUESTION_PAGE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Property<String> pageProperty = tag.getProperty(QuizTagSwitcher.PAGE, context);
if (pageProperty == null)
tag.createNewInitializedProperty(QuizTagSwitcher.PAGE, context, c -> QuizTagSwitcher.QUESTION_PAGE);
else
pageProperty.setValue(QuizTagSwitcher.QUESTION_PAGE);
}
}
}