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