Add field colouring to details view.
[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.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;
45
46 /**
47  * @author Ben Konrath <ben@bagu.org>
48  */
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;
55
56         ArrayList<Field> fields;
57         ArrayList<Portal> portals;
58
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();
65         }
66
67         /*
68          * (non-Javadoc)
69          * 
70          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
71          * com.google.gwt.event.shared.EventBus)
72          */
73         @Override
74         public void start(AcceptsOneWidget panel, EventBus eventBus) {
75                 // register this class as the presenter
76                 detailsView.setPresenter(this);
77
78                 // TODO here's where we should check for database authentication - see ListActivity.start() for how to do this
79
80                 // set the change handler for the table selection widget
81                 eventBus.addHandler(TableChangeEvent.TYPE, new TableChangeEventHandler() {
82                         @Override
83                         public void onTableChange(final TableChangeEvent event) {
84                                 goTo(new DetailsPlace(documentID, event.getTableName(), ""));
85                         }
86                 });
87
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()");
93                         }
94
95                         @Override
96                         public void onSuccess(DetailsLayoutAndData result) {
97
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);
101                                 }
102                                 fields = detailsView.getFields();
103                                 portals = detailsView.getPortals();
104
105                                 // set the data
106                                 DataItem[] data = result.getData();
107                                 if (data == null)
108                                         return;
109
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.");
113
114                                 for (int i = 0; i < Math.min(fields.size(), data.length); i++) {
115                                         Field field = fields.get(i);
116                                         if (data[i] != null) {
117
118                                                 // set the field data
119                                                 field.setData(data[i]);
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                                                                 String foreignKeyValue = Utils.getKeyValueStringForQuery(layoutItemField.getType(),
127                                                                                 data[i]);
128                                                                 if (foreignKeyValue == null)
129                                                                         continue;
130                                                                 RelatedListTable relatedListTable = new RelatedListTable(documentID, layoutItemPortal,
131                                                                                 foreignKeyValue);
132                                                                 portal.setContents(relatedListTable);
133                                                                 setRowCountForRelatedListTable(relatedListTable, layoutItemPortal.getName(),
134                                                                                 foreignKeyValue);
135                                                         }
136                                                 }
137                                         }
138                                 }
139
140                         }
141                 };
142                 OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutAndData(documentID, tableName, primaryKeyValue,
143                                 callback);
144
145                 // indicate that the view is ready to be displayed
146                 panel.setWidget(detailsView.asWidget());
147         }
148
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()");
156                         }
157
158                         @Override
159                         public void onSuccess(Integer result) {
160                                 relatedListTable.setRowCount(result.intValue());
161                         }
162                 };
163
164                 OnlineGlomServiceAsync.Util.getInstance().getRelatedListRowCount(documentID, tableName, relationshipName,
165                                 foreignKeyValue, callback);
166         }
167
168         /*
169          * (non-Javadoc)
170          * 
171          * @see com.google.gwt.activity.shared.Activity#onCancel()
172          */
173         @Override
174         public void onCancel() {
175                 detailsView.clear();
176         }
177
178         /*
179          * (non-Javadoc)
180          * 
181          * @see com.google.gwt.activity.shared.Activity#onStop()
182          */
183         @Override
184         public void onStop() {
185                 detailsView.clear();
186         }
187
188         /*
189          * (non-Javadoc)
190          * 
191          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
192          */
193         @Override
194         public void goTo(Place place) {
195                 clientFactory.getPlaceController().goTo(place);
196         }
197
198 }