Project

General

Profile

1
package eu.dnetlib.client;
2

    
3
import com.github.gwtbootstrap.client.ui.Alert;
4
import com.github.gwtbootstrap.client.ui.Button;
5
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
import com.google.gwt.event.dom.client.ClickEvent;
13
import com.google.gwt.event.dom.client.ClickHandler;
14
import com.google.gwt.user.client.rpc.AsyncCallback;
15
import com.google.gwt.user.client.ui.*;
16
import eu.dnetlib.client.widgets.FormFieldSet;
17
import eu.dnetlib.client.widgets.TextBox;
18
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
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
    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
    private TextBox requestIdHidden = new TextBox();
38
    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

    
43
    private FlowPanel accountingInfoPanel = new FlowPanel();
44
    private Label accountingInfoLabel = new Label();
45
    private Alert accountingInfoErrorLabel = new Alert();
46
    private Form accountingInfoForm = new Form();
47
    private TextBox apcTextBox = new TextBox();
48
    private TextBox goldDiscountTextBox = new TextBox();
49
    private TextBox projectParticipationTextBox = new TextBox();
50
    private TextBox fundingRequestedTextBox = new TextBox();
51

    
52
    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
    private FundingWizardStepCompleteListener fundingWizardStepCompleteListener;
59

    
60
    public AccountingStepWidget(String title) {
61
        super(title);
62

    
63
        accountingStepPanel.addStyleName("accountingForm");
64

    
65
        uploadInvoiceLabel.addStyleName("uploadInvoiceLabel");
66
        uploadInvoiceLabel.setText("Upload your invoice");
67

    
68
        uploadInvoiceSuccessLabel.addStyleName("alertLabel");
69
        uploadInvoiceSuccessLabel.setType(AlertType.SUCCESS);
70
        uploadInvoiceSuccessLabel.setVisible(false);
71
        uploadInvoiceSuccessLabel.setClose(false);
72

    
73
        UploadInvoiceErrorLabel.addStyleName("alertLabel");
74
        UploadInvoiceErrorLabel.setType(AlertType.ERROR);
75
        UploadInvoiceErrorLabel.setVisible(false);
76
        UploadInvoiceErrorLabel.setClose(false);
77

    
78
        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
        requestIdHidden.setVisible(false);
90
        requestIdHidden.setName("requestId");
91
        uploadInvoiceForm.add(new FormFieldSet(null, requestIdHidden));
92

    
93
        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
                }
118
            }
119
        });
120
        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
            }
132
        });
133

    
134

    
135
        accountingInfoLabel.addStyleName("accountingInformationLabel");
136
        accountingInfoLabel.setText("Accounting Information");
137

    
138
        accountingInfoErrorLabel.addStyleName("alertLabel");
139
        accountingInfoErrorLabel.setType(AlertType.ERROR);
140
        accountingInfoErrorLabel.setVisible(false);
141
        accountingInfoErrorLabel.setClose(false);
142

    
143
        accountingInfoPanel.addStyleName("accountingInformationPanel");
144
        accountingInfoPanel.add(accountingInfoLabel);
145
        accountingInfoPanel.add(accountingInfoErrorLabel);
146

    
147
        accountingInfoForm.setType(FormType.HORIZONTAL);
148
        accountingInfoPanel.add(accountingInfoForm);
149

    
150
        apcTextBox.setAlternateSize(AlternateSize.LARGE);
151
        apcTextBox.setEnabled(false);
152
        accountingInfoForm.add(new FormFieldSet("APC", apcTextBox));
153

    
154
        goldDiscountTextBox.setAlternateSize(AlternateSize.LARGE);
155
        goldDiscountTextBox.setEnabled(false);
156
        accountingInfoForm.add(new FormFieldSet("OpenAIRE Gold Discount", goldDiscountTextBox));
157

    
158

    
159
        Label percentageLabel = new Label("  %");
160
        percentageLabel.setStyleName("inlineBlock");
161

    
162
        projectParticipationTextBox.setAlternateSize(AlternateSize.LARGE);
163
        projectParticipationTextBox.addStyleName("inlineBlock");
164
        projectParticipationTextBox.setValueChangeHandler(new ValueChangeHandler() {
165

    
166
            @Override
167
            public void handle(ValueChangeEvent valueChangeEvent) {
168

    
169
                accountingInfoErrorLabel.setVisible(false);
170
                String percentage = projectParticipationTextBox.getValue().trim();
171

    
172
                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
        accountingInfoForm.add(new FormFieldSet("Project participation", projectParticipationTextBox, percentageLabel));
186

    
187
        fundingRequestedTextBox.setAlternateSize(AlternateSize.LARGE);
188
        fundingRequestedTextBox.setEnabled(false);
189
        accountingInfoForm.add(new FormFieldSet("Funding Requested", fundingRequestedTextBox));
190

    
191

    
192
        accountingStepPanel.add(uploadInvoicePanel);
193
        accountingStepPanel.add(accountingInfoPanel);
194
    }
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
    public void save() {
208

    
209
    }
210

    
211
    @Override
212
    public void loadContent(FundingWizardState fundingWizardState) {
213

    
214
        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
    }
263

    
264
    @Override
265
    public void setFundingWizardStepCompleteListener(FundingWizardStepCompleteListener fundingWizardStepCompleteListener) {
266
        this.fundingWizardStepCompleteListener = fundingWizardStepCompleteListener;
267
    }
268

    
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
}
(1-1/33)