/*
 * Copyright (c) 2008-2014 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.cuba.desktop.gui;

import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.Configuration;
import com.haulmont.cuba.core.global.RemoteException;
import com.haulmont.cuba.desktop.App;
import com.haulmont.cuba.desktop.Connection;
import com.haulmont.cuba.desktop.DesktopConfig;
import com.haulmont.cuba.security.app.UserSessionService;
import com.haulmont.cuba.security.global.NoUserSessionException;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.annotation.ManagedBean;
import javax.annotation.Nullable;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;

/**
 * @author artamonov
 * @version $Id$
 */
@ManagedBean(SessionMessagesNotifier.NAME)
public class SessionMessagesNotifier {

    public final static String NAME = "cuba_SessionMessagesNotifier";

    private final static Log log = LogFactory.getLog(SessionMessagesNotifier.class);

    protected Timer timer;
    protected SwingWorker<String, Void> asyncMessageLoader;

    public void activate() {
        if (timer != null) {
            return;
        }

        Configuration configuration = AppBeans.get(Configuration.NAME);
        int timeout = configuration.getConfig(DesktopConfig.class).getSessionMessagesIntervalSec();

        if (timeout > 0) {
            timer = new Timer(timeout * 1000, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    syncMessages();
                }
            });
            timer.start();
        }
    }

    public void deactivate() {
        if (timer != null) {
            timer.stop();
            if (asyncMessageLoader != null) {
                asyncMessageLoader.cancel(true);
                asyncMessageLoader = null;
            }
            timer = null;
        }
    }

    protected void syncMessages() {
        Connection connection = App.getInstance().getConnection();
        if (connection.isConnected()) {
            log.trace("Check session messages");

            asyncMessageLoader = new SwingWorker<String, Void>() {
                @Override
                protected String doInBackground() throws Exception {
                    try {
                        UserSessionService uss = AppBeans.get(UserSessionService.NAME);
                        return uss.getMessages();
                    } catch (NoUserSessionException e) {
                        log.warn("Unable to get messages for session, user session not found");
                    } catch (RemoteException e) {
                        if (ExceptionUtils.getRootCause(e) instanceof NoUserSessionException) {
                            log.warn("Unable to get messages for session, user session not found");
                        }
                    } catch (Exception e) {
                        log.warn("Session messages exception", e);
                    }
                    return null;
                }

                @Override
                protected void done() {
                    try {
                        processServerMessage(get());
                    } catch (InterruptedException | ExecutionException ignored) {
                        // do nothing
                    } finally {
                        asyncMessageLoader = null;
                    }
                }
            };
            asyncMessageLoader.execute();
        }
    }

    protected void processServerMessage(@Nullable String message) {
        if (message != null) {
            log.debug("Received session message");

            Connection connection = App.getInstance().getConnection();
            if (connection.isConnected() && timer != null && timer.isRunning()) {
                SessionMessageWindow dialog = new SessionMessageWindow(App.getInstance().getMainFrame());
                dialog.setMessage(message);
                dialog.setVisible(true);
            }
        }
    }
}
@@ -292,62 +292,8 @@
 on;%0A
-import com.haulmont.cuba.core.global.RemoteException;%0A
 impo
@@ -546,65 +546,8 @@
 on;%0A
-import org.apache.commons.lang.exception.ExceptionUtils;%0A
 impo
@@ -2733,280 +2733,8 @@
 %22);%0A
-                    %7D catch (RemoteException e) %7B%0A                        if (ExceptionUtils.getRootCause(e) instanceof NoUserSessionException) %7B%0A                            log.warn(%22Unable to get messages for session, user session not found%22);%0A                        %7D%0A
     
@@ -2837,12 +2837,26 @@
 tion
-%22, e
+: %22 + e.toString()
 );%0A 

Implement system notifications receiving #PL-1588