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.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;
48 * @author Ben Konrath <ben@bagu.org>
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;
57 ArrayList<Field> fields;
58 ArrayList<Portal> portals;
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();
71 * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
72 * com.google.gwt.event.shared.EventBus)
75 public void start(AcceptsOneWidget panel, EventBus eventBus) {
76 // register this class as the presenter
77 detailsView.setPresenter(this);
79 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
81 // set the change handler for the table selection widget
82 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
84 public void onTableChange(final TableChangeEvent event) {
85 goTo(new DetailsPlace(documentID, event.getTableName(), ""));
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()");
97 public void onSuccess(DetailsLayoutAndData result) {
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);
103 fields = detailsView.getFields();
104 portals = detailsView.getPortals();
107 DataItem[] data = result.getData();
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.");
115 for (int i = 0; i < Math.min(fields.size(), data.length); i++) {
116 Field field = fields.get(i);
117 if (data[i] != null) {
119 // set the field data
120 field.setData(data[i]);
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(),
129 if (foreignKeyValue == null)
131 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
133 portal.setContents(relatedListTable);
134 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(),
143 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
146 // indicate that the view is ready to be displayed
147 panel.setWidget(detailsView.asWidget());
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()");
160 public void onSuccess(Integer result) {
161 relatedListTable.setRowCount(result.intValue());
165 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
166 foreignKeyValue, callback);
172 * @see com.google.gwt.activity.shared.Activity#onCancel()
175 public void onCancel() {
182 * @see com.google.gwt.activity.shared.Activity#onStop()
185 public void onStop() {
192 * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
195 public void goTo(Place place) {
196 clientFactory.getPlaceController().goTo(place);