Project

General

Profile

1
package eu.dnetlib.client;
2

    
3
import com.github.gwtbootstrap.client.ui.Alert;
4
import com.github.gwtbootstrap.client.ui.constants.AlertType;
5
import com.google.gwt.core.client.GWT;
6
import com.google.gwt.dom.client.Document;
7
import com.google.gwt.dom.client.Style;
8
import com.google.gwt.i18n.client.DateTimeFormat;
9
import com.google.gwt.user.client.Window;
10
import com.google.gwt.user.client.rpc.AsyncCallback;
11
import com.google.gwt.user.client.ui.*;
12
import eu.dnetlib.goldoa.domain.Affiliation;
13
import eu.dnetlib.goldoa.domain.Budget;
14

    
15
import java.util.ArrayList;
16
import java.util.List;
17

    
18
/**
19
 * Created by stefania on 4/6/15.
20
 */
21
public class ExistingBudgetRequestsWidget implements MyWidget {
22

    
23
    private String token = "";
24

    
25
    private FlowPanel existingBudgetsPagePanel = new FlowPanel();
26
    private Label existingBudgetsTitleLabel = new Label();
27
    private Label existingBudgetsInfoLabel = new Label();
28

    
29
    private Alert errorLabel = new Alert();
30
    private Alert warningLabel = new Alert();
31

    
32
    private FlowPanel budgetsForApprovalPanel = new FlowPanel();
33

    
34
    private DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy/MM/dd");
35
    private DataServiceAsync dataService = GWT.create(DataService.class);
36

    
37
    private GoldOAConstants goldOAConstants = GWT.create(GoldOAConstants.class);
38

    
39
    public ExistingBudgetRequestsWidget() {
40

    
41
        existingBudgetsPagePanel.addStyleName("content");
42

    
43
        existingBudgetsTitleLabel.setText("Existing Budgets");
44
        existingBudgetsTitleLabel.addStyleName("contentTitleLabel");
45

    
46
        existingBudgetsInfoLabel.setText("View all your budget requests and the amount of money left in each one.");
47
        existingBudgetsInfoLabel.addStyleName("contentInfoLabel");
48

    
49
        errorLabel.addStyleName("alertLabel");
50
        errorLabel.setType(AlertType.ERROR);
51
        errorLabel.setClose(false);
52
        errorLabel.setVisible(false);
53

    
54
        warningLabel.addStyleName("alertLabel");
55
        warningLabel.setType(AlertType.WARNING);
56
        warningLabel.setClose(false);
57
        warningLabel.setVisible(false);
58

    
59
        budgetsForApprovalPanel.addStyleName("budgetsListPanel");
60

    
61
        existingBudgetsPagePanel.add(existingBudgetsTitleLabel);
62
        existingBudgetsPagePanel.add(existingBudgetsInfoLabel);
63
        existingBudgetsPagePanel.add(errorLabel);
64
        existingBudgetsPagePanel.add(warningLabel);
65
        existingBudgetsPagePanel.add(budgetsForApprovalPanel);
66
    }
67

    
68
    @Override
69
    public void clear() {
70

    
71
        errorLabel.setVisible(false);
72
        warningLabel.setVisible(false);
73
        budgetsForApprovalPanel.clear();
74
    }
75

    
76
    @Override
77
    public void reload() {
78

    
79
        MyWidgetHelper.hideSidebar();
80

    
81
        SidebarPanel helpPanel = new SidebarPanel("Help");
82
        MyWidgetHelper.loadHelp(helpPanel, token.split("\\.")[0]);
83

    
84
        loadBudgets();
85
    }
86

    
87
    @Override
88
    public void setToken(String token) {
89
        this.token = token;
90
    }
91

    
92
    @Override
93
    public void afterAdditionToRootPanel() {
94

    
95
    }
96

    
97
    @Override
98
    public Widget asWidget() {
99
        return existingBudgetsPagePanel;
100
    }
101

    
102
    private void loadBudgets() {
103

    
104
        errorLabel.setVisible(false);
105
        warningLabel.setVisible(false);
106

    
107
        final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
108
        budgetsForApprovalPanel.addStyleName("loading");
109
        budgetsForApprovalPanel.add(loadingWheel);
110

    
111
        if(Utils.currentUserHasRole("library_staff")) {
112

    
113
            List<String> organizationIds = new ArrayList<>();
114
            for(Affiliation affiliation : GoldOAPortal.currentUser.getAffiliations()) {
115
                if(affiliation.getOrganization()!=null) {
116
                    organizationIds.add(affiliation.getOrganization().getId());
117
                }
118
            }
119

    
120
            dataService.getBudgetsForOrganizations(organizationIds, new AsyncCallback<List<Budget>>() {
121

    
122
                @Override
123
                public void onFailure(Throwable throwable) {
124

    
125
                    budgetsForApprovalPanel.clear();
126
                    budgetsForApprovalPanel.removeStyleName("loading");
127

    
128
                    errorLabel.setText(goldOAConstants.errorGettingListOfBudgets());
129
                    errorLabel.setVisible(true);
130
                }
131

    
132
                @Override
133
                public void onSuccess(List<Budget> budgetInfoList) {
134

    
135
                    budgetsForApprovalPanel.clear();
136
                    budgetsForApprovalPanel.removeStyleName("loading");
137

    
138
                    if (budgetInfoList.isEmpty()) {
139
                        warningLabel.setText(goldOAConstants.warningNoBudgetsAvailable());
140
                        warningLabel.setVisible(true);
141
                    } else {
142
                        for (int i = 0; i < budgetInfoList.size(); i++)
143
                            drawBudgetInfo(budgetInfoList.get(i), i);
144
                    }
145
                }
146
            });
147

    
148
        } else {
149

    
150
            dataService.getBudgetsForUser(GoldOAPortal.currentUser.getEmail(), new AsyncCallback<List<Budget>>() {
151

    
152
                @Override
153
                public void onFailure(Throwable throwable) {
154

    
155
                    budgetsForApprovalPanel.clear();
156
                    budgetsForApprovalPanel.removeStyleName("loading");
157

    
158
                    errorLabel.setText(goldOAConstants.errorGettingListOfBudgets());
159
                    errorLabel.setVisible(true);
160
                }
161

    
162
                @Override
163
                public void onSuccess(List<Budget> budgetInfoList) {
164

    
165
                    budgetsForApprovalPanel.clear();
166
                    budgetsForApprovalPanel.removeStyleName("loading");
167

    
168
                    if (budgetInfoList.isEmpty()) {
169
                        warningLabel.setText(goldOAConstants.warningNoBudgetsAvailable());
170
                        warningLabel.setVisible(true);
171
                    } else {
172

    
173
                        for (int i = 0; i < budgetInfoList.size(); i++)
174
                            drawBudgetInfo(budgetInfoList.get(i), i);
175
                    }
176
                }
177
            });
178
        }
179
    }
180

    
181
    private void drawBudgetInfo(Budget budgetInfo, int i) {
182

    
183
        FlowPanel budgetElement = new FlowPanel();
184
        budgetElement.addStyleName("budgetRequest");
185
        if(((i+1)%2)==1)
186
            budgetElement.addStyleName("odd");
187

    
188
        BudgetInfoElement budgetInfoElement = new BudgetInfoElement(budgetInfo);
189

    
190
        budgetElement.add(budgetInfoElement.asWidget());
191
        budgetsForApprovalPanel.add(budgetElement);
192
    }
193
}
(9-9/22)