Authentication: Reduce some copy/paste by moving code to base class.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / HasTableActivity.java
1 /*
2  * Copyright (C) 2012 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.event.TableChangeEvent;
25 import org.glom.web.client.place.HasTablePlace;
26 import org.glom.web.client.ui.AuthenticationPopup;
27 import org.glom.web.client.ui.View;
28
29 import com.google.gwt.activity.shared.AbstractActivity;
30 import com.google.gwt.core.client.GWT;
31 import com.google.gwt.event.dom.client.ClickEvent;
32 import com.google.gwt.event.dom.client.ClickHandler;
33 import com.google.gwt.event.shared.EventBus;
34 import com.google.gwt.place.shared.Place;
35 import com.google.gwt.user.client.rpc.AsyncCallback;
36
37 /**
38  * @author Ben Konrath <ben@bagu.org>
39  * 
40  */
41 public abstract class HasTableActivity extends AbstractActivity implements View.Presenter {
42
43         protected final ClientFactory clientFactory;
44         protected final String documentID;
45         protected final String tableName;
46         protected final AuthenticationPopup authenticationPopup;
47
48         /**
49          * 
50          */
51         public HasTableActivity(final HasTablePlace place, final ClientFactory clientFactory) {
52                 super();
53                 this.documentID = place.getDocumentID(); // TODO: Just store the place?
54                 this.tableName = place.getTableName();
55                 this.clientFactory = clientFactory;
56                 this.authenticationPopup = clientFactory.getAuthenticationPopup();
57         }
58
59         /*
60          * (non-Javadoc)
61          * 
62          * @see org.glom.web.client.ui.View.Presenter#goTo(com.google.gwt.place.shared.Place)
63          */
64         @Override
65         public void goTo(final Place place) {
66                 clientFactory.getPlaceController().goTo(place);
67         }
68         
69         /**
70          * @param eventBus
71          */
72         protected void checkAuthentication(final EventBus eventBus) {
73                 // TODO this should really be it's own Place/Activity
74                 // check if the authentication info has been set for the document
75                 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
76                         @Override
77                         public void onFailure(final Throwable caught) {
78                                 // TODO: create a way to notify users of asynchronous callback failures
79                                 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated(): " + caught.getMessage());
80                         }
81
82                         @Override
83                         public void onSuccess(final Boolean result) {
84                                 if (!result) {
85                                         setUpAuthClickHandler(eventBus);
86                                         authenticationPopup.center();
87                                 }
88                         }
89                 };
90                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
91         }
92         
93         private void setUpAuthClickHandler(final EventBus eventBus) {
94                 authenticationPopup.setClickOkHandler(new ClickHandler() {
95                         @Override
96                         public void onClick(final ClickEvent event) {
97                                 authenticationPopup.setTextFieldsEnabled(false);
98
99                                 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
100                                         @Override
101                                         public void onFailure(final Throwable caught) {
102                                                 // TODO: create a way to notify users of asynchronous callback failures
103                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication(): " + caught.getMessage());
104                                         }
105
106                                         @Override
107                                         public void onSuccess(final Boolean result) {
108                                                 if (result) {
109                                                         // If authentication succeeded, take us to the requested table:
110                                                         authenticationPopup.hide();
111                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
112                                                                         .getSelectedTableName()));
113                                                 } else {
114                                                         // If authentication failed, tell the user:
115                                                         authenticationPopup.setTextFieldsEnabled(true);
116                                                         authenticationPopup.setError();
117                                                 }
118                                         }
119                                 };
120                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
121                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
122                         }
123
124                 });
125         }
126
127 }