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.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;
40 import com.google.gwt.activity.shared.AbstractActivity;
41 import com.google.gwt.event.shared.EventBus;
42 import com.google.gwt.place.shared.Place;
43 import com.google.gwt.user.client.rpc.AsyncCallback;
44 import com.google.gwt.user.client.ui.AcceptsOneWidget;
47 * @author Ben Konrath <ben@bagu.org>
49 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
50 private final String documentID;
51 private final String tableName;
52 private final String primaryKeyValue;
53 private final ClientFactory clientFactory;
54 private final DetailsView detailsView;
56 ArrayList<Field> fields;
57 ArrayList<Portal> portals;
59 public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
60 this.documentID = place.getDocumentID();
61 this.tableName = place.getTableName();
62 this.primaryKeyValue = place.getPrimaryKeyValue();
63 this.clientFactory = clientFactory;
64 detailsView = clientFactory.getDetailsView();
70 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
71 * com.google.gwt.event.shared.EventBus)
74 public void start(AcceptsOneWidget panel, EventBus eventBus) {
75 // register this class as the presenter
76 detailsView.setPresenter(this);
78 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
80 // set the change handler for the table selection widget
81 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
83 public void onTableChange(final TableChangeEvent event) {
84 goTo(new DetailsPlace(documentID, event.getTableName(), ""));
88 // get the layout and data for the DetailsView
89 AsyncCallback<DetailsLayoutAndData> callback = new AsyncCallback<DetailsLayoutAndData>() {
90 public void onFailure(Throwable caught) {
91 // FIXME: need to deal with failure
92 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayoutAndData()");
96 public void onSuccess(DetailsLayoutAndData result) {
98 // create the layout and get the array of layout item fields, data labels and layout item portals
99 for (LayoutGroup layoutGroup : result.getLayout()) {
100 detailsView.addGroup(layoutGroup);
102 fields = detailsView.getFields();
103 portals = detailsView.getPortals();
106 DataItem[] data = result.getData();
110 // FIXME create proper client side logging
111 if (data.length != fields.size())
112 System.out.println("Warning: The number of data items doesn't match the number of data fields.");
114 for (int i = 0; i < Math.min(fields.size(), data.length); i++) {
115 Field field = fields.get(i);
116 if (data[i] != null) {
118 // set the field data
119 field.setData(data[i]);
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 String foreignKeyValue = Utils.getKeyValueStringForQuery(layoutItemField.getType(),
128 if (foreignKeyValue == null)
130 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
132 portal.setContents(relatedListTable);
133 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(),
142 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
145 // indicate that the view is ready to be displayed
146 panel.setWidget(detailsView.asWidget());
149 // sets the row count for the related list table
150 private void setRowCountForRelatedListTable(final RelatedListTable relatedListTable, String relationshipName,
151 String foreignKeyValue) {
152 AsyncCallback<Integer> callback = new AsyncCallback<Integer>() {
153 public void onFailure(Throwable caught) {
154 // FIXME: need to deal with failure
155 System.out.println("AsyncCallback Failed: OnlineGlomService.getRelatedListRowCount()");
159 public void onSuccess(Integer result) {
160 relatedListTable.setRowCount(result.intValue());
164 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
165 foreignKeyValue, callback);
171 * @see com.google.gwt.activity.shared.Activity#onCancel()
174 public void onCancel() {
181 * @see com.google.gwt.activity.shared.Activity#onStop()
184 public void onStop() {
191 * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
194 public void goTo(Place place) {
195 clientFactory.getPlaceController().goTo(place);