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
    private Project project = null;
83

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

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

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

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

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

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

    
99
    private RecaptchaWidget captchaWidget;
100

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

    
103
    public UserRegistrationForm(final boolean edit) {
104

    
105

    
106
        userRegistrationFormPanel.addStyleName("content");
107

    
108
        isEdit = edit;
109

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

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

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

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

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

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

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

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

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

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

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

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

    
210

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

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

    
228
                userRoleFormFieldSet.setControlGroupType(ControlGroupType.NONE);
229

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

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

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

    
257
        userRegistrationForm.add(userRoleFormFieldSet);
258

    
259
        userRole.setAlternateSize(AlternateSize.XXLARGE);
260

    
261
        userRegistrationForm.add(affiliationsFormFieldSet);
262

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

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

    
279
        AutoCompleteWidget.AutoCompleteListener publisherAutoCompleteListener = new AutoCompleteWidget.AutoCompleteListener() {
280
            @Override
281
            public void valueSelected(Vocabulary vocabulary) {
282
                isPublisherSelected = true;
283
                publisher = vocabulary.getPublisher();
284
                Window.alert(publisher.getId());
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(project!=null)
511
            coordinatedProjects.add(project);
512

    
513
        person.setCoordinatedProjects(coordinatedProjects);
514

    
515
        if(!publisherAutoComplete.getValue().equals("")) {
516

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

    
527
        List<UserRole> personRoles = new ArrayList<>();
528

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

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

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

    
555
        for(String roleId : notShownRoles) {
556

    
557
            UserRole personRole = new UserRole();
558
            UserRolePK urPk = new UserRolePK();
559

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

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

    
586
    private void registerUser(User person) {
587

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

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

    
594
            @Override
595
            public void onFailure(Throwable throwable) {
596

    
597
                Window.scrollTo(0, 0);
598

    
599
                userRegistrationFormPanel.remove(loadingWheel);
600
                userRegistrationFormPanel.removeStyleName("loading");
601

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

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

    
623
            @Override
624
            public void onSuccess(Void aVoid) {
625

    
626
                Window.scrollTo(0, 0);
627

    
628
                userRegistrationFormPanel.remove(loadingWheel);
629
                userRegistrationFormPanel.removeStyleName("loading");
630

    
631
                String text = goldOAConstants.registerSuccessGeneral();
632

    
633
                boolean hasExtraRoles = false;
634

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

    
644
                if(hasExtraRoles)
645
                    text += goldOAConstants.registerSuccessExtraRoles();
646

    
647
                informationLabel.setText(text);
648
                informationLabel.setVisible(true);
649
            }
650
        });
651
    }
652

    
653
    private void updateUser(User person) {
654

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

    
659
        dataService.updateUser(person, new AsyncCallback<User>() {
660

    
661
            @Override
662
            public void onFailure(Throwable throwable) {
663

    
664
                userRegistrationFormPanel.remove(loadingWheel);
665
                userRegistrationFormPanel.removeStyleName("loading");
666

    
667
                errorLabel.setText(goldOAConstants.updateAccountInfoError());
668
                errorLabel.setVisible(true);
669
            }
670

    
671
            @Override
672
            public void onSuccess(User person) {
673

    
674
                userRegistrationFormPanel.remove(loadingWheel);
675
                userRegistrationFormPanel.removeStyleName("loading");
676

    
677
                GoldOAPortal.currentUser = person;
678

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

    
683
                History.newItem(GoldOAPortal.previousToken);
684
            }
685
        });
686
    }
687

    
688
    private void loadUserInfo() {
689
        registeredPerson = GoldOAPortal.currentUser;
690

    
691
        firstNameTextBox.setValue(registeredPerson.getFirstname());
692
        lastNameTextBox.setValue(registeredPerson.getLastname());
693

    
694
        if(registeredPerson.getInitials()!=null)
695
            initialsTextBox.setValue(registeredPerson.getInitials());
696

    
697
        emailTextBox.setValue(registeredPerson.getEmail());
698
        passwordTextBox.setValue(registeredPerson.getPassword());
699
        confirmPasswordTextBox.setValue(registeredPerson.getPassword());
700

    
701
        if(registeredPerson.getTelephone()!=null)
702
            telephoneTextBox.setValue(registeredPerson.getTelephone());
703

    
704
        multipleAffiliationsWidget.loadAffiliations(registeredPerson.getAffiliations());
705

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

    
714
        orcidFormFieldSet.setVisible(false);
715
        projectFormFieldSet.setVisible(false);
716
        publisherFormFieldSet.setVisible(false);
717
        affiliationsFormFieldSet.setVisible(true);
718

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

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

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

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

    
751
    private void clearUserInfo() {
752

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

    
761
        multipleAffiliationsWidget.clear();
762

    
763

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

    
771
        isProjectSelected = false;
772
        projectId = null;
773

    
774
        isPublisherSelected = false;
775
        publisher = null;
776

    
777
        selectedRoles = new ArrayList<>();
778
        notShownRoles = new ArrayList<>();
779

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

    
786
    @Override
787
    public void clear() {
788

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

    
796
    @Override
797
    public void reload() {
798

    
799
        MyWidgetHelper.hideSidebar();
800

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

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

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

    
816
    @Override
817
    public void afterAdditionToRootPanel() {
818

    
819
    }
820

    
821
    private boolean isCaptchaCompleted() {
822

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

    
826
        return  false;
827
    }
828
}
(10-10/10)