Turn the authenication popup into a real Place.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / DocumentLoginActivity.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.event.TableChangeEvent;
26 import org.glom.web.client.place.DocumentSelectionPlace;
27 import org.glom.web.client.place.HasDocumentPlace;
28 import org.glom.web.client.place.ListPlace;
29 import org.glom.web.client.ui.DocumentLoginView;
30
31 import com.google.gwt.core.client.GWT;
32 import com.google.gwt.event.dom.client.ClickEvent;
33 import com.google.gwt.event.dom.client.ClickHandler;
34 import com.google.gwt.event.shared.EventBus;
35 import com.google.gwt.user.client.rpc.AsyncCallback;
36 import com.google.gwt.user.client.ui.AcceptsOneWidget;
37
38 public class DocumentLoginActivity extends HasDocumentActivity {
39
40         private final DocumentLoginView view;
41
42         public DocumentLoginActivity(final HasDocumentPlace place, final ClientFactory clientFactory) {
43                 super(place, clientFactory);
44                 this.view = clientFactory.getDocumentLoginView();
45         }
46
47         @Override
48         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
49                 if (StringUtils.isEmpty(documentID)) {
50                         goTo(new DocumentSelectionPlace());
51                 }
52
53                 // register this class as the presenter
54                 view.setPresenter(this);
55
56                 //Find out if there is any need for authentication,
57                 //asking for the credentials if necessary:
58                 checkAuthentication(eventBus);
59
60                 // indicate that the view is ready to be displayed
61                 panel.setWidget(view.asWidget());
62         }
63
64         @Override
65         protected void clearView() {
66                 super.clearView();
67                 view.clear();
68         }
69
70         /*
71          * (non-Javadoc)
72          * 
73          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
74          */
75         @Override
76         public void onCancel() {
77                 clearView();
78         }
79
80         /*
81          * (non-Javadoc)
82          * 
83          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
84          */
85         @Override
86         public void onStop() {
87                 clearView();
88         }
89         
90         //TODO: Remove or modify this in the base class.
91         /**
92          * @param eventBus
93          */
94         protected void checkAuthentication(final EventBus eventBus) {
95                 if(StringUtils.isEmpty(documentID)) {
96                         //TODO: Show that no document was chosen?
97                         return;
98                 }
99
100                 // Check if the authentication info has been set for the document
101                 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
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.isAuthenticated(): " + caught.getMessage());
106                         }
107         
108                         @Override
109                         public void onSuccess(final Boolean result) {
110                                 if (!result) {
111                                         //If the user is not already authenticated,
112                                         //then attempt that:
113                                         setUpAuthClickHandlers(eventBus);
114                                 } else {
115                                         // The user was already authenticated, so go to the previous (or default) page:
116                                         goTo(new DocumentSelectionPlace());
117                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
118                                                         .getSelectedTableName()));
119                                 }
120                         }
121                 };
122                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
123         }
124
125         private void setUpAuthClickHandlers(final EventBus eventBus) {
126                 
127                 //The login button:
128                 view.setClickLoginHandler(new ClickHandler() {
129                         @Override
130                         public void onClick(final ClickEvent event) {
131                                 view.setTextFieldsEnabled(false);
132         
133                                 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
134                                         @Override
135                                         public void onFailure(final Throwable caught) {
136                                                 // TODO: create a way to notify users of asynchronous callback failures
137                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication(): " + caught.getMessage());
138                                         }
139         
140                                         @Override
141                                         public void onSuccess(final Boolean result) {
142                                                 if (result) {
143                                                         // If authentication succeeded, take us to the requested table:
144                                                         // TODO: Take us to the previous page.
145                                                         goTo(new ListPlace(documentID, null, null));
146                                                         //eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
147                                                         //              .getSelectedTableName()));
148                                                 } else {
149                                                         // If authentication failed, tell the user:
150                                                         view.setTextFieldsEnabled(true);
151                                                         view.setError();
152                                                 }
153                                         }
154                                 };
155                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
156                                                 view.getUsername(), view.getPassword(), callback);
157                         }
158                 });
159                 
160                 //The cancel button:
161                 view.setClickCancelHandler(new ClickHandler() {
162                         @Override
163                         public void onClick(final ClickEvent event) {
164                                 //TODO: Return to the previous (or default page):
165                                 goTo(new DocumentSelectionPlace());     
166                         }
167                 });
168         }
169
170 }