Fix a crasher when refreshing the list view with the default table.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / DetailsActivity.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 java.util.ArrayList;
23
24 import org.glom.web.client.ClientFactory;
25 import org.glom.web.client.OnlineGlomServiceAsync;
26 import org.glom.web.client.Utils;
27 import org.glom.web.client.event.TableChangeEvent;
28 import org.glom.web.client.event.TableChangeEventHandler;
29 import org.glom.web.client.place.DetailsPlace;
30 import org.glom.web.client.place.DocumentSelectionPlace;
31 import org.glom.web.client.ui.DetailsView;
32 import org.glom.web.client.ui.cell.NavigationButtonCell;
33 import org.glom.web.client.ui.details.DetailsCell;
34 import org.glom.web.client.ui.details.Portal;
35 import org.glom.web.client.ui.details.RelatedListTable;
36 import org.glom.web.shared.DataItem;
37 import org.glom.web.shared.DetailsLayoutAndData;
38 import org.glom.web.shared.NavigationRecord;
39 import org.glom.web.shared.TypedDataItem;
40 import org.glom.web.shared.layout.LayoutGroup;
41 import org.glom.web.shared.layout.LayoutItemField;
42 import org.glom.web.shared.layout.LayoutItemPortal;
43
44 import com.google.gwt.activity.shared.AbstractActivity;
45 import com.google.gwt.cell.client.ValueUpdater;
46 import com.google.gwt.core.client.GWT;
47 import com.google.gwt.dom.client.Element;
48 import com.google.gwt.dom.client.NativeEvent;
49 import com.google.gwt.event.dom.client.ClickEvent;
50 import com.google.gwt.event.dom.client.ClickHandler;
51 import com.google.gwt.event.shared.EventBus;
52 import com.google.gwt.place.shared.Place;
53 import com.google.gwt.user.client.rpc.AsyncCallback;
54 import com.google.gwt.user.client.ui.AcceptsOneWidget;
55
56 /**
57  * @author Ben Konrath <ben@bagu.org>
58  */
59 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
60         /*
61          * Cell renderer for the related list open buttons. Normally this wouldn't be in an Activity class but since it's
62          * making a call to the server it makes sense for it to be here.
63          */
64         private class RelatedListNavigationButtonCell extends NavigationButtonCell {
65
66                 private String relationshipName;
67
68                 public RelatedListNavigationButtonCell(String relationshipName) {
69                         this.relationshipName = relationshipName;
70                 }
71
72                 /*
73                  * (non-Javadoc)
74                  * 
75                  * @see com.google.gwt.cell.client.ButtonCell#onEnterKeyDown(com.google.gwt.cell.client.Cell.Context,
76                  * com.google.gwt.dom.client.Element, java.lang.String, com.google.gwt.dom.client.NativeEvent,
77                  * com.google.gwt.cell.client.ValueUpdater)
78                  */
79                 @Override
80                 protected void onEnterKeyDown(Context context, Element parent, String value, NativeEvent event,
81                                 ValueUpdater<String> valueUpdater) {
82                         AsyncCallback<NavigationRecord> callback = new AsyncCallback<NavigationRecord>() {
83                                 public void onFailure(Throwable caught) {
84                                         // TODO: create a way to notify users of asynchronous callback failures
85                                         GWT.log("AsyncCallback Failed: OnlineGlomService.getSuitableRecordToViewDetails()");
86                                 }
87
88                                 @Override
89                                 public void onSuccess(NavigationRecord result) {
90                                         processNavigation(result.getTableName(), result.getPrimaryKeyValue());
91                                 }
92
93                         };
94                         OnlineGlomServiceAsync.Util.getInstance().getSuitableRecordToViewDetails(documentID, tableName,
95                                         relationshipName, (TypedDataItem) context.getKey(), callback);
96                 }
97         }
98
99         private final String documentID;
100         private final String tableName;
101         private TypedDataItem primaryKeyValue;
102         private final ClientFactory clientFactory;
103         private final DetailsView detailsView;
104         ArrayList<DetailsCell> detailsCells;
105         ArrayList<Portal> portals;
106
107         public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
108                 this.documentID = place.getDocumentID();
109                 this.tableName = place.getTableName();
110                 this.primaryKeyValue = place.getPrimaryKeyValue();
111                 this.clientFactory = clientFactory;
112                 detailsView = clientFactory.getDetailsView();
113         }
114
115         /*
116          * (non-Javadoc)
117          * 
118          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
119          * com.google.gwt.event.shared.EventBus)
120          */
121         @Override
122         public void start(AcceptsOneWidget panel, EventBus eventBus) {
123                 if (documentID.isEmpty())
124                         goTo(new DocumentSelectionPlace());
125
126                 // register this class as the presenter
127                 detailsView.setPresenter(this);
128
129                 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
130
131                 // set the change handler for the table selection widget
132                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
133                         @Override
134                         public void onTableChange(final TableChangeEvent event) {
135                                 // note the empty primary key item
136                                 goTo(new DetailsPlace(documentID, event.getNewTableName(), new TypedDataItem()));
137                         }
138                 });
139
140                 // get the layout and data for the DetailsView
141                 AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
142                         public void onFailure(Throwable caught) {
143                                 // TODO: create a way to notify users of asynchronous callback failures
144                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
145                         }
146
147                         @Override
148                         public void onSuccess(DetailsLayoutAndData result) {
149                                 if (result == null) {
150                                         // The result is null only when the documentID was not found. There's nothing to display without the
151                                         // documentID.
152                                         goTo(new DocumentSelectionPlace());
153                                 } else {
154                                         // create the layout and set the data
155                                         createLayout(result.getLayout());
156                                         setData(result.getData());
157                                 }
158                         }
159
160                 };
161                 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
162                                 callback);
163
164                 // indicate that the view is ready to be displayed
165                 panel.setWidget(detailsView.asWidget());
166         }
167
168         /*
169          * Create the layout.
170          */
171         private void createLayout(ArrayList<LayoutGroup> layout) {
172                 // add the groups
173                 for (LayoutGroup layoutGroup : layout) {
174                         detailsView.addGroup(layoutGroup);
175                 }
176
177                 // save references to the DetailsCells and the Portals
178                 detailsCells = detailsView.getCells();
179                 portals = detailsView.getPortals();
180
181                 // Setup click handlers for the navigation buttons
182                 for (final DetailsCell detailsCell : detailsCells) {
183                         final LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
184                         if (layoutItemField.getAddNavigation()) {
185                                 detailsCell.setOpenButtonClickHandler(new ClickHandler() {
186                                         @Override
187                                         public void onClick(ClickEvent event) {
188                                                 TypedDataItem primaryKeyItem = Utils.getTypedDataItem(layoutItemField.getType(),
189                                                                 detailsCell.getData());
190                                                 processNavigation(layoutItemField.getNavigationTableName(), primaryKeyItem);
191
192                                         }
193                                 });
194                         }
195                 }
196
197         }
198
199         /*
200          * Set the data.
201          */
202         private void setData(DataItem[] data) {
203
204                 if (data == null)
205                         return;
206
207                 // TODO create proper client side logging
208                 if (data.length != detailsCells.size())
209                         GWT.log("Warning: The number of data items doesn't match the number of data detailsCells.");
210
211                 for (int i = 0; i < Math.min(detailsCells.size(), data.length); i++) {
212                         DetailsCell detailsCell = detailsCells.get(i);
213                         if (data[i] != null) {
214
215                                 // set the DatailsItem
216                                 detailsCell.setData(data[i]);
217
218                                 // see if there are any related lists that need to be setup
219                                 for (Portal portal : portals) {
220                                         LayoutItemField layoutItemField = detailsCell.getLayoutItemField();
221                                         LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
222
223                                         if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
224                                                 if (data[i] == null)
225                                                         continue;
226
227                                                 TypedDataItem foreignKeyValue = Utils.getTypedDataItem(layoutItemField.getType(), data[i]);
228
229                                                 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
230                                                                 foreignKeyValue, new RelatedListNavigationButtonCell(layoutItemPortal.getName()));
231
232                                                 if (!layoutItemPortal.getAddNavigation()
233                                                                 || layoutItemPortal.getNavigationType() == LayoutItemPortal.NavigationType.NAVIGATION_NONE) {
234                                                         relatedListTable.hideNavigationButtons();
235                                                 }
236                                                 portal.setContents(relatedListTable);
237
238                                                 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
239                                         }
240                                 }
241                         }
242                 }
243         }
244
245         private void refreshData() {
246
247                 // get the data for the DetailsView
248                 AsyncCallback<DataItem[]> callback = new AsyncCallback<DataItem[]>() {
249                         public void onFailure(Throwable caught) {
250                                 // TODO: create a way to notify users of asynchronous callback failures
251                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
252                         }
253
254                         @Override
255                         public void onSuccess(DataItem[] result) {
256                                 setData(result);
257                         }
258                 };
259
260                 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentID, tableName, primaryKeyValue, callback);
261
262         }
263
264         // sets the row count for the related list table
265         private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, String relationshipName,
266                         TypedDataItem foreignKeyValue) {
267                 AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
268                         public void onFailure(Throwable caught) {
269                                 // TODO: create a way to notify users of asynchronous callback failures
270                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
271                         }
272
273                         @Override
274                         public void onSuccess(Integer result) {
275                                 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
276                                         // Set the table row count to the minimum row count if the data row count is less than or equal to
277                                         // the minimum row count. This ensures that data with fewer rows than the minimum will not create
278                                         // indexes in the underlying CellTable that will override the rendering of the empty rows.
279                                         relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
280                                 } else {
281                                         // Set the table row count to the data row count if it's larger than the minimum number of rows
282                                         // visible.
283                                         relatedListTable.setRowCount(result.intValue());
284                                 }
285                         }
286                 };
287
288                 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
289                                 foreignKeyValue, callback);
290         }
291
292         /*
293          * Process a navigation by either doing: nothing if the navigation isn't valid, refreshing the data for the current
294          * table with a new primary key, or going to a new table with a new primary key.
295          */
296         private void processNavigation(String navigationTableName, TypedDataItem navigationPrimaryKeyValue) {
297
298                 // Ensure the new table name is valid.
299                 String newTableName;
300                 if (navigationTableName != null && !navigationTableName.isEmpty()) {
301                         newTableName = navigationTableName;
302                 } else {
303                         newTableName = tableName;
304                 }
305
306                 // Only process the navigation if there's a valid primary key value.
307                 if (navigationPrimaryKeyValue != null && !navigationPrimaryKeyValue.isEmpty()) {
308                         if (!newTableName.equals(tableName)) {
309                                 // Go to a new DetailsPlace because the table name has changed.
310                                 goTo(new DetailsPlace(documentID, newTableName, navigationPrimaryKeyValue));
311                         } else {
312                                 // Refresh the details view with the new primary because the table name has not changed.
313                                 primaryKeyValue = navigationPrimaryKeyValue;
314                                 refreshData();
315                         }
316                 } else {
317                         // TODO notify the user that navigation isn't possible.
318                         // This is what Glom displays:
319                         // Frame_Glom::show_ok_dialog(_("No Corresponding Record Exists"),
320                         // _("No record with this value exists. Therefore navigation to the related record is not possible."),
321                         // *window, Gtk::MESSAGE_WARNING);
322                         // TODO: Make it more clear to the user exactly what record, what field, and what value, we are talking
323                         // about.
324                 }
325         }
326
327         /*
328          * (non-Javadoc)
329          * 
330          * @see com.google.gwt.activity.shared.Activity#onCancel()
331          */
332         @Override
333         public void onCancel() {
334                 detailsView.clear();
335         }
336
337         /*
338          * (non-Javadoc)
339          * 
340          * @see com.google.gwt.activity.shared.Activity#onStop()
341          */
342         @Override
343         public void onStop() {
344                 detailsView.clear();
345         }
346
347         /*
348          * (non-Javadoc)
349          * 
350          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
351          */
352         @Override
353         public void goTo(Place place) {
354                 clientFactory.getPlaceController().goTo(place);
355         }
356
357 }