-
Notifications
You must be signed in to change notification settings - Fork 5
/
ContextAction.java
310 lines (277 loc) · 12.8 KB
/
ContextAction.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package org.genericsystem.reactor.context;
import java.io.Serializable;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import org.genericsystem.api.core.exceptions.RollbackException;
import org.genericsystem.common.Generic;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.EncryptionUtils;
import org.genericsystem.reactor.HtmlDomNode;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.contextproperties.ConvertedValueDefaults;
import org.genericsystem.reactor.contextproperties.GSBuilderDefaults;
import org.genericsystem.reactor.contextproperties.GSBuilderDefaults.GenericValueComponents;
import org.genericsystem.reactor.contextproperties.PasswordDefaults;
import org.genericsystem.reactor.contextproperties.SelectionDefaults;
import org.genericsystem.reactor.contextproperties.StepperDefaults;
import org.genericsystem.reactor.gscomponents.Composite.Header;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlInputText;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlSpan;
import org.genericsystem.reactor.gscomponents.InstanceBuilder.GSHolderBuilderDiv;
import org.genericsystem.reactor.gscomponents.Modal.ModalWithDisplay;
import org.genericsystem.security.model.User;
import org.genericsystem.security.model.User.Password;
import org.genericsystem.security.model.User.Salt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.beans.property.Property;
public interface ContextAction extends BiConsumer<Context, Tag> {
public static final Logger log = LoggerFactory.getLogger(ContextAction.class);
public static class ADD_HOLDER implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Property<Serializable> convertedValue = ((ConvertedValueDefaults) tag.getParent()).getConvertedValueProperty(context.getParent());
if (convertedValue.getValue() != null) {
Serializable newValue = convertedValue.getValue();
convertedValue.setValue(null);
context.getGenerics()[1].addHolder(context.getGeneric(), newValue);
} else if (Boolean.class.equals(context.getGeneric().getInstanceValueClassConstraint()))
context.getGenerics()[1].addHolder(context.getGeneric(), false);
}
}
public static class REMOVE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.remove();
}
}
public static class FLUSH implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.flush();
context.unmount();
}
}
public static class FLASH implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Context rootContext = context.getRootContext();
Tag rootTag = rootContext.getHtmlDomNodesMap().keySet().iterator().next();
HtmlDomNode rootNode = rootContext.getHtmlDomNode(rootTag);
String body = rootNode.toHTMLString(new String());
rootNode.toHtmlFile(rootNode.header() + body + rootNode.footer(), "html", "/home/middleware/git/genericsystem2015/gs-reactor/src/main/resources/");
}
}
public static class FLUSH_CLOSE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.flush();
context.unmount();
if (SelectionDefaults.class.isAssignableFrom(tag.getClass()))
((SelectionDefaults) tag).getSelectionProperty(context).setValue(null);
else
log.warn("The RESET_SELECTION action can apply only to a tag class implementing SelectionDefaults.");
}
}
public static class CANCEL implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.cancel();
context.unmount();
}
}
public static class MOUNT implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.mount();
}
}
public static class UNMOUNT implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.unmount();
}
}
public static class UNMOUNT_CLOSE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.unmount();
if (SelectionDefaults.class.isAssignableFrom(tag.getClass()))
((SelectionDefaults) tag).getSelectionProperty(context).setValue(null);
else
log.warn("The RESET_SELECTION action can apply only to a tag class implementing SelectionDefaults.");
}
}
public static class SHIFTTS implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.shiftTs();
}
}
public static class GC implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
System.gc();
}
}
public static class DISPLAY_NONE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
tag.getDisplayProperty(context).setValue("none");
}
}
public static class DISPLAY_NONE_CANCEL implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
tag.getParent().getParent().find(HtmlInputText.class).getDomNodeAttributes(context).put("value", "");
tag.getParent().getParent().find(HtmlInputText.class, 1).getDomNodeAttributes(context).put("value", "");
tag.getParent().getParent().find(HtmlInputText.class, 2).getDomNodeAttributes(context).put("value", "");
tag.getDisplayProperty(context).setValue("none");
}
}
public static class MODAL_DISPLAY_FLEX implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
tag.getParent().find(ModalWithDisplay.class).getDisplayProperty(context).setValue("flex");
}
}
public static class SET_SELECTION implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.mount();
if (SelectionDefaults.class.isAssignableFrom(tag.getClass()))
((SelectionDefaults) tag).getSelectionProperty(context).setValue(context);
else
log.warn("The SET_SELECTION action can apply only to a tag class implementing SelectionDefaults.");
}
}
public static class RESET_SELECTION implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
if (SelectionDefaults.class.isAssignableFrom(tag.getClass()))
((SelectionDefaults) tag).getSelectionProperty(context).setValue(null);
else
log.warn("The RESET_SELECTION action can apply only to a tag class implementing SelectionDefaults.");
}
}
public static class CREATE_USER implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.mount();
HtmlInputText name = tag.getParent().getParent().find(HtmlInputText.class);
HtmlInputText passwordInput = tag.getParent().getParent().find(HtmlInputText.class, 1);
HtmlInputText confirmPassword = tag.getParent().getParent().find(HtmlInputText.class, 2);
HtmlSpan invalidUsername = tag.getParent().getParent().find(HtmlSpan.class);
HtmlSpan invalidConfirmPassword = tag.getParent().getParent().find(HtmlSpan.class, 1);
String psw1 = passwordInput.getDomNodeAttributes(context).get("value");
String psw2 = confirmPassword.getDomNodeAttributes(context).get("value");
Generic user;
try {
user = context.find(User.class).addInstance(name.getDomNodeAttributes(context).get("value"));
} catch (RollbackException e) {
invalidUsername.addStyle(context, "display", "inline");
return;
}
if (psw1 != null && psw1.equals(psw2)) {
invalidConfirmPassword.addStyle(context, "display", "none");
invalidUsername.addStyle(context, "display", "none");
byte[] salt = EncryptionUtils.generateSalt();
byte[] hash = EncryptionUtils.getEncryptedPassword(psw1, salt);
Generic hashGeneric = user.setHolder(context.find(Password.class), hash);
hashGeneric.setHolder(context.find(Salt.class), salt);
tag.getDisplayProperty(context).setValue("none");
name.getDomNodeAttributes(context).put("value", "");
passwordInput.getDomNodeAttributes(context).put("value", "");
confirmPassword.getDomNodeAttributes(context).put("value", "");
context.flush();
context.unmount();
context.flush();
} else {
invalidConfirmPassword.addStyle(context, "display", "inline");
context.unmount();
}
}
}
public static class CREATE_INSTANCE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
if (GSBuilderDefaults.class.isAssignableFrom(tag.getClass())) {
GSBuilderDefaults buildTag = (GSBuilderDefaults) tag;
Generic type = context.getGeneric();
Map<Generic, GenericValueComponents> gvc = buildTag.getGenericValueComponents(context).getValue();
Generic[] components = gvc.get(type).getComponents().entrySet().stream().filter(obs -> obs.getValue() != null && obs.getValue().getValue() != null).map(entry -> getOrSetInstance(entry.getKey(), entry.getValue().getValue()))
.filter(gen -> gen != null).toArray(Generic[]::new);
if ((gvc.get(type).getGenericValue().getValue() != null || components.length != 0) && components.length == type.getComponents().size()) {
Generic newInstance = type.setInstance(gvc.get(type).getGenericValue().getValue(), components);
for (Entry<Generic, GenericValueComponents> entry : gvc.entrySet().stream().filter(e -> !e.getKey().equals(type)).collect(Collectors.toSet())) {
Generic[] selectedGenerics = entry.getValue().getComponents().entrySet().stream().filter(entry_ -> entry_.getValue() != null && entry_.getValue().getValue() != null)
.map(entry_ -> getOrSetInstance(entry_.getKey(), entry_.getValue().getValue())).filter(gen -> gen != null).toArray(Generic[]::new);
if ((entry.getValue().getGenericValue().getValue() != null || selectedGenerics.length != 0) && selectedGenerics.length + 1 == entry.getKey().getComponents().size()) {
Generic newHolder = newInstance.setHolder(entry.getKey(), entry.getValue().getGenericValue().getValue(), selectedGenerics);
if (PasswordDefaults.class.isAssignableFrom(tag.getParent().getParent().getClass()) && context.find(Password.class) != null && newHolder.isInstanceOf(context.find(Password.class)))
newHolder.setHolder(context.find(Salt.class), ((PasswordDefaults) tag.getParent().getParent()).getSaltProperty(context).getValue());
}
entry.getValue().getComponents().values().stream().forEach(sel -> sel.setValue(null));
entry.getValue().getGenericValue().setValue(null);
}
gvc.get(type).getComponents().values().stream().forEach(sel -> sel.setValue(null));
gvc.get(type).getGenericValue().setValue(null);
Map<Generic, Map<Generic, Property<Serializable>>> relationMap = buildTag.getMultipleRelationProperty(context).getValue();
for (Entry<Generic, Map<Generic, Property<Serializable>>> entry : relationMap.entrySet())
for (Generic target : entry.getValue().keySet())
newInstance.setHolder(entry.getKey(), null, target);
for (Property<Serializable> convertedProperty : relationMap.values().stream().flatMap(hm -> hm.values().stream()).collect(Collectors.toList()))
convertedProperty.setValue(null);
tag.getParent().getParent().find(Header.class).find(GSHolderBuilderDiv.class).find(HtmlSpan.class).addStyle(context, "display", "none");
} else {
tag.getParent().getParent().find(Header.class).find(GSHolderBuilderDiv.class).find(HtmlSpan.class).addStyle(context, "display", "inline");
}
} else
log.warn("The CREATE_INSTANCE action can apply only to a tag class implementing GSBuilderDefaults.");
}
// Necessary because setInstance returns only instances without a super, whereas getInstance can return instances with a super too.
private Generic getOrSetInstance(Generic type, Serializable value) {
Generic instance = type.getInstance(value);
return instance != null ? instance : type.addInstance(value);
}
}
public static class PREVIOUS implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
if (StepperDefaults.class.isAssignableFrom(tag.getClass()))
((StepperDefaults) tag).prev(context);
else
log.warn("The PREVIOUS action is applicable only to a tag implementing StepperDefaults.");
}
}
public static class NEXT implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
if (StepperDefaults.class.isAssignableFrom(tag.getClass()))
((StepperDefaults) tag).next(context);
else
log.warn("The NEXT action is applicable only to a tag implementing SwitchDefaults.");
}
}
public static class SET_ADMIN_MODE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
tag.getAdminModeProperty(context).setValue(true);
}
}
public static class SET_NORMAL_MODE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
tag.getAdminModeProperty(context).setValue(false);
}
}
public static class DISCONNECT implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
tag.getLoggedUserProperty(context).setValue(null);
tag.getAdminModeProperty(context).setValue(false);
}
}
}