Rename GlomField to DataItem and update associated methods.
[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.event.TableChangeEvent;
27 import org.glom.web.client.event.TableChangeEventHandler;
28 import org.glom.web.client.place.DetailsPlace;
29 import org.glom.web.client.ui.DetailsView;
30 import org.glom.web.client.ui.details.Field;
31 import org.glom.web.client.ui.details.Portal;
32 import org.glom.web.client.ui.details.RelatedListTable;
33 import org.glom.web.shared.DetailsLayoutAndData;
34 import org.glom.web.shared.DataItem;
35 import org.glom.web.shared.layout.LayoutGroup;
36 import org.glom.web.shared.layout.LayoutItemField;
37 import org.glom.web.shared.layout.LayoutItemPortal;
38
39 import com.google.gwt.activity.shared.AbstractActivity;
40 import com.google.gwt.event.shared.EventBus;
41 import com.google.gwt.place.shared.Place;
42 import com.google.gwt.user.client.rpc.AsyncCallback;
43 import com.google.gwt.user.client.ui.AcceptsOneWidget;
44
45 /**
46  * @author Ben Konrath <ben@bagu.org>
47  */
48 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
49         private final String documentID;
50         private final String tableName;
51         private final String primaryKeyValue;
52         private final ClientFactory clientFactory;
53         private final DetailsView detailsView;
54
55         ArrayList<Field> fields;
56         ArrayList<Portal> portals;
57
58         public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
59                 this.documentID = place.getDocumentID();
60                 this.tableName = place.getTableName();
61                 this.primaryKeyValue = place.getPrimaryKeyValue();
62                 this.clientFactory = clientFactory;
63                 detailsView = clientFactory.getDetailsView();
64         }
65
66         /*
67          * (non-Javadoc)
68          * 
69          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
70          * com.google.gwt.event.shared.EventBus)
71          */
72         @Override
73         public void start(AcceptsOneWidget panel, EventBus eventBus) {
74                 // register this class as the presenter
75                 detailsView.setPresenter(this);
76
77                 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
78
79                 // set the change handler for the table selection widget
80                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
81                         @Override
82                         public void onTableChange(final TableChangeEvent event) {
83                                 goTo(new DetailsPlace(documentID, event.getTableName(), ""));
84                         }
85                 });
86
87                 // get the layout and data for the DetailsView
88                 AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
89                         public void onFailure(Throwable caught) {
90                                 // FIXME: need to deal with failure
91                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
92                         }
93
94                         @Override
95                         public void onSuccess(DetailsLayoutAndData result) {
96
97                                 // create the layout and get the array of layout item fields, data labels and layout item portals
98                                 for (LayoutGroup layoutGroup : result.getLayout()) {
99                                         detailsView.addGroup(layoutGroup);
100                                 }
101                                 fields = detailsView.getFields();
102                                 portals = detailsView.getPortals();
103
104                                 // set the data
105                                 DataItem[] data = result.getData();
106                                 if (data == null)
107                                         return;
108
109                                 // FIXME create proper client side logging
110                                 if (data.length != fields.size())
111                                         System.out.println("Warning: The number of data items doesn't match the number of data fields.");
112
113                                 for (int i = 0; i < Math.min(fields.size(), data.length); i++) {
114                                         Field field = fields.get(i);
115                                         if (data[i] != null) {
116                                                 String dataValue = data[i].getText();
117
118                                                 // set the field data
119                                                 field.setText(dataValue);
120
121                                                 // see if there are any related lists that need to be setup
122                                                 for (Portal portal : portals) {
123                                                         LayoutItemField layoutItemField = field.getLayoutItem();
124                                                         LayoutItemPortal layoutItemPortal = portal.getLayoutItem();
125                                                         if (layoutItemField.getName().equals(layoutItemPortal.getFromField())) {
126                                                                 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
127                                                                                 dataValue);
128                                                                 portal.setContents(relatedListTable);
129                                                                 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), dataValue);
130                                                         }
131                                                 }
132                                         }
133                                 }
134
135                         }
136                 };
137                 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
138                                 callback);
139
140                 // indicate that the view is ready to be displayed
141                 panel.setWidget(detailsView.asWidget());
142         }
143
144         // sets the row count for the related list table
145         private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, String relationshipName,
146                         String foreignKeyValue) {
147                 AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
148                         public void onFailure(Throwable caught) {
149                                 // FIXME: need to deal with failure
150                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
151                         }
152
153                         @Override
154                         public void onSuccess(Integer result) {
155                                 relatedListTable.setRowCount(result.intValue());
156                         }
157                 };
158
159                 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
160                                 foreignKeyValue, callback);
161         }
162
163         /*
164          * (non-Javadoc)
165          * 
166          * @see com.google.gwt.activity.shared.Activity#onCancel()
167          */
168         @Override
169         public void onCancel() {
170                 detailsView.clear();
171         }
172
173         /*
174          * (non-Javadoc)
175          * 
176          * @see com.google.gwt.activity.shared.Activity#onStop()
177          */
178         @Override
179         public void onStop() {
180                 detailsView.clear();
181         }
182
183         /*
184          * (non-Javadoc)
185          * 
186          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
187          */
188         @Override
189         public void goTo(Place place) {
190                 clientFactory.getPlaceController().goTo(place);
191         }
192
193 }