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