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