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.constants.AlertType;
5
import com.google.gwt.core.client.GWT;
6
import com.google.gwt.dom.client.*;
7
import com.google.gwt.query.client.Function;
8
import com.google.gwt.user.client.Event;
9
import com.google.gwt.user.client.Window;
10
import com.google.gwt.user.client.rpc.AsyncCallback;
11
import com.google.gwt.user.client.ui.*;
12
import eu.dnetlib.client.*;
13
import eu.dnetlib.goldoa.domain.User;
14

    
15
import java.util.List;
16

    
17
import static com.google.gwt.query.client.GQuery.$;
18

    
19
/**
20
 * Created by stefania on 4/4/15.
21
 */
22
public class MonitorUserRolesWidget implements MyWidget {
23

    
24
    private String token = "";
25

    
26
    private FlowPanel monitorUsersPagePanel = new FlowPanel();
27
    private Label monitorUsersTitleLabel = new Label();
28
    private Label monitorUsersInfoLabel = new Label();
29

    
30
    private Alert errorLabel = new Alert();
31
    private Alert warningLabel = new Alert();
32
    private Alert successLabel = new Alert();
33

    
34
    private FlowPanel usersPanel = new FlowPanel();
35

    
36
    private DataServiceAsync dataService = GWT.create(DataService.class);
37
    private GoldOAConstants goldOAConstants = GWT.create(GoldOAConstants.class);
38

    
39
    public MonitorUserRolesWidget() {
40

    
41
        monitorUsersPagePanel.addStyleName("content");
42

    
43
        monitorUsersTitleLabel.setText("Monitor User Roles");
44
        monitorUsersTitleLabel.addStyleName("contentTitleLabel");
45

    
46
        monitorUsersInfoLabel.setText("Monitor the users' roles and approve/reject them as necessary");
47
        monitorUsersInfoLabel.addStyleName("contentInfoLabel");
48

    
49
        errorLabel.addStyleName("alertLabel");
50
        errorLabel.setType(AlertType.ERROR);
51
        errorLabel.setClose(false);
52
        errorLabel.setVisible(false);
53

    
54
        successLabel.addStyleName("alertLabel");
55
        successLabel.setType(AlertType.SUCCESS);
56
        successLabel.setClose(false);
57
        successLabel.setVisible(false);
58

    
59
        warningLabel.addStyleName("alertLabel");
60
        warningLabel.setType(AlertType.WARNING);
61
        warningLabel.setClose(false);
62
        warningLabel.setVisible(false);
63

    
64
        monitorUsersPagePanel.add(monitorUsersTitleLabel);
65
        monitorUsersPagePanel.add(monitorUsersInfoLabel);
66
        monitorUsersPagePanel.add(successLabel);
67
        monitorUsersPagePanel.add(errorLabel);
68
        monitorUsersPagePanel.add(warningLabel);
69
        monitorUsersPagePanel.add(usersPanel);
70
    }
71

    
72
    @Override
73
    public void clear() {
74

    
75
        errorLabel.setVisible(false);
76
        warningLabel.setVisible(false);
77
        successLabel.setVisible(false);
78
        usersPanel.clear();
79
    }
80

    
81
    @Override
82
    public void reload() {
83

    
84
        MyWidgetHelper.hideSidebar();
85

    
86
        SidebarPanel helpPanel = new SidebarPanel("Help");
87
        MyWidgetHelper.loadHelp(helpPanel, token.split("\\.")[0]);
88

    
89
        hideAlertLabels();
90
    }
91

    
92
    @Override
93
    public void setToken(String token) {
94
        this.token = token;
95
    }
96

    
97
    @Override
98
    public void afterAdditionToRootPanel() {
99
        loadUsers();
100
    }
101

    
102
    @Override
103
    public Widget asWidget() {
104
        return monitorUsersPagePanel;
105
    }
106

    
107
    private void loadUsers() {
108

    
109
        hideAlertLabels();
110

    
111
        final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
112
        usersPanel.addStyleName("loading");
113
        usersPanel.add(loadingWheel);
114

    
115
        dataService.getUsers(new AsyncCallback<List<User>>() {
116
            @Override
117
            public void onFailure(Throwable throwable) {
118

    
119
                usersPanel.clear();
120
                usersPanel.removeStyleName("loading");
121

    
122
                errorLabel.setText(goldOAConstants.errorRetrievingUsers());
123
                errorLabel.setVisible(true);
124
            }
125

    
126
            @Override
127
            public void onSuccess(List<User> users) {
128

    
129
                usersPanel.clear();
130
                usersPanel.removeStyleName("loading");
131

    
132
                if (users.isEmpty()) {
133
                    warningLabel.setText(goldOAConstants.warningNoAvailableUsers());
134
                    warningLabel.setVisible(true);
135
                } else {
136
                    drawUsersGrid(users);
137
                    addActionHandlers();
138
                }
139
            }
140
        });
141
    }
142

    
143
    private void hideAlertLabels() {
144

    
145
        errorLabel.setVisible(false);
146
        warningLabel.setVisible(false);
147
        successLabel.setVisible(false);
148
    }
149

    
150
    private void drawUsersGrid(List<User> users) {
151

    
152
        HTML usersTable = new HTML();
153
        String content = "";
154

    
155
        content += "<table class=\"tablesorter\" cellspacing=\"0\">";
156
        content += "<thead><tr><th></th><th>Name</th><th>Email</th>" +
157
                "<th>Role</th>" +
158
                "</tr></thead>";
159
        content += "<tbody>";
160

    
161
        for(User user : users) {
162
            content += "<tr>";
163
            content += "<td></td>";
164
            content += "<td>" + user.getFirstname() + " " + user.getInitials() + " " + user.getLastname() + "</td>";
165
            content += "<td>" + user.getEmail() + "</td>";
166

    
167
            content += "<td>";
168
            if(Utils.userHasRole(user, "researcher"))
169
                content += "<input id=\"" + user.getEmail() + "#researcher\" class=\"checkBox\" type=\"checkbox\" checked> Researcher<br>";
170
            else
171
                content += "<input id=\"" + user.getEmail() + "#researcher\" class=\"checkBox\" type=\"checkbox\">Researcher<br>";
172

    
173

    
174
            if(Utils.userHasRole(user, "library_staff") && Utils.isRoleApprovedForUser(user, "library_staff"))
175
                content += "<input id=\"" + user.getEmail() + "#library_staff\" class=\"checkBox\" type=\"checkbox\" checked>Library Staff<br>";
176
            else if(Utils.userHasRole(user, "library_staff") && !Utils.isRoleApprovedForUser(user, "library_staff"))
177
                content += "<input id=\"" + user.getEmail() + "#library_staff\" class=\"checkBox\" type=\"checkbox\">Library Staff (pending)<br>";
178
            else
179
                content += "<input id=\"" + user.getEmail() + "#library_staff\" class=\"checkBox\" type=\"checkbox\">Library Staff<br>";
180

    
181

    
182
            if(Utils.userHasRole(user, "project_coordinator") && Utils.isRoleApprovedForUser(user, "project_coordinator"))
183
                content += "<input id=\"" + user.getEmail() + "#project_coordinator\" class=\"checkBox\" type=\"checkbox\" checked>Project Coordinator<br>";
184
            else if(Utils.userHasRole(user, "project_coordinator") && !Utils.isRoleApprovedForUser(user, "project_coordinator"))
185
                content += "<input id=\"" + user.getEmail() + "#project_coordinator\" class=\"checkBox\" type=\"checkbox\">Project Coordinator (pending)<br>";
186
            else
187
                content += "<input id=\"" + user.getEmail() + "#project_coordinator\" class=\"checkBox\" type=\"checkbox\">Project Coordinator<br>";
188

    
189

    
190
            if(Utils.userHasRole(user, "publisher") && Utils.isRoleApprovedForUser(user, "publisher"))
191
                content += "<input id=\"" + user.getEmail() + "#publisher\" class=\"checkBox\" type=\"checkbox\" checked>Publisher<br>";
192
            else if(Utils.userHasRole(user, "publisher") && !Utils.isRoleApprovedForUser(user, "publisher"))
193
                content += "<input id=\"" + user.getEmail() + "#publisher\" class=\"checkBox\" type=\"checkbox\">Publisher (pending)<br>";
194
            else
195
                content += "<input id=\"" + user.getEmail() + "#publisher\" class=\"checkBox\" type=\"checkbox\">Publisher<br>";
196

    
197

    
198
            if(Utils.userHasRole(user, "accounting") && Utils.isRoleApprovedForUser(user, "accounting"))
199
                content += "<input id=\"" + user.getEmail() + "#accounting\" class=\"checkBox\" type=\"checkbox\" checked>Accounting Officer<br>";
200
            else if(Utils.userHasRole(user, "accounting") && !Utils.isRoleApprovedForUser(user, "accounting"))
201
                content += "<input id=\"" + user.getEmail() + "#accounting\" class=\"checkBox\" type=\"checkbox\">Accounting Officer (pending)<br>";
202
            else
203
                content += "<input id=\"" + user.getEmail() + "#accounting\" class=\"checkBox\" type=\"checkbox\">Accounting Officer<br>";
204

    
205

    
206
            if(Utils.userHasRole(user, "moderator") && Utils.isRoleApprovedForUser(user, "moderator"))
207
                content += "<input id=\"" + user.getEmail() + "#moderator\" class=\"checkBox\" type=\"checkbox\" checked>OpenAIRE moderator<br>";
208
            else if(Utils.userHasRole(user, "moderator") && !Utils.isRoleApprovedForUser(user, "moderator"))
209
                content += "<input id=\"" + user.getEmail() + "#moderator\" class=\"checkBox\" type=\"checkbox\">OpenAIRE moderator (pending)<br>";
210
            else
211
                content += "<input id=\"" + user.getEmail() + "#moderator\" class=\"checkBox\" type=\"checkbox\">OpenAIRE moderator<br>";
212

    
213

    
214
            if(Utils.userHasRole(user, "administrator") && Utils.isRoleApprovedForUser(user, "administrator"))
215
                content += "<input id=\"" + user.getEmail() + "#administrator\" class=\"checkBox\" type=\"checkbox\" checked>Administrator<br>";
216
            else if(Utils.userHasRole(user, "administrator") && !Utils.isRoleApprovedForUser(user, "administrator"))
217
                content += "<input id=\"" + user.getEmail() + "#administrator\" class=\"checkBox\" type=\"checkbox\">Administrator (pending)<br>";
218
            else
219
                content += "<input id=\"" + user.getEmail() + "#administrator\" class=\"checkBox\" type=\"checkbox\">Administrator<br>";
220

    
221
            content += "</td>";
222
            content += "</tr>";
223
        }
224

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

    
228
        usersTable.setHTML(content);
229

    
230
        usersPanel.add(usersTable);
231
    }
232

    
233
    private void addActionHandlers() {
234

    
235
        $(".checkBox").click(new Function() {
236

    
237
            public boolean f(Event e) {
238

    
239
                String[] idParts = $(e).get(0).getId().split("#");
240
                final String email = idParts[0];
241
                String roleId = idParts[1];
242
                InputElement checkbox = (InputElement) Document.get().getElementById($(e).get(0).getId());
243

    
244
                if(checkbox.isChecked()) {
245

    
246
                    dataService.acceptUserRole(email, roleId, new AsyncCallback<Void>() {
247

    
248
                        @Override
249
                        public void onFailure(Throwable throwable) {
250
                            errorLabel.setText(goldOAConstants.errorAcceptingUserRole());
251
                            errorLabel.setVisible(true);
252
                        }
253

    
254
                        @Override
255
                        public void onSuccess(Void aVoid) {
256
                            successLabel.setText(goldOAConstants.successAcceptingUserRole());
257
                            successLabel.setVisible(true);
258
                            loadUsers();
259

    
260
                            if(email.equals(GoldOAPortal.currentUser.getEmail()))
261
                                refreshCurrentUser();
262
                        }
263
                    });
264
                } else {
265

    
266
                    dataService.rejectRole(email, roleId, new AsyncCallback<Void>() {
267

    
268
                        @Override
269
                        public void onFailure(Throwable throwable) {
270
                            errorLabel.setText(goldOAConstants.errorRejectingUserRole());
271
                            errorLabel.setVisible(true);
272
                        }
273

    
274
                        @Override
275
                        public void onSuccess(Void aVoid) {
276
                            successLabel.setText(goldOAConstants.successRejectingUserRole());
277
                            successLabel.setVisible(true);
278
                            loadUsers();
279

    
280
                            if(email.equals(GoldOAPortal.currentUser.getEmail()))
281
                                refreshCurrentUser();
282
                        }
283
                    });
284
                }
285

    
286
                return true;
287
            }
288
        });
289
    }
290

    
291
    private void refreshCurrentUser() {
292

    
293
        dataService.getUserByEmail(GoldOAPortal.currentUser.getEmail(), new AsyncCallback<User>() {
294

    
295
            @Override
296
            public void onFailure(Throwable throwable) {
297

    
298
            }
299

    
300
            @Override
301
            public void onSuccess(User user) {
302
                GoldOAPortal.currentUser = user;
303
            }
304
        });
305
    }
306
}
(5-5/8)