Authentication: Reduce some copy/paste by moving code to base class.
[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.place.DocumentSelectionPlace;
27 import org.glom.web.client.place.ReportPlace;
28 import org.glom.web.client.ui.OnlineGlomConstants;
29 import org.glom.web.client.ui.ReportView;
30 import org.glom.web.client.ui.TableSelectionView;
31 import org.glom.web.client.ui.View;
32
33 import com.google.gwt.core.client.GWT;
34 import com.google.gwt.event.shared.EventBus;
35 import com.google.gwt.place.shared.Place;
36 import com.google.gwt.user.client.rpc.AsyncCallback;
37 import com.google.gwt.user.client.ui.AcceptsOneWidget;
38
39 public class ReportActivity extends HasTableActivity implements View.Presenter {
40
41         private final String reportName;
42         private final String quickFind;
43         private final ClientFactory clientFactory;
44         private final ReportView reportView;
45
46         // OnlineGlomConstants.java is generated in the target/ directory,
47         // from OnlineGlomConstants.properties
48         // by the gwt-maven-plugin's i18n (mvn:i18n) goal.
49         private final OnlineGlomConstants constants = GWT.create(OnlineGlomConstants.class);
50
51         public ReportActivity(final ReportPlace place, final ClientFactory clientFactory) {
52                 super(place, clientFactory);
53                 this.quickFind = place.getQuickFind();
54                 this.reportName = place.getReportName();
55                 this.clientFactory = clientFactory;
56                 reportView = clientFactory.getReportView();
57         }
58
59         @Override
60         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
61                 if (StringUtils.isEmpty(documentID)) {
62                         goTo(new DocumentSelectionPlace());
63                 }
64
65                 // register this class as the presenter
66                 reportView.setPresenter(this);
67
68                 checkAuthentication(eventBus);
69
70                 // populate the report part:
71                 final AsyncCallback<String> callback = new AsyncCallback<String>() {
72                         @Override
73                         public void onFailure(final Throwable caught) {
74                                 // TODO: create a way to notify users of asynchronous callback failures
75                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getReportHTML(): " + caught.getMessage());
76                         }
77
78                         @Override
79                         public void onSuccess(final String result) {
80                                 reportView.setReportHTML(result);
81                         }
82                 };
83
84                 final TableSelectionView tableSelectionView = clientFactory.getTableSelectionView();
85                 tableSelectionView.setSelectedReport(reportName);
86
87                 reportView.setWaitingText(constants.generatingReport()); // This is cleared by setReportHTML().
88                 final String localeID = Utils.getCurrentLocaleID();
89                 OnlineGlomServiceAsync.Util.getInstance().getReportHTML(documentID, tableName, reportName, quickFind, localeID,
90                                 callback);
91
92                 // indicate that the view is ready to be displayed
93                 panel.setWidget(reportView.asWidget());
94         }
95
96         private void clearView() {
97                 authenticationPopup.hide();
98                 authenticationPopup.clear();
99                 reportView.clear();
100         }
101
102         /*
103          * (non-Javadoc)
104          * 
105          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
106          */
107         @Override
108         public void onCancel() {
109                 clearView();
110         }
111
112         /*
113          * (non-Javadoc)
114          * 
115          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
116          */
117         @Override
118         public void onStop() {
119                 clearView();
120         }
121
122         /*
123          * (non-Javadoc)
124          * 
125          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
126          */
127         @Override
128         public void goTo(final Place place) {
129                 clientFactory.getPlaceController().goTo(place);
130         }
131
132 }