Project

General

Profile

1
package eu.dnetlib.client;
2

    
3
import com.claudiushauptmann.gwt.recaptcha.client.RecaptchaWidget;
4
import com.github.gwtbootstrap.client.ui.Alert;
5
import com.github.gwtbootstrap.client.ui.Form;
6
import com.github.gwtbootstrap.client.ui.SubmitButton;
7
import com.github.gwtbootstrap.client.ui.TextArea;
8
import com.github.gwtbootstrap.client.ui.constants.*;
9
import com.google.gwt.core.client.GWT;
10
import com.google.gwt.event.dom.client.ClickEvent;
11
import com.google.gwt.event.dom.client.ClickHandler;
12
import com.google.gwt.user.client.Window;
13
import com.google.gwt.user.client.rpc.AsyncCallback;
14
import com.google.gwt.user.client.ui.*;
15
import eu.dnetlib.client.widgets.FormFieldSet;
16
import eu.dnetlib.client.widgets.TextBox;
17
import eu.dnetlib.goldoa.domain.Contact;
18

    
19
/**
20
 * Created by stefania on 4/2/15.
21
 */
22
public class ContactForm implements IsWidget {
23

    
24
    private FlowPanel contactFormPanel = new FlowPanel();
25

    
26
    private Alert errorLabel = new Alert();
27

    
28
    private Label label = new Label();
29

    
30
    private Form contactForm = new Form();
31

    
32
    private TextBox firstNameTextBox = new TextBox();
33
    private TextBox lastNameTextBox = new TextBox();
34
    private TextBox initialsTextBox = new TextBox();
35
    private FormFieldSet nameFormFieldSet = new FormFieldSet("Name (*)", firstNameTextBox, initialsTextBox, lastNameTextBox);
36

    
37
    private TextBox emailTextBox = new TextBox();
38
    private FormFieldSet emailFormFieldSet = new FormFieldSet("E-mail Address (*)", emailTextBox);
39

    
40
    private TextBox subjectTextBox = new TextBox();
41
    private FormFieldSet subjectFormFieldSet = new FormFieldSet("Subject (*)", subjectTextBox);
42

    
43
    private TextArea messageTextArea = new TextArea();
44
    private FormFieldSet messageFormFieldSet = new FormFieldSet("Message (*)", messageTextArea);
45

    
46
    private RecaptchaWidget captchaWidget;
47

    
48
    private SubmitButton submitButton = new SubmitButton();
49

    
50
    private ContactSubmittedListener contactSubmittedListener;
51

    
52
    private DataServiceAsync dataService = GWT.create(DataService.class);
53
    private GoldOAConstants goldOAConstants = GWT.create(GoldOAConstants.class);
54

    
55
    public ContactForm() {
56

    
57
        contactFormPanel.add(label);
58
        contactFormPanel.add(errorLabel);
59

    
60
        errorLabel.addStyleName("alertLabel");
61
        errorLabel.setType(AlertType.ERROR);
62
        errorLabel.setVisible(false);
63
        errorLabel.setClose(false);
64

    
65
        contactForm.setType(FormType.HORIZONTAL);
66
        contactFormPanel.add(contactForm);
67

    
68
        firstNameTextBox.setAlternateSize(AlternateSize.MEDIUM);
69
        firstNameTextBox.setPlaceholder("First name (*)");
70

    
71
        lastNameTextBox.setAlternateSize(AlternateSize.MEDIUM);
72
        lastNameTextBox.setPlaceholder("Last name (*)");
73

    
74
        initialsTextBox.setAlternateSize(AlternateSize.MINI);
75
        initialsTextBox.setPlaceholder("Initials");
76
        contactForm.add(nameFormFieldSet);
77

    
78
        emailTextBox.setAlternateSize(AlternateSize.XXLARGE);
79
        contactForm.add(emailFormFieldSet);
80

    
81
        subjectTextBox.setAlternateSize(AlternateSize.XXLARGE);
82
        contactForm.add(subjectFormFieldSet);
83

    
84
        messageTextArea.setAlternateSize(AlternateSize.XXLARGE);
85
        contactForm.add(messageFormFieldSet);
86

    
87
        captchaWidget = new RecaptchaWidget(GoldOAPortal.publicCaptchaKey, "en", "clean");
88
        contactForm.add(new FormFieldSet(null, captchaWidget));
89

    
90
        submitButton.setText("Submit");
91
        submitButton.setType(ButtonType.PRIMARY);
92
        submitButton.addClickHandler(new ClickHandler() {
93
            @Override
94
            public void onClick(ClickEvent clickEvent) {
95

    
96
                errorLabel.setVisible(false);
97

    
98
                if(isComplete()) {
99

    
100
                    if(isCaptchaCompleted()) {
101

    
102
                        final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
103
                        contactFormPanel.addStyleName("loading");
104
                        contactFormPanel.add(loadingWheel);
105

    
106
                        dataService.submitContactForm(getContact(), captchaWidget.getChallenge(), captchaWidget.getResponse(),
107
                                new AsyncCallback<Void>() {
108

    
109
                            @Override
110
                            public void onFailure(Throwable throwable) {
111

    
112
                                contactFormPanel.remove(loadingWheel);
113
                                contactFormPanel.removeStyleName("loading");
114

    
115
                                if(throwable.getMessage().equals("captcha")) {
116
                                    errorLabel.setText(goldOAConstants.errorContactFormIncorrectCaptcha());
117
                                    captchaWidget.reload();
118
                                } else {
119
                                    errorLabel.setText(goldOAConstants.errorSubmittingContactForm());
120
                                }
121
                                errorLabel.setVisible(true);
122
                            }
123

    
124
                            @Override
125
                            public void onSuccess(Void aVoid) {
126

    
127
                                contactFormPanel.remove(loadingWheel);
128
                                contactFormPanel.removeStyleName("loading");
129

    
130
                                if (contactSubmittedListener != null)
131
                                    contactSubmittedListener.contactSubmitted();
132
                            }
133
                        });
134
                    } else {
135
                        errorLabel.setText(goldOAConstants.errorContactFormMissingCaptcha());
136
                        errorLabel.setVisible(true);
137
                    }
138
                } else {
139
                    errorLabel.setText(goldOAConstants.errorContactFormAllFieldsRequired());
140
                    errorLabel.setVisible(true);
141
                }
142
            }
143
        });
144
        contactForm.add(new FormFieldSet(null, submitButton));
145

    
146
        if(GoldOAPortal.currentUser!=null)
147
            preLoadContactForm();
148
    }
149

    
150
    @Override
151
    public Widget asWidget() {
152
        return contactFormPanel;
153
    }
154

    
155
    public interface ContactSubmittedListener {
156
        public void contactSubmitted();
157
    }
158

    
159
    public void setContactSubmittedListener(ContactSubmittedListener contactSubmittedListener) {
160
        this.contactSubmittedListener = contactSubmittedListener;
161
    }
162

    
163
    private Contact getContact() {
164

    
165
        Contact contact = new Contact();
166

    
167
        contact.setFirstName(firstNameTextBox.getValue().trim());
168
        contact.setInitials(initialsTextBox.getValue().trim());
169
        contact.setLastName(lastNameTextBox.getValue().trim());
170
        contact.setEmail(emailTextBox.getValue().trim());
171
        contact.setSubject(subjectTextBox.getValue().trim());
172
        contact.setMessage(messageTextArea.getValue().trim());
173

    
174
        return contact;
175
    }
176

    
177
    private void preLoadContactForm() {
178

    
179
        firstNameTextBox.setValue(GoldOAPortal.currentUser.getFirstname());
180
        initialsTextBox.setValue(GoldOAPortal.currentUser.getInitials());
181
        lastNameTextBox.setValue(GoldOAPortal.currentUser.getLastname());
182
        emailTextBox.setValue(GoldOAPortal.currentUser.getEmail());
183
    }
184

    
185
    private boolean isComplete() {
186

    
187
        if(!firstNameTextBox.getValue().trim().isEmpty() && !lastNameTextBox.getValue().trim().isEmpty()
188
                && !emailTextBox.getValue().trim().isEmpty() && !subjectTextBox.getValue().trim().isEmpty()
189
                && !messageTextArea.getValue().trim().isEmpty()) {
190
            return true;
191
        }
192

    
193
        return false;
194
    }
195

    
196
    private boolean isCaptchaCompleted() {
197

    
198
        if(captchaWidget.getResponse()!=null && !captchaWidget.getResponse().equals(""))
199
            return true;
200

    
201
        return  false;
202
    }
203
}
(3-3/22)