Project

General

Profile

1
package eu.dnetlib.client.user;
2

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

    
24
import java.util.ArrayList;
25
import java.util.HashMap;
26
import java.util.List;
27
import java.util.Map;
28

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

    
34
    private String token = "";
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

    
45
    private TextBox firstNameTextBox = new TextBox();
46
    private TextBox lastNameTextBox = new TextBox();
47
    private TextBox initialsTextBox = new TextBox();
48
    private FormFieldSet nameFormFieldSet = new FormFieldSet("Name (*)", firstNameTextBox, initialsTextBox, lastNameTextBox);
49

    
50
    private TextBox emailTextBox = new TextBox();
51
    private FormFieldSet emailFormFieldSet = new FormFieldSet("E-mail Address (*)", emailTextBox);
52

    
53
    private PasswordTextBox passwordTextBox = new PasswordTextBox();
54
    private FormFieldSet passwordFormFieldSet;
55
    private FormFieldSet passwordOptionalFormFieldSet;
56

    
57
    private PasswordTextBox confirmPasswordTextBox = new PasswordTextBox();
58
    private FormFieldSet confirmPasswordFormFieldSet;
59
    private FormFieldSet confirmPasswordOptionalFormFieldSet;
60

    
61
    private MultipleAffiliationsWidget multipleAffiliationsWidget = new MultipleAffiliationsWidget();
62
    private FormFieldSet affiliationsFormFieldSet = new FormFieldSet("Affiliations (*)", multipleAffiliationsWidget.asWidget());
63

    
64
    private TextBox telephoneTextBox = new TextBox();
65

    
66
    private ListBox userRole = new ListBox(true);
67
    private FormFieldSet userRoleFormFieldSet = new FormFieldSet("I am a (*)", userRole);
68

    
69
    private TextBox orcidID = new TextBox();
70
    private FormFieldSet orcidFormFieldSet = new FormFieldSet("ORCID", orcidID);
71

    
72
    private AutoCompleteWidget projectAutoComplete = new AutoCompleteWidget("project", "Search project");
73
    private FormFieldSet projectFormFieldSet = new FormFieldSet("Coordinated Project (*)", projectAutoComplete.asWidget());
74

    
75
    private AutoCompleteWidget publisherAutoComplete = new AutoCompleteWidget("publisher", "Search or insert publisher's name...");
76
    private FormFieldSet publisherFormFieldSet = new FormFieldSet("Publisher (*)", publisherAutoComplete.asWidget());
77

    
78
    private SubmitButton registerButton = new SubmitButton();
79

    
80
    private boolean isProjectSelected = false;
81
    private String projectId = null;
82

    
83
    private boolean isPublisherSelected = false;
84
    private Publisher publisher = null;
85

    
86
    private List<String> selectedRoles = new ArrayList<>();
87
    private List<String> notShownRoles = new ArrayList<>();
88

    
89
    private DataServiceAsync dataService = GWT.create(DataService.class);
90

    
91
    private boolean isEdit = false;
92
    private User registeredPerson;
93

    
94
    private Map<String, Integer> rolesMap = new HashMap<>();
95

    
96
    private Map<String, String> roles = new HashMap<>();
97

    
98
    private RecaptchaWidget captchaWidget;
99

    
100
    private GoldOAConstants goldOAConstants = GWT.create(GoldOAConstants.class);
101

    
102
    public UserRegistrationForm(final boolean edit) {
103

    
104

    
105
        userRegistrationFormPanel.addStyleName("content");
106

    
107
        isEdit = edit;
108

    
109
        if(edit)
110
            label.setText("Edit your information");
111
        else
112
            label.setText("Create an account");
113
        label.addStyleName("contentTitleLabel");
114

    
115
        userRegistrationFormPanel.add(label);
116
        userRegistrationFormPanel.add(informationLabel);
117
        userRegistrationFormPanel.add(errorLabel);
118

    
119
        informationLabel.addStyleName("alertLabel");
120
        informationLabel.setType(AlertType.SUCCESS);
121
        informationLabel.setVisible(false);
122
        informationLabel.setClose(false);
123

    
124
        errorLabel.addStyleName("alertLabel");
125
        errorLabel.setType(AlertType.ERROR);
126
        errorLabel.setVisible(false);
127
        errorLabel.setClose(false);
128

    
129
        userRegistrationForm.setType(FormType.HORIZONTAL);
130
        userRegistrationForm.addStyleName("userRegistrationForm");
131
        userRegistrationFormPanel.add(userRegistrationForm);
132

    
133
        firstNameTextBox.setAlternateSize(AlternateSize.MEDIUM);
134
        firstNameTextBox.setPlaceholder("First name (*)");
135
        firstNameTextBox.setValueChangeHandler(new ValueChangeHandler() {
136
            @Override
137
            public void handle(ValueChangeEvent valueChangeEvent) {
138
                nameFormFieldSet.setControlGroupType(ControlGroupType.NONE);
139
            }
140
        });
141

    
142
        lastNameTextBox.setAlternateSize(AlternateSize.MEDIUM);
143
        lastNameTextBox.setPlaceholder("Last name (*)");
144
        lastNameTextBox.setValueChangeHandler(new ValueChangeHandler() {
145
            @Override
146
            public void handle(ValueChangeEvent valueChangeEvent) {
147
                nameFormFieldSet.setControlGroupType(ControlGroupType.NONE);
148
            }
149
        });
150

    
151
        initialsTextBox.setAlternateSize(AlternateSize.MINI);
152
        initialsTextBox.setPlaceholder("Initials");
153
        initialsTextBox.setValueChangeHandler(new ValueChangeHandler() {
154
            @Override
155
            public void handle(ValueChangeEvent valueChangeEvent) {
156
                nameFormFieldSet.setControlGroupType(ControlGroupType.NONE);
157
            }
158
        });
159
        userRegistrationForm.add(nameFormFieldSet);
160

    
161
        emailTextBox.setAlternateSize(AlternateSize.XXLARGE);
162
        emailTextBox.setValueChangeHandler(new ValueChangeHandler() {
163
            @Override
164
            public void handle(ValueChangeEvent valueChangeEvent) {
165
                emailFormFieldSet.setControlGroupType(ControlGroupType.NONE);
166
            }
167
        });
168
        if(edit)
169
            emailTextBox.setEnabled(false);
170
        userRegistrationForm.add(emailFormFieldSet);
171

    
172
        passwordTextBox.setAlternateSize(AlternateSize.XXLARGE);
173
        passwordTextBox.setValueChangeHandler(new ValueChangeHandler() {
174
            @Override
175
            public void handle(ValueChangeEvent valueChangeEvent) {
176
                passwordOptionalFormFieldSet.setControlGroupType(ControlGroupType.NONE);
177
                passwordFormFieldSet.setControlGroupType(ControlGroupType.NONE);
178
            }
179
        });
180
        if(edit) {
181
            passwordOptionalFormFieldSet = new FormFieldSet("Password (Optional)", passwordTextBox);
182
            userRegistrationForm.add(passwordOptionalFormFieldSet);
183
        }
184
        else {
185
            passwordFormFieldSet = new FormFieldSet("Password (*)", passwordTextBox);
186
            userRegistrationForm.add(passwordFormFieldSet);
187
        }
188

    
189
        confirmPasswordTextBox.setAlternateSize(AlternateSize.XXLARGE);
190
        confirmPasswordTextBox.setValueChangeHandler(new ValueChangeHandler() {
191
            @Override
192
            public void handle(ValueChangeEvent valueChangeEvent) {
193
                confirmPasswordOptionalFormFieldSet.setControlGroupType(ControlGroupType.NONE);
194
                confirmPasswordFormFieldSet.setControlGroupType(ControlGroupType.NONE);
195
            }
196
        });
197
        if(edit) {
198
            confirmPasswordOptionalFormFieldSet = new FormFieldSet("Confirm Password (Optional)", confirmPasswordTextBox);
199
            userRegistrationForm.add(confirmPasswordOptionalFormFieldSet);
200
        }
201
        else {
202
            confirmPasswordFormFieldSet = new FormFieldSet("Confirm Password (*)", confirmPasswordTextBox);
203
            userRegistrationForm.add(confirmPasswordFormFieldSet);
204
        }
205

    
206
        telephoneTextBox.setAlternateSize(AlternateSize.XXLARGE);
207
        userRegistrationForm.add(new FormFieldSet("Telephone", telephoneTextBox));
208

    
209

    
210
        userRole.setAlternateSize(AlternateSize.XXLARGE);
211
        int j = 0;
212
        for(int i=0; i<GoldOAPortal.userRoles.size(); i++) {
213
            if(!(GoldOAPortal.userRoles.get(i).getId().equals("administrator")
214
                    || GoldOAPortal.userRoles.get(i).getId().equals("moderator")
215
                || GoldOAPortal.userRoles.get(i).getId().equals("accounting"))) {
216
                userRole.addItem(GoldOAPortal.userRoles.get(i).getRole(), GoldOAPortal.userRoles.get(i).getId());
217
                roles.put(GoldOAPortal.userRoles.get(i).getId(),GoldOAPortal.userRoles.get(i).getRole());
218
                rolesMap.put(GoldOAPortal.userRoles.get(i).getId(), j);
219
                j++;
220
            }
221
        }
222

    
223
        userRole.addChangeHandler(new ChangeHandler() {
224
            @Override
225
            public void onChange(ChangeEvent changeEvent) {
226

    
227
                userRoleFormFieldSet.setControlGroupType(ControlGroupType.NONE);
228

    
229
                selectedRoles.clear();
230
                for (int i = 0; i < userRole.getItemCount(); i++) {
231
                    if (userRole.isItemSelected(i)) {
232
                        selectedRoles.add(userRole.getValue(i));
233
                    }
234
                }
235

    
236
                orcidFormFieldSet.setVisible(false);
237
                projectFormFieldSet.setVisible(false);
238
                publisherFormFieldSet.setVisible(false);
239
                affiliationsFormFieldSet.setVisible(true);
240

    
241
                for(String selectedValue : selectedRoles) {
242
                    if(selectedValue.equals("researcher")) {
243
                        orcidFormFieldSet.setVisible(true);
244
                    }
245
                    if(selectedValue.equals("project_coordinator")) {
246
                        projectFormFieldSet.setVisible(true);
247
                    }
248
                    if(selectedValue.equals("publisher")) {
249
                        publisherFormFieldSet.setVisible(true);
250
                        affiliationsFormFieldSet.setVisible(false);
251
                    }
252
                }
253
            }
254
        });
255

    
256
        userRegistrationForm.add(userRoleFormFieldSet);
257

    
258
        userRole.setAlternateSize(AlternateSize.XXLARGE);
259

    
260
        userRegistrationForm.add(affiliationsFormFieldSet);
261

    
262
        orcidID.setAlternateSize(AlternateSize.XXLARGE);
263
        orcidFormFieldSet.setVisible(false);
264
        userRegistrationForm.add(orcidFormFieldSet);
265

    
266
        AutoCompleteWidget.AutoCompleteListener projectAutoCompleteListener = new AutoCompleteWidget.AutoCompleteListener() {
267
            @Override
268
            public void valueSelected(Vocabulary vocabulary) {
269
                isProjectSelected = true;
270
                projectId = vocabulary.getId();
271
            }
272
        };
273
        projectAutoComplete.setAutoCompleteListener(projectAutoCompleteListener);
274
        projectAutoComplete.addStyleName("userRegistrationCoordinatedProject");
275
        projectFormFieldSet.setVisible(false);
276
        userRegistrationForm.add(projectFormFieldSet);
277

    
278
        AutoCompleteWidget.AutoCompleteListener publisherAutoCompleteListener = new AutoCompleteWidget.AutoCompleteListener() {
279
            @Override
280
            public void valueSelected(Vocabulary vocabulary) {
281
                isPublisherSelected = true;
282
                publisher = new Publisher();
283
                publisher.setId(vocabulary.getId());
284
                publisher.setName(vocabulary.getName());
285
            }
286
        };
287
        publisherAutoComplete.setAutoCompleteListener(publisherAutoCompleteListener);
288
        publisherAutoComplete.addStyleName("userRegistrationAffiliatedPublisher");
289
        publisherFormFieldSet.setVisible(false);
290
        userRegistrationForm.add(publisherFormFieldSet);
291

    
292
        if(edit)
293
            registerButton.setText("Submit");
294
        else {
295
            registerButton.setText("Register");
296
            captchaWidget = new RecaptchaWidget(GoldOAPortal.publicCaptchaKey, "en", "clean");
297
            userRegistrationForm.add(new FormFieldSet(null, captchaWidget));
298
        }
299
        registerButton.setType(ButtonType.PRIMARY);
300

    
301
        userRegistrationForm.add(new FormFieldSet(null, registerButton));
302

    
303
        registerButton.addClickHandler(new ClickHandler() {
304

    
305
            @Override
306
            public void onClick(ClickEvent event) {
307
                if(edit) {
308
                    informationLabel.setVisible(false);
309
                    errorLabel.setVisible(false);
310

    
311
                    if(isUpdateComplete()) {
312

    
313
                        if(!passwordTextBox.getValue().trim().isEmpty() || !confirmPasswordTextBox.getValue().trim().isEmpty()) {
314

    
315
                            if(!passwordTextBox.getValue().trim().isEmpty() && !confirmPasswordTextBox.getValue().trim().isEmpty()) {
316
                                if(!passwordTextBox.getValue().equals(confirmPasswordTextBox.getValue())) {
317

    
318
                                    informationLabel.setVisible(false);
319
                                    errorLabel.setText(goldOAConstants.registerErrorPassFieldsDoNotMatch());
320
                                    errorLabel.setVisible(true);
321
                                } else {
322
                                    updateUser(getPerson());
323
                                }
324
                            } else {
325
                                informationLabel.setVisible(false);
326
                                errorLabel.setText(goldOAConstants.registerErrorPassFieldsDoNotMatch());
327
                                errorLabel.setVisible(true);
328
                            }
329
                        } else {
330
                            updateUser(getPerson());
331
                        }
332
                    } else {
333
                        informationLabel.setVisible(false);
334
                        errorLabel.setText(goldOAConstants.registerErrorAllFieldsAreRequired());
335
                        errorLabel.setVisible(true);
336
                    }
337

    
338
                } else {
339

    
340
                    informationLabel.setVisible(false);
341
                    errorLabel.setVisible(false);
342

    
343
                    if (isRegisterComplete()) {
344
                        if (passwordsMatch()) {
345
                            if(isCaptchaCompleted()) {
346

    
347
                                registerUser(getPerson());
348

    
349
                            } else {
350
                                errorLabel.setText(goldOAConstants.registerErrorCaptchaMissing());
351
                                errorLabel.setVisible(true);
352
                            }
353
                        } else {
354
                            errorLabel.setText(goldOAConstants.registerErrorPassFieldsDoNotMatch());
355
                            errorLabel.setVisible(true);
356
                        }
357
                    } else {
358
                        errorLabel.setText(goldOAConstants.registerErrorAllFieldsAreRequired());
359
                        errorLabel.setVisible(true);
360
                    }
361
                }
362
            }
363
        });
364
    }
365

    
366
    @Override
367
    public Widget asWidget() {
368
        return userRegistrationFormPanel;
369
    }
370

    
371
    private boolean isRegisterComplete() {
372

    
373
        if(!firstNameTextBox.getValue().trim().isEmpty() && !lastNameTextBox.getValue().trim().isEmpty()
374
                && !emailTextBox.getValue().trim().isEmpty() && !passwordTextBox.getValue().trim().isEmpty()
375
                && !confirmPasswordTextBox.getValue().trim().isEmpty() && !selectedRoles.isEmpty()) {
376

    
377
            boolean isPublisher = false;
378
            boolean isProjectCoordinator = false;
379
            for(String role : selectedRoles) {
380
                if(role.equals("project_coordinator")) {
381
                    isProjectCoordinator = true;
382
                    break;
383
                }
384
                if(role.equals("publisher")) {
385
                    isPublisher = true;
386
                    break;
387
                }
388
            }
389

    
390
            if(isPublisher && isProjectCoordinator) {
391

    
392
                if(isPublisherSelected && isProjectSelected)
393
                    return true;
394

    
395
            } else if(isPublisher && !isProjectCoordinator) {
396

    
397
                if(isPublisherSelected)
398
                    return true;
399

    
400
            } else if(!isPublisher && isProjectCoordinator) {
401

    
402
                if(isProjectSelected && !multipleAffiliationsWidget.getAffiliations().isEmpty())
403
                    return true;
404

    
405
            } else {
406

    
407
                if(!multipleAffiliationsWidget.getAffiliations().isEmpty())
408
                    return true;
409
            }
410

    
411
        } else {
412

    
413
//            if(firstNameTextBox.getValue().trim().equals("") || lastNameTextBox.getValue().trim().equals(""))
414
//                nameFormFieldSet.setControlGroupType(ControlGroupType.ERROR);
415
//
416
//            if(emailTextBox.getValue().trim().equals(""))
417
//                emailFormFieldSet.setControlGroupType(ControlGroupType.ERROR);
418
//
419
//            if(passwordTextBox.getValue().trim().equals(""))
420
//                passwordFormFieldSet.setControlGroupType(ControlGroupType.ERROR);
421
//
422
//            if(confirmPasswordTextBox.getValue().trim().equals(""))
423
//                confirmPasswordFormFieldSet.setControlGroupType(ControlGroupType.ERROR);
424
//
425
//            if(selectedRoles.isEmpty())
426
//                userRoleFormFieldSet.setControlGroupType(ControlGroupType.ERROR);
427
//
428
//            if(multipleAffiliationsWidget.getAffiliations().isEmpty())
429
//                affiliationsFormFieldSet.setControlGroupType(ControlGroupType.ERROR);
430
        }
431

    
432
        return false;
433
    }
434

    
435
    private boolean isUpdateComplete() {
436

    
437
        if(!firstNameTextBox.getValue().trim().isEmpty() && !lastNameTextBox.getValue().trim().isEmpty()
438
                && !emailTextBox.getValue().trim().isEmpty() && !selectedRoles.isEmpty()) {
439

    
440
            boolean isPublisher = false;
441
            boolean isProjectCoordinator = false;
442
            for(String role : selectedRoles) {
443
                if(role.equals("project_coordinator")) {
444
                    isProjectCoordinator = true;
445
                    break;
446
                }
447
                if(role.equals("publisher")) {
448
                    isPublisher = true;
449
                    break;
450
                }
451
            }
452

    
453
            if(isPublisher && isProjectCoordinator) {
454

    
455
                if(isPublisherSelected && isProjectSelected)
456
                    return true;
457

    
458
            } else if(isPublisher && !isProjectCoordinator) {
459

    
460
                if(isPublisherSelected)
461
                    return true;
462

    
463
            } else if(!isPublisher && isProjectCoordinator) {
464

    
465
                if(isProjectSelected && !multipleAffiliationsWidget.getAffiliations().isEmpty())
466
                    return true;
467

    
468
            } else {
469

    
470
                if(!multipleAffiliationsWidget.getAffiliations().isEmpty())
471
                    return true;
472
            }
473
        }
474

    
475
        return false;
476
    }
477

    
478
    private boolean passwordsMatch() {
479

    
480
        if(passwordTextBox.getValue().trim().equals(confirmPasswordTextBox.getValue().trim()))
481
            return true;
482

    
483
        return false;
484
    }
485

    
486
    private User  getPerson() {
487

    
488
        final User person;
489
        if(isEdit)
490
            person = registeredPerson;
491
        else
492
            person = new User();
493

    
494
        person.setFirstname(firstNameTextBox.getValue().trim());
495
        person.setLastname(lastNameTextBox.getValue().trim());
496
        person.setInitials(initialsTextBox.getValue().trim());
497
        person.setEmail(emailTextBox.getValue().trim());
498
        person.setPassword(passwordTextBox.getValue().trim());
499
        person.setTelephone(telephoneTextBox.getValue().trim());
500

    
501
        List<Affiliation> affiliations = multipleAffiliationsWidget.getAffiliations();
502
        for(Affiliation affiliation : affiliations) {
503
            affiliation.getUsers().add(person);
504
        }
505
        person.setAffiliations(affiliations);
506

    
507
        person.setOrcidid(orcidID.getValue().trim());
508

    
509
        List<Project> coordinatedProjects = new ArrayList<>();
510
        if(projectId!=null){
511
            Project p = new Project();
512
            p.setId(projectId);
513
            coordinatedProjects.add(p);
514
        }
515

    
516
        person.setCoordinatedProjects(coordinatedProjects);
517

    
518
        if(!publisherAutoComplete.getValue().equals("")) {
519

    
520
            if(publisher!=null && publisher.getName().equals(publisherAutoComplete.getValue())) {
521
                person.setPublisher(publisher);
522
            } else {
523
                Publisher publisher = new Publisher();
524
                publisher.setName(publisherAutoComplete.getValue());
525
                person.setPublisher(publisher);
526
            }
527
        }
528

    
529
        List<UserRole> personRoles = new ArrayList<>();
530

    
531
        for(final String roleId : selectedRoles) {
532
            UserRole personRole = new UserRole();
533
            UserRolePK urPk = new UserRolePK();
534

    
535
            urPk.setUser(person);
536
            urPk.setRole(new Role(roleId,roles.get(roleId)));
537
            personRole.setPk(urPk);
538

    
539
            if(GoldOAPortal.currentUser==null) {
540
                if (roleId.equals("researcher"))
541
                    personRole.setApproved(true);
542
                else
543
                    personRole.setApproved(false);
544
            } else {
545
                if(Utils.currentUserHasRole(roleId)) {
546
                    personRole.setApproved(Utils.isRoleApprovedForCurrentUser(roleId));
547
                } else {
548
                    if (roleId.equals("researcher"))
549
                        personRole.setApproved(true);
550
                    else
551
                        personRole.setApproved(false);
552
                }
553
            }
554
            personRoles.add(personRole);
555
        }
556

    
557
        for(String roleId : notShownRoles) {
558

    
559
            UserRole personRole = new UserRole();
560
            UserRolePK urPk = new UserRolePK();
561

    
562
            urPk.setUser(person);
563
            urPk.setRole(new Role(roleId,roles.get(roleId)));
564
            personRole.setPk(urPk);
565

    
566
            if(GoldOAPortal.currentUser==null) {
567
                if (roleId.equals("researcher"))
568
                    personRole.setApproved(true);
569
                else
570
                    personRole.setApproved(false);
571
            } else {
572
                if(Utils.currentUserHasRole(roleId)) {
573
                    personRole.setApproved(Utils.isRoleApprovedForCurrentUser(roleId));
574
                } else {
575
                    if (roleId.equals("researcher"))
576
                        personRole.setApproved(true);
577
                    else
578
                        personRole.setApproved(false);
579
                }
580
            }
581
            personRoles.add(personRole);
582
        }
583
        person.setRole(personRoles);
584
        person.setActive(false);
585
        return person;
586
    }
587

    
588
    private void registerUser(User person) {
589

    
590
        final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
591
        userRegistrationFormPanel.addStyleName("loading");
592
        userRegistrationFormPanel.add(loadingWheel);
593

    
594
        dataService.registerUser(person, captchaWidget.getChallenge(), captchaWidget.getResponse(), new AsyncCallback<Void>() {
595

    
596
            @Override
597
            public void onFailure(Throwable throwable) {
598

    
599
                Window.scrollTo(0, 0);
600

    
601
                userRegistrationFormPanel.remove(loadingWheel);
602
                userRegistrationFormPanel.removeStyleName("loading");
603

    
604
                if (throwable instanceof PersonManagerException) {
605
                    PersonManagerException personManagerException = (PersonManagerException) throwable;
606
                    if (personManagerException.getErrorCause().equals(PersonManagerException.ErrorCause.ALREADY_EXISTS)) {
607
                        errorLabel.setText(goldOAConstants.registerErrorUserAlreadyExists());
608
                        errorLabel.setVisible(true);
609
                    } else {
610
                        errorLabel.setText(goldOAConstants.registerErrorGeneral());
611
                        errorLabel.setVisible(true);
612
                    }
613
                } else {
614

    
615
                    if(throwable.getMessage().equals("captcha")) {
616
                        errorLabel.setText("Incorrect captcha");
617
                        captchaWidget.reload();
618
                    } else {
619
                        errorLabel.setText(goldOAConstants.registerErrorGeneral());
620
                    }
621
                    errorLabel.setVisible(true);
622
                }
623
            }
624

    
625
            @Override
626
            public void onSuccess(Void aVoid) {
627

    
628
                Window.scrollTo(0, 0);
629

    
630
                userRegistrationFormPanel.remove(loadingWheel);
631
                userRegistrationFormPanel.removeStyleName("loading");
632

    
633
                String text = goldOAConstants.registerSuccessGeneral();
634

    
635
                boolean hasExtraRoles = false;
636

    
637
                for (String role : selectedRoles) {
638
                    if (role.equals("researcher"))
639
                        continue;
640
                    else {
641
                        hasExtraRoles = true;
642
                        break;
643
                    }
644
                }
645

    
646
                if(hasExtraRoles)
647
                    text += goldOAConstants.registerSuccessExtraRoles();
648

    
649
                informationLabel.setText(text);
650
                informationLabel.setVisible(true);
651
            }
652
        });
653
    }
654

    
655
    private void updateUser(User person) {
656

    
657
        final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
658
        userRegistrationFormPanel.addStyleName("loading");
659
        userRegistrationFormPanel.add(loadingWheel);
660

    
661
        dataService.updateUser(person, new AsyncCallback<User>() {
662

    
663
            @Override
664
            public void onFailure(Throwable throwable) {
665

    
666
                userRegistrationFormPanel.remove(loadingWheel);
667
                userRegistrationFormPanel.removeStyleName("loading");
668

    
669
                errorLabel.setText(goldOAConstants.updateAccountInfoError());
670
                errorLabel.setVisible(true);
671
            }
672

    
673
            @Override
674
            public void onSuccess(User person) {
675

    
676
                userRegistrationFormPanel.remove(loadingWheel);
677
                userRegistrationFormPanel.removeStyleName("loading");
678

    
679
                GoldOAPortal.currentUser = person;
680

    
681
                UserInfoElement userInfoElement = new UserInfoElement();
682
                RootPanel.get("loginRegister").clear();
683
                RootPanel.get("loginRegister").add(userInfoElement.asWidget());
684

    
685
                History.newItem(GoldOAPortal.previousToken);
686
            }
687
        });
688
    }
689

    
690
    private void loadUserInfo() {
691
        registeredPerson = GoldOAPortal.currentUser;
692

    
693
        firstNameTextBox.setValue(registeredPerson.getFirstname());
694
        lastNameTextBox.setValue(registeredPerson.getLastname());
695

    
696
        if(registeredPerson.getInitials()!=null)
697
            initialsTextBox.setValue(registeredPerson.getInitials());
698

    
699
        emailTextBox.setValue(registeredPerson.getEmail());
700
        passwordTextBox.setValue(registeredPerson.getPassword());
701
        confirmPasswordTextBox.setValue(registeredPerson.getPassword());
702

    
703
        if(registeredPerson.getTelephone()!=null)
704
            telephoneTextBox.setValue(registeredPerson.getTelephone());
705

    
706
        multipleAffiliationsWidget.loadAffiliations(registeredPerson.getAffiliations());
707

    
708
        for(UserRole personRole : registeredPerson.getRoles()) {
709
            if(personRole.getPk().getRole().getId().equals("administrator") || personRole.getPk().getRole().getId().equals("moderator")
710
                    || personRole.getPk().getRole().getId().equals("accounting"))
711
                notShownRoles.add(personRole.getPk().getRole().getId());
712
            else
713
                selectedRoles.add(personRole.getPk().getRole().getId());
714
        }
715

    
716
        orcidFormFieldSet.setVisible(false);
717
        projectFormFieldSet.setVisible(false);
718
        publisherFormFieldSet.setVisible(false);
719
        affiliationsFormFieldSet.setVisible(true);
720

    
721
        for(String selectedValue : selectedRoles) {
722
            if(selectedValue.equals("researcher")) {
723
                if(registeredPerson.getOrcidid()!=null)
724
                    orcidID.setValue(registeredPerson.getOrcidid());
725
                orcidFormFieldSet.setVisible(true);
726
            }
727
            if(selectedValue.equals("project_coordinator")) {
728

    
729
                Project coordinatedProject = registeredPerson.getCoordinatedProjects().get(0);
730
                String projectValue = coordinatedProject.getAcronym() + " - " + coordinatedProject.getGrant()
731
                        + " (" + coordinatedProject.getTitle() + ")";
732
                projectAutoComplete.setValue(projectValue);
733
                projectFormFieldSet.setVisible(true);
734
                isProjectSelected = true;
735
                projectId = registeredPerson.getCoordinatedProjects().get(0).getId();
736
            }
737
            if(selectedValue.equals("publisher")) {
738

    
739
                publisherAutoComplete.setValue(registeredPerson.getPublisher().getName());
740
                publisherFormFieldSet.setVisible(true);
741
                affiliationsFormFieldSet.setVisible(false);
742
                isPublisherSelected = true;
743
                publisher = registeredPerson.getPublisher();
744
            }
745
        }
746

    
747
        userRole.setMultipleSelect(true);
748
        for(String roleId : selectedRoles) {
749
            userRole.setItemSelected(rolesMap.get(roleId), true);
750
        }
751
    }
752

    
753
    private void clearUserInfo() {
754

    
755
        firstNameTextBox.setValue("");
756
        lastNameTextBox.setValue("");
757
        initialsTextBox.setValue("");
758
        emailTextBox.setValue("");
759
        passwordTextBox.setValue("");
760
        confirmPasswordTextBox.setValue("");
761
        telephoneTextBox.setValue("");
762

    
763
        multipleAffiliationsWidget.clear();
764

    
765

    
766
        for(Map.Entry<String, Integer> entry : rolesMap.entrySet()) {
767
            userRole.setItemSelected(entry.getValue(), false);
768
        }
769
        orcidID.setValue("");
770
        projectAutoComplete.setValue("");
771
        publisherAutoComplete.setValue("");
772

    
773
        isProjectSelected = false;
774
        projectId = null;
775

    
776
        isPublisherSelected = false;
777
        publisher = null;
778

    
779
        selectedRoles = new ArrayList<>();
780
        notShownRoles = new ArrayList<>();
781

    
782
        orcidFormFieldSet.setVisible(false);
783
        projectFormFieldSet.setVisible(false);
784
        publisherFormFieldSet.setVisible(false);
785
        affiliationsFormFieldSet.setVisible(true);
786
    }
787

    
788
    @Override
789
    public void clear() {
790

    
791
        informationLabel.setVisible(false);
792
        errorLabel.setVisible(false);
793
        clearUserInfo();
794
        if(isEdit)
795
            loadUserInfo();
796
    }
797

    
798
    @Override
799
    public void reload() {
800

    
801
        MyWidgetHelper.hideSidebar();
802

    
803
        SidebarPanel helpPanel = new SidebarPanel("Help");
804
        MyWidgetHelper.loadHelp(helpPanel, token.split("\\.")[0]);
805

    
806
        informationLabel.setVisible(false);
807
        errorLabel.setVisible(false);
808
        clearUserInfo();
809
        if(isEdit)
810
            loadUserInfo();
811
    }
812

    
813
    @Override
814
    public void setToken(String token) {
815
        this.token = token;
816
    }
817

    
818
    @Override
819
    public void afterAdditionToRootPanel() {
820

    
821
    }
822

    
823
    private boolean isCaptchaCompleted() {
824

    
825
        if(captchaWidget.getResponse()!=null && !captchaWidget.getResponse().equals(""))
826
            return true;
827

    
828
        return  false;
829
    }
830
}
(10-10/10)