Project

General

Profile

1
package eu.dnetlib.client;
2

    
3
import com.github.gwtbootstrap.client.ui.*;
4
import com.github.gwtbootstrap.client.ui.Button;
5
import com.github.gwtbootstrap.client.ui.ListBox;
6
import com.github.gwtbootstrap.client.ui.SubmitButton;
7
import com.github.gwtbootstrap.client.ui.base.IconAnchor;
8
import com.github.gwtbootstrap.client.ui.constants.*;
9
import com.github.gwtbootstrap.datepicker.client.ui.DateBox;
10
import com.google.gwt.core.client.GWT;
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.Window;
14
import com.google.gwt.user.client.rpc.AsyncCallback;
15
import com.google.gwt.user.client.ui.*;
16
import com.google.gwt.user.client.ui.FileUpload;
17
import com.google.gwt.user.client.ui.Label;
18
import com.google.gwt.user.datepicker.client.CalendarUtil;
19
import eu.dnetlib.client.widgets.FormFieldSet;
20
import eu.dnetlib.client.widgets.TextBox;
21
import eu.dnetlib.goldoa.domain.*;
22

    
23
import java.util.Date;
24

    
25
/**
26
 * Created by stefania on 4/2/15.
27
 */
28
public class NewBudgetRequestWidget implements MyWidget {
29

    
30
    private String token = "";
31

    
32
    private FlowPanel newBudgetRequestPanel = new FlowPanel();
33

    
34
    private Label titleLabel = new Label();
35
    private Label informationLabel = new Label();
36

    
37
    private Alert informationAlert = new Alert();
38
    private Alert errorLabel = new Alert();
39

    
40
    private FlowPanel budgetFormPanel = new FlowPanel();
41

    
42
    private Form budgetForm = new Form();
43
    private ListBox institutions = new ListBox();
44
    private Label publisher = new Label();
45
    private TextBox budgetRequestedTextBox = new TextBox();
46
    private DateBox startDateDateBox = new DateBox();
47
    private DateBox endDateDateBox = new DateBox();
48

    
49
    private Label bankInfoLabel = new Label();
50
    private TextBox nameOfBank = new TextBox();
51
    private TextBox addressOfBank = new TextBox();
52
    private TextBox accountHolder = new TextBox();
53
    private TextBox accountNumber = new TextBox();
54
    private TextBox bankCode = new TextBox();
55

    
56
    private Button submit = new Button("Submit");
57

    
58
    private HTML loadingWheel = new HTML("<div class=\"loader\"></div><div class=\"whiteFilm\"></div>");
59

    
60
    private DataServiceAsync dataService = GWT.create(DataService.class);
61

    
62
    public NewBudgetRequestWidget() {
63

    
64
        newBudgetRequestPanel.addStyleName("content");
65

    
66
        titleLabel.setText("New Budget Request");
67
        titleLabel.addStyleName("contentTitleLabel");
68

    
69
        informationLabel.setText("Request a new budget for an institution that you are affiliated with.");
70
        informationLabel.addStyleName("contentInfoLabel");
71

    
72
        informationAlert.addStyleName("alertLabel");
73
        informationAlert.setType(AlertType.SUCCESS);
74
        informationAlert.setClose(false);
75
        informationAlert.setVisible(false);
76

    
77
        errorLabel.addStyleName("alertLabel");
78
        errorLabel.setType(AlertType.ERROR);
79
        errorLabel.setClose(false);
80
        errorLabel.setVisible(false);
81

    
82
        budgetForm.setType(FormType.HORIZONTAL);
83
        budgetForm.addStyleName("budgetForm");
84

    
85
        budgetFormPanel.add(budgetForm);
86

    
87
        if(Utils.currentUserHasRoleApproved("publisher") && GoldOAPortal.currentUser.getPublisher()!=null) {
88
            publisher.setText(GoldOAPortal.currentUser.getPublisher().getName());
89
            budgetForm.add(new FormFieldSet("Publisher (*)", publisher));
90
        } else {
91
            for (Affiliation affiliation : GoldOAPortal.currentUser.getAffiliations()) {
92
                Organization organization = affiliation.getOrganization();
93
                if (organization != null)
94
                    institutions.addItem(organization.getName(), organization.getId());
95
            }
96
            institutions.setAlternateSize(AlternateSize.XXLARGE);
97
            budgetForm.add(new FormFieldSet("Organization (*)", institutions));
98
        }
99

    
100
        Label budgetLabel = new Label("Euros");
101
        budgetLabel.addStyleName("inlineBlock");
102

    
103
        budgetRequestedTextBox.setAlternateSize(AlternateSize.XLARGE);
104
        budgetRequestedTextBox.addStyleName("inlineBlock");
105
        budgetForm.add(new FormFieldSet("Budget Requested (*)", budgetRequestedTextBox, budgetLabel));
106

    
107
        startDateDateBox.setValue(new Date());
108
        startDateDateBox.setFormat("yyyy/mm/dd");
109
        budgetForm.add(new FormFieldSet("Start Date (*)", startDateDateBox));
110

    
111
        Date endDate = new Date();
112
        CalendarUtil.addMonthsToDate(endDate, 6);
113
        endDateDateBox.setValue(endDate);
114
        endDateDateBox.setFormat("yyyy/mm/dd");
115
        budgetForm.add(new FormFieldSet("End Date (*)", endDateDateBox));
116

    
117
        bankInfoLabel.setText("Bank Information");
118
        bankInfoLabel.addStyleName("strong");
119
        budgetForm.add(new FormFieldSet(null, bankInfoLabel));
120

    
121
        nameOfBank.setAlternateSize(AlternateSize.XLARGE);
122
        budgetForm.add(new FormFieldSet("Name of bank (*)", nameOfBank));
123

    
124
        addressOfBank.setAlternateSize(AlternateSize.XLARGE);
125
        budgetForm.add(new FormFieldSet("Address of bank (*)", addressOfBank));
126

    
127
        accountHolder.setAlternateSize(AlternateSize.XLARGE);
128
        budgetForm.add(new FormFieldSet("Account holder (*)", accountHolder));
129

    
130
        accountNumber.setAlternateSize(AlternateSize.XLARGE);
131
        budgetForm.add(new FormFieldSet("Account number (IBAN format) (*)", accountNumber));
132

    
133
        bankCode.setAlternateSize(AlternateSize.XLARGE);
134
        budgetForm.add(new FormFieldSet("Bank code (BIC) / SWIFT code (*)", bankCode));
135

    
136
        submit.setType(ButtonType.PRIMARY);
137
        submit.addClickHandler(new ClickHandler() {
138

    
139
            @Override
140
            public void onClick(ClickEvent clickEvent) {
141

    
142
                errorLabel.setVisible(false);
143
                informationAlert.setVisible(false);
144

    
145
                if(isComplete()) {
146

    
147
                    if(isPositiveFloat(budgetRequestedTextBox.getValue().trim())) {
148

    
149
                        BankAccount bankAccount = new BankAccount();
150
                        bankAccount.setAccountHolder(accountHolder.getValue().trim());
151
                        bankAccount.setBankAddress(addressOfBank.getValue().trim());
152
                        bankAccount.setBankCode(bankCode.getValue().trim());
153
                        bankAccount.setBankName(nameOfBank.getValue().trim());
154
                        bankAccount.setIban(accountNumber.getValue().trim());
155

    
156
                        final Budget budget = new Budget();
157
                        budget.setUser(GoldOAPortal.currentUser);
158
                        budget.setBankAccount(bankAccount);
159
                        budget.setAmountRequested(Float.parseFloat(budgetRequestedTextBox.getValue().trim()));
160

    
161
                        if(Utils.currentUserHasRoleApproved("publisher") && GoldOAPortal.currentUser.getPublisher()!=null) {
162
                            budget.setPublisher(GoldOAPortal.currentUser.getPublisher());
163
                        } else {
164
                            dataService.getOrganization(institutions.getSelectedValue(), new AsyncCallback<Organization>() {
165
                                @Override
166
                                public void onFailure(Throwable throwable) {
167

    
168
                                }
169

    
170
                                @Override
171
                                public void onSuccess(Organization organization) {
172
                                    budget.setOrganization(organization);
173
                                }
174
                            });
175
                        }
176

    
177
                        budget.setStartdate(startDateDateBox.getValue());
178
                        budget.setEnddate(endDateDateBox.getValue());
179
                        budget.setDate(new Date());
180
                        budget.setCurrency(Currency.EUR);
181

    
182
                        budgetFormPanel.addStyleName("loading-small");
183
                        budgetFormPanel.add(loadingWheel);
184

    
185
                        dataService.submitBudgetRequest(budget, new AsyncCallback<Budget>() {
186

    
187
                            @Override
188
                            public void onFailure(Throwable caught) {
189

    
190
                                budgetFormPanel.removeStyleName("loading-small");
191
                                budgetFormPanel.remove(loadingWheel);
192

    
193
                                errorLabel.setText("System error submitting budget request");
194
                                errorLabel.setVisible(true);
195
                            }
196

    
197
                            @Override
198
                            public void onSuccess(Budget result) {
199

    
200
                                budgetFormPanel.removeStyleName("loading-small");
201
                                budgetFormPanel.remove(loadingWheel);
202

    
203
                                informationAlert.setText("Budget request was submitted successfully");
204
                                informationAlert.setVisible(true);
205

    
206
                                budgetFormPanel.remove(budgetForm);
207
                            }
208
                        });
209

    
210
                    } else {
211
                        errorLabel.setText("Budget must be a number greater than 0.");
212
                        errorLabel.setVisible(true);
213
                    }
214
                } else {
215
                    errorLabel.setText("All asterisk (*) fields are required.");
216
                    errorLabel.setVisible(true);
217
                }
218
            }
219
        });
220
        budgetForm.add(new FormFieldSet(null, submit));
221

    
222
        newBudgetRequestPanel.add(titleLabel);
223
        newBudgetRequestPanel.add(informationLabel);
224
        newBudgetRequestPanel.add(informationAlert);
225
        newBudgetRequestPanel.add(errorLabel);
226
        newBudgetRequestPanel.add(budgetFormPanel);
227
    }
228

    
229
    @Override
230
    public Widget asWidget() {
231
        return newBudgetRequestPanel;
232
    }
233

    
234
    @Override
235
    public void clear() {
236

    
237
        errorLabel.setVisible(false);
238
        informationAlert.setVisible(false);
239

    
240
        budgetRequestedTextBox.setValue("");
241
        institutions.clear();
242

    
243
        budgetForm.reset();
244
    }
245

    
246
    @Override
247
    public void reload() {
248

    
249
        MyWidgetHelper.hideSidebar();
250

    
251
        SidebarPanel helpPanel = new SidebarPanel("Help");
252
        MyWidgetHelper.loadHelp(helpPanel, token.split("\\.")[0]);
253

    
254
        for(Affiliation affiliation : GoldOAPortal.currentUser.getAffiliations()) {
255
            Organization organization = affiliation.getOrganization();
256
            if(organization!=null)
257
                institutions.addItem(organization.getName(), organization.getId());
258
        }
259

    
260
        startDateDateBox.setValue(new Date());
261
        Date endDate = new Date();
262
        CalendarUtil.addMonthsToDate(endDate, 6);
263
        endDateDateBox.setValue(endDate);
264
    }
265

    
266
    @Override
267
    public void setToken(String token) {
268
        this.token = token;
269
    }
270

    
271
    @Override
272
    public void afterAdditionToRootPanel() {
273

    
274
    }
275

    
276
    private boolean isComplete() {
277

    
278
        if(!budgetRequestedTextBox.getValue().trim().equals("")
279
                && !nameOfBank.getValue().trim().equals("") && !addressOfBank.getValue().trim().equals("")
280
                && !accountHolder.getValue().trim().equals("") && !accountNumber.getValue().trim().equals("")
281
                && !bankCode.getValue().trim().equals(""))
282
            return true;
283

    
284
        return false;
285
    }
286

    
287
    private boolean isPositiveFloat(String number) {
288

    
289
        try {
290
            Float numberValue = Float.parseFloat(number);
291
            if(numberValue > 0)
292
                return true;
293
        } catch(NumberFormatException nfe) {
294
            return false;
295
        }
296

    
297
        return false;
298
    }
299
}
(17-17/22)