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.Style;
6
import com.google.gwt.event.dom.client.ClickEvent;
7
import com.google.gwt.event.dom.client.ClickHandler;
8
import com.google.gwt.user.client.rpc.AsyncCallback;
9
import com.google.gwt.user.client.ui.HTML;
10
import com.google.gwt.user.client.ui.PasswordTextBox;
11
import com.google.gwt.user.client.ui.RootPanel;
12
import eu.dnetlib.domain.functionality.UserProfile;
13
import eu.dnetlib.gwt.client.MyFormGroup;
14
import eu.dnetlib.repo.manager.client.services.UserService;
15
import eu.dnetlib.repo.manager.client.services.UserServiceAsync;
16
import eu.dnetlib.repo.manager.client.widgets.TextBox;
17
import eu.dnetlib.repo.manager.shared.UserAccessException;
18
import org.gwtbootstrap3.client.ui.Alert;
19
import org.gwtbootstrap3.client.ui.Anchor;
20
import org.gwtbootstrap3.client.ui.Form;
21
import org.gwtbootstrap3.client.ui.SubmitButton;
22
import org.gwtbootstrap3.client.ui.constants.AlertType;
23
import org.gwtbootstrap3.client.ui.constants.ButtonType;
24
import org.gwtbootstrap3.client.ui.html.Paragraph;
25

    
26
/**
27
 * Created by stefania on 12/2/15.
28
 */
29
public class RegisterPage {
30

    
31
    private static RegisterPage instance = null;
32

    
33
    private Form userRegistrationForm = new Form();
34

    
35
    private Alert successLabel = new Alert();
36
    private Alert errorLabel = new Alert();
37

    
38
    private TextBox username = new TextBox();
39
    private TextBox firstName = new TextBox();
40
    private TextBox lastName = new TextBox();
41
    private TextBox email = new TextBox();
42
    private TextBox institution = new TextBox();
43
    private PasswordTextBox password = new PasswordTextBox();
44
    private PasswordTextBox confirmPassword = new PasswordTextBox();
45

    
46
    private SubmitButton register = new SubmitButton();
47

    
48
    private Paragraph paragraph = new Paragraph();
49
    private Anchor login = new Anchor();
50

    
51
    private UserServiceAsync userService = GWT.create(UserService.class);
52

    
53
    private RegisterPage() {
54

    
55
        successLabel.setType(AlertType.SUCCESS);
56
        successLabel.setVisible(false);
57
        successLabel.setDismissable(false);
58
        userRegistrationForm.add(successLabel);
59

    
60
        errorLabel.setType(AlertType.DANGER);
61
        errorLabel.setVisible(false);
62
        errorLabel.setDismissable(false);
63
        userRegistrationForm.add(errorLabel);
64

    
65
        userRegistrationForm.addStyleName("m-t");
66

    
67
        username.setPlaceholder("Username (*)");
68
        username.addStyleName("form-control");
69
        userRegistrationForm.add(new MyFormGroup(false, null, username));
70

    
71
        firstName.setPlaceholder("First Name (*)");
72
        firstName.addStyleName("form-control");
73
        userRegistrationForm.add(new MyFormGroup(false, null, firstName));
74

    
75
        lastName.setPlaceholder("Last Name (*)");
76
        lastName.addStyleName("form-control");
77
        userRegistrationForm.add(new MyFormGroup(false, null, lastName));
78

    
79
        email.setPlaceholder("Email (*)");
80
        email.addStyleName("form-control");
81
        userRegistrationForm.add(new MyFormGroup(false, null, email));
82

    
83
        institution.setPlaceholder("Institution");
84
        institution.addStyleName("form-control");
85
        userRegistrationForm.add(new MyFormGroup(false, null, institution));
86

    
87
        password.getElement().setPropertyString("placeholder", "Password (*)");
88
        password.addStyleName("form-control");
89
        userRegistrationForm.add(new MyFormGroup(false, null, password));
90

    
91
        confirmPassword.getElement().setPropertyString("placeholder", "Re-enter password (*)");
92
        confirmPassword.addStyleName("form-control");
93
        userRegistrationForm.add(new MyFormGroup(false, null, confirmPassword));
94

    
95
        register.setType(ButtonType.PRIMARY);
96
        register.setText("Register");
97
        register.addStyleName("block full-width m-b");
98
        register.addClickHandler(new ClickHandler() {
99

    
100
            @Override
101
            public void onClick(ClickEvent event) {
102

    
103
                errorLabel.setVisible(false);
104
                successLabel.setVisible(false);
105

    
106
                if(username.getValue().trim().isEmpty() || firstName.getValue().trim().isEmpty()
107
                        || lastName.getValue().trim().isEmpty() || email.getValue().trim().isEmpty()
108
                        || password.getValue().trim().isEmpty() || confirmPassword.getValue().trim().isEmpty()) {
109

    
110
                    errorLabel.setText("All asterisk (*) fields are required");
111
                    errorLabel.setVisible(true);
112

    
113
                } else if(!password.getValue().equals(confirmPassword.getValue())) {
114

    
115
                    errorLabel.setText("Password fields do not match");
116
                    errorLabel.setVisible(true);
117

    
118
                } else {
119

    
120
                    UserProfile userProfile = new UserProfile();
121
                    userProfile.setFirstname(firstName.getValue());
122
                    userProfile.setLastname(lastName.getValue());
123
                    userProfile.setUsername(username.getValue());
124
                    userProfile.setEmail(email.getValue());
125
                    userProfile.setPassword(password.getValue());
126
                    if(institution.getValue()!=null && !institution.getValue().isEmpty())
127
                        userProfile.setInstitution(institution.getValue());
128

    
129
                    final HTML loadingWheel = new HTML("<div class=\"loader-small\"></div><div class=\"whiteFilm\"></div>");
130
                    userRegistrationForm.addStyleName("loading");
131
                    userRegistrationForm.add(loadingWheel);
132

    
133
                    userService.register(userProfile, new AsyncCallback<Void>() {
134

    
135
                        @Override
136
                        public void onFailure(Throwable throwable) {
137

    
138
                            userRegistrationForm.removeStyleName("loading");
139
                            userRegistrationForm.remove(loadingWheel);
140

    
141
                            if(throwable instanceof UserAccessException) {
142
                                UserAccessException uae = (UserAccessException) throwable;
143
                                errorLabel.setText(uae.getMessage());
144
                                errorLabel.setVisible(true);
145
                            } else {
146
                                errorLabel.setText("Registration failed - Something went wrong, please try again.");
147
                                errorLabel.setVisible(true);
148
                            }
149
                        }
150

    
151
                        @Override
152
                        public void onSuccess(Void aVoid) {
153

    
154
                            userRegistrationForm.removeStyleName("loading");
155
                            userRegistrationForm.remove(loadingWheel);
156

    
157
                            successLabel.setText("An activation e-mail has been sent to the e-mail " +
158
                                    "address you specified. Please follow the link included in that e-mail to activate your account.");
159
                            successLabel.setVisible(true);
160
                        }
161
                    });
162
                }
163
            }
164
        });
165
        userRegistrationForm.add(register);
166

    
167
        paragraph.setHTML("<small>Do not have an account?</small>");
168
        paragraph.addStyleName("text-muted text-center");
169
        userRegistrationForm.add(paragraph);
170

    
171
        login.setHref("#login");
172
        login.setText("Already have an account?");
173
        login.addStyleName("btn btn-sm btn-white btn-block");
174
        userRegistrationForm.add(login);
175

    
176
        RootPanel.get("registerForm").add(userRegistrationForm);
177
    }
178

    
179
    public static final RegisterPage getInstance() {
180

    
181
        if(instance==null)
182
            instance = new RegisterPage();
183

    
184
        return instance;
185
    }
186

    
187
    public void showRegisterPage() {
188

    
189
        Document.get().getElementById("landingPage").getStyle().setDisplay(Style.Display.NONE);
190
        Document.get().getElementById("login").getStyle().setDisplay(Style.Display.NONE);
191
        Document.get().getElementById("wrapper").getStyle().setDisplay(Style.Display.NONE);
192
        Document.get().getElementById("register").getStyle().setDisplay(Style.Display.BLOCK);
193

    
194
//        Document.get().getBody().removeClassName("landing-page");
195
//        Document.get().getBody().addClassName("gray-bg");
196
    }
197
}
(7-7/12)