2 * Copyright (C) 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.client.activity;
22 import java.util.ArrayList;
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;
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;
46 * @author Ben Konrath <ben@bagu.org>
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;
55 ArrayList<Field> fields;
56 ArrayList<Portal> portals;
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();
69 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
70 * com.google.gwt.event.shared.EventBus)
73 public void start(AcceptsOneWidget panel, EventBus eventBus) {
74 // register this class as the presenter
75 detailsView.setPresenter(this);
77 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
79 // set the change handler for the table selection widget
80 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
82 public void onTableChange(final TableChangeEvent event) {
83 goTo(new DetailsPlace(documentID, event.getTableName(), ""));
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()");
95 public void onSuccess(DetailsLayoutAndData result) {
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);
101 fields = detailsView.getFields();
102 portals = detailsView.getPortals();
105 DataItem[] data = result.getData();
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.");
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();
118 // set the field data
119 field.setText(dataValue);
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,
128 portal.setContents(relatedListTable);
129 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(), dataValue);
137 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
140 // indicate that the view is ready to be displayed
141 panel.setWidget(detailsView.asWidget());
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()");
154 public void onSuccess(Integer result) {
155 relatedListTable.setRowCount(result.intValue());
159 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
160 foreignKeyValue, callback);
166 * @see com.google.gwt.activity.shared.Activity#onCancel()
169 public void onCancel() {
176 * @see com.google.gwt.activity.shared.Activity#onStop()
179 public void onStop() {
186 * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
189 public void goTo(Place place) {
190 clientFactory.getPlaceController().goTo(place);