Add data to the DetailsView using a hard-coded primary key value.
[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.place.DetailsPlace;
27 import org.glom.web.client.ui.DetailsView;
28 import org.glom.web.shared.GlomField;
29 import org.glom.web.shared.layout.LayoutGroup;
30 import org.glom.web.shared.layout.LayoutItem;
31 import org.glom.web.shared.layout.LayoutItemField;
32
33 import com.google.gwt.activity.shared.AbstractActivity;
34 import com.google.gwt.event.shared.EventBus;
35 import com.google.gwt.place.shared.Place;
36 import com.google.gwt.user.client.rpc.AsyncCallback;
37 import com.google.gwt.user.client.ui.AcceptsOneWidget;
38
39 /**
40  * @author Ben Konrath <ben@bagu.org>
41  */
42 public class DetailsActivity extends AbstractActivity implements DetailsView.Presenter {
43         private final String documentTitle;
44         private final ClientFactory clientFactory;
45         private final DetailsView detailsView;
46
47         public DetailsActivity(DetailsPlace place, ClientFactory clientFactory) {
48                 this.documentTitle = place.getDocumentTitle();
49                 this.clientFactory = clientFactory;
50                 detailsView = clientFactory.getDetailsView();
51         }
52
53         /*
54          * (non-Javadoc)
55          * 
56          * @see com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client.ui.AcceptsOneWidget,
57          * com.google.gwt.event.shared.EventBus)
58          */
59         @Override
60         public void start(AcceptsOneWidget panel, EventBus eventBus) {
61                 // register this class as the presenter
62                 detailsView.setPresenter(this);
63
64                 // get the layout for the DetailsView
65                 final String selectedTable = clientFactory.getTableSelectionView().getSelectedTable();
66                 if (!selectedTable.isEmpty()) {
67                         // The table name has been set so we can use the selected table name to populate the cell table.
68                         AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
69                                 public void onFailure(Throwable caught) {
70                                         // FIXME: need to deal with failure
71                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsLayout()");
72                                 }
73
74                                 @Override
75                                 public void onSuccess(LayoutGroup result) {
76                                         addLayoutGroup(result, "");
77                                 }
78                         };
79                         OnlineGlomServiceAsync.Util.getInstance().getDetailsLayoutGroup(documentTitle, selectedTable, callback);
80                 } else {
81                         // The table name has not been set so we need to fill in the details layout using the default table for the
82                         // glom document.
83                         AsyncCallback<LayoutGroup> callback = new AsyncCallback<LayoutGroup>() {
84                                 public void onFailure(Throwable caught) {
85                                         // FIXME: need to deal with failure
86                                         System.out.println("AsyncCallback Failed: OnlineGlomService.getDefaultDetailsLayout()");
87                                 }
88
89                                 @Override
90                                 public void onSuccess(LayoutGroup result) {
91                                         addLayoutGroup(result, "");
92                                 }
93                         };
94                         OnlineGlomServiceAsync.Util.getInstance().getDefaultDetailsLayoutGroup(documentTitle, callback);
95                 }
96
97                 // get the data from the server
98                 AsyncCallback<GlomField[]> callback = new AsyncCallback<GlomField[]>() {
99                         public void onFailure(Throwable caught) {
100                                 // FIXME: need to deal with failure
101                                 System.out.println("AsyncCallback Failed: OnlineGlomService.getDetailsData()");
102                         }
103
104                         @Override
105                         public void onSuccess(GlomField[] result) {
106                                 // FIXME there's no guarantee that the layout will be ready for this
107                                 detailsView.setData(result);
108                         }
109                 };
110                 OnlineGlomServiceAsync.Util.getInstance().getDetailsData(documentTitle, selectedTable, "0", callback);
111
112                 // indicate that the view is ready to be displayed
113                 panel.setWidget(detailsView.asWidget());
114         }
115
116         /*
117          * This is just a temporary method for creating a basic layout without the flowtable/spreadtable that Glom has.
118          */
119         private void addLayoutGroup(LayoutGroup layoutGroup, String indent) {
120                 if (layoutGroup == null)
121                         return;
122
123                 // look at each child item
124                 ArrayList<LayoutItem> layoutItems = layoutGroup.getItems();
125                 for (LayoutItem layoutItem : layoutItems) {
126
127                         if (layoutItem == null)
128                                 continue;
129
130                         String title = layoutItem.getTitle();
131                         if (layoutItem instanceof LayoutItemField)
132                                 detailsView.addLayoutField(indent + title);
133                         else if (!title.isEmpty())
134                                 detailsView.addLayoutGroup(indent + title);
135
136                         // recurse into child groups
137                         if (layoutItem instanceof LayoutGroup)
138                                 addLayoutGroup((LayoutGroup) layoutItem, indent + "-- ");
139                 }
140         }
141
142         /*
143          * (non-Javadoc)
144          * 
145          * @see com.google.gwt.activity.shared.Activity#onCancel()
146          */
147         @Override
148         public void onCancel() {
149                 detailsView.clear();
150         }
151
152         /*
153          * (non-Javadoc)
154          * 
155          * @see com.google.gwt.activity.shared.Activity#onStop()
156          */
157         @Override
158         public void onStop() {
159                 detailsView.clear();
160         }
161
162         /*
163          * (non-Javadoc)
164          * 
165          * @see org.glom.web.client.ui.DetailsView.Presenter#goTo(com.google.gwt.place.shared.Place)
166          */
167         @Override
168         public void goTo(Place place) {
169                 clientFactory.getPlaceController().goTo(place);
170         }
171
172 }