Add some comments
[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.OnlineGlomConstants;
31 import org.glom.web.client.ui.ReportView;
32 import org.glom.web.client.ui.TableSelectionView;
33 import org.glom.web.client.ui.View;
34
35 import com.google.gwt.activity.shared.AbstractActivity;
36 import com.google.gwt.core.client.GWT;
37 import com.google.gwt.event.dom.client.ClickEvent;
38 import com.google.gwt.event.dom.client.ClickHandler;
39 import com.google.gwt.event.shared.EventBus;
40 import com.google.gwt.place.shared.Place;
41 import com.google.gwt.user.client.rpc.AsyncCallback;
42 import com.google.gwt.user.client.ui.AcceptsOneWidget;
43
44 public class ReportActivity extends AbstractActivity implements View.Presenter {
45
46         private final String documentID;
47         private final String tableName;
48         private final String reportName;
49         private final String quickFind;
50         private final ClientFactory clientFactory;
51         private final ReportView reportView;
52         private final AuthenticationPopup authenticationPopup;
53
54         // OnlineGlomConstants.java is generated in the target/ directory,
55         // from OnlineGlomConstants.properties
56         // by the gwt-maven-plugin's i18n (mvn:i18n) goal.
57         private final OnlineGlomConstants constants = GWT.create(OnlineGlomConstants.class);
58
59         public ReportActivity(final ReportPlace place, final ClientFactory clientFactory) {
60                 this.documentID = place.getDocumentID(); // TODO: Just store the place?
61                 this.tableName = place.getTableName();
62                 this.quickFind = place.getQuickFind();
63                 this.reportName = place.getReportName();
64                 this.clientFactory = clientFactory;
65                 reportView = clientFactory.getReportView();
66                 authenticationPopup = clientFactory.getAuthenticationPopup();
67         }
68
69         @Override
70         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
71                 if (StringUtils.isEmpty(documentID)) {
72                         goTo(new DocumentSelectionPlace());
73                 }
74
75                 // register this class as the presenter
76                 reportView.setPresenter(this);
77
78                 // TODO this should really be it's own Place/Activity
79                 // check if the authentication info has been set for the document
80                 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
81                         @Override
82                         public void onFailure(final Throwable caught) {
83                                 // TODO: create a way to notify users of asynchronous callback failures
84                                 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated(): " + caught.getMessage());
85                         }
86
87                         @Override
88                         public void onSuccess(final Boolean result) {
89                                 
90                                 // If the user's session is not authenticated
91                                 // then attempt authentication, asking the user:
92                                 if (!result) {
93                                         setUpAuthClickHandler(eventBus);
94                                         authenticationPopup.center();
95                                 }
96                         }
97                 };
98                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
99
100                 // populate the report part:
101                 final AsyncCallback<String> callback = new AsyncCallback<String>() {
102                         @Override
103                         public void onFailure(final Throwable caught) {
104                                 // TODO: create a way to notify users of asynchronous callback failures
105                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getReportHTML(): " + caught.getMessage());
106                         }
107
108                         @Override
109                         public void onSuccess(final String result) {
110                                 reportView.setReportHTML(result);
111                         }
112                 };
113
114                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
115                 tableSelectionView.setSelectedReport(reportName);
116
117                 reportView.setWaitingText(constants.generatingReport()); // This is cleared by setReportHTML().
118                 final String localeID = Utils.getCurrentLocaleID();
119                 OnlineGlomServiceAsync.Util.getInstance().getReportHTML(documentID, tableName, reportName, quickFind, localeID,
120                                 callback);
121
122                 // indicate that the view is ready to be displayed
123                 panel.setWidget(reportView.asWidget());
124         }
125
126         private void setUpAuthClickHandler(final EventBus eventBus) {
127                 authenticationPopup.setClickOkHandler(new ClickHandler() {
128                         @Override
129                         public void onClick(final ClickEvent event) {
130                                 authenticationPopup.setTextFieldsEnabled(false);
131                                 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
132                                         @Override
133                                         public void onFailure(final Throwable caught) {
134                                                 // TODO: create a way to notify users of asynchronous callback failures
135                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication(): " + caught.getMessage());
136                                         }
137
138                                         @Override
139                                         public void onSuccess(final Boolean result) {
140                                                 if (result) {
141                                                         // If authentication succeeded, take us to the requested table:
142                                                         authenticationPopup.hide();
143                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
144                                                                         .getSelectedTableName()));
145                                                 } else {
146                                                         // If authentication failed, tell the user:
147                                                         authenticationPopup.setTextFieldsEnabled(true);
148                                                         authenticationPopup.setError();
149                                                 }
150                                         }
151                                 };
152                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
153                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
154                         }
155
156                 });
157         }
158
159         private void clearView() {
160                 authenticationPopup.hide();
161                 authenticationPopup.clear();
162                 reportView.clear();
163         }
164
165         /*
166          * (non-Javadoc)
167          * 
168          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
169          */
170         @Override
171         public void onCancel() {
172                 clearView();
173         }
174
175         /*
176          * (non-Javadoc)
177          * 
178          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
179          */
180         @Override
181         public void onStop() {
182                 clearView();
183         }
184
185         /*
186          * (non-Javadoc)
187          * 
188          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
189          */
190         @Override
191         public void goTo(final Place place) {
192                 clientFactory.getPlaceController().goTo(place);
193         }
194
195 }