Create separate methods for layout and data the details view.
[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.details.Field;
32 import org.glom.web.client.ui.details.Portal;
33 import org.glom.web.client.ui.details.RelatedListTable;
34 import org.glom.web.shared.DataItem;
35 import org.glom.web.shared.DetailsLayoutAndData;
36 import org.glom.web.shared.layout.LayoutGroup;
37 import org.glom.web.shared.layout.LayoutItemField;
38 import org.glom.web.shared.layout.LayoutItemPortal;
39
40 import com.google.gwt.activity.shared.AbstractActivity;
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.event.shared.EventBus;
43 import com.google.gwt.place.shared.Place;
44 import com.google.gwt.user.client.rpc.AsyncCallback;
45 import com.google.gwt.user.client.ui.AcceptsOneWidget;
46
47 /**
48  * @author Ben Konrath <ben@bagu.org>
49  */
50 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
51         private final String documentID;
52         private final String tableName;
53         private final String primaryKeyValue;
54         private final ClientFactory clientFactory;
55         private final DetailsView detailsView;
56
57         ArrayList<Field> fields;
58         ArrayList<Portal> portals;
59
60         public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
61                 this.documentID = place.getDocumentID();
62                 this.tableName = place.getTableName();
63                 this.primaryKeyValue = place.getPrimaryKeyValue();
64                 this.clientFactory = clientFactory;
65                 detailsView = clientFactory.getDetailsView();
66         }
67
68         /*
69          * (non-Javadoc)
70          * 
71          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
72          * com.google.gwt.event.shared.EventBus)
73          */
74         @Override
75         public void start(AcceptsOneWidget panel, EventBus eventBus) {
76                 // register this class as the presenter
77                 detailsView.setPresenter(this);
78
79                 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
80
81                 // set the change handler for the table selection widget
82                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
83                         @Override
84                         public void onTableChange(final TableChangeEvent event) {
85                                 goTo(new DetailsPlace(documentID, event.getNewTableName(), ""));
86                         }
87                 });
88
89                 // get the layout and data for the DetailsView
90                 AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
91                         public void onFailure(Throwable caught) {
92                                 // TODO: create a way to notify users of asynchronous callback failures
93                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
94                         }
95
96                         @Override
97                         public void onSuccess(DetailsLayoutAndData result) {
98
99                                 createLayout(result.getLayout());
100
101                                 setData(result.getData());
102
103                         }
104                 };
105                 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
106                                 callback);
107
108                 // indicate that the view is ready to be displayed
109                 panel.setWidget(detailsView.asWidget());
110         }
111
112         /*
113          * Create the layout.
114          */
115         private void createLayout(ArrayList<LayoutGroup> layout) {
116                 // add the groups
117                 for (LayoutGroup layoutGroup : layout) {
118                         detailsView.addGroup(layoutGroup);
119                 }
120
121                 // save references to the Fields and the Portals
122                 fields = detailsView.getFields();
123                 portals = detailsView.getPortals();
124         }
125
126         /*
127          * Set the data.
128          */
129         private void setData(DataItem[] data) {
130
131                 if (data == null)
132                         return;
133
134                 // TODO create proper client side logging
135                 if (data.length != fields.size())
136                         GWT.log("Warning: The number of data items doesn't match the number of data fields.");
137
138                 for (int i = 0; i < Math.min(fields.size(), data.length); i++) {
139                         Field field = fields.get(i);
140                         if (data[i] != null) {
141
142                                 // set the field data
143                                 field.setData(data[i]);
144
145                                 // see if there are any related lists that need to be setup
146                                 for (Portal portal : portals) {
147                                         LayoutItemField layoutItemField = field.getLayoutItem();
148                                         LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
149                                         if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
150                                                 String foreignKeyValue = Utils.getKeyValueStringForQuery(layoutItemField.getType(), data[i]);
151                                                 if (foreignKeyValue == null)
152                                                         continue;
153                                                 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
154                                                                 foreignKeyValue);
155                                                 portal.setContents(relatedListTable);
156                                                 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), foreignKeyValue);
157                                         }
158                                 }
159                         }
160                 }
161         }
162
163         // sets the row count for the related list table
164         private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, String relationshipName,
165                         String foreignKeyValue) {
166                 AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
167                         public void onFailure(Throwable caught) {
168                                 // TODO: create a way to notify users of asynchronous callback failures
169                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
170                         }
171
172                         @Override
173                         public void onSuccess(Integer result) {
174                                 if (result.intValue() <= relatedListTable.getMinNumVisibleRows()) {
175                                         // Set the table row count to the minimum row count if the data row count is less than or equal to
176                                         // the minimum row count. This ensures that data with fewer rows than the minimum will not create
177                                         // indexes in the underlying CellTable that will override the rendering of the empty rows.
178                                         relatedListTable.setRowCount(relatedListTable.getMinNumVisibleRows());
179                                 } else {
180                                         // Set the table row count to the data row count if it's larger than the minimum number of rows
181                                         // visible.
182                                         relatedListTable.setRowCount(result.intValue());
183                                 }
184                         }
185                 };
186
187                 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
188                                 foreignKeyValue, callback);
189         }
190
191         /*
192          * (non-Javadoc)
193          * 
194          * @see com.google.gwt.activity.shared.Activity#onCancel()
195          */
196         @Override
197         public void onCancel() {
198                 detailsView.clear();
199         }
200
201         /*
202          * (non-Javadoc)
203          * 
204          * @see com.google.gwt.activity.shared.Activity#onStop()
205          */
206         @Override
207         public void onStop() {
208                 detailsView.clear();
209         }
210
211         /*
212          * (non-Javadoc)
213          * 
214          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
215          */
216         @Override
217         public void goTo(Place place) {
218                 clientFactory.getPlaceController().goTo(place);
219         }
220
221 }