Project

General

Profile

1 35292 stefania.m
package eu.dnetlib.client;
2
3 35515 stefania.m
import com.github.gwtbootstrap.client.ui.Alert;
4 35292 stefania.m
import com.github.gwtbootstrap.client.ui.Button;
5 35515 stefania.m
import com.github.gwtbootstrap.client.ui.Form;
6
import com.github.gwtbootstrap.client.ui.constants.AlertType;
7
import com.github.gwtbootstrap.client.ui.constants.AlternateSize;
8
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
9
import com.github.gwtbootstrap.client.ui.constants.FormType;
10
import com.github.gwtbootstrap.datepicker.client.ui.DateBox;
11
import com.google.gwt.core.client.GWT;
12 35292 stefania.m
import com.google.gwt.event.dom.client.ClickEvent;
13
import com.google.gwt.event.dom.client.ClickHandler;
14 35520 stefania.m
import com.google.gwt.user.client.rpc.AsyncCallback;
15 35515 stefania.m
import com.google.gwt.user.client.ui.*;
16
import eu.dnetlib.client.widgets.FormFieldSet;
17
import eu.dnetlib.client.widgets.TextBox;
18 35520 stefania.m
import eu.dnetlib.client.widgets.ValueChangeEvent;
19
import eu.dnetlib.client.widgets.ValueChangeHandler;
20
import eu.dnetlib.goldoa.domain.Journal;
21
import eu.dnetlib.goldoa.domain.Publisher;
22
import eu.dnetlib.goldoa.domain.Request;
23 35292 stefania.m
import eu.dnetlib.shared.FundingWizardState;
24
25
/**
26
 * Created by stefania on 3/9/15.
27
 */
28
public class AccountingStepWidget extends FundingWizardStepWidget {
29
30
    private FlowPanel accountingStepPanel = new FlowPanel();
31
32 35515 stefania.m
    private FlowPanel uploadInvoicePanel = new FlowPanel();
33
    private Label uploadInvoiceLabel = new Label();
34
    private Alert UploadInvoiceErrorLabel = new Alert();
35
    private Alert uploadInvoiceSuccessLabel = new Alert();
36
    private Form uploadInvoiceForm = new Form();
37 35520 stefania.m
    private TextBox requestIdHidden = new TextBox();
38 35515 stefania.m
    private TextBox invoiceNumber = new TextBox();
39
    private DateBox dateIssued = new DateBox();
40
    private FileUpload fileUpload = new FileUpload();
41
    private Button upload = new Button("Upload");
42 35292 stefania.m
43 35515 stefania.m
    private FlowPanel accountingInfoPanel = new FlowPanel();
44
    private Label accountingInfoLabel = new Label();
45
    private Alert accountingInfoErrorLabel = new Alert();
46
    private Form accountingInfoForm = new Form();
47 35520 stefania.m
    private TextBox apcTextBox = new TextBox();
48
    private TextBox goldDiscountTextBox = new TextBox();
49
    private TextBox projectParticipationTextBox = new TextBox();
50
    private TextBox fundingRequestedTextBox = new TextBox();
51 35515 stefania.m
52 35520 stefania.m
    private float apc = 0;
53
    private float goldDiscount = 0;
54
    private float fundingRequested = 0;
55
56
    private DataServiceAsync dataService = GWT.create(DataService.class);
57
58 35347 stefania.m
    private FundingWizardStepCompleteListener fundingWizardStepCompleteListener;
59 35292 stefania.m
60
    public AccountingStepWidget(String title) {
61
        super(title);
62
63
        accountingStepPanel.addStyleName("accountingForm");
64
65 35515 stefania.m
        uploadInvoiceLabel.addStyleName("uploadInvoiceLabel");
66
        uploadInvoiceLabel.setText("Upload your invoice");
67 35292 stefania.m
68 35515 stefania.m
        uploadInvoiceSuccessLabel.addStyleName("alertLabel");
69
        uploadInvoiceSuccessLabel.setType(AlertType.SUCCESS);
70
        uploadInvoiceSuccessLabel.setVisible(false);
71
        uploadInvoiceSuccessLabel.setClose(false);
72 35292 stefania.m
73 35515 stefania.m
        UploadInvoiceErrorLabel.addStyleName("alertLabel");
74
        UploadInvoiceErrorLabel.setType(AlertType.ERROR);
75
        UploadInvoiceErrorLabel.setVisible(false);
76
        UploadInvoiceErrorLabel.setClose(false);
77 35292 stefania.m
78 35515 stefania.m
        uploadInvoicePanel.addStyleName("uploadInvoicePanel");
79
        uploadInvoicePanel.add(uploadInvoiceLabel);
80
        uploadInvoicePanel.add(uploadInvoiceSuccessLabel);
81
        uploadInvoicePanel.add(UploadInvoiceErrorLabel);
82
83
        uploadInvoiceForm.setType(FormType.HORIZONTAL);
84
        uploadInvoiceForm.setAction(GWT.getModuleBaseURL() + "fileupload");
85
        uploadInvoiceForm.setEncoding(FormPanel.ENCODING_MULTIPART);
86
        uploadInvoiceForm.setMethod(FormPanel.METHOD_POST);
87
        uploadInvoicePanel.add(uploadInvoiceForm);
88
89 35520 stefania.m
        requestIdHidden.setVisible(false);
90
        requestIdHidden.setName("requestId");
91
        uploadInvoiceForm.add(new FormFieldSet(null, requestIdHidden));
92
93 35515 stefania.m
        invoiceNumber.setAlternateSize(AlternateSize.LARGE);
94
        invoiceNumber.setName("invoiceNumber");
95
        uploadInvoiceForm.add(new FormFieldSet("Invoice number (*)", invoiceNumber));
96
97
        dateIssued.setAlternateSize(AlternateSize.LARGE);
98
        dateIssued.setName("dateIssued");
99
        uploadInvoiceForm.add(new FormFieldSet("Date issued (*)", dateIssued));
100
101
        fileUpload.setName("fileUpload");
102
        uploadInvoiceForm.add(new FormFieldSet("Invoice (*)", fileUpload));
103
104
        upload.setType(ButtonType.SUCCESS);
105
        upload.addClickHandler(new ClickHandler() {
106
            @Override
107
            public void onClick(ClickEvent clickEvent) {
108
109
                uploadInvoiceSuccessLabel.setVisible(false);
110
                UploadInvoiceErrorLabel.setVisible(false);
111
112
                if(!invoiceNumber.getValue().trim().equals("") && fileUpload.getFilename()!=null && !fileUpload.getFilename().equals(""))
113
                    uploadInvoiceForm.submit();
114
                else {
115
                    UploadInvoiceErrorLabel.setText("All asterisk (*) fields are required.");
116
                    UploadInvoiceErrorLabel.setVisible(true);
117 35292 stefania.m
                }
118
            }
119
        });
120 35515 stefania.m
        uploadInvoiceForm.add(new FormFieldSet(null, upload));
121
122
        uploadInvoiceForm.addSubmitCompleteHandler(new Form.SubmitCompleteHandler() {
123
            @Override
124
            public void onSubmitComplete(Form.SubmitCompleteEvent submitCompleteEvent) {
125
126
                uploadInvoiceSuccessLabel.setVisible(false);
127
                UploadInvoiceErrorLabel.setVisible(false);
128
129
                uploadInvoiceSuccessLabel.setText("Upload completed successfully");
130
                uploadInvoiceSuccessLabel.setVisible(true);
131 35292 stefania.m
            }
132
        });
133
134 35515 stefania.m
135 35520 stefania.m
        accountingInfoLabel.addStyleName("accountingInformationLabel");
136 35515 stefania.m
        accountingInfoLabel.setText("Accounting Information");
137
138
        accountingInfoErrorLabel.addStyleName("alertLabel");
139
        accountingInfoErrorLabel.setType(AlertType.ERROR);
140
        accountingInfoErrorLabel.setVisible(false);
141
        accountingInfoErrorLabel.setClose(false);
142
143 35520 stefania.m
        accountingInfoPanel.addStyleName("accountingInformationPanel");
144 35515 stefania.m
        accountingInfoPanel.add(accountingInfoLabel);
145
        accountingInfoPanel.add(accountingInfoErrorLabel);
146
147
        accountingInfoForm.setType(FormType.HORIZONTAL);
148
        accountingInfoPanel.add(accountingInfoForm);
149
150 35520 stefania.m
        apcTextBox.setAlternateSize(AlternateSize.LARGE);
151
        apcTextBox.setEnabled(false);
152
        accountingInfoForm.add(new FormFieldSet("APC", apcTextBox));
153 35515 stefania.m
154 35520 stefania.m
        goldDiscountTextBox.setAlternateSize(AlternateSize.LARGE);
155
        goldDiscountTextBox.setEnabled(false);
156
        accountingInfoForm.add(new FormFieldSet("OpenAIRE Gold Discount", goldDiscountTextBox));
157 35515 stefania.m
158 35575 stefania.m
159
        Label percentageLabel = new Label("  %");
160
        percentageLabel.setStyleName("inlineBlock");
161
162 35520 stefania.m
        projectParticipationTextBox.setAlternateSize(AlternateSize.LARGE);
163 35575 stefania.m
        projectParticipationTextBox.addStyleName("inlineBlock");
164 35520 stefania.m
        projectParticipationTextBox.setValueChangeHandler(new ValueChangeHandler() {
165 35515 stefania.m
166 35520 stefania.m
            @Override
167
            public void handle(ValueChangeEvent valueChangeEvent) {
168 35515 stefania.m
169 35520 stefania.m
                accountingInfoErrorLabel.setVisible(false);
170
                String percentage = projectParticipationTextBox.getValue().trim();
171 35515 stefania.m
172 35520 stefania.m
                if(!percentage.equals("")) {
173
                    if (isPercentage(percentage)) {
174
175
                        fundingRequested = (apc / (1 + (goldDiscount / 100))) * (Float.parseFloat(percentage) / 100);
176
                        fundingRequestedTextBox.setValue(fundingRequested + "");
177
178
                    } else {
179
                        accountingInfoErrorLabel.setText("Project participation must be a number between 0 and 100");
180
                        accountingInfoErrorLabel.setVisible(true);
181
                    }
182
                }
183
            }
184
        });
185 35575 stefania.m
        accountingInfoForm.add(new FormFieldSet("Project participation", projectParticipationTextBox, percentageLabel));
186 35520 stefania.m
187
        fundingRequestedTextBox.setAlternateSize(AlternateSize.LARGE);
188
        fundingRequestedTextBox.setEnabled(false);
189
        accountingInfoForm.add(new FormFieldSet("Funding Requested", fundingRequestedTextBox));
190
191
192 35515 stefania.m
        accountingStepPanel.add(uploadInvoicePanel);
193
        accountingStepPanel.add(accountingInfoPanel);
194 35292 stefania.m
    }
195
196
    @Override
197
    public Widget asWidget() {
198
        return accountingStepPanel;
199
    }
200
201
    @Override
202
    public void updateState(FundingWizardState fundingWizardState) {
203
204
    }
205
206
    @Override
207 35347 stefania.m
    public void save() {
208 35292 stefania.m
209
    }
210
211 35347 stefania.m
    @Override
212 35515 stefania.m
    public void loadContent(FundingWizardState fundingWizardState) {
213
214 35520 stefania.m
        accountingInfoErrorLabel.setVisible(false);
215
216
        Request request = fundingWizardState.getRequest();
217
        requestIdHidden.setValue(request.getId());
218
219
        if(request.getJournal()!=null) {
220
221
            dataService.getJournal(request.getJournal(), new AsyncCallback<Journal>() {
222
223
                @Override
224
                public void onFailure(Throwable throwable) {
225
                    accountingInfoErrorLabel.setText("Failed to retrieve accounting information for the selected journal");
226
                    accountingInfoErrorLabel.setVisible(true);
227
                }
228
229
                @Override
230
                public void onSuccess(Journal journal) {
231
232
                    apcTextBox.setValue(journal.getApc() + "");
233
                    apc = journal.getApc();
234
235
                    goldDiscountTextBox.setValue(journal.getDiscount() + "");
236
                    goldDiscount = journal.getDiscount();
237
                }
238
            });
239
240
        } else if(request.getPublisher()!=null) {
241
242
            dataService.getPublisher(request.getPublisher(), new AsyncCallback<Publisher>() {
243
244
                @Override
245
                public void onFailure(Throwable throwable) {
246
                    accountingInfoErrorLabel.setText("Failed to retrieve accounting information for the selected publisher");
247
                    accountingInfoErrorLabel.setVisible(true);
248
                }
249
250
                @Override
251
                public void onSuccess(Publisher publisher) {
252
253
                    //TODO uncomment when Antonis updates publisher
254
//                    apcTextBox.setValue(publisher.getApc() + "");
255
//                    apc = publisher.getApc();
256
257
                    goldDiscountTextBox.setValue(publisher.getDiscount() + "");
258
                    goldDiscount = publisher.getDiscount();
259
                }
260
            });
261
        }
262 35515 stefania.m
    }
263
264
    @Override
265 35347 stefania.m
    public void setFundingWizardStepCompleteListener(FundingWizardStepCompleteListener fundingWizardStepCompleteListener) {
266
        this.fundingWizardStepCompleteListener = fundingWizardStepCompleteListener;
267
    }
268 35520 stefania.m
269
    private boolean isPercentage(String percentage) {
270
271
        try {
272
273
            Float percentageValue = Float.parseFloat(percentage);
274
            if(percentageValue>=0 && percentageValue<=100)
275
                return true;
276
277
        } catch(NumberFormatException nfe) {
278
            return false;
279
        }
280
281
        return false;
282
    }
283 35292 stefania.m
}