Project

General

Profile

1
package eu.dnetlib.client.monitor;
2

    
3
import com.github.gwtbootstrap.client.ui.Alert;
4
import com.github.gwtbootstrap.client.ui.Button;
5
import com.github.gwtbootstrap.client.ui.Modal;
6
import com.github.gwtbootstrap.client.ui.constants.AlertType;
7
import com.github.gwtbootstrap.client.ui.constants.BackdropType;
8
import com.github.gwtbootstrap.client.ui.constants.ButtonType;
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.rpc.AsyncCallback;
13
import com.google.gwt.user.client.ui.FlowPanel;
14
import com.google.gwt.user.client.ui.HTML;
15
import com.google.gwt.user.client.ui.IsWidget;
16
import com.google.gwt.user.client.ui.Widget;
17
import eu.dnetlib.client.DOIAndRepositoryForm;
18
import eu.dnetlib.client.DataService;
19
import eu.dnetlib.client.DataServiceAsync;
20
import eu.dnetlib.client.GoldOAPortal;
21
import eu.dnetlib.client.fundingrequest.stepinfo.*;
22
import eu.dnetlib.client.widgets.ConfirmActionModal;
23
import eu.dnetlib.goldoa.domain.Budget;
24
import eu.dnetlib.goldoa.domain.Comment;
25
import eu.dnetlib.goldoa.domain.CommentTemplate;
26

    
27
import java.sql.Timestamp;
28
import java.util.Date;
29

    
30
/**
31
 * Created by stefania on 5/5/16.
32
 */
33
public class BudgetInfoElement implements IsWidget {
34

    
35
    private FlowPanel budgetInfoPanel = new FlowPanel();
36

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

    
40
    private FlowPanel budgetSummaryPanel = new FlowPanel();
41
    private FlowPanel firstDivider = new FlowPanel();
42

    
43
    private FlowPanel actionButtons = new FlowPanel();
44
    private Button initialApprove = new Button("Initial Approve");
45
    private Button approve = new Button("Approve");
46
    private Button reject = new Button("Reject");
47

    
48
    private Button editRequest = new Button("Edit request");
49
    private Button uploadContract = new Button("Upload Contract");
50

    
51
    private FlowPanel secondDivider = new FlowPanel();
52

    
53
    private ActionListener actionListener;
54
    private DataServiceAsync dataService = GWT.create(DataService.class);
55

    
56
    public BudgetInfoElement(final Budget budget) {
57

    
58
        budgetInfoPanel.addStyleName("requestInfoPanel");
59

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

    
65
        errorLabel.addStyleName("alertLabel");
66
        errorLabel.setType(AlertType.ERROR);
67
        errorLabel.setVisible(false);
68
        errorLabel.setClose(false);
69

    
70
        budgetSummaryPanel.addStyleName("requestDetailedInfoPanel");
71

    
72
        if(budget.getUser()!=null) {
73
            ResearcherInlineInfo researcherInlineInfo = new ResearcherInlineInfo(budget.getUser());
74
            budgetSummaryPanel.add(researcherInlineInfo.asWidget());
75
        }
76

    
77
        if(budget.getPublisher()!=null) {
78
            PublisherInlineInfo publisherInlineInfo = new PublisherInlineInfo(budget.getPublisher());
79
            budgetSummaryPanel.add(publisherInlineInfo.asWidget());
80
        }
81

    
82
        if(budget.getOrganization()!=null) {
83
            OrganisationInlineInfo organisationInlineInfo = new OrganisationInlineInfo(budget.getOrganization());
84
            budgetSummaryPanel.add(organisationInlineInfo.asWidget());
85
        }
86

    
87
        BudgetDetailsInlineInfo budgetDetailsInlineInfo = new BudgetDetailsInlineInfo(budget);
88
        budgetSummaryPanel.add(budgetDetailsInlineInfo.asWidget());
89

    
90
        if(budget.getBankAccount()!=null &&
91
                ((budget.getBankAccount().getBankName()!=null && !budget.getBankAccount().getBankName().equals(""))
92
                        || (budget.getBankAccount().getBankAddress()!=null && !budget.getBankAccount().getBankAddress().equals(""))
93
                        || (budget.getBankAccount().getAccountHolder()!=null && !budget.getBankAccount().getAccountHolder().equals(""))
94
                        || (budget.getBankAccount().getIban()!=null && !budget.getBankAccount().getIban().equals(""))
95
                        || (budget.getBankAccount().getBankCode()!=null && !budget.getBankAccount().getBankCode().equals("")))) {
96
            BankAccountInlineInfo bankAccountInlineInfo = new BankAccountInlineInfo(budget.getBankAccount());
97
            budgetSummaryPanel.add(bankAccountInlineInfo.asWidget());
98
        }
99

    
100

    
101

    
102
        //        if(budgetInfo.getInvoice()!=null) {
103
//            InvoiceInlineInfo invoiceInlineInfo = new InvoiceInlineInfo(budgetInfo.getInvoice());
104
//            budgetSummaryPanel.add(invoiceInlineInfo.asWidget());
105
//        }
106

    
107
//        if (budgetInfo.getStatus().equals(Budget.Status.ACCOUNTING_PAID) && budgetInfo.getAmountGranted()!=null
108
//                && budgetInfo.getTransferCost()!=null && budgetInfo.getDatePaid()!=null) {
109
//            BankTransferInlineInfo bankTransferInlineInfo = new BankTransferInlineInfo(budgetInfo);
110
//            budgetSummaryPanel.add(bankTransferInlineInfo.asWidget());
111
//        }
112

    
113
        actionButtons.addStyleName("requestInfoActionButtons");
114

    
115
        initialApprove.addStyleName("requestInfoActionButton");
116
        initialApprove.setType(ButtonType.DEFAULT);
117
        initialApprove.addClickHandler(new ClickHandler() {
118

    
119
            @Override
120
            public void onClick(ClickEvent event) {
121

    
122
                errorLabel.setVisible(false);
123
                successLabel.setVisible(false);
124

    
125
                ConfirmActionModal confirmActionModal = new ConfirmActionModal("Initial Approve", null);
126
                ConfirmActionModal.ActionConfirmedListener actionConfirmedListener = new ConfirmActionModal.ActionConfirmedListener() {
127
                    @Override
128
                    public void actionConfirmed(final String comment, final String templateId) {
129

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

    
134
                        dataService.initiallyApproveBudgetRequest(budget.getId(), comment, GoldOAPortal.currentUser.getId(),
135
                                new AsyncCallback<Void>() {
136

    
137
                            @Override
138
                            public void onFailure(Throwable caught) {
139

    
140
                                budgetInfoPanel.removeStyleName("loading");
141
                                budgetInfoPanel.remove(loadingWheel);
142

    
143
                                errorLabel.setText("System error initial approving this budget request");
144
                                errorLabel.setVisible(true);
145
                                if(actionListener!=null) {
146
                                    //TODO fix comment template
147
                                    CommentTemplate ct = new CommentTemplate();
148
                                    /*ct.setId(templateId);
149
                                    ct.setComment(comment);*/
150
                                    Comment newComment = new Comment(GoldOAPortal.currentUser, new Timestamp(new Date().getTime()), comment, ct);
151
                                    actionListener.actionHappened(false, Budget.Status.INITIALLY_APPROVED, newComment);
152
                                }
153
                            }
154

    
155
                            @Override
156
                            public void onSuccess(Void result) {
157

    
158
                                budgetInfoPanel.removeStyleName("loading");
159
                                budgetInfoPanel.remove(loadingWheel);
160

    
161
                                successLabel.setText("Budget request initially approved successfully");
162
                                successLabel.setVisible(true);
163

    
164
                                //TODO remove initial approve button and add approve button
165
                                actionButtons.clear();
166
                                actionButtons.add(approve);
167
                                actionButtons.add(reject);
168
                                actionButtons.add(uploadContract);
169

    
170
                                if(actionListener!=null) {
171
                                    //TODO fix comment template
172
                                    CommentTemplate ct = new CommentTemplate();
173
                                    /*ct.setId(templateId);
174
                                    ct.setComment(comment);*/
175
                                    Comment newComment = new Comment(GoldOAPortal.currentUser, new Timestamp(new Date().getTime()), comment, ct);
176
                                    actionListener.actionHappened(true, Budget.Status.INITIALLY_APPROVED, newComment);
177
                                }
178
                            }
179
                        });
180
                    }
181
                };
182
                confirmActionModal.setActionConfirmedListener(actionConfirmedListener);
183
                confirmActionModal.show();
184
            }
185
        });
186

    
187
        approve.addStyleName("requestInfoActionButton");
188
        approve.setType(ButtonType.DEFAULT);
189
        approve.addClickHandler(new ClickHandler() {
190

    
191
            @Override
192
            public void onClick(ClickEvent event) {
193

    
194
            }
195
        });
196

    
197
        reject.addStyleName("requestInfoActionButton");
198
        reject.setType(ButtonType.DEFAULT);
199
        reject.addClickHandler(new ClickHandler() {
200

    
201
            @Override
202
            public void onClick(ClickEvent event) {
203

    
204
                errorLabel.setVisible(false);
205
                successLabel.setVisible(false);
206

    
207
                ConfirmActionModal confirmActionModal = new ConfirmActionModal("Reject", null);
208
                ConfirmActionModal.ActionConfirmedListener actionConfirmedListener = new ConfirmActionModal.ActionConfirmedListener() {
209
                    @Override
210
                    public void actionConfirmed(final String comment, final String templateId) {
211

    
212
                        final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
213
                        budgetInfoPanel.addStyleName("loading");
214
                        budgetInfoPanel.add(loadingWheel);
215

    
216
                        dataService.rejectBudgetRequest(budget.getId(), comment, GoldOAPortal.currentUser.getId(),
217
                                new AsyncCallback<Void>() {
218

    
219
                                    @Override
220
                                    public void onFailure(Throwable caught) {
221

    
222
                                        budgetInfoPanel.removeStyleName("loading");
223
                                        budgetInfoPanel.remove(loadingWheel);
224

    
225
                                        errorLabel.setText("System error rejecting this budget request");
226
                                        errorLabel.setVisible(true);
227
                                        if(actionListener!=null) {
228
                                            //TODO fix comment template
229
                                            CommentTemplate ct = new CommentTemplate();
230
                                            /*ct.setId(templateId);
231
                                            ct.setComment(comment);*/
232
                                            Comment newComment = new Comment(GoldOAPortal.currentUser, new Timestamp(new Date().getTime()), comment, ct);
233
                                            actionListener.actionHappened(false, Budget.Status.REJECTED, newComment);
234
                                        }
235
                                    }
236

    
237
                                    @Override
238
                                    public void onSuccess(Void result) {
239

    
240
                                        budgetInfoPanel.removeStyleName("loading");
241
                                        budgetInfoPanel.remove(loadingWheel);
242

    
243
                                        successLabel.setText("Budget request rejected successfully");
244
                                        successLabel.setVisible(true);
245
                                        budgetInfoPanel.remove(actionButtons);
246
                                        if(actionListener!=null) {
247
                                            //TODO fix comment template
248
                                            CommentTemplate ct = new CommentTemplate();
249
                                            /*ct.setId(templateId);
250
                                            ct.setComment(comment);*/
251
                                            Comment newComment = new Comment(GoldOAPortal.currentUser, new Timestamp(new Date().getTime()), comment, ct);
252
                                            actionListener.actionHappened(true, Budget.Status.REJECTED, newComment);
253
                                        }
254
                                    }
255
                                });
256
                    }
257
                };
258
                confirmActionModal.setActionConfirmedListener(actionConfirmedListener);
259
                confirmActionModal.show();
260
            }
261
        });
262

    
263
        uploadContract.addStyleName("float-right");
264
        uploadContract.addStyleName("marginLeft10");
265
        uploadContract.setType(ButtonType.PRIMARY);
266
        uploadContract.addClickHandler(new ClickHandler() {
267
            @Override
268
            public void onClick(ClickEvent event) {
269

    
270
//                final Modal addDOIAndRepositoryModal = new Modal();
271
//
272
//                addDOIAndRepositoryModal.addStyleName("contactModal");
273
//                addDOIAndRepositoryModal.setTitle("Add DOI and Repository");
274
//                addDOIAndRepositoryModal.setAnimation(true);
275
//                addDOIAndRepositoryModal.setBackdrop(BackdropType.STATIC);
276
//                addDOIAndRepositoryModal.setDynamicSafe(true);
277
//
278
//                DOIAndRepositoryForm doiAndRepositoryForm = new DOIAndRepositoryForm(requestInfo.getPublication());
279
//                DOIAndRepositoryForm.DOIAndRepositorySubmittedListener doiAndRepositorySubmittedListener =
280
//                        new DOIAndRepositoryForm.DOIAndRepositorySubmittedListener() {
281
//                            @Override
282
//                            public void doiAndRepositorySubmitted() {
283
//                                addDOIAndRepositoryModal.hide();
284
//                                if(publicationUpdatedListener!=null)
285
//                                    publicationUpdatedListener.publicationUpdated();
286
//                            }
287
//                        };
288
//                doiAndRepositoryForm.setDoiAndRepositorySubmittedListener(doiAndRepositorySubmittedListener);
289
//
290
//                addDOIAndRepositoryModal.add(doiAndRepositoryForm.asWidget());
291
//
292
//                addDOIAndRepositoryModal.show();
293
            }
294
        });
295

    
296
        firstDivider.addStyleName("uk-grid-divider");
297
        secondDivider.addStyleName("uk-grid-divider");
298

    
299
        budgetInfoPanel.add(successLabel);
300
        budgetInfoPanel.add(errorLabel);
301
        budgetInfoPanel.add(firstDivider);
302
        budgetInfoPanel.add(budgetSummaryPanel);
303

    
304
        budgetInfoPanel.add(secondDivider);
305
        if(budget.getComments()!=null && !budget.getComments().isEmpty()) {
306
            CommentsInfo commentsInfo = new CommentsInfo(budget.getComments());
307
            commentsInfo.addStyleName("requestComments");
308
            budgetInfoPanel.add(commentsInfo.asWidget());
309
        }
310

    
311
        if(budget.getStatus().equals(Budget.Status.SUBMITTED)) {
312
            actionButtons.add(initialApprove);
313
            actionButtons.add(reject);
314
        } else if(budget.getStatus().equals(Budget.Status.INITIALLY_APPROVED)) {
315
            actionButtons.add(approve);
316
            actionButtons.add(reject);
317
        }
318

    
319
        if(budget.getStatus().equals(Budget.Status.INITIALLY_APPROVED) || budget.getStatus().equals(Budget.Status.APPROVED)
320
                || budget.getStatus().equals(Budget.Status.ACCOUNTING_ONHOLD)) {
321
            actionButtons.add(uploadContract);
322
        }
323
//        if(!budgetInfo.getStatus().equals(Budget.Status.APPROVED) && !budgetInfo.getStatus().equals(Budget.Status.REJECTED)
324
//                && !budgetInfo.getStatus().equals(Budget.Status.ACCOUNTING_PAID)) {
325
//            actionButtons.add(approve);
326
//            actionButtons.add(reject);
327
//        }
328

    
329
        budgetInfoPanel.add(actionButtons);
330
    }
331

    
332
    @Override
333
    public Widget asWidget() {
334
        return budgetInfoPanel;
335
    }
336

    
337
    public interface ActionListener {
338
        public void actionHappened(boolean succeeded, Budget.Status status, Comment comment);
339
    }
340

    
341
    public void setActionListener(ActionListener actionListener) {
342
        this.actionListener = actionListener;
343
    }
344
}
(1-1/8)