Project

General

Profile

1
package eu.dnetlib.client.user;
2

    
3
import com.github.gwtbootstrap.client.ui.*;
4
import com.github.gwtbootstrap.client.ui.ListBox;
5
import com.github.gwtbootstrap.client.ui.PasswordTextBox;
6
import com.github.gwtbootstrap.client.ui.SubmitButton;
7
import com.github.gwtbootstrap.client.ui.TextBox;
8
import com.github.gwtbootstrap.client.ui.constants.*;
9
import com.google.gwt.core.client.GWT;
10
import com.google.gwt.event.dom.client.ChangeEvent;
11
import com.google.gwt.event.dom.client.ChangeHandler;
12
import com.google.gwt.event.dom.client.ClickEvent;
13
import com.google.gwt.event.dom.client.ClickHandler;
14
import com.google.gwt.user.client.History;
15
import com.google.gwt.user.client.Window;
16
import com.google.gwt.user.client.rpc.AsyncCallback;
17
import com.google.gwt.user.client.ui.*;
18
import com.google.gwt.user.client.ui.Label;
19
import eu.dnetlib.client.*;
20
import eu.dnetlib.client.widgets.AutoCompleteWidget;
21
import eu.dnetlib.client.widgets.FormFieldSet;
22
import eu.dnetlib.client.widgets.MultipleAffiliationsWidget;
23
import eu.dnetlib.goldoa.domain.*;
24

    
25
import java.awt.event.WindowEvent;
26
import java.util.ArrayList;
27
import java.util.HashMap;
28
import java.util.List;
29
import java.util.Map;
30

    
31
/**
32
 * Created by stefania on 2/25/15.
33
 */
34
public class UserRegistrationForm implements MyWidget {
35

    
36
    private FlowPanel userRegistrationFormPanel = new FlowPanel();
37

    
38
    private Alert errorLabel = new Alert();
39
    private Alert informationLabel = new Alert();
40

    
41
    private Label label = new Label();
42

    
43
    private Form userRegistrationForm = new Form();
44
    private TextBox firstNameTextBox = new TextBox();
45
    private TextBox lastNameTextBox = new TextBox();
46
    private TextBox initialsTextBox = new TextBox();
47
    private TextBox emailTextBox = new TextBox();
48
    private PasswordTextBox passwordTextBox = new PasswordTextBox();
49
    private PasswordTextBox confirmPasswordTextBox = new PasswordTextBox();
50

    
51
    private MultipleAffiliationsWidget multipleAffiliationsWidget = new MultipleAffiliationsWidget();
52

    
53
    private TextBox telephoneTextBox = new TextBox();
54

    
55
    private Form userRoleForm = new Form();
56
    private ListBox userRole = new ListBox(true);
57
    private TextBox orcidID = new TextBox();
58
    private AutoCompleteWidget projectAutoComplete = new AutoCompleteWidget("project", "Search project");
59

    
60
    private SubmitButton registerButton = new SubmitButton();
61

    
62
    private boolean isProjectSelected = false;
63
    private String projectId = null;
64

    
65
    private List<String> selectedRoles = new ArrayList<>();
66

    
67
    private DataServiceAsync dataService = GWT.create(DataService.class);
68

    
69
    private boolean isEdit = false;
70

    
71
    private Map<String, Integer> rolesMap = new HashMap<>();
72

    
73
//    private RecaptchaWidget recaptchaWidget;
74

    
75
    public UserRegistrationForm(final boolean edit) {
76

    
77
        isEdit = edit;
78

    
79
        if(edit)
80
            label.setText("Edit your information");
81
        else
82
            label.setText("Are you new to Gold OA? Register here.");
83
        label.addStyleName("contentTitleLabel");
84

    
85
        userRegistrationFormPanel.add(label);
86
        userRegistrationFormPanel.add(informationLabel);
87
        userRegistrationFormPanel.add(errorLabel);
88

    
89
        informationLabel.addStyleName("alertLabel");
90
        informationLabel.setType(AlertType.SUCCESS);
91
        informationLabel.setVisible(false);
92
        informationLabel.setClose(false);
93

    
94
        errorLabel.addStyleName("alertLabel");
95
        errorLabel.setType(AlertType.ERROR);
96
        errorLabel.setVisible(false);
97
        errorLabel.setClose(false);
98

    
99
        userRegistrationForm.setType(FormType.HORIZONTAL);
100
        userRegistrationForm.addStyleName("userRegistrationForm");
101
        userRegistrationFormPanel.add(userRegistrationForm);
102

    
103
        firstNameTextBox.setAlternateSize(AlternateSize.MEDIUM);
104
        firstNameTextBox.setPlaceholder("First name");
105
        lastNameTextBox.setAlternateSize(AlternateSize.MEDIUM);
106
        lastNameTextBox.setPlaceholder("Last name");
107
        initialsTextBox.setAlternateSize(AlternateSize.MINI);
108
        initialsTextBox.setPlaceholder("Initials");
109
        userRegistrationForm.add(new FormFieldSet("Name (*)", firstNameTextBox, initialsTextBox, lastNameTextBox));
110

    
111
        emailTextBox.setAlternateSize(AlternateSize.XXLARGE);
112
        if(edit)
113
            emailTextBox.setEnabled(false);
114
        userRegistrationForm.add(new FormFieldSet("E-mail Address (*)", emailTextBox));
115

    
116
        passwordTextBox.setAlternateSize(AlternateSize.XXLARGE);
117
        if(edit)
118
            userRegistrationForm.add(new FormFieldSet("Password (Optional)", passwordTextBox));
119
        else
120
            userRegistrationForm.add(new FormFieldSet("Password (*)", passwordTextBox));
121

    
122
        confirmPasswordTextBox.setAlternateSize(AlternateSize.XXLARGE);
123
        if(edit)
124
            userRegistrationForm.add(new FormFieldSet("Confirm Password (Optional)", confirmPasswordTextBox));
125
        else
126
            userRegistrationForm.add(new FormFieldSet("Confirm Password (*)", confirmPasswordTextBox));
127

    
128
        telephoneTextBox.setAlternateSize(AlternateSize.XXLARGE);
129
        userRegistrationForm.add(new FormFieldSet("Telephone", telephoneTextBox));
130

    
131

    
132
        userRegistrationForm.add(new FormFieldSet("Affiliations (*)", multipleAffiliationsWidget.asWidget()));
133

    
134
        userRoleForm.setType(FormType.HORIZONTAL);
135
        userRoleForm.addStyleName("rolesForm");
136
        userRegistrationFormPanel.add(userRoleForm);
137

    
138
        userRole.setAlternateSize(AlternateSize.XXLARGE);
139

    
140
        for(int i=0; i<GoldOAPortal.userRoles.size(); i++) {
141
            userRole.addItem(GoldOAPortal.userRoles.get(i).getName(), GoldOAPortal.userRoles.get(i).getId());
142
            rolesMap.put(GoldOAPortal.userRoles.get(i).getId(), i);
143
        }
144

    
145
        userRole.addChangeHandler(new ChangeHandler() {
146
            @Override
147
            public void onChange(ChangeEvent changeEvent) {
148

    
149
                selectedRoles.clear();
150
                for (int i = 0; i < userRole.getItemCount(); i++) {
151
                    if (userRole.isItemSelected(i)) {
152
                        selectedRoles.add(userRole.getValue(i));
153
                    }
154
                }
155

    
156
                userRoleForm.clear();
157
                userRoleForm.add(new FormFieldSet("I am a (*)", userRole));
158
                for(String selectedValue : selectedRoles) {
159
                    if(selectedValue.equals("researcher")) {
160
                        userRoleForm.add(new FormFieldSet("ORCID", orcidID));
161
                    }
162
                    if(selectedValue.equals("project_coordinator")) {
163
                        userRoleForm.add(new FormFieldSet("Coordinated Project (*)", projectAutoComplete.asWidget()));
164
                    }
165
                }
166
                userRoleForm.add(new FormFieldSet(null, registerButton));
167
            }
168
        });
169
        userRoleForm.add(new FormFieldSet("I am a (*)", userRole));
170

    
171
        userRole.setAlternateSize(AlternateSize.XXLARGE);
172

    
173
        orcidID.setAlternateSize(AlternateSize.XXLARGE);
174
        AutoCompleteWidget.AutoCompleteListener projectAutoCompleteListener = new AutoCompleteWidget.AutoCompleteListener() {
175
            @Override
176
            public void valueSelected(Vocabulary vocabulary) {
177
                isProjectSelected = true;
178
                projectId = vocabulary.getId();
179
            }
180
        };
181
        projectAutoComplete.setAutoCompleteListener(projectAutoCompleteListener);
182
        projectAutoComplete.addStyleName("userRegistrationCoordinatedProject");
183

    
184
        if(edit)
185
            registerButton.setText("Submit");
186
        else
187
            registerButton.setText("Register");
188
        registerButton.setType(ButtonType.PRIMARY);
189

    
190
        userRoleForm.add(new FormFieldSet(null, registerButton));
191

    
192
        registerButton.addClickHandler(new ClickHandler() {
193

    
194
            @Override
195
            public void onClick(ClickEvent event) {
196

    
197
                if(edit) {
198

    
199
                    informationLabel.setVisible(false);
200
                    errorLabel.setVisible(false);
201

    
202
                    if(isUpdateComplete()) {
203

    
204
                        if(!passwordTextBox.getValue().trim().isEmpty() || !confirmPasswordTextBox.getValue().trim().isEmpty()) {
205

    
206
                            if(!passwordTextBox.getValue().trim().isEmpty() && !confirmPasswordTextBox.getValue().trim().isEmpty()) {
207
                                if(!passwordTextBox.getValue().equals(confirmPasswordTextBox.getValue())) {
208

    
209
                                    informationLabel.setVisible(false);
210
                                    errorLabel.setText("The two password fields do not match.");
211
                                    errorLabel.setVisible(true);
212
                                } else {
213
                                    updateUser(getPerson());
214
                                }
215
                            } else {
216
                                informationLabel.setVisible(false);
217
                                errorLabel.setText("The two password fields do not match.");
218
                                errorLabel.setVisible(true);
219
                            }
220
                        } else {
221
                            updateUser(getPerson());
222
                        }
223
                    } else {
224
                        informationLabel.setVisible(false);
225
                        errorLabel.setText("All asterisk (*) fields are required.");
226
                        errorLabel.setVisible(true);
227
                    }
228

    
229
                } else {
230

    
231
                    informationLabel.setVisible(false);
232
                    errorLabel.setVisible(false);
233
                    if (isRegisterComplete()) {
234
                        if (passwordsMatch()) {
235
                            registerUser(getPerson());
236
                        } else {
237
                            errorLabel.setText("The password fields do not match.");
238
                            errorLabel.setVisible(true);
239
                        }
240
                    } else {
241
                        errorLabel.setText("All asterisk (*) fields are required.");
242
                        errorLabel.setVisible(true);
243
                    }
244
                }
245
            }
246
        });
247
    }
248

    
249
    @Override
250
    public Widget asWidget() {
251
        return userRegistrationFormPanel;
252
    }
253

    
254
    private boolean isRegisterComplete() {
255

    
256
        if(!firstNameTextBox.getValue().trim().isEmpty() && !lastNameTextBox.getValue().trim().isEmpty()
257
                && !emailTextBox.getValue().trim().isEmpty() && !passwordTextBox.getValue().trim().isEmpty()
258
                && !confirmPasswordTextBox.getValue().trim().isEmpty() && !selectedRoles.isEmpty()
259
                && !multipleAffiliationsWidget.getAffiliations().isEmpty()) {
260

    
261
            boolean isProjectCoordinator = false;
262
            for(String role : selectedRoles) {
263
                if(role.equals("project_coordinator")) {
264
                    isProjectCoordinator = true;
265
                    break;
266
                }
267
            }
268
            if(!isProjectCoordinator)
269
                return true;
270
            else if(isProjectSelected)
271
                return true;
272
        }
273

    
274
        return false;
275
    }
276

    
277
    private boolean isUpdateComplete() {
278

    
279
        if(!firstNameTextBox.getValue().trim().isEmpty() && !lastNameTextBox.getValue().trim().isEmpty()
280
                && !emailTextBox.getValue().trim().isEmpty() && !selectedRoles.isEmpty()
281
                && !multipleAffiliationsWidget.getAffiliations().isEmpty()) {
282

    
283
            boolean isProjectCoordinator = false;
284
            for(String role : selectedRoles) {
285
                if(role.equals("project_coordinator")) {
286
                    isProjectCoordinator = true;
287
                    break;
288
                }
289
            }
290
            if(!isProjectCoordinator)
291
                return true;
292
            else if(isProjectSelected)
293
                return true;
294
        }
295

    
296
        return false;
297
    }
298

    
299
    private boolean passwordsMatch() {
300

    
301
        if(passwordTextBox.getValue().trim().equals(confirmPasswordTextBox.getValue().trim()))
302
            return true;
303

    
304
        return false;
305
    }
306

    
307
    private Person getPerson() {
308

    
309
        Person person = new Person();
310

    
311
        person.setName(firstNameTextBox.getValue().trim());
312
        person.setLastname(lastNameTextBox.getValue().trim());
313
        person.setInitials(initialsTextBox.getValue().trim());
314
        person.setEmail(emailTextBox.getValue().trim());
315
        person.setPassword(passwordTextBox.getValue().trim());
316
        person.setTelephone(telephoneTextBox.getValue().trim());
317

    
318
        List<Affiliation> affiliations = multipleAffiliationsWidget.getAffiliations();
319
        for(Affiliation affiliation : affiliations) {
320
            Person affiliationPerson = new Person();
321
            affiliationPerson.setEmail(emailTextBox.getValue().trim());
322
            affiliation.setPerson(affiliationPerson);
323
        }
324
        person.setAffiliations(affiliations);
325

    
326
        person.setOrcidId(orcidID.getValue().trim());
327

    
328
        List<Project> coordinatedProjects = new ArrayList<>();
329
        if(projectId!=null)
330
            coordinatedProjects.add(new Project(projectId));
331
        person.setCoordinatedProjects(coordinatedProjects);
332

    
333
        List<PersonRole> personRoles = new ArrayList<>();
334
        for(String role : selectedRoles) {
335
            PersonRole personRole = new PersonRole();
336
            personRole.setPerson(new Person(emailTextBox.getValue().trim()));
337
            personRole.setRole(new Role(role));
338

    
339
            if(GoldOAPortal.currentUser==null) {
340
                if (role.equals("researcher"))
341
                    personRole.setApproved(true);
342
                else
343
                    personRole.setApproved(false);
344
            } else {
345
                if(userHasRole(role)) {
346
                    personRole.setApproved(isApproved(role));
347
                } else {
348
                    if (role.equals("researcher"))
349
                        personRole.setApproved(true);
350
                    else
351
                        personRole.setApproved(false);
352
                }
353
            }
354

    
355
            personRoles.add(personRole);
356
        }
357
        person.setRoles(personRoles);
358

    
359
        person.setActive(false);
360

    
361
        return person;
362
    }
363

    
364
    private void registerUser(Person person) {
365

    
366
        dataService.registerUser(person, new AsyncCallback<Void>() {
367

    
368
            @Override
369
            public void onFailure(Throwable throwable) {
370
                if (throwable instanceof PersonManagerException) {
371
                    PersonManagerException personManagerException = (PersonManagerException) throwable;
372
                    if (personManagerException.getErrorCause().equals(PersonManagerException.ErrorCause.ALREADY_EXISTS)) {
373
                        errorLabel.setText("Registration failed - A user with this e-mail already exists.");
374
                        errorLabel.setVisible(true);
375
                    } else {
376
                        errorLabel.setText("Registration failed - System error.");
377
                        errorLabel.setVisible(true);
378
                    }
379
                } else {
380
                    errorLabel.setText("Registration failed - System error.");
381
                    errorLabel.setVisible(true);
382
                }
383
            }
384

    
385
            @Override
386
            public void onSuccess(Void aVoid) {
387

    
388
                String text = "Registration succeeded. You will receive an e-mail to activate your account. ";
389
                for (String role : selectedRoles) {
390
                    if (role.equals("researcher"))
391
                        continue;
392
                    else {
393
                        text += "Your extra roles (other than researcher must be approved by OpenAIRE";
394
                    }
395
                }
396

    
397
                informationLabel.setText(text);
398
                informationLabel.setVisible(true);
399
            }
400
        });
401
    }
402

    
403
    private void updateUser(Person person) {
404

    
405
        dataService.updateUser(person, new AsyncCallback<Person>() {
406

    
407
            @Override
408
            public void onFailure(Throwable throwable) {
409
                errorLabel.setText("System error updating account information.");
410
                errorLabel.setVisible(true);
411
            }
412

    
413
            @Override
414
            public void onSuccess(Person person) {
415

    
416
                GoldOAPortal.currentUser = person;
417

    
418
                UserInfoElement userInfoElement = new UserInfoElement();
419
                RootPanel.get("loginRegister").clear();
420
                RootPanel.get("loginRegister").add(userInfoElement.asWidget());
421

    
422
                History.newItem(GoldOAPortal.previousToken);
423
            }
424
        });
425
    }
426

    
427
    private void loadUserInfo() {
428

    
429
        Person person = GoldOAPortal.currentUser;
430

    
431
        firstNameTextBox.setValue(person.getName());
432
        lastNameTextBox.setValue(person.getLastname());
433

    
434
        if(person.getInitials()!=null)
435
            initialsTextBox.setValue(person.getInitials());
436

    
437
        emailTextBox.setValue(person.getEmail());
438
        passwordTextBox.setValue(person.getPassword());
439
        confirmPasswordTextBox.setValue(person.getPassword());
440

    
441
        if(person.getTelephone()!=null)
442
            telephoneTextBox.setValue(person.getTelephone());
443

    
444
        multipleAffiliationsWidget.loadAffiliations(person.getAffiliations());
445

    
446
        for(PersonRole personRole : person.getRoles()) {
447
            selectedRoles.add(personRole.getRole().getId());
448
        }
449

    
450
        userRoleForm.clear();
451
        userRoleForm.add(new FormFieldSet("I am a (*)", userRole));
452
        for(String selectedValue : selectedRoles) {
453
            if(selectedValue.equals("researcher")) {
454
                if(person.getOrcidId()!=null)
455
                    orcidID.setValue(person.getOrcidId());
456
                userRoleForm.add(new FormFieldSet("ORCID", orcidID));
457
            }
458
            if(selectedValue.equals("project_coordinator")) {
459

    
460
                Project coordinatedProject = person.getCoordinatedProjects().get(0);
461
                String projectValue = coordinatedProject.getAcronym() + " - " + coordinatedProject.getGrant()
462
                        + " (" + coordinatedProject.getTitle() + ")";
463
                projectAutoComplete.setValue(projectValue);
464
                userRoleForm.add(new FormFieldSet("Coordinated Project (*)", projectAutoComplete.asWidget()));
465
                isProjectSelected = true;
466
                projectId = person.getCoordinatedProjects().get(0).getId();
467
            }
468
        }
469

    
470
        userRole.setMultipleSelect(true);
471
        for(String roleId : selectedRoles) {
472
            userRole.setItemSelected(rolesMap.get(roleId), true);
473
        }
474

    
475
        userRoleForm.add(new FormFieldSet(null, registerButton));
476
    }
477

    
478
    private void clearUserInfo() {
479

    
480
        firstNameTextBox.setValue("");
481
        lastNameTextBox.setValue("");
482
        initialsTextBox.setValue("");
483
        emailTextBox.setValue("");
484
        passwordTextBox.setValue("");
485
        confirmPasswordTextBox.setValue("");
486
        telephoneTextBox.setValue("");
487

    
488
        multipleAffiliationsWidget.clear();
489

    
490
        for(Map.Entry<String, Integer> entry : rolesMap.entrySet()) {
491
            userRole.setItemSelected(entry.getValue(), false);
492
        }
493
        orcidID.setValue("");
494
        projectAutoComplete.setValue("");
495

    
496
        isProjectSelected = false;
497
        projectId = null;
498

    
499
        selectedRoles = new ArrayList<>();
500
    }
501

    
502
    @Override
503
    public void clear() {
504
        informationLabel.setVisible(false);
505
        errorLabel.setVisible(false);
506
        clearUserInfo();
507
        if(isEdit)
508
            loadUserInfo();
509
    }
510

    
511
    @Override
512
    public void reload() {
513
        informationLabel.setVisible(false);
514
        errorLabel.setVisible(false);
515
        clearUserInfo();
516
        if(isEdit)
517
            loadUserInfo();
518
    }
519

    
520
    @Override
521
    public void setToken(String token) {
522

    
523
    }
524

    
525
    private boolean userHasRole(String roleId) {
526

    
527
        List<PersonRole> personRoles = GoldOAPortal.currentUser.getRoles();
528
        for(PersonRole personRole : personRoles) {
529
            if(personRole.getRole().getId().equals(roleId))
530
                return true;
531
        }
532

    
533
        return false;
534
    }
535

    
536
    private boolean isApproved(String roleId) {
537

    
538
        List<PersonRole> personRoles = GoldOAPortal.currentUser.getRoles();
539
        for(PersonRole personRole : personRoles) {
540
            if(personRole.getRole().getId().equals(roleId))
541
                return personRole.isApproved();
542
        }
543

    
544
        return false;
545
    }
546
}
(2-2/2)