/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. package com.nimbits.client;
  14. import com.extjs.gxt.ui.client.Style.LayoutRegion;
  15. import com.extjs.gxt.ui.client.widget.ContentPanel;
  16. import com.extjs.gxt.ui.client.widget.Viewport;
  17. import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
  18. import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
  19. import com.extjs.gxt.ui.client.widget.layout.FillLayout;
  20. import com.google.gwt.core.client.EntryPoint;
  21. import com.google.gwt.core.client.GWT;
  22. import com.google.gwt.user.client.Window;
  23. import com.google.gwt.user.client.Window.Location;
  24. import com.google.gwt.user.client.rpc.AsyncCallback;
  25. import com.google.gwt.user.client.ui.RootPanel;
  26. import com.nimbits.client.controls.MainMenuToolBar;
  27. import com.nimbits.client.exception.NimbitsException;
  28. import com.nimbits.client.exceptions.DiagramNotFoundException;
  29. import com.nimbits.client.exceptions.NotLoggedInException;
  30. import com.nimbits.client.exceptions.ObjectProtectionException;
  31. import com.nimbits.client.model.ClientType;
  32. import com.nimbits.client.model.Const;
  33. import com.nimbits.client.model.LoginInfo;
  34. import com.nimbits.client.model.category.Category;
  35. import com.nimbits.client.model.diagram.Diagram;
  36. import com.nimbits.client.model.point.Point;
  37. import com.nimbits.client.panels.*;
  38. import com.nimbits.client.service.LoginService;
  39. import com.nimbits.client.service.LoginServiceAsync;
  40. import com.nimbits.client.service.category.CategoryService;
  41. import com.nimbits.client.service.category.CategoryServiceAsync;
  42. import com.nimbits.client.service.datapoints.PointService;
  43. import com.nimbits.client.service.datapoints.PointServiceAsync;
  44. import com.nimbits.client.service.diagram.DiagramService;
  45. import com.nimbits.client.service.diagram.DiagramServiceAsync;
  46. import com.nimbits.client.service.recordedvalues.RecordedValueService;
  47. import com.nimbits.client.service.recordedvalues.RecordedValueServiceAsync;
  48. import com.nimbits.client.service.settings.SettingsService;
  49. import com.nimbits.client.service.settings.SettingsServiceAsync;
  50. import com.nimbits.client.service.twitter.TwitterService;
  51. import com.nimbits.client.service.twitter.TwitterServiceAsync;
  52. import com.nimbits.shared.Utils;
  53. import java.util.*;
  54. import static com.google.gwt.user.client.Window.alert;
  55. /**
  56. * Entry point classes define <code>onModuleLoad()</code>
  57. */
  58. public class nimbits implements EntryPoint {
  59. private MainPanel mainPanel;
  60. private LoginInfo loginInfo = null;
  61. private Viewport viewport;
  62. private final static String heading = (Const.CONST_SERVER_NAME + " " + Const.CONST_SERVER_VERSION);
  63. private void loadLayout(final LoginInfo loginInfo,
  64. final Map<String, String> settings) throws NimbitsException {
  65. final ContentPanel contentPanel = new ContentPanel(new FillLayout());
  66. final String logoutUrl = (loginInfo != null) ? loginInfo.getLogoutUrl() : Const.PATH_NIMBITS_HOME;
  67. // final String welcomeUrl = (settings != null && settings.containsKey(Const.PARAM_WELCOME_URL)) ? settings.get(Const.PARAM_WELCOME_URL) : Const.PATH_WELCOME_URL;
  68. final boolean loadConnections = (settings != null && settings.containsKey(Const.PARAM_ENABLE_CONNECTIONS)
  69. && settings.get(Const.PARAM_ENABLE_CONNECTIONS).equals("1"));
  70. viewport = new Viewport();
  71. viewport.setLayout(new BorderLayout());
  72. viewport.setBorders(false);
  73. mainPanel = new MainPanel(loginInfo, loadConnections);
  74. contentPanel.setHeaderVisible(true);
  75. contentPanel.setHeading(heading + " " + loginInfo.getEmailAddress().getValue());
  76. contentPanel.setTopComponent(new MainMenuToolBar(logoutUrl, loginInfo, settings));
  77. contentPanel.add(mainPanel);
  78. contentPanel.setLayout(new FillLayout());
  79. addListeners();
  80. viewport.add(contentPanel, new BorderLayoutData(LayoutRegion.CENTER));
  81. viewport.setHeight("100%");
  82. RootPanel.get("main").add(viewport);
  83. }
  84. private void loadDiagramView(final Diagram diagram,
  85. final ClientType clientType) {
  86. viewport = new Viewport();
  87. viewport.setLayout(new BorderLayout());
  88. viewport.setBorders(false);
  89. final ContentPanel contentPanel = new ContentPanel(new FillLayout());
  90. contentPanel.setHeaderVisible(true);
  91. contentPanel.setHeading(Const.HTML_HOME_LINK + " | " + heading + " "
  92. + diagram.getName());
  93. diagram.setFullScreenView(true);
  94. final DiagramPanel diagramPanel = new DiagramPanel(diagram, false, Window.getClientWidth(), Window.getClientHeight());
  95. diagramPanel.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
  96. @Override
  97. public void onPointClicked(final Point p) {
  98. if (clientType == ClientType.other) {
  99. showAnnotatedTimeLine(p);
  100. } else {
  101. Window.Location.replace("?" + Const.PARAM_CLIENT + "=" + Const.WORD_ANDROID + "&" + Const.PARAM_POINT + "=" + p.getName());
  102. }
  103. }
  104. });
  105. diagramPanel.addDiagramClickedListeners(new NavigationEventProvider.DiagramClickedListener() {
  106. @Override
  107. public void onDiagramClicked(final Diagram d) {
  108. RootPanel.get().remove(viewport);
  109. loadDiagramView(d, clientType);
  110. }
  111. });
  112. diagramPanel.addUrlClickedListeners(new NavigationEventProvider.UrlClickedListener() {
  113. @Override
  114. public void onUrlClicked(String url, String target) {
  115. Window.Location.replace(url);
  116. }
  117. });
  118. diagramPanel.setHeight("100%");
  119. contentPanel.add(diagramPanel);
  120. contentPanel.setLayout(new FillLayout());
  121. viewport.add(contentPanel, new BorderLayoutData(LayoutRegion.CENTER));
  122. RootPanel.get().add(viewport);
  123. }
  124. void showAnnotatedTimeLine(final Point point) {
  125. final com.extjs.gxt.ui.client.widget.Window w = new com.extjs.gxt.ui.client.widget.Window();
  126. final RecordedValueServiceAsync dataService = GWT.create(RecordedValueService.class);
  127. final ContentPanel p = new ContentPanel();
  128. p.setHeading(point.getName().getValue());
  129. final List<Point> points = Arrays.asList(point);
  130. //the chart panel will determine the end date for the first show
  131. final AnnotatedTimeLinePanel annotatedTimeLinePanel = new AnnotatedTimeLinePanel(false, Const.DEFAULT_CHART_NAME);
  132. // final Date start = new Date(result.getTime() - (1000 * 60 * 60 * 24) );
  133. // annotatedTimeLinePanel.setTimespan(new TimespanModel(start, result));
  134. // annotatedTimeLinePanel.setPoints(points);
  135. p.add(annotatedTimeLinePanel);
  136. p.setWidth(600);
  137. p.setHeight(400);
  138. annotatedTimeLinePanel.initChart();
  139. annotatedTimeLinePanel.addPoint(point);
  140. w.add(p);
  141. w.setHeight(400);
  142. w.setWidth(600);
  143. w.show();
  144. // dataService.getLastRecordedDate(points, new AsyncCallback<Date>() {
  145. // @Override
  146. // public void onFailure(Throwable caught) {
  147. //
  148. // }
  149. //
  150. // @Override
  151. // public void onSuccess(final Date result) {
  152. //
  153. //
  154. // }
  155. //
  156. //
  157. // });
  158. }
  159. private void addListeners() {
  160. mainPanel.addCategoryClickedListeners(new NavigationEventProvider.CategoryClickedListener() {
  161. //need to getInstance a fresh copy here
  162. @Override
  163. public void onCategoryClicked(final Category c, final boolean readOnly) throws NimbitsException {
  164. final CategoryServiceAsync categoryService = GWT.create(CategoryService.class);
  165. categoryService.getCategoryByName(c.getName(), true, true, new AsyncCallback<Category>() {
  166. @Override
  167. public void onFailure(final Throwable throwable) {
  168. }
  169. @Override
  170. public void onSuccess(final Category category) {
  171. if (category.getPoints() != null) {
  172. for (final Point p : category.getPoints()) {
  173. p.setReadOnly(readOnly);
  174. mainPanel.addPoint(p);
  175. }
  176. }
  177. }
  178. });
  179. }
  180. });
  181. mainPanel.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
  182. @Override
  183. public void onPointClicked(final Point p) {
  184. mainPanel.addPoint(p);
  185. }
  186. });
  187. mainPanel.addDiagramClickedListeners(new NavigationEventProvider.DiagramClickedListener() {
  188. @Override
  189. public void onDiagramClicked(final Diagram d) {
  190. mainPanel.addDiagram(d);
  191. // showDiagram(d);
  192. }
  193. });
  194. }
  195. // private void showDiagram(final Diagram d) {
  196. // final int h = Window.getClientHeight();
  197. // final int w = Window.getClientWidth();
  198. // final com.extjs.gxt.ui.client.widget.Window window = new com.extjs.gxt.ui.client.widget.Window();
  199. // final DiagramPanel diagramPanel = new DiagramPanel(d, Double.valueOf(h * .65).intValue(), Double.valueOf(w * .65).intValue());
  200. //
  201. // diagramPanel.addPointClickedListeners(new NavigationEventProvider.PointClickedListener() {
  202. //
  203. // @Override
  204. // public void onPointClicked(final Point p) {
  205. // nimbits.this.mainPanel.addPoint(p);
  206. // }
  207. //
  208. // });
  209. // diagramPanel.addDiagramClickedListeners(new NavigationEventProvider.DiagramClickedListener() {
  210. //
  211. //
  212. // @Override
  213. // public void onDiagramClicked(final Diagram d, final String target) {
  214. // window.hide();
  215. // showDiagram(d);
  216. // }
  217. // });
  218. //
  219. // window.setHeight(Double.valueOf(h * .65).intValue());
  220. // window.setWidth(Double.valueOf(w * .65).intValue());
  221. // window.setHeading(d.getName().getValue());
  222. // window.add(diagramPanel);
  223. // window.getHeader().addTool(
  224. // new ToolButton("x-tool-maximize",
  225. // new SelectionListener<IconButtonEvent>() {
  226. // boolean isMax;
  227. //
  228. // @Override
  229. // public void componentSelected(IconButtonEvent ce) {
  230. // window.setHeight(h);
  231. // window.setWidth(w);
  232. // diagramPanel.resizePanel(h, w);
  233. // window.setPagePosition(0, 0);
  234. //
  235. //
  236. // }
  237. // }));
  238. // window.show();
  239. // }
  240. public void onModuleLoad() {
  241. // final String point = Location.getParameter(Const.PARAM_POINT);
  242. // final String user = Location.getParameter("user");
  243. final String uuid = Location.getParameter(Const.PARAM_UUID);
  244. // final String source = Location.getParameter("source");
  245. final String fb = Location.getParameter(Const.PARAM_FACEBOOK);
  246. final String code = Location.getParameter(Const.PARAM_CODE);
  247. final String tw = Location.getParameter(Const.PARAM_TWITTER);
  248. final String oauth_token = Location.getParameter(Const.PARAM_OAUTH);
  249. final String diagramUUID = Location.getParameter(Const.PARAM_DIAGRAM);
  250. final String clientTypeParam = Location.getParameter(Const.PARAM_CLIENT);
  251. final boolean doFacebook = ((fb != null) || (code != null));
  252. final boolean doTwitter = ((tw != null) && (oauth_token == null));
  253. final boolean doTwitterFinish = ((tw != null) && (oauth_token != null));
  254. final boolean doDiagram = (diagramUUID != null);
  255. final ClientType clientType;
  256. if (!Utils.isEmptyString(clientTypeParam) && clientTypeParam.equals(Const.WORD_ANDROID)) {
  257. clientType = ClientType.android;
  258. } else {
  259. clientType = ClientType.other;
  260. }
  261. if (doDiagram) {
  262. processDiagramRequest(diagramUUID, clientType);
  263. } else {
  264. try {
  265. loadPortalView(uuid, code, oauth_token, doFacebook, doTwitter, doTwitterFinish);
  266. } catch (NimbitsException e) {
  267. Window.alert(e.getMessage());
  268. }
  269. }
  270. }
  271. private void processDiagramRequest(final String diagramName, final ClientType clientType) {
  272. DiagramServiceAsync diagramService = GWT.create(DiagramService.class);
  273. try {
  274. diagramService.getDiagramByUuid(diagramName, new AsyncCallback<Diagram>() {
  275. @Override
  276. public void onFailure(Throwable throwable) {
  277. handleError(throwable);
  278. }
  279. @Override
  280. public void onSuccess(final Diagram diagram) {
  281. loadDiagramView(diagram, clientType);
  282. }
  283. });
  284. } catch (ObjectProtectionException e) {
  285. handleError(e);
  286. } catch (DiagramNotFoundException e) {
  287. handleError(e);
  288. } catch (NimbitsException e) {
  289. handleError(e);
  290. }
  291. }
  292. private void loadPortalView(final String uuid, final String code, final String oauth_token, final boolean doFacebook, final boolean doTwitter, final boolean doTwitterFinish) throws NimbitsException {
  293. SettingsServiceAsync settingService = GWT.create(SettingsService.class);
  294. settingService.getSettings(new AsyncCallback<Map<String, String>>() {
  295. @Override
  296. public void onFailure(Throwable caught) {
  297. GWT.log(caught.getMessage(), caught);
  298. Window.Location.replace(loginInfo.getLogoutUrl());
  299. }
  300. @Override
  301. public void onSuccess(final Map<String, String> settings) {
  302. if (uuid != null) {
  303. try {
  304. loadSinglePointDisplay(uuid);
  305. } catch (NimbitsException e) {
  306. Window.alert(e.getMessage());
  307. }
  308. } else if (doFacebook) {
  309. finishFacebookAuthentication(settings, code);
  310. } else if (doTwitterFinish) {
  311. try {
  312. finishTwitterAuthentication(settings, oauth_token, doTwitter);
  313. } catch (NimbitsException e) {
  314. Window.alert(e.getMessage());
  315. }
  316. } else {
  317. try {
  318. loadPortal(doTwitter, settings);
  319. } catch (NimbitsException e) {
  320. Window.alert(e.getMessage());
  321. }
  322. }
  323. }
  324. });
  325. }
  326. private void finishFacebookAuthentication(final Map<String, String> settings, final String code) {
  327. getViewport();
  328. FacebookPanel fbPanel = new FacebookPanel(code, settings);
  329. fbPanel.setHeight(500);
  330. fbPanel.setWidth(600);
  331. viewport.add(fbPanel);
  332. viewport.setWidth(600);
  333. viewport.setHeight(500);
  334. RootPanel.get("main").add(viewport);
  335. }
  336. private void finishTwitterAuthentication(final Map<String, String> settings, final String oauth_token, final boolean doTwitter) throws NimbitsException {
  337. TwitterServiceAsync twitterService = GWT.create(TwitterService.class);
  338. twitterService.updateUserToken(oauth_token,
  339. new AsyncCallback<Void>() {
  340. @Override
  341. public void onFailure(Throwable caught) {
  342. handleError(caught);
  343. }
  344. @Override
  345. public void onSuccess(Void result) {
  346. Window.alert(Const.MESSAGE_TWITTER_ADDED);
  347. try {
  348. loadPortal(doTwitter, settings);
  349. } catch (NimbitsException e) {
  350. Window.alert(e.getMessage());
  351. }
  352. }
  353. });
  354. }
  355. private void getViewport() {
  356. viewport = new Viewport();
  357. viewport.setLayout(new FillLayout());
  358. viewport.setBorders(false);
  359. }
  360. private void loadSinglePointDisplay(final String uuid) throws NimbitsException {
  361. PointServiceAsync pointService;
  362. pointService = GWT.create(PointService.class);
  363. pointService.getPointByUUID(uuid, new AsyncCallback<Point>() {
  364. @Override
  365. public void onFailure(Throwable throwable) {
  366. handleError(throwable);
  367. }
  368. @Override
  369. public void onSuccess(Point point) {
  370. try {
  371. loadData(point);
  372. } catch (NimbitsException e) {
  373. alert(e.getMessage());
  374. }
  375. }
  376. });
  377. }
  378. private void loadPortal(final boolean doTwitter, final Map<String, String> settings) throws NimbitsException {
  379. LoginServiceAsync loginService = GWT
  380. .create(LoginService.class);
  381. loginService.login(GWT.getHostPageBaseURL(),
  382. new AsyncCallback<LoginInfo>() {
  383. @Override
  384. public void onFailure(Throwable error) {
  385. GWT.log(error.getMessage(), error);
  386. handleError(error);
  387. }
  388. @Override
  389. public void onSuccess(LoginInfo result) {
  390. loginInfo = result;
  391. if (loginInfo.isLoggedIn()) {
  392. try {
  393. loadLayout(loginInfo, settings);
  394. } catch (NimbitsException ignored) {
  395. }
  396. if (doTwitter) {
  397. final TwitterServiceAsync twitterService = GWT.create(TwitterService.class);
  398. try {
  399. twitterService.twitterAuthorise(loginInfo.getEmailAddress(), new AsyncCallback<String>() {
  400. @Override
  401. public void onFailure(Throwable caught) {
  402. GWT.log(caught.getMessage(), caught);
  403. }
  404. @Override
  405. public void onSuccess(String result) {
  406. Location.replace(result);
  407. }
  408. });
  409. } catch (NimbitsException e) {
  410. Window.alert("There was a problem loading the settings from the server.");
  411. }
  412. }
  413. } else {
  414. loadLogin();
  415. }
  416. }
  417. });
  418. }
  419. private void loadData(final Point point) throws NimbitsException {
  420. final List<Point> points = new ArrayList<Point>();
  421. final ContentPanel mainContentPanel = new ContentPanel();
  422. points.add(point);
  423. getViewport();
  424. mainContentPanel.setHeading(point.getName() + " " + point.getDescription());
  425. viewport.add(mainContentPanel);
  426. RootPanel.get("main").add(viewport);
  427. LoginServiceAsync loginService = GWT.create(LoginService.class);
  428. loginService.login(GWT.getHostPageBaseURL(),
  429. new AsyncCallback<LoginInfo>() {
  430. @Override
  431. public void onFailure(Throwable error) {
  432. handleError(error);
  433. }
  434. @Override
  435. public void onSuccess(LoginInfo result) {
  436. loginInfo = result;
  437. if ((loginInfo.isLoggedIn() && loginInfo.getUser().getId() == point.getUserFK()) || point.isPublic()) {
  438. loadChart(points, mainContentPanel);
  439. } else {
  440. loadLogin();
  441. }
  442. }
  443. });
  444. }
  445. private void loadChart(final List<Point> points, final ContentPanel p) {
  446. RecordedValueServiceAsync dataService;
  447. dataService = GWT.create(RecordedValueService.class);
  448. dataService.getLastRecordedDate(points, new AsyncCallback<Date>() {
  449. @Override
  450. public void onFailure(Throwable caught) {
  451. }
  452. @Override
  453. public void onSuccess(Date result) {
  454. final AnnotatedTimeLinePanel annotatedTimeLinePanel = new AnnotatedTimeLinePanel(false, Const.DEFAULT_CHART_NAME);
  455. // annotatedTimeLinePanel.setPoints(points);
  456. p.add(annotatedTimeLinePanel);
  457. p.setWidth(viewport.getWidth());
  458. p.setHeight(viewport.getHeight());
  459. annotatedTimeLinePanel.initChart();
  460. viewport.layout(true);
  461. }
  462. });
  463. }
  464. private void loadLogin() {
  465. Window.Location.replace(loginInfo.getLoginUrl());
  466. }
  467. private void handleError(Throwable error) {
  468. if (error instanceof NotLoggedInException) {
  469. Window.Location.replace(loginInfo.getLogoutUrl());
  470. } else if (error instanceof ObjectProtectionException) {
  471. Window.Location.replace(Const.PATH_OBJECT_PROTECTION_URL);
  472. } else {
  473. Window.alert(error.getMessage());
  474. // Window.Location.replace(Const.PATH_NIMBITS_HOME);
  475. }
  476. }
  477. }