Add a (empty) Report Place, View, and Activity.
[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.StringUtils;
23 import org.glom.web.client.ClientFactory;
24 import org.glom.web.client.OnlineGlomServiceAsync;
25 import org.glom.web.client.Utils;
26 import org.glom.web.client.event.LocaleChangeEvent;
27 import org.glom.web.client.event.LocaleChangeEventHandler;
28 import org.glom.web.client.event.QuickFindChangeEvent;
29 import org.glom.web.client.event.QuickFindChangeEventHandler;
30 import org.glom.web.client.event.TableChangeEvent;
31 import org.glom.web.client.event.TableChangeEventHandler;
32 import org.glom.web.client.place.DocumentSelectionPlace;
33 import org.glom.web.client.place.ReportPlace;
34 import org.glom.web.client.ui.AuthenticationPopup;
35 import org.glom.web.client.ui.ReportView;
36 import org.glom.web.client.ui.View;
37 import org.glom.web.shared.layout.LayoutGroup;
38
39 import com.google.gwt.activity.shared.AbstractActivity;
40 import com.google.gwt.core.client.GWT;
41 import com.google.gwt.event.dom.client.ClickEvent;
42 import com.google.gwt.event.dom.client.ClickHandler;
43 import com.google.gwt.event.shared.EventBus;
44 import com.google.gwt.place.shared.Place;
45 import com.google.gwt.user.client.rpc.AsyncCallback;
46 import com.google.gwt.user.client.ui.AcceptsOneWidget;
47
48 public class ReportActivity extends AbstractActivity implements View.Presenter {
49
50         private final String documentID;
51         private final String tableName;
52         private final String reportName;
53         private final String quickFind;
54         private final ClientFactory clientFactory;
55         private final ReportView reportView;
56         private final AuthenticationPopup authenticationPopup;
57
58         public ReportActivity(final ReportPlace place, final ClientFactory clientFactory) {
59                 this.documentID = place.getDocumentID(); // TODO: Just store the place?
60                 this.tableName = place.getTableName();
61                 this.quickFind = place.getQuickFind();
62                 this.reportName = place.getReportName();
63                 this.clientFactory = clientFactory;
64                 reportView = clientFactory.getReportView();
65                 authenticationPopup = clientFactory.getAuthenticationPopup();
66         }
67
68         @Override
69         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
70                 if (StringUtils.isEmpty(documentID))
71                         goTo(new DocumentSelectionPlace());
72
73                 // register this class as the presenter
74                 reportView.setPresenter(this);
75
76                 // TODO this should really be it's own Place/Activity
77                 // check if the authentication info has been set for the document
78                 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
79                         @Override
80                         public void onFailure(final Throwable caught) {
81                                 // TODO: create a way to notify users of asynchronous callback failures
82                                 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated()");
83                         }
84
85                         @Override
86                         public void onSuccess(final Boolean result) {
87                                 if (!result) {
88                                         setUpAuthClickHandler(eventBus);
89                                         authenticationPopup.center();
90                                 }
91                         }
92                 };
93                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
94
95                 // set the change handler for the table selection widget
96                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
97                         @Override
98                         public void onTableChange(final TableChangeEvent event) {
99                                 goTo(new ReportPlace(documentID, event.getNewTableName(), reportName, ""));
100                         }
101                 });
102
103                 // populate the cell table with data
104                 final AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
105                         @Override
106                         public void onFailure(final Throwable caught) {
107                                 // TODO: create a way to notify users of asynchronous callback failures
108                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getListViewLayout()");
109                         }
110
111                         @Override
112                         public void onSuccess(final LayoutGroup result) {
113                                 // TODO check if result.getTableName() is the same as the tableName field. Update it if it's not the
114                                 // same.
115                                 //reportView.setCellTable(documentID, result, reportName, quickFind);
116                         }
117                 };
118                 
119                 final String localeID = Utils.getCurrentLocaleID();
120                 OnlineGlomServiceAsync.Util.getInstance().getReportLayout(documentID, tableName, reportName, localeID, callback);
121
122                 // TODO: Avoid the code duplication with DetailsActivity.
123                 // set the change handler for the quickfind text widget
124                 eventBus.addHandler(QuickFindChangeEvent.TYPE, new QuickFindChangeEventHandler() {
125                         @Override
126                         public void onQuickFindChange(final QuickFindChangeEvent event) {
127                                 // We switch to the List view, to show search results.
128                                 // TODO: Show the details view if there is only one result.
129                                 goTo(new ReportPlace(documentID, tableName, reportName, event.getNewQuickFindText()));
130                         }
131                 });
132
133                 // Set the change handler for the table selection widget
134                 eventBus.addHandler(LocaleChangeEvent.TYPE, new LocaleChangeEventHandler() {
135                         @Override
136                         public void onLocaleChange(final LocaleChangeEvent event) {
137                                 // note the empty primary key item
138                                 goTo(new ReportPlace(documentID, tableName, reportName, quickFind));
139                         }
140                 });
141
142                 // indicate that the view is ready to be displayed
143                 panel.setWidget(reportView.asWidget());
144         }
145
146         private void setUpAuthClickHandler(final EventBus eventBus) {
147                 authenticationPopup.setClickOkHandler(new ClickHandler() {
148                         @Override
149                         public void onClick(final ClickEvent event) {
150                                 authenticationPopup.setTextFieldsEnabled(false);
151                                 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
152                                         @Override
153                                         public void onFailure(final Throwable caught) {
154                                                 // TODO: create a way to notify users of asynchronous callback failures
155                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
156                                         }
157
158                                         @Override
159                                         public void onSuccess(final Boolean result) {
160                                                 if (result) {
161                                                         authenticationPopup.hide();
162                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
163                                                                         .getSelectedTableName()));
164                                                 } else {
165                                                         authenticationPopup.setTextFieldsEnabled(true);
166                                                         authenticationPopup.setError();
167                                                 }
168                                         }
169                                 };
170                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
171                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
172                         }
173
174                 });
175         }
176
177         private void clearView() {
178                 authenticationPopup.hide();
179                 authenticationPopup.clear();
180                 reportView.clear();
181         }
182
183         /*
184          * (non-Javadoc)
185          * 
186          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
187          */
188         @Override
189         public void onCancel() {
190                 clearView();
191         }
192
193         /*
194          * (non-Javadoc)
195          * 
196          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
197          */
198         @Override
199         public void onStop() {
200                 clearView();
201         }
202
203         /*
204          * (non-Javadoc)
205          * 
206          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
207          */
208         @Override
209         public void goTo(final Place place) {
210                 clientFactory.getPlaceController().goTo(place);
211         }
212
213 }