Project

General

Profile

1
package eu.dnetlib.repo.manager.client;
2

    
3
import com.google.gwt.core.client.GWT;
4
import com.google.gwt.dom.client.Document;
5
import com.google.gwt.dom.client.Element;
6
import com.google.gwt.dom.client.InputElement;
7
import com.google.gwt.dom.client.Style;
8
import com.google.gwt.event.dom.client.ClickEvent;
9
import com.google.gwt.event.dom.client.ClickHandler;
10
import com.google.gwt.user.client.rpc.AsyncCallback;
11
import com.google.gwt.user.client.ui.Button;
12
import eu.dnetlib.domain.functionality.UserProfile;
13
import eu.dnetlib.repo.manager.shared.UserAccessException;
14

    
15
/**
16
 * Created by stefania on 12/2/15.
17
 */
18
public class RegisterPage {
19

    
20
    private static RegisterPage instance = null;
21

    
22
    private Element userRegistrationError = Document.get().getElementById("userRegistrationError");
23
    private Element userRegistrationSuccess = Document.get().getElementById("userRegistrationSuccess");
24
    private InputElement usernameElement = (InputElement) Document.get().getElementById("username");
25
    private InputElement firstNameElement = (InputElement) Document.get().getElementById("firstName");
26
    private InputElement lastNameElement = (InputElement) Document.get().getElementById("lastName");
27
    private InputElement emailElement = (InputElement) Document.get().getElementById("email");
28
    private InputElement institutionElement = (InputElement) Document.get().getElementById("institution");
29
    private InputElement passwordElement = (InputElement) Document.get().getElementById("password");
30
    private InputElement confirmPasswordElement = (InputElement) Document.get().getElementById("confirmPassword");
31
    private Element registerElement = Document.get().getElementById("registerButton");
32
    private Button registerButton = Button.wrap(registerElement);
33

    
34
    private UserServiceAsync userService = GWT.create(UserService.class);
35

    
36
    private RegisterPage() {
37

    
38
        registerButton.addClickHandler(new ClickHandler() {
39

    
40
            @Override
41
            public void onClick(ClickEvent clickEvent) {
42

    
43
                userRegistrationError.getStyle().setDisplay(Style.Display.NONE);
44
                userRegistrationSuccess.getStyle().setDisplay(Style.Display.NONE);
45

    
46
                if(passwordElement.getValue()!=null && !passwordElement.getValue().trim().isEmpty()
47
                        && confirmPasswordElement.getValue()!=null && !confirmPasswordElement.getValue().trim().isEmpty()) {
48

    
49
                    if(!passwordElement.getValue().equals(confirmPasswordElement.getValue())) {
50

    
51
                        userRegistrationError.setInnerText("Password fields do not match");
52
                        userRegistrationError.getStyle().setDisplay(Style.Display.BLOCK);
53

    
54
                    } else {
55

    
56
                        UserProfile userProfile = new UserProfile();
57
                        userProfile.setFirstname(firstNameElement.getValue());
58
                        userProfile.setLastname(lastNameElement.getValue());
59
                        userProfile.setUsername(usernameElement.getValue());
60
                        userProfile.setEmail(emailElement.getValue());
61
                        userProfile.setPassword(passwordElement.getValue());
62
                        if(institutionElement.getValue()!=null && !institutionElement.getValue().isEmpty())
63
                            userProfile.setInstitution(institutionElement.getValue());
64

    
65
                        userService.register(userProfile, new AsyncCallback<Void>() {
66

    
67
                            @Override
68
                            public void onFailure(Throwable throwable) {
69

    
70
                                if(throwable instanceof UserAccessException) {
71
                                    UserAccessException uae = (UserAccessException) throwable;
72
                                    userRegistrationError.setInnerText(uae.getMessage());
73
                                    userRegistrationError.getStyle().setDisplay(Style.Display.BLOCK);
74
                                } else {
75
                                    userRegistrationError.setInnerText("Registration failed - Something went wrong, please try again.");
76
                                    userRegistrationError.getStyle().setDisplay(Style.Display.BLOCK);
77
                                }
78
                            }
79

    
80
                            @Override
81
                            public void onSuccess(Void aVoid) {
82
                                userRegistrationSuccess.setInnerText("An activation e-mail has been sent to the e-mail " +
83
                                        "address you specified. Please follow the link included in that e-mail to activate your account.");
84
                                userRegistrationSuccess.getStyle().setDisplay(Style.Display.BLOCK);
85
                            }
86
                        });
87
                    }
88
                }
89
            }
90
        });
91
    }
92

    
93
    public static final RegisterPage getInstance() {
94

    
95
        if(instance==null)
96
            instance = new RegisterPage();
97

    
98
        return instance;
99
    }
100

    
101
    public void showRegisterPage() {
102

    
103
        Document.get().getElementById("landingPage").getStyle().setDisplay(Style.Display.NONE);
104
        Document.get().getElementById("login").getStyle().setDisplay(Style.Display.NONE);
105
        Document.get().getElementById("wrapper").getStyle().setDisplay(Style.Display.NONE);
106
        Document.get().getElementById("register").getStyle().setDisplay(Style.Display.BLOCK);
107

    
108
        Document.get().getBody().removeAttribute("class");
109
        Document.get().getBody().addClassName("gray-bg");
110
    }
111
}
(5-5/13)