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