Replace deprecated DOM.setElementAttribute().
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / HasDocumentActivity.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.OnlineGlomLoginServiceAsync;
24 import org.glom.web.client.StringUtils;
25 import org.glom.web.client.place.DocumentLoginPlace;
26 import org.glom.web.client.place.HasDocumentPlace;
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.shared.EventBus;
32 import com.google.gwt.place.shared.Place;
33 import com.google.gwt.user.client.rpc.AsyncCallback;
34
35 /**
36  * @author Ben Konrath <ben@bagu.org>
37  *
38  */
39 public abstract class HasDocumentActivity extends AbstractActivity implements View.Presenter {
40
41         protected final ClientFactory clientFactory;
42         protected final String documentID;
43
44         /**
45          * 
46          */
47         public HasDocumentActivity(final HasDocumentPlace place, final ClientFactory clientFactory) {
48                 super();
49                 this.documentID = place.getDocumentID(); // TODO: Just store the place?
50                 this.clientFactory = clientFactory;
51         }
52
53         @Override
54         public void goTo(final Place place) {
55                 clientFactory.getPlaceController().goTo(place);
56         }
57
58         /**
59          * @param eventBus
60          */
61         protected void checkAuthentication(final EventBus eventBus) {
62                 if(StringUtils.isEmpty(documentID)) {
63                         //TODO: Show that no document was chosen?
64                         return;
65                 }
66
67                 // check if the authentication info has been set for the document
68                 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
69                         @Override
70                         public void onFailure(final Throwable caught) {
71                                 // TODO: create a way to notify users of asynchronous callback failures
72                                 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated(): " + caught.getMessage());
73                         }
74         
75                         @Override
76                         public void onSuccess(final Boolean result) {
77                                 if (!result) {
78                                         //If the user/session is not authenticated,
79                                         //then go to the login page, so he can try:
80                                         goTo(new DocumentLoginPlace(documentID));
81                                 }
82                         }
83                 };
84                 OnlineGlomLoginServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
85         }
86         
87         protected void clearView() {
88         }
89
90 }