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.CmsBroadcast.ContentMode;
033import org.opencms.main.CmsSessionInfo;
034import org.opencms.main.OpenCms;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.apps.sessions.CmsSessionsApp.MessageValidator;
038import org.opencms.ui.components.CmsBasicDialog;
039import org.opencms.ui.components.CmsRichTextArea;
040
041import java.util.Map;
042import java.util.Set;
043import java.util.concurrent.ConcurrentHashMap;
044
045import com.vaadin.server.UserError;
046import com.vaadin.ui.Button;
047import com.vaadin.ui.Button.ClickEvent;
048import com.vaadin.ui.CheckBox;
049import com.vaadin.v7.data.Validator.InvalidValueException;
050
051/**
052 * Class for the dialiog to send broadcasts.<p>
053 */
054public class CmsSendBroadcastDialog extends CmsBasicDialog {
055
056    /** Map for storing the last message sent by a user. */
057    private static final Map<CmsUser, CmsBroadcast> USER_BROADCAST = new ConcurrentHashMap<CmsUser, CmsBroadcast>();
058
059    /**vaadin serial id.*/
060    private static final long serialVersionUID = -7642289972554010162L;
061
062    /**cancel button.*/
063    private Button m_cancel;
064
065    /**Message text area.*/
066    private CmsRichTextArea m_message;
067
068    /**ok button.*/
069    private Button m_ok;
070
071    /** Button for clearing broadcasts. */
072    private Button m_resetBroadcasts;
073
074    /** Check box for setting a message to repeating. */
075    private CheckBox m_repeat;
076
077    /**
078     * public constructor.<p>
079     *
080     * @param sessionIds to send broadcast to
081     * @param closeRunnable called on cancel
082     */
083    public CmsSendBroadcastDialog(final Set<String> sessionIds, final Runnable closeRunnable) {
084
085        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
086        if (sessionIds != null) {
087            displayResourceInfoDirectly(CmsSessionsApp.getUserInfos(sessionIds));
088        } else {
089            if (USER_BROADCAST.containsKey(A_CmsUI.getCmsObject().getRequestContext().getCurrentUser())) {
090                m_message.setValue(
091                    USER_BROADCAST.get(A_CmsUI.getCmsObject().getRequestContext().getCurrentUser()).getMessage());
092            }
093        }
094
095        m_resetBroadcasts.addClickListener(event -> removeAllBroadcasts(sessionIds));
096
097        m_cancel.addClickListener(new Button.ClickListener() {
098
099            private static final long serialVersionUID = 3105449865170606831L;
100
101            public void buttonClick(ClickEvent event) {
102
103                closeRunnable.run();
104            }
105        });
106
107        m_ok.addClickListener(new Button.ClickListener() {
108
109            private static final long serialVersionUID = -1148041995591262401L;
110
111            public void buttonClick(ClickEvent event) {
112
113                if (validateMessage()) {
114                    sendBroadcast(sessionIds);
115                    closeRunnable.run();
116                }
117            }
118        });
119    }
120
121    /**
122     * Sends broadcast.<p>
123     *
124     * @param sessionIds to send broadcast to
125     */
126    protected void sendBroadcast(Set<String> sessionIds) {
127
128        String cleanedHtml = CmsRichTextArea.cleanHtml(m_message.getValue(), true);
129        if (sessionIds == null) {
130            OpenCms.getSessionManager().sendBroadcast(
131                A_CmsUI.getCmsObject(),
132                cleanedHtml,
133                m_repeat.getValue().booleanValue(),
134                ContentMode.html);
135            USER_BROADCAST.put(
136                A_CmsUI.getCmsObject().getRequestContext().getCurrentUser(),
137                new CmsBroadcast(
138                    A_CmsUI.getCmsObject().getRequestContext().getCurrentUser(),
139                    cleanedHtml,
140                    m_repeat.getValue().booleanValue(),
141                    ContentMode.html));
142        } else {
143            for (String id : sessionIds) {
144                OpenCms.getSessionManager().sendBroadcast(
145                    A_CmsUI.getCmsObject(),
146                    cleanedHtml,
147                    id,
148                    m_repeat.getValue().booleanValue(),
149                    ContentMode.html);
150            }
151        }
152    }
153
154    /**
155     * Removes all pending broadcasts
156     *
157     * @param sessionIds to remove broadcast for (or null for all sessions)
158     */
159    private void removeAllBroadcasts(Set<String> sessionIds) {
160
161        if (sessionIds == null) {
162            for (CmsSessionInfo info : OpenCms.getSessionManager().getSessionInfos()) {
163                OpenCms.getSessionManager().getBroadcastQueue(info.getSessionId().getStringValue()).clear();
164            }
165            return;
166        }
167        for (String sessionId : sessionIds) {
168            OpenCms.getSessionManager().getBroadcastQueue(sessionId).clear();
169        }
170    }
171
172    /**
173     * Validates the broadcast message, sets the error status of the field appropriately, and returns the result.
174     *
175     * @return true if the validation was successful
176     */
177    private boolean validateMessage() {
178
179        m_message.setComponentError(null);
180        try {
181            new MessageValidator().validate(m_message.getValue());
182            return true;
183        } catch (InvalidValueException e) {
184            m_message.setComponentError(new UserError(e.getLocalizedMessage()));
185            return false;
186        }
187    }
188}