Example usage for com.vaadin.server VaadinSession getCurrent

List of usage examples for com.vaadin.server VaadinSession getCurrent

Introduction

In this page you can find the example usage for com.vaadin.server VaadinSession getCurrent.

Prototype

public static VaadinSession getCurrent() 

Source Link

Document

Gets the currently used session.

Usage

From source file:org.ikasan.dashboard.ui.monitor.component.MonitorPanel.java

License:BSD License

@Subscribe
public void receiveFlowStateEvent(final FlowStateEvent event) {
    UI.getCurrent().access(new Runnable() {
        @Override/* ww  w .  j a  va  2s  .  c  o m*/
        public void run() {
            VaadinSession.getCurrent().getLockInstance().lock();
            try {
                stateMap = event.getFlowStateMap();
                setStatusLabel();
                populate(event.getFlowStateMap());
            } finally {
                VaadinSession.getCurrent().getLockInstance().unlock();
            }

            UI.getCurrent().push();
        }
    });
}

From source file:org.ikasan.dashboard.ui.monitor.component.MonitorPanel.java

License:BSD License

@Override
public void enter(ViewChangeEvent event) {
    TopologyStateCache cache = (TopologyStateCache) VaadinSession.getCurrent()
            .getAttribute(DashboardSessionValueConstants.TOPOLOGY_STATE_CACHE);

    stateMap = cache.getStateMap();//from w w w . jav  a2s  .co  m
    setStatusLabel();
    populate(stateMap);

}

From source file:org.ikasan.dashboard.ui.topology.panel.TopologyViewPanel.java

License:BSD License

@Subscribe
public void receiveFlowStateEvent(final FlowStateEvent event) {
    UI.getCurrent().access(new Runnable() {
        @Override/*  w w  w . j a v a  2  s  .c om*/
        public void run() {
            VaadinSession.getCurrent().getLockInstance().lock();
            try {
                flowStates = event.getFlowStateMap();
                moduleTree.markAsDirty();
            } finally {
                VaadinSession.getCurrent().getLockInstance().unlock();
            }

            UI.getCurrent().push();
        }
    });
}

From source file:org.jdal.vaadin.beans.VaadinScope.java

License:Apache License

/**
 * {@inheritDoc}// w  w w.j a  v a2s.  c  o  m
 */
public String getConversationId() {
    Integer uiId = null;

    UI ui = UI.getCurrent();
    if (ui == null) {
        UIid id = CurrentInstance.get(UIid.class);
        if (id != null) {
            uiId = id.getUiId();
        }
    } else if (ui != null) {
        if (!sessions.containsKey(ui)) {
            ui.addDetachListener(this);
            sessions.put(ui, VaadinSession.getCurrent().getSession().getId());
        }

        uiId = ui.getUIId();
    }

    return uiId != null ? getConversationId(uiId) : null;
}

From source file:org.jdal.vaadin.beans.VaadinScope.java

License:Apache License

private String getConversationId(Integer id) {
    VaadinSession session = VaadinSession.getCurrent();
    if (session == null) {
        log.info("Request Conversation id without session");
        return null;
    }/*from  ww w. ja va  2s .  c  o  m*/

    return session.getSession().getId() + ":" + id.toString();
}

From source file:org.jdal.vaadin.VaadinUtils.java

License:Apache License

/**
 * Exit application/*w  w w  .  j a v a2 s .c  o  m*/
 */
public static void exit() {
    UI.getCurrent().close();
    VaadinSession.getCurrent().close();
    Page page = Page.getCurrent();
    String location = StringUtils.substringBeforeLast(page.getLocation().toString(), page.getUriFragment());
    location = StringUtils.substringAfterLast(location, "#");
    page.setLocation(location);

}

From source file:org.jumpmind.metl.ui.common.TopBar.java

License:Open Source License

protected void logout() {
    URI uri = Page.getCurrent().getLocation();
    VaadinSession.getCurrent().close();
    Page.getCurrent().setLocation(uri.getPath());
}

From source file:org.jumpmind.metl.ui.init.AppUI.java

License:Open Source License

@SuppressWarnings("serial")
@Override/*from w ww  .j  av  a 2s .c  o m*/
protected void init(VaadinRequest request) {
    WebApplicationContext ctx = getWebApplicationContext();

    backgroundRefresherService = ctx.getBean(BackgroundRefresherService.class);
    backgroundRefresherService.init(this);

    UI.getCurrent().setErrorHandler(new DefaultErrorHandler() {
        public void error(com.vaadin.server.ErrorEvent event) {
            String intro = "Exception of type <b>";
            String message = "";
            for (Throwable t = event.getThrowable(); t != null; t = t.getCause()) {
                if (t.getCause() == null) {
                    intro += t.getClass().getName() + "</b> with the following message:<br/><br/>";
                    message = t.getMessage();
                }
            }
            ErrorWindow window = new ErrorWindow(intro, message);
            window.show();

            Throwable ex = event.getThrowable();
            if (ex != null) {
                log.error(ex.getMessage(), ex);
            } else {
                log.error("An unexpected error occurred");
            }
        }
    });

    VaadinSession.getCurrent().setConverterFactory(new DefaultConverterFactory() {
        private static final long serialVersionUID = 1L;

        @Override
        protected Converter<Date, ?> createDateConverter(Class<?> sourceType) {
            return super.createDateConverter(sourceType);
        }

        protected Converter<String, ?> createStringConverter(Class<?> sourceType) {
            if (Double.class.isAssignableFrom(sourceType)) {
                return new StringToDoubleConverter();
            } else if (Float.class.isAssignableFrom(sourceType)) {
                return new StringToFloatConverter();
            } else if (Integer.class.isAssignableFrom(sourceType)) {
                return new StringToIntegerConverter() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    protected NumberFormat getFormat(Locale locale) {
                        NumberFormat format = super.getFormat(locale);
                        format.setGroupingUsed(false);
                        return format;
                    }
                };
            } else if (Long.class.isAssignableFrom(sourceType)) {
                return new StringToLongConverter() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    protected NumberFormat getFormat(Locale locale) {
                        NumberFormat format = super.getFormat(locale);
                        format.setGroupingUsed(false);
                        return format;
                    }
                };
            } else if (BigDecimal.class.isAssignableFrom(sourceType)) {
                return new StringToBigDecimalConverter();
            } else if (Boolean.class.isAssignableFrom(sourceType)) {
                return new StringToBooleanConverter();
            } else if (Date.class.isAssignableFrom(sourceType)) {
                return new StringToDateConverter();
            } else {
                return null;
            }
        }

    });

    Responsive.makeResponsive(this);
    ApplicationContext appCtx = ctx.getBean(ApplicationContext.class);
    if (appCtx.getConfigurationService().isUserLoginEnabled()) {
        LoginDialog login = new LoginDialog(appCtx, this);
        UI.getCurrent().addWindow(login);
    } else {
        User user = appCtx.getConfigurationService().findUserByLoginId(DEFAULT_USER);
        if (user == null) {
            user = new User();
            user.setLoginId(DEFAULT_USER);
            appCtx.getConfigurationService().save(user);
            Group group = new Group(DEFAULT_USER);
            user.getGroups().add(group);
            appCtx.getConfigurationService().save(group);
            for (Privilege priv : Privilege.values()) {
                GroupPrivilege groupPriv = new GroupPrivilege(group.getId(), priv.name());
                group.getGroupPrivileges().add(groupPriv);
                appCtx.getConfigurationService().save(groupPriv);
            }
            UserGroup userGroup = new UserGroup(user.getId(), group.getId());
            appCtx.getConfigurationService().save(userGroup);
        }
        appCtx.setUser(user);
        login(user);
    }
}

From source file:org.jumpmind.vaadin.ui.common.AbstractSpringUI.java

License:Open Source License

@Override
protected void init(VaadinRequest request) {
    setErrorHandler(new DefaultErrorHandler() {

        private static final long serialVersionUID = 1L;

        @Override//from   ww  w . ja v a2  s  .  com
        public void error(com.vaadin.server.ErrorEvent event) {
            Throwable ex = event.getThrowable();
            CommonUiUtils.notify(ex);
            if (ex != null) {
                log.error(ex.getMessage(), ex);
            } else {
                log.error("An unexpected error occurred");
            }
        }
    });

    VaadinSession.getCurrent().setConverterFactory(new DefaultConverterFactory() {
        private static final long serialVersionUID = 1L;

        @Override
        protected Converter<Date, ?> createDateConverter(Class<?> sourceType) {
            return super.createDateConverter(sourceType);
        }

        protected Converter<String, ?> createStringConverter(Class<?> sourceType) {
            if (Double.class.isAssignableFrom(sourceType)) {
                return new StringToDoubleConverter();
            } else if (Float.class.isAssignableFrom(sourceType)) {
                return new StringToFloatConverter();
            } else if (Integer.class.isAssignableFrom(sourceType)) {
                return new StringToIntegerConverter() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    protected NumberFormat getFormat(Locale locale) {
                        NumberFormat format = super.getFormat(locale);
                        format.setGroupingUsed(false);
                        return format;
                    }
                };
            } else if (Long.class.isAssignableFrom(sourceType)) {
                return new StringToLongConverter() {
                    private static final long serialVersionUID = 1L;

                    @Override
                    protected NumberFormat getFormat(Locale locale) {
                        NumberFormat format = super.getFormat(locale);
                        format.setGroupingUsed(false);
                        return format;
                    }
                };
            } else if (BigDecimal.class.isAssignableFrom(sourceType)) {
                return new StringToBigDecimalConverter();
            } else if (Boolean.class.isAssignableFrom(sourceType)) {
                return new StringToBooleanConverter();
            } else if (Date.class.isAssignableFrom(sourceType)) {
                return new StringToDateConverter();
            } else {
                return null;
            }
        }

    });

    Responsive.makeResponsive(this);
}

From source file:org.jumpmind.vaadin.ui.sqlexplorer.QueryPanel.java

License:Open Source License

protected boolean execute(final boolean runAsScript, String sqlText, final int tabPosition,
        final boolean forceNewTab) {
    boolean scheduled = false;
    if (runnersInProgress == null) {
        runnersInProgress = new HashSet<SqlRunner>();
    }// w  w  w . j a  v  a  2s  .  c  o m

    if (sqlText == null) {
        if (!runAsScript) {
            sqlText = selectSqlToRun();
        } else {
            sqlText = sqlArea.getValue();
        }

        sqlText = sqlText != null ? sqlText.trim() : null;
    }

    if (StringUtils.isNotBlank(sqlText)) {

        final HorizontalLayout executingLayout = new HorizontalLayout();
        executingLayout.setMargin(true);
        executingLayout.setSizeFull();
        final Label label = new Label("Executing:\n\n" + StringUtils.abbreviate(sqlText, 250),
                ContentMode.PREFORMATTED);
        label.setEnabled(false);
        executingLayout.addComponent(label);
        executingLayout.setComponentAlignment(label, Alignment.TOP_LEFT);

        final String sql = sqlText;
        final Tab executingTab;
        if (!forceNewTab && generalResultsTab != null) {
            replaceGeneralResultsWith(executingLayout, FontAwesome.SPINNER);
            executingTab = null;
        } else {
            executingTab = resultsTabs.addTab(executingLayout, StringUtils.abbreviate(sql, 20),
                    FontAwesome.SPINNER, tabPosition);
        }

        if (executingTab != null) {
            executingTab.setClosable(true);
            resultsTabs.setSelectedTab(executingTab);
        }

        final SqlRunner runner = new SqlRunner(sql, runAsScript, user, db, settingsProvider.get(), this,
                generalResultsTab != null);
        runnersInProgress.add(runner);
        runner.setConnection(connection);
        runner.setListener(new SqlRunner.ISqlRunnerListener() {

            private static final long serialVersionUID = 1L;

            @Override
            public void writeSql(String sql) {
                QueryPanel.this.appendSql(sql);
            }

            @Override
            public void reExecute(String sql) {
                QueryPanel.this.reExecute(sql);
            }

            public void finished(final FontAwesome icon, final List<Component> results,
                    final long executionTimeInMs, final boolean transactionStarted,
                    final boolean transactionEnded) {
                VaadinSession.getCurrent().access(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            if (transactionEnded) {
                                transactionEnded();
                            } else if (transactionStarted) {
                                rollbackButtonValue = true;
                                commitButtonValue = true;
                                setButtonsEnabled();
                                sqlArea.setStyleName("transaction-in-progress");
                                connection = runner.getConnection();
                            }

                            addToSqlHistory(StringUtils.abbreviate(sql, 1024 * 8), runner.getStartTime(),
                                    executionTimeInMs, user);

                            for (Component resultComponent : results) {
                                resultComponent.setSizeFull();

                                if (forceNewTab || generalResultsTab == null || results.size() > 1) {
                                    if (resultComponent instanceof TabularResultLayout) {
                                        resultComponent = ((TabularResultLayout) resultComponent)
                                                .refreshWithoutSaveButton();
                                    }
                                    addResultsTab(resultComponent, StringUtils.abbreviate(sql, 20), icon,
                                            tabPosition);
                                } else {
                                    replaceGeneralResultsWith(resultComponent, icon);
                                    resultsTabs.setSelectedTab(generalResultsTab.getComponent());
                                }

                                String statusVal;
                                if (canceled) {
                                    statusVal = "Sql canceled after " + executionTimeInMs + " ms for "
                                            + db.getName() + ".  Finished at "
                                            + SimpleDateFormat.getTimeInstance().format(new Date());
                                } else {
                                    statusVal = "Sql executed in " + executionTimeInMs + " ms for "
                                            + db.getName() + ".  Finished at "
                                            + SimpleDateFormat.getTimeInstance().format(new Date());
                                }
                                status.setValue(statusVal);
                                resultStatuses.put(resultComponent, statusVal);
                                canceled = false;
                            }
                        } finally {
                            setButtonsEnabled();
                            if (executingTab != null) {
                                resultsTabs.removeTab(executingTab);
                            } else if (results.size() > 1) {
                                resetGeneralResultsTab();
                            }
                            runnersInProgress.remove(runner);
                            runner.setListener(null);
                        }
                    }
                });

            }

        });

        final Button cancel = new Button("Cancel");
        cancel.addClickListener(new ClickListener() {
            private static final long serialVersionUID = 1L;

            @Override
            public void buttonClick(ClickEvent event) {
                log.info("Canceling sql: " + sql);
                label.setValue("Canceling" + label.getValue().substring(9));
                executingLayout.removeComponent(cancel);
                canceled = true;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        runner.cancel();
                    }
                }).start();
            }
        });
        executingLayout.addComponent(cancel);

        scheduled = true;
        runner.start();

    }
    setButtonsEnabled();
    return scheduled;
}