1
|
package eu.dnetlib.client.monitor;
|
2
|
|
3
|
import com.github.gwtbootstrap.client.ui.Alert;
|
4
|
import com.github.gwtbootstrap.client.ui.Button;
|
5
|
import com.github.gwtbootstrap.client.ui.constants.AlertType;
|
6
|
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
|
7
|
import com.google.gwt.core.client.GWT;
|
8
|
import com.google.gwt.dom.client.*;
|
9
|
import com.google.gwt.event.dom.client.ClickEvent;
|
10
|
import com.google.gwt.event.dom.client.ClickHandler;
|
11
|
import com.google.gwt.query.client.Function;
|
12
|
import com.google.gwt.user.client.Event;
|
13
|
import com.google.gwt.user.client.Window;
|
14
|
import com.google.gwt.user.client.rpc.AsyncCallback;
|
15
|
import com.google.gwt.user.client.ui.*;
|
16
|
import eu.dnetlib.client.*;
|
17
|
import eu.dnetlib.goldoa.domain.User;
|
18
|
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.List;
|
21
|
|
22
|
import static com.google.gwt.query.client.GQuery.$;
|
23
|
|
24
|
/**
|
25
|
* Created by stefania on 4/3/15.
|
26
|
*/
|
27
|
public class MonitorUsersWidget implements MyWidget {
|
28
|
|
29
|
private String token = "";
|
30
|
|
31
|
private FlowPanel monitorUsersPagePanel = new FlowPanel();
|
32
|
private Label monitorUsersTitleLabel = new Label();
|
33
|
private Label monitorUsersInfoLabel = new Label();
|
34
|
|
35
|
private Alert errorLabel = new Alert();
|
36
|
private Alert warningLabel = new Alert();
|
37
|
private Alert successLabel = new Alert();
|
38
|
|
39
|
private FlowPanel usersPanel = new FlowPanel();
|
40
|
private FlowPanel actionButtonsPanel = new FlowPanel();
|
41
|
private Button activateSelected = new Button();
|
42
|
private Button deactivateSelected = new Button();
|
43
|
|
44
|
private DataServiceAsync dataService = GWT.create(DataService.class);
|
45
|
private GoldOAConstants goldOAConstants = GWT.create(GoldOAConstants.class);
|
46
|
|
47
|
public MonitorUsersWidget() {
|
48
|
|
49
|
monitorUsersPagePanel.addStyleName("content");
|
50
|
|
51
|
monitorUsersTitleLabel.setText("Monitor Users");
|
52
|
monitorUsersTitleLabel.addStyleName("contentTitleLabel");
|
53
|
|
54
|
monitorUsersInfoLabel.setText("Monitor the system's users and activate/deactivate them when necessary");
|
55
|
monitorUsersInfoLabel.addStyleName("contentInfoLabel");
|
56
|
|
57
|
errorLabel.addStyleName("alertLabel");
|
58
|
errorLabel.setType(AlertType.ERROR);
|
59
|
errorLabel.setClose(false);
|
60
|
errorLabel.setVisible(false);
|
61
|
|
62
|
successLabel.addStyleName("alertLabel");
|
63
|
successLabel.setType(AlertType.SUCCESS);
|
64
|
successLabel.setClose(false);
|
65
|
successLabel.setVisible(false);
|
66
|
|
67
|
warningLabel.addStyleName("alertLabel");
|
68
|
warningLabel.setType(AlertType.WARNING);
|
69
|
warningLabel.setClose(false);
|
70
|
warningLabel.setVisible(false);
|
71
|
|
72
|
activateSelected.setText("Activate Selected");
|
73
|
activateSelected.setType(ButtonType.PRIMARY);
|
74
|
activateSelected.addStyleName("marginLeft10");
|
75
|
activateSelected.addClickHandler(new ClickHandler() {
|
76
|
@Override
|
77
|
public void onClick(ClickEvent clickEvent) {
|
78
|
|
79
|
List<String> userIds = getSelectedUsers();
|
80
|
|
81
|
if(!userIds.isEmpty()) {
|
82
|
activateSelectedUsers(userIds);
|
83
|
} else {
|
84
|
warningLabel.setText(goldOAConstants.warningNoUsersSelected());
|
85
|
warningLabel.setVisible(true);
|
86
|
}
|
87
|
}
|
88
|
});
|
89
|
|
90
|
deactivateSelected.setText("Deactivate Selected");
|
91
|
deactivateSelected.setType(ButtonType.PRIMARY);
|
92
|
deactivateSelected.addStyleName("marginLeft10");
|
93
|
deactivateSelected.addClickHandler(new ClickHandler() {
|
94
|
@Override
|
95
|
public void onClick(ClickEvent clickEvent) {
|
96
|
|
97
|
List<String> emails = getSelectedUsers();
|
98
|
|
99
|
if(!emails.isEmpty()) {
|
100
|
deactivateSelectedUsers(emails);
|
101
|
} else {
|
102
|
warningLabel.setText(goldOAConstants.warningNoUsersSelected());
|
103
|
warningLabel.setVisible(true);
|
104
|
}
|
105
|
}
|
106
|
});
|
107
|
|
108
|
actionButtonsPanel.addStyleName("userActionButtons");
|
109
|
actionButtonsPanel.add(activateSelected);
|
110
|
actionButtonsPanel.add(deactivateSelected);
|
111
|
|
112
|
usersPanel.addStyleName("requestsListPanel");
|
113
|
|
114
|
monitorUsersPagePanel.add(monitorUsersTitleLabel);
|
115
|
monitorUsersPagePanel.add(monitorUsersInfoLabel);
|
116
|
monitorUsersPagePanel.add(successLabel);
|
117
|
monitorUsersPagePanel.add(errorLabel);
|
118
|
monitorUsersPagePanel.add(warningLabel);
|
119
|
monitorUsersPagePanel.add(usersPanel);
|
120
|
}
|
121
|
|
122
|
@Override
|
123
|
public void clear() {
|
124
|
|
125
|
errorLabel.setVisible(false);
|
126
|
warningLabel.setVisible(false);
|
127
|
successLabel.setVisible(false);
|
128
|
usersPanel.clear();
|
129
|
}
|
130
|
|
131
|
@Override
|
132
|
public void reload() {
|
133
|
|
134
|
MyWidgetHelper.hideSidebar();
|
135
|
|
136
|
SidebarPanel helpPanel = new SidebarPanel("Help");
|
137
|
MyWidgetHelper.loadHelp(helpPanel, token.split("\\.")[0]);
|
138
|
|
139
|
hideAlertLabels();
|
140
|
}
|
141
|
|
142
|
@Override
|
143
|
public void setToken(String token) {
|
144
|
this.token = token;
|
145
|
}
|
146
|
|
147
|
@Override
|
148
|
public void afterAdditionToRootPanel() {
|
149
|
loadUsers();
|
150
|
}
|
151
|
|
152
|
@Override
|
153
|
public Widget asWidget() {
|
154
|
return monitorUsersPagePanel;
|
155
|
}
|
156
|
|
157
|
private void loadUsers() {
|
158
|
|
159
|
hideAlertLabels();
|
160
|
|
161
|
final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
|
162
|
usersPanel.addStyleName("loading");
|
163
|
usersPanel.add(loadingWheel);
|
164
|
|
165
|
dataService.getUsers(new AsyncCallback<List<User>>() {
|
166
|
|
167
|
@Override
|
168
|
public void onFailure(Throwable throwable) {
|
169
|
|
170
|
usersPanel.clear();
|
171
|
usersPanel.removeStyleName("loading");
|
172
|
|
173
|
errorLabel.setText(goldOAConstants.errorRetrievingUsers());
|
174
|
errorLabel.setVisible(true);
|
175
|
}
|
176
|
|
177
|
@Override
|
178
|
public void onSuccess(List<User> persons) {
|
179
|
|
180
|
usersPanel.clear();
|
181
|
usersPanel.removeStyleName("loading");
|
182
|
|
183
|
if (persons.isEmpty()) {
|
184
|
warningLabel.setText(goldOAConstants.warningNoAvailableUsers());
|
185
|
warningLabel.setVisible(true);
|
186
|
} else {
|
187
|
drawUsersGrid(persons);
|
188
|
addActionHandlers();
|
189
|
}
|
190
|
}
|
191
|
});
|
192
|
}
|
193
|
|
194
|
private void hideAlertLabels() {
|
195
|
|
196
|
errorLabel.setVisible(false);
|
197
|
warningLabel.setVisible(false);
|
198
|
successLabel.setVisible(false);
|
199
|
}
|
200
|
|
201
|
private void drawUsersGrid(List<User> users) {
|
202
|
|
203
|
HTML usersTable = new HTML();
|
204
|
String content = "";
|
205
|
|
206
|
content += "<table class=\"tablesorter\" cellspacing=\"0\">";
|
207
|
content += "<thead><tr><th><input id=\"allUsersCheckbox\" type=\"checkbox\"></th><th>Email</th>" +
|
208
|
"<th>Name</th><th>Activated</th></tr></thead>";
|
209
|
content += "<tbody>";
|
210
|
|
211
|
for(User user : users) {
|
212
|
content += "<tr>";
|
213
|
content += "<td><input id=\"" + user.getEmail() + "#checkBox\" class=\"checkBox\" type=\"checkbox\"></td>";
|
214
|
content += "<td>" + user.getFirstname() + " " + user.getInitials() + " " + user.getLastname() + "</td>";
|
215
|
content += "<td>" + user.getEmail() + "</td>";
|
216
|
if(user.isActive()) {
|
217
|
content += "<td><input id=\"" + user.getEmail() + "#deactivate\" class=\"deactivate\" " +
|
218
|
"type=\"image\" src=\"imgs/check-icon.png\" width=20 height=20 title=\"Deactivate\"></td>";
|
219
|
} else {
|
220
|
content += "<td><input id=\"" + user.getEmail() + "#activate\" class=\"activate\" " +
|
221
|
"type=\"image\" src=\"imgs/x-icon.png\" width=20 height=20 title=\"Activate\"></td>";
|
222
|
}
|
223
|
content += "</tr>";
|
224
|
}
|
225
|
|
226
|
content += "</tbody>";
|
227
|
content += "</table>";
|
228
|
|
229
|
usersTable.setHTML(content);
|
230
|
|
231
|
usersPanel.add(actionButtonsPanel);
|
232
|
usersPanel.add(usersTable);
|
233
|
}
|
234
|
|
235
|
private void addActionHandlers() {
|
236
|
|
237
|
$("#allUsersCheckbox").click(new Function() {
|
238
|
|
239
|
public boolean f(Event e) {
|
240
|
|
241
|
InputElement allUsers = (InputElement) Document.get().getElementById("allUsersCheckbox");
|
242
|
|
243
|
NodeList<Element> checkBoxes = $(".checkBox").get();
|
244
|
|
245
|
if(allUsers.isChecked()) {
|
246
|
for(int i=0; i<checkBoxes.getLength(); i++) {
|
247
|
InputElement checkBox = (InputElement) checkBoxes.getItem(i);
|
248
|
checkBox.setChecked(true);
|
249
|
}
|
250
|
} else {
|
251
|
for(int i=0; i<checkBoxes.getLength(); i++) {
|
252
|
InputElement checkBox = (InputElement) checkBoxes.getItem(i);
|
253
|
checkBox.setChecked(false);
|
254
|
}
|
255
|
}
|
256
|
|
257
|
return true;
|
258
|
}
|
259
|
});
|
260
|
|
261
|
$(".activate").click(new Function() {
|
262
|
|
263
|
public boolean f(Event e) {
|
264
|
|
265
|
String[] idParts = $(e).get(0).getId().split("#");
|
266
|
activate(idParts[0]);
|
267
|
|
268
|
return true;
|
269
|
}
|
270
|
});
|
271
|
|
272
|
$(".deactivate").click(new Function() {
|
273
|
|
274
|
public boolean f(Event e) {
|
275
|
|
276
|
String[] idParts = $(e).get(0).getId().split("#");
|
277
|
deactivate(idParts[0]);
|
278
|
|
279
|
return true;
|
280
|
}
|
281
|
});
|
282
|
}
|
283
|
|
284
|
private List<String> getSelectedUsers() {
|
285
|
|
286
|
List<String> ids = new ArrayList<String>();
|
287
|
|
288
|
NodeList<Element> checkBoxes = $(".checkBox").get();
|
289
|
for(int i=0; i<checkBoxes.getLength(); i++) {
|
290
|
InputElement checkBox = (InputElement) checkBoxes.getItem(i);
|
291
|
if(checkBox.isChecked()) {
|
292
|
String[] idParts = checkBox.getId().split("#");
|
293
|
ids.add(idParts[0]);
|
294
|
}
|
295
|
}
|
296
|
|
297
|
return ids;
|
298
|
}
|
299
|
|
300
|
private void activate(String email) {
|
301
|
|
302
|
dataService.activateUser(email, new AsyncCallback<Void>() {
|
303
|
|
304
|
@Override
|
305
|
public void onFailure(Throwable throwable) {
|
306
|
errorLabel.setText(goldOAConstants.errorActivatingSingleUser());
|
307
|
errorLabel.setVisible(true);
|
308
|
}
|
309
|
|
310
|
@Override
|
311
|
public void onSuccess(Void aVoid) {
|
312
|
successLabel.setText(goldOAConstants.successActivatingSingleUser());
|
313
|
successLabel.setVisible(true);
|
314
|
loadUsers();
|
315
|
}
|
316
|
});
|
317
|
}
|
318
|
|
319
|
private void activateSelectedUsers(List<String> emails) {
|
320
|
|
321
|
dataService.activateUsers(emails, new AsyncCallback<Void>() {
|
322
|
|
323
|
@Override
|
324
|
public void onFailure(Throwable throwable) {
|
325
|
errorLabel.setText(goldOAConstants.errorActivatingSelectedUsers());
|
326
|
errorLabel.setVisible(true);
|
327
|
}
|
328
|
|
329
|
@Override
|
330
|
public void onSuccess(Void aVoid) {
|
331
|
successLabel.setText(goldOAConstants.successActivatingSelectedUsers());
|
332
|
successLabel.setVisible(true);
|
333
|
loadUsers();
|
334
|
}
|
335
|
});
|
336
|
}
|
337
|
|
338
|
private void deactivate(String email) {
|
339
|
|
340
|
dataService.deactivateUser(email, new AsyncCallback<Void>() {
|
341
|
|
342
|
@Override
|
343
|
public void onFailure(Throwable throwable) {
|
344
|
errorLabel.setText(goldOAConstants.errorDeactivatingSingleUser());
|
345
|
errorLabel.setVisible(true);
|
346
|
}
|
347
|
|
348
|
@Override
|
349
|
public void onSuccess(Void aVoid) {
|
350
|
successLabel.setText(goldOAConstants.successDeactivatingSingleUser());
|
351
|
successLabel.setVisible(true);
|
352
|
loadUsers();
|
353
|
}
|
354
|
});
|
355
|
}
|
356
|
|
357
|
private void deactivateSelectedUsers(List<String> emails) {
|
358
|
|
359
|
Window.alert(emails.toString());
|
360
|
dataService.deactivateUsers(emails, new AsyncCallback<Void>() {
|
361
|
|
362
|
@Override
|
363
|
public void onFailure(Throwable throwable) {
|
364
|
errorLabel.setText(goldOAConstants.errorDeactivatingSelectedUsers());
|
365
|
errorLabel.setVisible(true);
|
366
|
}
|
367
|
|
368
|
@Override
|
369
|
public void onSuccess(Void aVoid) {
|
370
|
successLabel.setText(goldOAConstants.successDeactivatingSelectedUsers());
|
371
|
successLabel.setVisible(true);
|
372
|
loadUsers();
|
373
|
}
|
374
|
});
|
375
|
}
|
376
|
}
|