001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.ui.apps.sessions;
029
030import org.opencms.file.CmsUser;
031import org.opencms.main.CmsBroadcast;
032import org.opencms.main.CmsSessionInfo;
033import org.opencms.main.OpenCms;
034import org.opencms.ui.A_CmsUI;
035import org.opencms.ui.CmsVaadinUtils;
036import org.opencms.ui.apps.sessions.CmsSessionsApp.MessageValidator;
037import org.opencms.ui.components.CmsBasicDialog;
038import org.opencms.ui.components.CmsRichTextAreaV7;
039
040import java.util.HashMap;
041import java.util.Map;
042import java.util.Set;
043
044import com.vaadin.ui.Button;
045import com.vaadin.ui.Button.ClickEvent;
046import com.vaadin.v7.ui.CheckBox;
047
048/**
049 * Class for the dialiog to send broadcasts.<p>
050 */
051public class CmsSendBroadcastDialog extends CmsBasicDialog {
052
053    private static final Map<CmsUser, CmsBroadcast> USER_BROADCAST = new HashMap<CmsUser, CmsBroadcast>();
054
055    /**vaadin serial id.*/
056    private static final long serialVersionUID = -7642289972554010162L;
057
058    /**cancel button.*/
059    private Button m_cancel;
060
061    /**Message text area.*/
062    private CmsRichTextAreaV7 m_message;
063
064    /**ok button.*/
065    private Button m_ok;
066
067    private Button m_resetBroadcasts;
068
069    private CheckBox m_repeat;
070
071    /**
072     * public constructor.<p>
073     *
074     * @param sessionIds to send broadcast to
075     * @param closeRunnable called on cancel
076     */
077    public CmsSendBroadcastDialog(final Set<String> sessionIds, final Runnable closeRunnable) {
078
079        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
080        if (sessionIds != null) {
081            displayResourceInfoDirectly(CmsSessionsApp.getUserInfos(sessionIds));
082        } else {
083            if (USER_BROADCAST.containsKey(A_CmsUI.getCmsObject().getRequestContext().getCurrentUser())) {
084                m_message.setValue(
085                    USER_BROADCAST.get(A_CmsUI.getCmsObject().getRequestContext().getCurrentUser()).getMessage());
086            }
087        }
088
089        m_resetBroadcasts.addClickListener(event -> removeAllBroadcasts(sessionIds));
090
091        m_cancel.addClickListener(new Button.ClickListener() {
092
093            private static final long serialVersionUID = 3105449865170606831L;
094
095            public void buttonClick(ClickEvent event) {
096
097                closeRunnable.run();
098            }
099        });
100
101        m_ok.addClickListener(new Button.ClickListener() {
102
103            private static final long serialVersionUID = -1148041995591262401L;
104
105            public void buttonClick(ClickEvent event) {
106
107                addValidator();
108                if (isMessageValid()) {
109                    sendBroadcast(sessionIds);
110                    closeRunnable.run();
111                }
112            }
113        });
114    }
115
116    /**
117     * Adds validator to field.<p>
118     */
119    protected void addValidator() {
120
121        m_message.removeAllValidators();
122        m_message.addValidator(new MessageValidator());
123    }
124
125    /**
126     * Checks if message is valid.<p>
127     *
128     * @return true if message is valid, false otherwise
129     */
130    protected boolean isMessageValid() {
131
132        return m_message.isValid();
133    }
134
135    /**
136     * Sends broadcast.<p>
137     *
138     * @param sessionIds to send broadcast to
139     */
140    protected void sendBroadcast(Set<String> sessionIds) {
141
142        if (sessionIds == null) {
143            OpenCms.getSessionManager().sendBroadcast(
144                A_CmsUI.getCmsObject(),
145                m_message.getValue(),
146                m_repeat.getValue().booleanValue());
147            USER_BROADCAST.put(
148                A_CmsUI.getCmsObject().getRequestContext().getCurrentUser(),
149                new CmsBroadcast(
150                    A_CmsUI.getCmsObject().getRequestContext().getCurrentUser(),
151                    m_message.getValue(),
152                    m_repeat.getValue().booleanValue()));
153        } else {
154            for (String id : sessionIds) {
155                OpenCms.getSessionManager().sendBroadcast(
156                    A_CmsUI.getCmsObject(),
157                    m_message.getValue(),
158                    id,
159                    m_repeat.getValue().booleanValue());
160            }
161        }
162    }
163
164    /**
165     * Removes all pending broadcasts
166     *
167     * @param sessionIds to remove broadcast for (or null for all sessions)
168     */
169    private void removeAllBroadcasts(Set<String> sessionIds) {
170
171        if (sessionIds == null) {
172            for (CmsSessionInfo info : OpenCms.getSessionManager().getSessionInfos()) {
173                OpenCms.getSessionManager().getBroadcastQueue(info.getSessionId().getStringValue()).clear();
174            }
175            return;
176        }
177        for (String sessionId : sessionIds) {
178            OpenCms.getSessionManager().getBroadcastQueue(sessionId).clear();
179        }
180    }
181}