-
Notifications
You must be signed in to change notification settings - Fork 5
/
ContextActionCustom.java
221 lines (198 loc) · 7.91 KB
/
ContextActionCustom.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
package org.genericsystem.watch.gui.utils;
import java.util.Arrays;
import org.genericsystem.api.core.exceptions.RollbackException;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.cv.comparator.ComputeBestTextPerZone;
import org.genericsystem.cv.comparator.ComputeTrainedScores;
import org.genericsystem.cv.model.Doc.DocInstance;
import org.genericsystem.cv.model.DocClass.DocClassInstance;
import org.genericsystem.cv.model.ModelTools;
import org.genericsystem.reactor.Context;
import org.genericsystem.reactor.EncryptionUtils;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.context.ContextAction;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlInputText;
import org.genericsystem.reactor.gscomponents.HtmlTag.HtmlSpan;
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.genericsystem.watch.VerticleDeployerFromWatchApp;
import io.vertx.core.Verticle;
/**
* This class contains all the {@link ContextAction} needed across the app.
*
* @author Pierrik Lassalas
*/
public class ContextActionCustom {
/*
* === PAGE NAVIGATION ===
*/
public static class CALL_STATISTICS_PAGE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
System.out.println("Redirecting to statistics page");
tag.setInheritedContextPropertyValue(PageSwitcher.PAGE, context, PageSwitcher.FILTERS_STATISTICS);
}
}
public static class CALL_HOME_PAGE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
System.out.println("Redirecting to home page");
tag.setInheritedContextPropertyValue(PageSwitcher.PAGE, context, PageSwitcher.HOME_PAGE);
}
}
public static class TEST implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
System.out.println("called from " + tag.getTag());
}
}
/*
* === GENERIC SYSTEM ===
*/
public static class REMOVE_CUSTOM implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.remove();
context.flush();
}
}
public static class SAVE implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
System.out.println("Saving...");
Root root = context.getGeneric().getRoot();
System.out.println("Current thread (save): " + Thread.currentThread().getName());
long start = System.nanoTime();
root.getCurrentCache().flush();
long stop = System.nanoTime();
System.out.println("Saved in " + (stop - start) / 1_000_000 + "ms");
}
}
public static class MODAL_DISPLAY_FLEX_CUSTOM implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
Tag ancestor = tag.getParent().getParent(); // ZoneTextDiv
ancestor.find(ModalWithDisplay.class).getDisplayProperty(context).setValue("flex");
}
}
/*
* === BEST OCR TEXT ===
*/
public static class REFRESH_BEST_TEXT implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
System.out.println("Refreshing best text...");
Root root = context.getGeneric().getRoot();
DocInstance docInstance = (DocInstance) context.getGeneric();
String docType = docInstance.getDocClass().getValue().toString();
// System.out.println("Current thread (refresh): " + Thread.currentThread().getName());
Verticle worker = new WorkerVerticle(root) {
@Override
public void start() throws Exception {
ComputeBestTextPerZone.computeOneFile(root, docInstance, docType);
docInstance.setRefreshTimestamp(ModelTools.getCurrentDate());
root.getCurrentCache().flush();
System.out.println("Done!");
}
};
VerticleDeployerFromWatchApp.deployWorkerVerticle(worker, "Failed to execute the task");
}
}
/*
* === STATISTICS ===
*/
public static class COMPUTE_STATS implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
System.out.println("Computing scores...");
computeStatistics(context, tag, false);
}
}
public static class COMPUTE_STATS_STRICT implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
System.out.println("Computing scores (using strict mode)...");
computeStatistics(context, tag, true);
}
}
public static void computeStatistics(Context context, Tag tag, boolean useStrict) {
DocClassInstance docClassInstance = (DocClassInstance) context.getGeneric();
Root root = docClassInstance.getRoot();
Arrays.asList(context.getGenerics()).forEach(g -> System.out.println(g.info()));
Verticle worker = new WorkerVerticle() {
@Override
public void start() throws Exception {
ComputeTrainedScores.compute(root, docClassInstance.getValue().toString(), useStrict);
System.out.println("Done computing scores!");
}
};
VerticleDeployerFromWatchApp.deployWorkerVerticle(worker, "Failed to execute the task");
}
/*
* === LOGIN ===
*/
public static class CREATE_USER_CUSTOM implements ContextAction {
@Override
public void accept(Context context, Tag tag) {
context.mount();
HtmlInputText userNameInput = 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 userName = userNameInput.getDomNodeAttributes(context).get("value");
String psw1 = passwordInput.getDomNodeAttributes(context).get("value");
String psw2 = confirmPassword.getDomNodeAttributes(context).get("value");
Generic user;
if (userName != null) {
try {
user = context.find(User.class).addInstance(userName);
} catch (RollbackException e) {
invalidUsername.setText(context, "This username already exists");
invalidUsername.addStyle(context, "display", "inline");
invalidConfirmPassword.addStyle(context, "display", "none");
return;
}
if (psw1 != null) {
if (!psw1.isEmpty()) {
invalidConfirmPassword.setText(context, "Password can not be empty");
invalidUsername.addStyle(context, "display", "none");
invalidConfirmPassword.addStyle(context, "display", "inline");
} else {
if (psw1.equals(psw2)) {
invalidUsername.addStyle(context, "display", "none");
invalidConfirmPassword.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");
userNameInput.getDomNodeAttributes(context).put("value", "");
passwordInput.getDomNodeAttributes(context).put("value", "");
confirmPassword.getDomNodeAttributes(context).put("value", "");
context.flush();
context.unmount();
context.flush();
} else { // Non-matching passwords
invalidConfirmPassword.setText(context, "The passwords do not match");
invalidUsername.addStyle(context, "display", "none");
invalidConfirmPassword.addStyle(context, "display", "inline");
context.unmount();
}
}
} else { // Empty password
invalidConfirmPassword.setText(context, "Password can not be empty");
invalidUsername.addStyle(context, "display", "none");
invalidConfirmPassword.addStyle(context, "display", "inline");
}
} else { // Empty username
invalidUsername.setText(context, "Username can not be empty");
invalidUsername.addStyle(context, "display", "inline");
invalidConfirmPassword.addStyle(context, "display", "none");
}
}
}
}