Project

General

Profile

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.rpc.AsyncCallback;
14
import com.google.gwt.user.client.ui.*;
15
import eu.dnetlib.client.*;
16
import eu.dnetlib.goldoa.domain.User;
17

    
18
import java.util.ArrayList;
19
import java.util.List;
20

    
21
import static com.google.gwt.query.client.GQuery.$;
22

    
23
/**
24
 * Created by stefania on 4/3/15.
25
 */
26
public class MonitorUsersWidget implements MyWidget {
27

    
28
    private String token = "";
29

    
30
    private FlowPanel monitorUsersPagePanel = new FlowPanel();
31
    private Label monitorUsersTitleLabel = new Label();
32
    private Label monitorUsersInfoLabel = new Label();
33

    
34
    private Alert errorLabel = new Alert();
35
    private Alert warningLabel = new Alert();
36
    private Alert successLabel = new Alert();
37

    
38
    private FlowPanel usersPanel = new FlowPanel();
39
    private FlowPanel actionButtonsPanel = new FlowPanel();
40
    private Button activateSelected = new Button();
41
    private Button deactivateSelected = new Button();
42

    
43
    private DataServiceAsync dataService = GWT.create(DataService.class);
44
    private GoldOAConstants goldOAConstants = GWT.create(GoldOAConstants.class);
45

    
46
    public MonitorUsersWidget() {
47

    
48
        monitorUsersPagePanel.addStyleName("content");
49

    
50
        monitorUsersTitleLabel.setText("Monitor Users");
51
        monitorUsersTitleLabel.addStyleName("contentTitleLabel");
52

    
53
        monitorUsersInfoLabel.setText("Monitor the system's users and activate/deactivate them when necessary");
54
        monitorUsersInfoLabel.addStyleName("contentInfoLabel");
55

    
56
        errorLabel.addStyleName("alertLabel");
57
        errorLabel.setType(AlertType.ERROR);
58
        errorLabel.setClose(false);
59
        errorLabel.setVisible(false);
60

    
61
        successLabel.addStyleName("alertLabel");
62
        successLabel.setType(AlertType.SUCCESS);
63
        successLabel.setClose(false);
64
        successLabel.setVisible(false);
65

    
66
        warningLabel.addStyleName("alertLabel");
67
        warningLabel.setType(AlertType.WARNING);
68
        warningLabel.setClose(false);
69
        warningLabel.setVisible(false);
70

    
71
        activateSelected.setText("Activate Selected");
72
        activateSelected.setType(ButtonType.PRIMARY);
73
        activateSelected.addStyleName("marginLeft10");
74
        activateSelected.addClickHandler(new ClickHandler() {
75
            @Override
76
            public void onClick(ClickEvent clickEvent) {
77

    
78
                List<String> userIds = getSelectedUsers();
79

    
80
                if(!userIds.isEmpty()) {
81
                    activateSelectedUsers(userIds);
82
                } else {
83
                    warningLabel.setText(goldOAConstants.warningNoUsersSelected());
84
                    warningLabel.setVisible(true);
85
                }
86
            }
87
        });
88

    
89
        deactivateSelected.setText("Deactivate Selected");
90
        deactivateSelected.setType(ButtonType.PRIMARY);
91
        deactivateSelected.addStyleName("marginLeft10");
92
        deactivateSelected.addClickHandler(new ClickHandler() {
93
            @Override
94
            public void onClick(ClickEvent clickEvent) {
95

    
96
                List<String> userIds = getSelectedUsers();
97

    
98
                if(!userIds.isEmpty()) {
99
                    deactivateSelectedUsers(userIds);
100
                } else {
101
                    warningLabel.setText(goldOAConstants.warningNoUsersSelected());
102
                    warningLabel.setVisible(true);
103
                }
104
            }
105
        });
106

    
107
        actionButtonsPanel.addStyleName("userActionButtons");
108
        actionButtonsPanel.add(activateSelected);
109
        actionButtonsPanel.add(deactivateSelected);
110

    
111
        usersPanel.addStyleName("requestsListPanel");
112

    
113
        monitorUsersPagePanel.add(monitorUsersTitleLabel);
114
        monitorUsersPagePanel.add(monitorUsersInfoLabel);
115
        monitorUsersPagePanel.add(successLabel);
116
        monitorUsersPagePanel.add(errorLabel);
117
        monitorUsersPagePanel.add(warningLabel);
118
        monitorUsersPagePanel.add(usersPanel);
119
    }
120

    
121
    @Override
122
    public void clear() {
123

    
124
        errorLabel.setVisible(false);
125
        warningLabel.setVisible(false);
126
        successLabel.setVisible(false);
127
        usersPanel.clear();
128
    }
129

    
130
    @Override
131
    public void reload() {
132

    
133
        MyWidgetHelper.hideSidebar();
134

    
135
        SidebarPanel helpPanel = new SidebarPanel("Help");
136
        MyWidgetHelper.loadHelp(helpPanel, token.split("\\.")[0]);
137

    
138
        hideAlertLabels();
139
    }
140

    
141
    @Override
142
    public void setToken(String token) {
143
        this.token = token;
144
    }
145

    
146
    @Override
147
    public void afterAdditionToRootPanel() {
148
        loadUsers();
149
    }
150

    
151
    @Override
152
    public Widget asWidget() {
153
        return monitorUsersPagePanel;
154
    }
155

    
156
    private void loadUsers() {
157

    
158
        hideAlertLabels();
159

    
160
        final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
161
        usersPanel.addStyleName("loading");
162
        usersPanel.add(loadingWheel);
163

    
164
        dataService.getUsers(new AsyncCallback<List<User>>() {
165

    
166
            @Override
167
            public void onFailure(Throwable throwable) {
168

    
169
                usersPanel.clear();
170
                usersPanel.removeStyleName("loading");
171

    
172
                errorLabel.setText(goldOAConstants.errorRetrievingUsers());
173
                errorLabel.setVisible(true);
174
            }
175

    
176
            @Override
177
            public void onSuccess(List<User> persons) {
178

    
179
                usersPanel.clear();
180
                usersPanel.removeStyleName("loading");
181

    
182
                if (persons.isEmpty()) {
183
                    warningLabel.setText(goldOAConstants.warningNoAvailableUsers());
184
                    warningLabel.setVisible(true);
185
                } else {
186
                    drawUsersGrid(persons);
187
                    addActionHandlers();
188
                }
189
            }
190
        });
191
    }
192

    
193
    private void hideAlertLabels() {
194

    
195
        errorLabel.setVisible(false);
196
        warningLabel.setVisible(false);
197
        successLabel.setVisible(false);
198
    }
199

    
200
    private void drawUsersGrid(List<User> users) {
201

    
202
        HTML usersTable = new HTML();
203
        String content = "";
204

    
205
        content += "<table class=\"tablesorter\" cellspacing=\"0\">";
206
        content += "<thead><tr><th><input id=\"allUsersCheckbox\" type=\"checkbox\"></th><th>Email</th>" +
207
                "<th>Name</th><th>Activated</th></tr></thead>";
208
        content += "<tbody>";
209

    
210
        for(User user : users) {
211
            content += "<tr>";
212
            content += "<td><input id=\"" + user.getId() + "#checkBox\" class=\"checkBox\" type=\"checkbox\"></td>";
213
            content += "<td>" + user.getFirstname() + " " + user.getInitials() + " " + user.getLastname() + "</td>";
214
            content += "<td>" + user.getEmail() + "</td>";
215
            if(user.isActive()) {
216
                content += "<td><input id=\"" + user.getId() + "#deactivate\" class=\"deactivate\" " +
217
                        "type=\"image\" src=\"imgs/check-icon.png\" width=20 height=20 title=\"Deactivate\"></td>";
218
            } else {
219
                content += "<td><input id=\"" + user.getId() + "#activate\" class=\"activate\" " +
220
                        "type=\"image\" src=\"imgs/x-icon.png\" width=20 height=20 title=\"Activate\"></td>";
221
            }
222
            content += "</tr>";
223
        }
224

    
225
        content += "</tbody>";
226
        content += "</table>";
227

    
228
        usersTable.setHTML(content);
229

    
230
        usersPanel.add(actionButtonsPanel);
231
        usersPanel.add(usersTable);
232
    }
233

    
234
    private void addActionHandlers() {
235

    
236
        $("#allUsersCheckbox").click(new Function() {
237

    
238
            public boolean f(Event e) {
239

    
240
                InputElement allUsers = (InputElement) Document.get().getElementById("allUsersCheckbox");
241

    
242
                NodeList<Element> checkBoxes = $(".checkBox").get();
243

    
244
                if(allUsers.isChecked()) {
245
                    for(int i=0; i<checkBoxes.getLength(); i++) {
246
                        InputElement checkBox = (InputElement) checkBoxes.getItem(i);
247
                        checkBox.setChecked(true);
248
                    }
249
                } else {
250
                    for(int i=0; i<checkBoxes.getLength(); i++) {
251
                        InputElement checkBox = (InputElement) checkBoxes.getItem(i);
252
                        checkBox.setChecked(false);
253
                    }
254
                }
255

    
256
                return true;
257
            }
258
        });
259

    
260
        $(".activate").click(new Function() {
261

    
262
            public boolean f(Event e) {
263

    
264
                String[] idParts = $(e).get(0).getId().split("#");
265
                activate(idParts[0]);
266

    
267
                return true;
268
            }
269
        });
270

    
271
        $(".deactivate").click(new Function() {
272

    
273
            public boolean f(Event e) {
274

    
275
                String[] idParts = $(e).get(0).getId().split("#");
276
                deactivate(idParts[0]);
277

    
278
                return true;
279
            }
280
        });
281
    }
282

    
283
    private List<String> getSelectedUsers() {
284

    
285
        List<String> ids = new ArrayList<String>();
286

    
287
        NodeList<Element> checkBoxes = $(".checkBox").get();
288
        for(int i=0; i<checkBoxes.getLength(); i++) {
289
            InputElement checkBox = (InputElement) checkBoxes.getItem(i);
290
            if(checkBox.isChecked()) {
291
                String[] idParts = checkBox.getId().split("#");
292
                ids.add(idParts[0]);
293
            }
294
        }
295

    
296
        return ids;
297
    }
298

    
299
    private void activate(String userId) {
300

    
301
        dataService.activateUser(userId, new AsyncCallback<Void>() {
302

    
303
            @Override
304
            public void onFailure(Throwable throwable) {
305
                errorLabel.setText(goldOAConstants.errorActivatingSingleUser());
306
                errorLabel.setVisible(true);
307
            }
308

    
309
            @Override
310
            public void onSuccess(Void aVoid) {
311
                successLabel.setText(goldOAConstants.successActivatingSingleUser());
312
                successLabel.setVisible(true);
313
                loadUsers();
314
            }
315
        });
316
    }
317

    
318
    private void activateSelectedUsers(List<String> userIds) {
319

    
320
        dataService.activateUsers(userIds, new AsyncCallback<Void>() {
321

    
322
            @Override
323
            public void onFailure(Throwable throwable) {
324
                errorLabel.setText(goldOAConstants.errorActivatingSelectedUsers());
325
                errorLabel.setVisible(true);
326
            }
327

    
328
            @Override
329
            public void onSuccess(Void aVoid) {
330
                successLabel.setText(goldOAConstants.successActivatingSelectedUsers());
331
                successLabel.setVisible(true);
332
                loadUsers();
333
            }
334
        });
335
    }
336

    
337
    private void deactivate(String userId) {
338

    
339
        dataService.deactivateUser(userId, new AsyncCallback<Void>() {
340

    
341
            @Override
342
            public void onFailure(Throwable throwable) {
343
                errorLabel.setText(goldOAConstants.errorDeactivatingSingleUser());
344
                errorLabel.setVisible(true);
345
            }
346

    
347
            @Override
348
            public void onSuccess(Void aVoid) {
349
                successLabel.setText(goldOAConstants.successDeactivatingSingleUser());
350
                successLabel.setVisible(true);
351
                loadUsers();
352
            }
353
        });
354
    }
355

    
356
    private void deactivateSelectedUsers(List<String> userIds) {
357

    
358
        dataService.deactivateUsers(userIds, new AsyncCallback<Void>() {
359

    
360
            @Override
361
            public void onFailure(Throwable throwable) {
362
                errorLabel.setText(goldOAConstants.errorDeactivatingSelectedUsers());
363
                errorLabel.setVisible(true);
364
            }
365

    
366
            @Override
367
            public void onSuccess(Void aVoid) {
368
                successLabel.setText(goldOAConstants.successDeactivatingSelectedUsers());
369
                successLabel.setVisible(true);
370
                loadUsers();
371
            }
372
        });
373
    }
374
}
(6-6/8)