Use GWT.log for client-side debugging statements.
[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.getTableName(), ""));
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                                 // create the layout and get the array of layout item fields, data labels and layout item portals
100                                 for (LayoutGroup layoutGroup : result.getLayout()) {
101                                         detailsView.addGroup(layoutGroup);
102                                 }
103                                 fields = detailsView.getFields();
104                                 portals = detailsView.getPortals();
105
106                                 // set the data
107                                 DataItem[] data = result.getData();
108                                 if (data == null)
109                                         return;
110
111                                 // FIXME create proper client side logging
112                                 if (data.length != fields.size())
113                                         GWT.log("Warning: The number of data items doesn't match the number of data fields.");
114
115                                 for (int i = 0; i < Math.min(fields.size(), data.length); i++) {
116                                         Field field = fields.get(i);
117                                         if (data[i] != null) {
118
119                                                 // set the field data
120                                                 field.setData(data[i]);
121
122                                                 // see if there are any related lists that need to be setup
123                                                 for (Portal portal : portals) {
124                                                         LayoutItemField layoutItemField = field.getLayoutItem();
125                                                         LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
126                                                         if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
127                                                                 String foreignKeyValue = Utils.getKeyValueStringForQuery(layoutItemField.getType(),
128                                                                                 data[i]);
129                                                                 if (foreignKeyValue == null)
130                                                                         continue;
131                                                                 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
132                                                                                 foreignKeyValue);
133                                                                 portal.setContents(relatedListTable);
134                                                                 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(),
135                                                                                 foreignKeyValue);
136                                                         }
137                                                 }
138                                         }
139                                 }
140
141                         }
142                 };
143                 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
144                                 callback);
145
146                 // indicate that the view is ready to be displayed
147                 panel.setWidget(detailsView.asWidget());
148         }
149
150         // sets the row count for the related list table
151         private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, String relationshipName,
152                         String foreignKeyValue) {
153                 AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
154                         public void onFailure(Throwable caught) {
155                                 // TODO: create a way to notify users of asynchronous callback failures
156                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
157                         }
158
159                         @Override
160                         public void onSuccess(Integer result) {
161                                 relatedListTable.setRowCount(result.intValue());
162                         }
163                 };
164
165                 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
166                                 foreignKeyValue, callback);
167         }
168
169         /*
170          * (non-Javadoc)
171          * 
172          * @see com.google.gwt.activity.shared.Activity#onCancel()
173          */
174         @Override
175         public void onCancel() {
176                 detailsView.clear();
177         }
178
179         /*
180          * (non-Javadoc)
181          * 
182          * @see com.google.gwt.activity.shared.Activity#onStop()
183          */
184         @Override
185         public void onStop() {
186                 detailsView.clear();
187         }
188
189         /*
190          * (non-Javadoc)
191          * 
192          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
193          */
194         @Override
195         public void goTo(Place place) {
196                 clientFactory.getPlaceController().goTo(place);
197         }
198
199 }