Reports: Show a message while waiting for the report.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / ReportActivity.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.client.activity;
21
22 import org.glom.web.client.ClientFactory;
23 import org.glom.web.client.OnlineGlomServiceAsync;
24 import org.glom.web.client.StringUtils;
25 import org.glom.web.client.Utils;
26 import org.glom.web.client.event.TableChangeEvent;
27 import org.glom.web.client.place.DocumentSelectionPlace;
28 import org.glom.web.client.place.ReportPlace;
29 import org.glom.web.client.ui.AuthenticationPopup;
30 import org.glom.web.client.ui.ReportView;
31 import org.glom.web.client.ui.TableSelectionView;
32 import org.glom.web.client.ui.View;
33
34 import com.google.gwt.activity.shared.AbstractActivity;
35 import com.google.gwt.core.client.GWT;
36 import com.google.gwt.event.dom.client.ClickEvent;
37 import com.google.gwt.event.dom.client.ClickHandler;
38 import com.google.gwt.event.shared.EventBus;
39 import com.google.gwt.place.shared.Place;
40 import com.google.gwt.user.client.rpc.AsyncCallback;
41 import com.google.gwt.user.client.ui.AcceptsOneWidget;
42
43 public class ReportActivity extends AbstractActivity implements View.Presenter {
44
45         private final String documentID;
46         private final String tableName;
47         private final String reportName;
48         private final String quickFind;
49         private final ClientFactory clientFactory;
50         private final ReportView reportView;
51         private final AuthenticationPopup authenticationPopup;
52
53         public ReportActivity(final ReportPlace place, final ClientFactory clientFactory) {
54                 this.documentID = place.getDocumentID(); // TODO: Just store the place?
55                 this.tableName = place.getTableName();
56                 this.quickFind = place.getQuickFind();
57                 this.reportName = place.getReportName();
58                 this.clientFactory = clientFactory;
59                 reportView = clientFactory.getReportView();
60                 authenticationPopup = clientFactory.getAuthenticationPopup();
61         }
62
63         @Override
64         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
65                 if (StringUtils.isEmpty(documentID))
66                         goTo(new DocumentSelectionPlace());
67
68                 // register this class as the presenter
69                 reportView.setPresenter(this);
70
71                 // TODO this should really be it's own Place/Activity
72                 // check if the authentication info has been set for the document
73                 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
74                         @Override
75                         public void onFailure(final Throwable caught) {
76                                 // TODO: create a way to notify users of asynchronous callback failures
77                                 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated()");
78                         }
79
80                         @Override
81                         public void onSuccess(final Boolean result) {
82                                 if (!result) {
83                                         setUpAuthClickHandler(eventBus);
84                                         authenticationPopup.center();
85                                 }
86                         }
87                 };
88                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
89
90                 // populate the report part:
91                 final AsyncCallback<String> callback = new AsyncCallback<String>() {
92                         @Override
93                         public void onFailure(final Throwable caught) {
94                                 // TODO: create a way to notify users of asynchronous callback failures
95                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getReportHTML()");
96                         }
97
98                         @Override
99                         public void onSuccess(final String result) {
100                                 reportView.setReportHTML(result);
101                         }
102                 };
103
104                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
105                 tableSelectionView.setSelectedReport(reportName);
106
107                 reportView.setWaitingText("Generating the report..."); //This is cleared by setReportHTML().
108                 final String localeID = Utils.getCurrentLocaleID();
109                 OnlineGlomServiceAsync.Util.getInstance().getReportHTML(documentID, tableName, reportName, quickFind, localeID, callback);
110
111                 // indicate that the view is ready to be displayed
112                 panel.setWidget(reportView.asWidget());
113         }
114
115         private void setUpAuthClickHandler(final EventBus eventBus) {
116                 authenticationPopup.setClickOkHandler(new ClickHandler() {
117                         @Override
118                         public void onClick(final ClickEvent event) {
119                                 authenticationPopup.setTextFieldsEnabled(false);
120                                 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
121                                         @Override
122                                         public void onFailure(final Throwable caught) {
123                                                 // TODO: create a way to notify users of asynchronous callback failures
124                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
125                                         }
126
127                                         @Override
128                                         public void onSuccess(final Boolean result) {
129                                                 if (result) {
130                                                         authenticationPopup.hide();
131                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
132                                                                         .getSelectedTableName()));
133                                                 } else {
134                                                         authenticationPopup.setTextFieldsEnabled(true);
135                                                         authenticationPopup.setError();
136                                                 }
137                                         }
138                                 };
139                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
140                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
141                         }
142
143                 });
144         }
145
146         private void clearView() {
147                 authenticationPopup.hide();
148                 authenticationPopup.clear();
149                 reportView.clear();
150         }
151
152         /*
153          * (non-Javadoc)
154          * 
155          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
156          */
157         @Override
158         public void onCancel() {
159                 clearView();
160         }
161
162         /*
163          * (non-Javadoc)
164          * 
165          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
166          */
167         @Override
168         public void onStop() {
169                 clearView();
170         }
171
172         /*
173          * (non-Javadoc)
174          * 
175          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
176          */
177         @Override
178         public void goTo(final Place place) {
179                 clientFactory.getPlaceController().goTo(place);
180         }
181
182 }