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