AsyncMessage onFailure() callbacks: Log the exception message.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / DocumentSelectionActivity.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 org.glom.web.client.ClientFactory;
23 import org.glom.web.client.OnlineGlomServiceAsync;
24 import org.glom.web.client.ui.DocumentSelectionView;
25 import org.glom.web.client.ui.View;
26 import org.glom.web.shared.Documents;
27
28 import com.google.gwt.activity.shared.AbstractActivity;
29 import com.google.gwt.core.client.GWT;
30 import com.google.gwt.event.shared.EventBus;
31 import com.google.gwt.place.shared.Place;
32 import com.google.gwt.user.client.Window;
33 import com.google.gwt.user.client.rpc.AsyncCallback;
34 import com.google.gwt.user.client.ui.AcceptsOneWidget;
35
36 public class DocumentSelectionActivity extends AbstractActivity implements View.Presenter {
37
38         // TODO inject with GIN
39         private final ClientFactory clientFactory;
40         private final DocumentSelectionView documentSelectionView;
41
42         public DocumentSelectionActivity(final ClientFactory clientFactory) {
43                 this.clientFactory = clientFactory;
44                 this.documentSelectionView = clientFactory.getDocumentSelectionView();
45         }
46
47         @Override
48         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
49                 Window.setTitle("Online Glom");
50
51                 documentSelectionView.setPresenter(this);
52
53                 final AsyncCallback<Documents> callback = new AsyncCallback<Documents>() {
54                         @Override
55                         public void onFailure(final Throwable caught) {
56                                 // Try to get an error message. Most likely this won't work but it's worth a try.
57                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getDocuments(): " + caught.getMessage());
58                                 getAndSetErrorMessage();
59                         }
60
61                         @Override
62                         public void onSuccess(final Documents documents) {
63                                 documentSelectionView.clearHyperLinks();
64                                 if (documents.getCount() > 0) {
65                                         for (int i = 0; i < documents.getCount(); i++) {
66                                                 documentSelectionView.addDocumentLink(documents.getDocumentID(i), documents.getTitle(i));
67                                                 // TODO: Get default locale.
68                                         }
69                                 } else {
70                                         getAndSetErrorMessage();
71                                 }
72                         }
73                 };
74                 OnlineGlomServiceAsync.Util.getInstance().getDocuments(callback);
75
76                 panel.setWidget(documentSelectionView.asWidget());
77         }
78
79         @Override
80         public void goTo(final Place place) {
81                 clientFactory.getPlaceController().goTo(place);
82         }
83
84         private void getAndSetErrorMessage() {
85
86                 final AsyncCallback<String> callback = new AsyncCallback<String>() {
87                         @Override
88                         public void onFailure(final Throwable caught) {
89                                 GWT.log("AsyncCallback Failed: OnlineGlomService.getConfigurationErrorMessage(): "
90                                                 + caught.getMessage());
91                                 documentSelectionView
92                                                 .setErrorMessage("Unable to communicate with the servlet. Check the error log for more information.");
93                         }
94
95                         @Override
96                         public void onSuccess(final String errorMessage) {
97                                 documentSelectionView.setErrorMessage("Configuration Error: " + errorMessage);
98                         }
99                 };
100                 OnlineGlomServiceAsync.Util.getInstance().getConfigurationErrorMessage(callback);
101
102         }
103 }