Project

General

Profile

1
package eu.dnetlib.client.fundingrequest.existingrequests;
2

    
3
import com.github.gwtbootstrap.client.ui.*;
4
import com.github.gwtbootstrap.client.ui.ListBox;
5
import com.github.gwtbootstrap.client.ui.constants.*;
6
import com.github.gwtbootstrap.client.ui.event.HideEvent;
7
import com.github.gwtbootstrap.client.ui.event.HideHandler;
8
import com.github.gwtbootstrap.client.ui.event.ShowEvent;
9
import com.github.gwtbootstrap.client.ui.event.ShowHandler;
10
import com.google.gwt.core.client.GWT;
11
import com.google.gwt.dom.client.Document;
12
import com.google.gwt.dom.client.Style;
13
import com.google.gwt.event.dom.client.ChangeEvent;
14
import com.google.gwt.event.dom.client.ChangeHandler;
15
import com.google.gwt.event.dom.client.ClickEvent;
16
import com.google.gwt.event.dom.client.ClickHandler;
17
import com.google.gwt.i18n.client.DateTimeFormat;
18
import com.google.gwt.user.client.Timer;
19
import com.google.gwt.user.client.rpc.AsyncCallback;
20
import com.google.gwt.user.client.ui.*;
21
import com.google.gwt.user.client.ui.Label;
22
import eu.dnetlib.client.*;
23
import eu.dnetlib.client.widgets.FormFieldSet;
24
import eu.dnetlib.client.widgets.TextBox;
25
import eu.dnetlib.client.widgets.ValueChangeEvent;
26
import eu.dnetlib.client.widgets.ValueChangeHandler;
27
import eu.dnetlib.goldoa.domain.RequestFilter;
28
import eu.dnetlib.goldoa.domain.RequestInfo;
29
import eu.dnetlib.goldoa.domain.RequestSort;
30
import eu.dnetlib.goldoa.domain.RequestSortOrder;
31

    
32
import java.util.Date;
33
import java.util.List;
34

    
35
/**
36
 * Created by stefania on 3/7/15.
37
 */
38
public class ExistingFundingRequestsWidget implements MyWidget {
39

    
40
    private FlowPanel existingFundingRequestsPanel = new FlowPanel();
41

    
42
    private Label titleLabel = new Label();
43
    private Label informationLabel = new Label();
44

    
45
    private Alert errorLabel = new Alert();
46
    private Alert warningLabel = new Alert();
47

    
48
    private FlowPanel sortAndFilterPanel = new FlowPanel();
49
    private TextBox search = new TextBox();
50
    private ListBox orderByTypes = new ListBox();
51
    private ListBox sortByTypes = new ListBox();
52

    
53
    private FlowPanel requestsForApprovalPanel = new FlowPanel();
54

    
55
    private DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy/MM/dd");
56
    private DataServiceAsync dataService = GWT.create(DataService.class);
57

    
58
    private Timer timer;
59

    
60
    public ExistingFundingRequestsWidget() {
61

    
62
        existingFundingRequestsPanel.addStyleName("content");
63

    
64
        titleLabel.setText("My Existing Requests");
65
        titleLabel.addStyleName("contentTitleLabel");
66

    
67
        informationLabel.setText("View all your existing requests. Click on each to go to the request funding wizard and " +
68
                "continue from where you left off.");
69
        informationLabel.addStyleName("contentInfoLabel");
70

    
71
        errorLabel.addStyleName("alertLabel");
72
        errorLabel.setType(AlertType.ERROR);
73
        errorLabel.setClose(false);
74
        errorLabel.setVisible(false);
75

    
76
        warningLabel.addStyleName("alertLabel");
77
        warningLabel.setType(AlertType.WARNING);
78
        warningLabel.setClose(false);
79
        warningLabel.setVisible(false);
80

    
81
        addUtilitiesBar();
82

    
83
        requestsForApprovalPanel.addStyleName("requestsListPanel");
84

    
85
        existingFundingRequestsPanel.add(titleLabel);
86
        existingFundingRequestsPanel.add(informationLabel);
87
        existingFundingRequestsPanel.add(sortAndFilterPanel);
88
        existingFundingRequestsPanel.add(errorLabel);
89
        existingFundingRequestsPanel.add(warningLabel);
90
        existingFundingRequestsPanel.add(requestsForApprovalPanel);
91
    }
92

    
93
    @Override
94
    public Widget asWidget() {
95
        return existingFundingRequestsPanel;
96
    }
97

    
98
    @Override
99
    public void clear() {
100

    
101
        errorLabel.setVisible(false);
102
        warningLabel.setVisible(false);
103
        requestsForApprovalPanel.clear();
104
    }
105

    
106
    @Override
107
    public void reload() {
108

    
109
        Document.get().getElementById("content").removeClassName("uk-width-medium-1-1");
110
        Document.get().getElementById("content").addClassName("uk-width-medium-3-4");
111
        Document.get().getElementById("sidebar").getStyle().setDisplay(Style.Display.BLOCK);
112

    
113
        SidebarPanel sidebarPanel = new SidebarPanel("Help");
114
        RootPanel.get("sidebar").add(sidebarPanel.asWidget());
115

    
116
        search.setValue("", false);
117
        orderByTypes.setSelectedValue(RequestSortOrder.ASCENDING.name());
118
        sortByTypes.setSelectedValue(RequestSort.DATE.name());
119
        timer = null;
120

    
121
        loadRequests(null, null, RequestSort.DATE, RequestSortOrder.ASCENDING, null, null);
122
    }
123

    
124
    @Override
125
    public void setToken(String token) {
126

    
127
    }
128

    
129
    private void addUtilitiesBar() {
130

    
131
        FlowPanel sortAndFilterBar = new FlowPanel();
132
        sortAndFilterBar.addStyleName("sortAndFilterBar");
133

    
134
        search.addStyleName("searchTextBox");
135
        search.addStyleName("float-right");
136
        search.setPlaceholder("Filter...");
137
        search.setValueChangeHandler(new ValueChangeHandler() {
138
            @Override
139
            public void handle(ValueChangeEvent valueChangeEvent) {
140
                sortAndFilter();
141
            }
142
        });
143

    
144
        sortAndFilterBar.add(search);
145

    
146
        orderByTypes.addItem("Ascending", RequestSortOrder.ASCENDING.name());
147
        orderByTypes.addItem("Descending", RequestSortOrder.DESCENDING.name());
148
        orderByTypes.setAlternateSize(AlternateSize.SMALL);
149
        orderByTypes.addChangeHandler(new ChangeHandler() {
150
            @Override
151
            public void onChange(ChangeEvent changeEvent) {
152
                sortAndFilter();
153
            }
154
        });
155

    
156
        Form orderBy = new Form();
157
        orderBy.setType(FormType.HORIZONTAL);
158
        orderBy.addStyleName("inlineBlock");
159
        orderBy.add(new FormFieldSet("Order by", orderByTypes));
160

    
161
        sortByTypes.addItem("Date", RequestSort.DATE.name());
162
        sortByTypes.addItem("Publication", RequestSort.PUBLICATION.name());
163
        sortByTypes.addItem("Status", RequestSort.STATUS.name());
164
        sortByTypes.setAlternateSize(AlternateSize.SMALL);
165
        sortByTypes.addChangeHandler(new ChangeHandler() {
166
            @Override
167
            public void onChange(ChangeEvent changeEvent) {
168
                sortAndFilter();
169
            }
170
        });
171

    
172
        Form sortBy = new Form();
173
        sortBy.setType(FormType.HORIZONTAL);
174
        sortBy.addStyleName("inlineBlock");
175
        sortBy.add(new FormFieldSet("Sort by", sortByTypes));
176

    
177
        sortAndFilterBar.add(sortBy);
178
        sortAndFilterBar.add(orderBy);
179

    
180
        sortAndFilterPanel.add(sortAndFilterBar);
181
    }
182

    
183
    private void sortAndFilter() {
184

    
185
        if(timer==null) {
186

    
187
            timer = new Timer() {
188

    
189
                @Override
190
                public void run() {
191

    
192
                    RequestSort requestSort = RequestSort.valueOf(sortByTypes.getSelectedValue());
193
                    RequestSortOrder requestSortOrder = RequestSortOrder.valueOf(orderByTypes.getSelectedValue());
194
                    String term = null;
195
                    if(!search.getValue().trim().equals(""))
196
                        term = search.getValue().trim();
197

    
198
                    loadRequests(null, null, requestSort, requestSortOrder, null, term);
199
                }
200
            };
201
            timer.schedule(300);
202

    
203
        } else {
204

    
205
            timer.cancel();
206
            timer.schedule(300);
207
        }
208
    }
209

    
210
    private void loadRequests(Date from, Date to, RequestSort requestSortBy, RequestSortOrder order, RequestFilter requestFilter, String term) {
211

    
212
        errorLabel.setVisible(false);
213
        warningLabel.setVisible(false);
214

    
215
        final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
216
        requestsForApprovalPanel.addStyleName("loading");
217
        requestsForApprovalPanel.add(loadingWheel);
218

    
219
        dataService.getRequestsForUser(GoldOAPortal.currentUser.getId(), from, to, requestSortBy, order, requestFilter, term,
220
                new AsyncCallback<List<RequestInfo>>() {
221

    
222
            @Override
223
            public void onFailure(Throwable throwable) {
224

    
225
                requestsForApprovalPanel.clear();
226
                requestsForApprovalPanel.removeStyleName("loading");
227

    
228
                errorLabel.setText("System error retrieving requests");
229
                errorLabel.setVisible(true);
230
            }
231

    
232
            @Override
233
            public void onSuccess(List<RequestInfo> requestInfoList) {
234

    
235
                requestsForApprovalPanel.clear();
236
                requestsForApprovalPanel.removeStyleName("loading");
237

    
238
                if(requestInfoList.isEmpty()) {
239
                    warningLabel.setText("No available requests at the moment.");
240
                    warningLabel.setVisible(true);
241
                } else {
242
                    for(RequestInfo requestInfo : requestInfoList)
243
                        drawRequestInfo(requestInfo);
244
                }
245
            }
246
        });
247
    }
248

    
249
    private void drawRequestInfo(final RequestInfo requestInfo) {
250

    
251
        final AccordionGroup dataRequestAccordionItem = new AccordionGroup();
252

    
253
        String heading = "\"" + requestInfo.getPublication().getTitle() + "\"" + " (" + dtf.format(requestInfo.getDate()) + ")"
254
                + ", STATUS: " + requestInfo.getStatus().getValue();
255

    
256
        dataRequestAccordionItem.setHeading(heading);
257
        dataRequestAccordionItem.setIcon(IconType.ANGLE_DOWN);
258

    
259
        dataRequestAccordionItem.addShowHandler(new ShowHandler() {
260

    
261
            @Override
262
            public void onShow(ShowEvent showEvent) {
263

    
264
                dataRequestAccordionItem.setIcon(IconType.ANGLE_UP);
265
                dataRequestAccordionItem.clear();
266

    
267
                final HTML loadingWheel = new HTML("<div class=\"loader-big\"></div><div class=\"whiteFilm\"></div>");
268
                FlowPanel accordionInner = new FlowPanel();
269
                accordionInner.addStyleName("loading");
270
                accordionInner.add(loadingWheel);
271

    
272
                dataRequestAccordionItem.add(accordionInner);
273

    
274
                dataService.getRequestById(requestInfo.getId(), new AsyncCallback<RequestInfo>() {
275

    
276
                    @Override
277
                    public void onFailure(Throwable throwable) {
278

    
279
                        dataRequestAccordionItem.clear();
280

    
281
                        Alert errorLabel = new Alert();
282
                        errorLabel.addStyleName("alertLabel");
283
                        errorLabel.setType(AlertType.ERROR);
284
                        errorLabel.setClose(false);
285
                        errorLabel.setText("System error getting request information");
286

    
287
                        dataRequestAccordionItem.add(errorLabel);
288
                    }
289

    
290
                    @Override
291
                    public void onSuccess(RequestInfo requestInfo) {
292

    
293
                        dataRequestAccordionItem.clear();
294

    
295
                        ExistingRequestElement existingRequestElement = new ExistingRequestElement(requestInfo);
296
                        dataRequestAccordionItem.add(existingRequestElement.asWidget());
297
                    }
298
                });
299
            }
300
        });
301
        dataRequestAccordionItem.addHideHandler(new HideHandler() {
302

    
303
            @Override
304
            public void onHide(HideEvent hideEvent) {
305
                dataRequestAccordionItem.setIcon(IconType.ANGLE_DOWN);
306
            }
307
        });
308

    
309
        requestsForApprovalPanel.add(dataRequestAccordionItem);
310
    }
311
}
(1-1/2)