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