PageRenderTime 32ms CodeModel.GetById 2ms app.highlight 25ms RepoModel.GetById 1ms app.codeStats 0ms

/v3.2/nimbits-tds/src/com/nimbits/client/nimbits.java

http://nimbits-server.googlecode.com/
Java | 608 lines | 391 code | 131 blank | 86 comment | 44 complexity | 883c7dcb0ceb79a64d7eb5022a7953d0 MD5 | raw file
  1/*
  2 * Copyright (c) 2010 Tonic Solutions LLC.
  3 *
  4 * http://www.nimbits.com
  5 *
  6 *
  7 * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
  8 *
  9 * http://www.gnu.org/licenses/gpl.html
 10 *
 11 * Unless required by applicable law or agreed to in writing, software distributed under the license is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 12 */
 13
 14package com.nimbits.client;
 15
 16import com.extjs.gxt.ui.client.Style.LayoutRegion;
 17import com.extjs.gxt.ui.client.widget.ContentPanel;
 18import com.extjs.gxt.ui.client.widget.Viewport;
 19import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
 20import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
 21import com.extjs.gxt.ui.client.widget.layout.FillLayout;
 22import com.google.gwt.core.client.EntryPoint;
 23import com.google.gwt.core.client.GWT;
 24import com.google.gwt.user.client.Window;
 25import com.google.gwt.user.client.Window.Location;
 26import com.google.gwt.user.client.rpc.AsyncCallback;
 27import com.google.gwt.user.client.ui.RootPanel;
 28import com.nimbits.client.controls.MainMenuToolBar;
 29import com.nimbits.client.exception.NimbitsException;
 30import com.nimbits.client.exceptions.DiagramNotFoundException;
 31import com.nimbits.client.exceptions.NotLoggedInException;
 32import com.nimbits.client.exceptions.ObjectProtectionException;
 33import com.nimbits.client.model.ClientType;
 34import com.nimbits.client.model.Const;
 35import com.nimbits.client.model.LoginInfo;
 36import com.nimbits.client.model.category.Category;
 37import com.nimbits.client.model.diagram.Diagram;
 38import com.nimbits.client.model.point.Point;
 39import com.nimbits.client.panels.*;
 40import com.nimbits.client.service.LoginService;
 41import com.nimbits.client.service.LoginServiceAsync;
 42import com.nimbits.client.service.category.CategoryService;
 43import com.nimbits.client.service.category.CategoryServiceAsync;
 44import com.nimbits.client.service.datapoints.PointService;
 45import com.nimbits.client.service.datapoints.PointServiceAsync;
 46import com.nimbits.client.service.diagram.DiagramService;
 47import com.nimbits.client.service.diagram.DiagramServiceAsync;
 48import com.nimbits.client.service.recordedvalues.RecordedValueService;
 49import com.nimbits.client.service.recordedvalues.RecordedValueServiceAsync;
 50import com.nimbits.client.service.settings.SettingsService;
 51import com.nimbits.client.service.settings.SettingsServiceAsync;
 52import com.nimbits.client.service.twitter.TwitterService;
 53import com.nimbits.client.service.twitter.TwitterServiceAsync;
 54import com.nimbits.shared.Utils;
 55
 56import java.util.*;
 57
 58import static com.google.gwt.user.client.Window.alert;
 59
 60/**
 61 * Entry point classes define <code>onModuleLoad()</code>
 62 */
 63public class nimbits implements EntryPoint {
 64
 65    private MainPanel mainPanel;
 66    private LoginInfo loginInfo = null;
 67    private Viewport viewport;
 68    private final static String heading = (Const.CONST_SERVER_NAME + " " + Const.CONST_SERVER_VERSION);
 69
 70
 71    private void loadLayout(final LoginInfo loginInfo,
 72                            final Map<String, String> settings) throws NimbitsException {
 73
 74        final ContentPanel contentPanel = new ContentPanel(new FillLayout());
 75
 76        final String logoutUrl = (loginInfo != null) ? loginInfo.getLogoutUrl() : Const.PATH_NIMBITS_HOME;
 77
 78//        final String welcomeUrl = (settings != null && settings.containsKey(Const.PARAM_WELCOME_URL)) ? settings.get(Const.PARAM_WELCOME_URL) :  Const.PATH_WELCOME_URL;
 79        final boolean loadConnections = (settings != null && settings.containsKey(Const.PARAM_ENABLE_CONNECTIONS)
 80                && settings.get(Const.PARAM_ENABLE_CONNECTIONS).equals("1"));
 81
 82
 83        viewport = new Viewport();
 84        viewport.setLayout(new BorderLayout());
 85        viewport.setBorders(false);
 86
 87        mainPanel = new MainPanel(loginInfo, loadConnections);
 88
 89        contentPanel.setHeaderVisible(true);
 90        contentPanel.setHeading(heading + " " + loginInfo.getEmailAddress().getValue());
 91        contentPanel.setTopComponent(new MainMenuToolBar(logoutUrl, loginInfo, settings));
 92        contentPanel.add(mainPanel);
 93        contentPanel.setLayout(new FillLayout());
 94        addListeners();
 95
 96        viewport.add(contentPanel, new BorderLayoutData(LayoutRegion.CENTER));
 97        viewport.setHeight("100%");
 98        RootPanel.get("main").add(viewport);
 99
100    }
101
102    private void loadDiagramView(final Diagram diagram,
103                                 final ClientType clientType) {
104
105        viewport = new Viewport();
106        viewport.setLayout(new BorderLayout());
107        viewport.setBorders(false);
108
109        final ContentPanel contentPanel = new ContentPanel(new FillLayout());
110        contentPanel.setHeaderVisible(true);
111        contentPanel.setHeading(Const.HTML_HOME_LINK + " | " + heading + " "
112                + diagram.getName());
113
114        diagram.setFullScreenView(true);
115
116        final DiagramPanel diagramPanel = new DiagramPanel(diagram, false, Window.getClientWidth(), Window.getClientHeight());
117        diagramPanel.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
118
119            @Override
120            public void onPointClicked(final Point p) {
121
122                if (clientType == ClientType.other) {
123                    showAnnotatedTimeLine(p);
124                } else {
125                    Window.Location.replace("?" + Const.PARAM_CLIENT + "=" + Const.WORD_ANDROID + "&" + Const.PARAM_POINT + "=" + p.getName());
126                }
127
128            }
129
130        });
131
132        diagramPanel.addDiagramClickedListeners(new NavigationEventProvider.DiagramClickedListener() {
133
134            @Override
135
136            public void onDiagramClicked(final Diagram d) {
137
138                RootPanel.get().remove(viewport);
139                loadDiagramView(d, clientType);
140
141
142            }
143
144        });
145
146        diagramPanel.addUrlClickedListeners(new NavigationEventProvider.UrlClickedListener() {
147
148            @Override
149            public void onUrlClicked(String url, String target) {
150                Window.Location.replace(url);
151            }
152        });
153        diagramPanel.setHeight("100%");
154        contentPanel.add(diagramPanel);
155        contentPanel.setLayout(new FillLayout());
156        viewport.add(contentPanel, new BorderLayoutData(LayoutRegion.CENTER));
157        RootPanel.get().add(viewport);
158
159    }
160
161    void showAnnotatedTimeLine(final Point point) {
162
163        final com.extjs.gxt.ui.client.widget.Window w = new com.extjs.gxt.ui.client.widget.Window();
164        final RecordedValueServiceAsync dataService = GWT.create(RecordedValueService.class);
165        final ContentPanel p = new ContentPanel();
166        p.setHeading(point.getName().getValue());
167
168        final List<Point> points = Arrays.asList(point);
169        //the chart panel will determine the end date for the first show
170        final AnnotatedTimeLinePanel annotatedTimeLinePanel = new AnnotatedTimeLinePanel(false, Const.DEFAULT_CHART_NAME);
171        //  final Date start = new Date(result.getTime() - (1000 * 60 * 60 * 24) );
172        // annotatedTimeLinePanel.setTimespan(new TimespanModel(start, result));
173        //  annotatedTimeLinePanel.setPoints(points);
174
175        p.add(annotatedTimeLinePanel);
176        p.setWidth(600);
177        p.setHeight(400);
178        annotatedTimeLinePanel.initChart();
179        annotatedTimeLinePanel.addPoint(point);
180        w.add(p);
181        w.setHeight(400);
182        w.setWidth(600);
183        w.show();
184
185//        dataService.getLastRecordedDate(points, new AsyncCallback<Date>() {
186//            @Override
187//            public void onFailure(Throwable caught) {
188//
189//            }
190//
191//            @Override
192//            public void onSuccess(final Date result) {
193//
194//
195//            }
196//
197//
198//        });
199    }
200
201    private void addListeners() {
202
203        mainPanel.addCategoryClickedListeners(new NavigationEventProvider.CategoryClickedListener() {
204            //need to getInstance a fresh copy here
205            @Override
206            public void onCategoryClicked(final Category c, final boolean readOnly) throws NimbitsException {
207
208                final CategoryServiceAsync categoryService = GWT.create(CategoryService.class);
209                categoryService.getCategoryByName(c.getName(), true, true, new AsyncCallback<Category>() {
210
211
212                    @Override
213                    public void onFailure(final Throwable throwable) {
214
215                    }
216
217                    @Override
218                    public void onSuccess(final Category category) {
219
220                        if (category.getPoints() != null) {
221                            for (final Point p : category.getPoints()) {
222                                p.setReadOnly(readOnly);
223                                mainPanel.addPoint(p);
224                            }
225                        }
226                    }
227                });
228            }
229        });
230
231        mainPanel.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
232
233            @Override
234            public void onPointClicked(final Point p) {
235                mainPanel.addPoint(p);
236            }
237
238        });
239
240        mainPanel.addDiagramClickedListeners(new NavigationEventProvider.DiagramClickedListener() {
241
242            @Override
243            public void onDiagramClicked(final Diagram d) {
244                mainPanel.addDiagram(d);
245
246                // showDiagram(d);
247            }
248
249        });
250    }
251
252//    private void showDiagram(final Diagram d) {
253//        final int h = Window.getClientHeight();
254//        final int w = Window.getClientWidth();
255//        final com.extjs.gxt.ui.client.widget.Window window = new com.extjs.gxt.ui.client.widget.Window();
256//        final DiagramPanel diagramPanel = new DiagramPanel(d, Double.valueOf(h * .65).intValue(), Double.valueOf(w * .65).intValue());
257//
258//        diagramPanel.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
259//
260//            @Override
261//            public void onPointClicked(final Point p) {
262//                nimbits.this.mainPanel.addPoint(p);
263//            }
264//
265//        });
266//        diagramPanel.addDiagramClickedListeners(new NavigationEventProvider.DiagramClickedListener() {
267//
268//
269//            @Override
270//            public void onDiagramClicked(final Diagram d, final String target) {
271//                window.hide();
272//                showDiagram(d);
273//            }
274//        });
275//
276//        window.setHeight(Double.valueOf(h * .65).intValue());
277//        window.setWidth(Double.valueOf(w * .65).intValue());
278//        window.setHeading(d.getName().getValue());
279//        window.add(diagramPanel);
280//        window.getHeader().addTool(
281//                new ToolButton("x-tool-maximize",
282//                        new SelectionListener<IconButtonEvent>() {
283//                            boolean isMax;
284//
285//                            @Override
286//                            public void componentSelected(IconButtonEvent ce) {
287//                                window.setHeight(h);
288//                                window.setWidth(w);
289//                                diagramPanel.resizePanel(h, w);
290//                                window.setPagePosition(0, 0);
291//
292//
293//                            }
294//                        }));
295//        window.show();
296//    }
297
298    public void onModuleLoad() {
299
300
301        // final String point = Location.getParameter(Const.PARAM_POINT);
302        // final String user = Location.getParameter("user");
303        final String uuid = Location.getParameter(Const.PARAM_UUID);
304        // final String source = Location.getParameter("source");
305        final String fb = Location.getParameter(Const.PARAM_FACEBOOK);
306        final String code = Location.getParameter(Const.PARAM_CODE);
307        final String tw = Location.getParameter(Const.PARAM_TWITTER);
308        final String oauth_token = Location.getParameter(Const.PARAM_OAUTH);
309        final String diagramUUID = Location.getParameter(Const.PARAM_DIAGRAM);
310
311        final String clientTypeParam = Location.getParameter(Const.PARAM_CLIENT);
312
313
314        final boolean doFacebook = ((fb != null) || (code != null));
315        final boolean doTwitter = ((tw != null) && (oauth_token == null));
316        final boolean doTwitterFinish = ((tw != null) && (oauth_token != null));
317        final boolean doDiagram = (diagramUUID != null);
318
319        final ClientType clientType;
320
321        if (!Utils.isEmptyString(clientTypeParam) && clientTypeParam.equals(Const.WORD_ANDROID)) {
322            clientType = ClientType.android;
323        } else {
324            clientType = ClientType.other;
325
326        }
327
328        if (doDiagram) {
329            processDiagramRequest(diagramUUID, clientType);
330        } else {
331            try {
332                loadPortalView(uuid, code, oauth_token, doFacebook, doTwitter, doTwitterFinish);
333            } catch (NimbitsException e) {
334                Window.alert(e.getMessage());
335            }
336        }
337
338    }
339
340    private void processDiagramRequest(final String diagramName, final ClientType clientType) {
341        DiagramServiceAsync diagramService = GWT.create(DiagramService.class);
342        try {
343            diagramService.getDiagramByUuid(diagramName, new AsyncCallback<Diagram>() {
344                @Override
345                public void onFailure(Throwable throwable) {
346                    handleError(throwable);
347                }
348
349                @Override
350                public void onSuccess(final Diagram diagram) {
351                    loadDiagramView(diagram, clientType);
352                }
353            });
354        } catch (ObjectProtectionException e) {
355            handleError(e);
356        } catch (DiagramNotFoundException e) {
357            handleError(e);
358        } catch (NimbitsException e) {
359            handleError(e);
360        }
361    }
362
363
364    private void loadPortalView(final String uuid, final String code, final String oauth_token, final boolean doFacebook, final boolean doTwitter, final boolean doTwitterFinish) throws NimbitsException {
365        SettingsServiceAsync settingService = GWT.create(SettingsService.class);
366        settingService.getSettings(new AsyncCallback<Map<String, String>>() {
367
368            @Override
369            public void onFailure(Throwable caught) {
370                GWT.log(caught.getMessage(), caught);
371                Window.Location.replace(loginInfo.getLogoutUrl());
372            }
373
374            @Override
375            public void onSuccess(final Map<String, String> settings) {
376
377                if (uuid != null) {
378                    try {
379                        loadSinglePointDisplay(uuid);
380                    } catch (NimbitsException e) {
381                        Window.alert(e.getMessage());
382                    }
383                } else if (doFacebook) {
384                    finishFacebookAuthentication(settings, code);
385                } else if (doTwitterFinish) {
386                    try {
387                        finishTwitterAuthentication(settings, oauth_token, doTwitter);
388                    } catch (NimbitsException e) {
389                        Window.alert(e.getMessage());
390                    }
391                } else {
392
393                    try {
394                        loadPortal(doTwitter, settings);
395                    } catch (NimbitsException e) {
396                        Window.alert(e.getMessage());
397                    }
398
399                }
400
401            }
402
403        });
404    }
405
406    private void finishFacebookAuthentication(final Map<String, String> settings, final String code) {
407        getViewport();
408        FacebookPanel fbPanel = new FacebookPanel(code, settings);
409        fbPanel.setHeight(500);
410        fbPanel.setWidth(600);
411        viewport.add(fbPanel);
412        viewport.setWidth(600);
413        viewport.setHeight(500);
414        RootPanel.get("main").add(viewport);
415    }
416
417    private void finishTwitterAuthentication(final Map<String, String> settings, final String oauth_token, final boolean doTwitter) throws NimbitsException {
418        TwitterServiceAsync twitterService = GWT.create(TwitterService.class);
419
420
421        twitterService.updateUserToken(oauth_token,
422                new AsyncCallback<Void>() {
423
424                    @Override
425                    public void onFailure(Throwable caught) {
426                        handleError(caught);
427
428                    }
429
430                    @Override
431                    public void onSuccess(Void result) {
432                        Window.alert(Const.MESSAGE_TWITTER_ADDED);
433                        try {
434                            loadPortal(doTwitter, settings);
435                        } catch (NimbitsException e) {
436                            Window.alert(e.getMessage());
437                        }
438                    }
439
440                });
441
442    }
443
444    private void getViewport() {
445        viewport = new Viewport();
446        viewport.setLayout(new FillLayout());
447        viewport.setBorders(false);
448    }
449
450    private void loadSinglePointDisplay(final String uuid) throws NimbitsException {
451        PointServiceAsync pointService;
452        pointService = GWT.create(PointService.class);
453
454        pointService.getPointByUUID(uuid, new AsyncCallback<Point>() {
455            @Override
456            public void onFailure(Throwable throwable) {
457                handleError(throwable);
458            }
459
460            @Override
461            public void onSuccess(Point point) {
462
463                try {
464                    loadData(point);
465                } catch (NimbitsException e) {
466                    alert(e.getMessage());
467                }
468
469            }
470        });
471    }
472
473    private void loadPortal(final boolean doTwitter, final Map<String, String> settings) throws NimbitsException {
474        LoginServiceAsync loginService = GWT
475                .create(LoginService.class);
476        loginService.login(GWT.getHostPageBaseURL(),
477                new AsyncCallback<LoginInfo>() {
478                    @Override
479                    public void onFailure(Throwable error) {
480                        GWT.log(error.getMessage(), error);
481                        handleError(error);
482                    }
483
484                    @Override
485                    public void onSuccess(LoginInfo result) {
486
487
488                        loginInfo = result;
489
490                        if (loginInfo.isLoggedIn()) {
491
492                            try {
493                                loadLayout(loginInfo, settings);
494                            } catch (NimbitsException ignored) {
495
496                            }
497                            if (doTwitter) {
498                                final TwitterServiceAsync twitterService = GWT.create(TwitterService.class);
499
500                                try {
501                                    twitterService.twitterAuthorise(loginInfo.getEmailAddress(), new AsyncCallback<String>() {
502
503                                        @Override
504                                        public void onFailure(Throwable caught) {
505                                            GWT.log(caught.getMessage(), caught);
506                                        }
507
508                                        @Override
509                                        public void onSuccess(String result) {
510
511                                            Location.replace(result);
512                                        }
513
514                                    });
515                                } catch (NimbitsException e) {
516                                    Window.alert("There was a problem loading the settings from the server.");
517                                }
518
519
520                            }
521
522                        } else {
523                            loadLogin();
524                        }
525                    }
526
527                });
528    }
529
530    private void loadData(final Point point) throws NimbitsException {
531        final List<Point> points = new ArrayList<Point>();
532        final ContentPanel mainContentPanel = new ContentPanel();
533        points.add(point);
534        getViewport();
535
536        mainContentPanel.setHeading(point.getName() + "  " + point.getDescription());
537        viewport.add(mainContentPanel);
538        RootPanel.get("main").add(viewport);
539        LoginServiceAsync loginService = GWT.create(LoginService.class);
540
541        loginService.login(GWT.getHostPageBaseURL(),
542                new AsyncCallback<LoginInfo>() {
543                    @Override
544                    public void onFailure(Throwable error) {
545                        handleError(error);
546                    }
547
548                    @Override
549                    public void onSuccess(LoginInfo result) {
550                        loginInfo = result;
551                        if ((loginInfo.isLoggedIn() && loginInfo.getUser().getId() == point.getUserFK()) || point.isPublic()) {
552                            loadChart(points, mainContentPanel);
553                        } else {
554                            loadLogin();
555                        }
556                    }
557
558                });
559
560
561    }
562
563    private void loadChart(final List<Point> points, final ContentPanel p) {
564        RecordedValueServiceAsync dataService;
565        dataService = GWT.create(RecordedValueService.class);
566
567        dataService.getLastRecordedDate(points, new AsyncCallback<Date>() {
568
569            @Override
570            public void onFailure(Throwable caught) {
571
572
573            }
574
575            @Override
576            public void onSuccess(Date result) {
577
578                final AnnotatedTimeLinePanel annotatedTimeLinePanel = new AnnotatedTimeLinePanel(false, Const.DEFAULT_CHART_NAME);
579                // annotatedTimeLinePanel.setPoints(points);
580                p.add(annotatedTimeLinePanel);
581                p.setWidth(viewport.getWidth());
582                p.setHeight(viewport.getHeight());
583                annotatedTimeLinePanel.initChart();
584                viewport.layout(true);
585            }
586        });
587    }
588
589    private void loadLogin() {
590
591        Window.Location.replace(loginInfo.getLoginUrl());
592
593    }
594
595    private void handleError(Throwable error) {
596
597
598        if (error instanceof NotLoggedInException) {
599            Window.Location.replace(loginInfo.getLogoutUrl());
600        } else if (error instanceof ObjectProtectionException) {
601            Window.Location.replace(Const.PATH_OBJECT_PROTECTION_URL);
602        } else {
603            Window.alert(error.getMessage());
604
605            // Window.Location.replace(Const.PATH_NIMBITS_HOME);
606        }
607    }
608}