Use filename based unique document ID in URL and for RPC.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / ListActivity.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.event.TableChangeEvent;
25 import org.glom.web.client.event.TableChangeEventHandler;
26 import org.glom.web.client.place.ListPlace;
27 import org.glom.web.client.ui.AuthenticationPopup;
28 import org.glom.web.client.ui.ListView;
29 import org.glom.web.shared.layout.LayoutGroup;
30
31 import com.google.gwt.activity.shared.AbstractActivity;
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.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 ListActivity extends AbstractActivity implements ListView.Presenter {
40
41         private final String documentID;
42         private final ClientFactory clientFactory;
43         private final ListView listView;
44         private final AuthenticationPopup authenticationPopup;
45
46         public ListActivity(ListPlace place, ClientFactory clientFactory) {
47                 this.documentID = place.getDocumentID();
48                 this.clientFactory = clientFactory;
49                 listView = clientFactory.getListView();
50                 authenticationPopup = clientFactory.getAuthenticationPopup();
51         }
52
53         @Override
54         public void start(AcceptsOneWidget panel, final EventBus eventBus) {
55                 // register this class as the presenter
56                 listView.setPresenter(this);
57
58                 // check if the authentication info has been set for the document
59                 AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
60                         public void onFailure(Throwable caught) {
61                                 // FIXME: need to deal with failure
62                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()");
63                         }
64
65                         public void onSuccess(Boolean result) {
66                                 if (!result) {
67                                         setUpAuthClickHandler(eventBus);
68                                         authenticationPopup.center();
69                                 }
70                         }
71                 };
72                 OnlineGlomServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
73
74                 // set the change handler for the table selection widget
75                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
76                         @Override
77                         public void onTableChange(final TableChangeEvent event) {
78                                 AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
79                                         public void onFailure(Throwable caught) {
80                                                 // FIXME: need to deal with failure
81                                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
82                                         }
83
84                                         public void onSuccess(LayoutGroup result) {
85                                                 listView.setCellTable(documentID, event.getTableName(), result);
86                                         }
87                                 };
88                                 OnlineGlomServiceAsync.Util.getInstance().getListLayout(documentID, event.getTableName(), callback);
89                         }
90                 });
91
92                 // populate the cell table with data
93                 final String selectedTable = clientFactory.getTableSelectionView().getSelectedTable();
94                 if (!selectedTable.isEmpty()) {
95                         // The table name has been set so we can use the selected table name to populate the cell table.
96                         AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
97                                 public void onFailure(Throwable caught) {
98                                         // FIXME: need to deal with failure
99                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
100                                 }
101
102                                 public void onSuccess(LayoutGroup result) {
103                                         listView.setCellTable(documentID, selectedTable, result);
104                                 }
105                         };
106                         OnlineGlomServiceAsync.Util.getInstance().getListLayout(documentID, selectedTable, callback);
107                 } else {
108                         // The table name has not been set so we need to fill in the cell table using the default table for the glom
109                         // document.
110                         AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
111                                 public void onFailure(Throwable caught) {
112                                         // FIXME: need to deal with failure
113                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()");
114                                 }
115
116                                 public void onSuccess(LayoutGroup result) {
117                                         listView.setCellTable(documentID, result.getDefaultTableName(), result);
118                                 }
119                         };
120                         OnlineGlomServiceAsync.Util.getInstance().getDefaultListLayout(documentID, callback);
121                 }
122
123                 // indicate that the view is ready to be displayed
124                 panel.setWidget(listView.asWidget());
125         }
126
127         private void setUpAuthClickHandler(final EventBus eventBus) {
128                 authenticationPopup.setClickOkHandler(new ClickHandler() {
129                         @Override
130                         public void onClick(ClickEvent event) {
131                                 authenticationPopup.setTextFieldsEnabled(false);
132                                 AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
133                                         @Override
134                                         public void onFailure(Throwable caught) {
135                                                 // FIXME: need to deal with failure
136                                                 System.out.println("AsyncCallback Failed: OnlineGlomService.checkAuthentication()");
137                                         }
138
139                                         @Override
140                                         public void onSuccess(Boolean result) {
141                                                 if (result) {
142                                                         authenticationPopup.hide();
143                                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
144                                                                         .getSelectedTable()));
145                                                 } else {
146                                                         authenticationPopup.setTextFieldsEnabled(true);
147                                                         authenticationPopup.setError();
148                                                 }
149                                         }
150                                 };
151                                 OnlineGlomServiceAsync.Util.getInstance().checkAuthentication(documentID,
152                                                 authenticationPopup.getUsername(), authenticationPopup.getPassword(), callback);
153                         }
154
155                 });
156         }
157
158         private void clearView() {
159                 authenticationPopup.hide();
160                 authenticationPopup.clear();
161                 listView.clear();
162         }
163
164         /*
165          * (non-Javadoc)
166          * 
167          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
168          */
169         @Override
170         public void onCancel() {
171                 clearView();
172         }
173
174         /*
175          * (non-Javadoc)
176          * 
177          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
178          */
179         @Override
180         public void onStop() {
181                 clearView();
182         }
183
184         /*
185          * (non-Javadoc)
186          * 
187          * @see org.glom.web.client.ui.ListView.Presenter#goTo(com.google.gwt.place.shared.Place)
188          */
189         public void goTo(Place place) {
190                 clientFactory.getPlaceController().goTo(place);
191         }
192
193 }