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 GmbH & Co. KG, 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.workplace.tools.workplace.broadcast;
029
030import org.opencms.jsp.CmsJspActionElement;
031import org.opencms.main.CmsBroadcast.ContentMode;
032import org.opencms.main.OpenCms;
033import org.opencms.util.CmsStringUtil;
034import org.opencms.widgets.CmsDisplayWidget;
035import org.opencms.widgets.CmsTextareaWidget;
036import org.opencms.workplace.CmsWidgetDialogParameter;
037import org.opencms.workplace.list.CmsHtmlList;
038
039import java.util.ArrayList;
040import java.util.Iterator;
041import java.util.List;
042
043import javax.servlet.http.HttpServletRequest;
044import javax.servlet.http.HttpServletResponse;
045import javax.servlet.jsp.PageContext;
046
047/**
048 * Dialog to edit a message to broadcast in the administration view.<p>
049 *
050 * @since 6.0.0
051 */
052public class CmsBroadcastMessageDialog extends A_CmsMessageDialog {
053
054    /** localized messages Keys prefix. */
055    public static final String KEY_PREFIX = "message";
056
057    /**
058     * Public constructor with JSP action element.<p>
059     *
060     * @param jsp an initialized JSP action element
061     */
062    public CmsBroadcastMessageDialog(CmsJspActionElement jsp) {
063
064        super(jsp);
065    }
066
067    /**
068     * Public constructor with JSP variables.<p>
069     *
070     * @param context the JSP page context
071     * @param req the JSP request
072     * @param res the JSP response
073     */
074    public CmsBroadcastMessageDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
075
076        this(new CmsJspActionElement(context, req, res));
077    }
078
079    /**
080     * Commits the edited project to the db.<p>
081     */
082    @Override
083    public void actionCommit() {
084
085        List<Throwable> errors = new ArrayList<Throwable>();
086
087        try {
088            if (isForAll()) {
089                OpenCms.getSessionManager().sendBroadcast(getCms(), m_msgInfo.getMsg(), ContentMode.html);
090            } else {
091                List<String> ids = CmsStringUtil.splitAsList(getParamSessionids(), CmsHtmlList.ITEM_SEPARATOR);
092                Iterator<String> itIds = ids.iterator();
093                while (itIds.hasNext()) {
094                    String id = itIds.next();
095                    OpenCms.getSessionManager().sendBroadcast(getCms(), m_msgInfo.getMsg(), id, ContentMode.html);
096                }
097            }
098        } catch (Throwable t) {
099            errors.add(t);
100        }
101        // set the list of errors to display when saving failed
102        setCommitErrors(errors);
103    }
104
105    /**
106     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
107     */
108    @Override
109    protected String createDialogHtml(String dialog) {
110
111        StringBuffer result = new StringBuffer(1024);
112
113        result.append(createWidgetTableStart());
114        // show error header once if there were validation errors
115        result.append(createWidgetErrorHeader());
116
117        if (dialog.equals(PAGES[0])) {
118            // create the widgets for the first dialog page
119            result.append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_HEADER_BLOCK_0)));
120            result.append(createWidgetTableStart());
121            result.append(createDialogRowsHtml(0, 1));
122            result.append(createWidgetTableEnd());
123            result.append(dialogBlockEnd());
124            result.append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_CONTENT_BLOCK_0)));
125            result.append(createWidgetTableStart());
126            result.append(createDialogRowsHtml(2, 2));
127            result.append(createWidgetTableEnd());
128            result.append(dialogBlockEnd());
129        }
130
131        result.append(createWidgetTableEnd());
132        return result.toString();
133    }
134
135    /**
136     * Creates the list of widgets for this dialog.<p>
137     */
138    @Override
139    protected void defineWidgets() {
140
141        // initialize the project object to use for the dialog
142        initMessageObject();
143
144        setKeyPrefix(KEY_PREFIX);
145
146        addWidget(new CmsWidgetDialogParameter(m_msgInfo, "from", PAGES[0], new CmsDisplayWidget()));
147        addWidget(new CmsWidgetDialogParameter(m_msgInfo, "to", PAGES[0], new CmsDisplayWidget()));
148        addWidget(new CmsWidgetDialogParameter(m_msgInfo, "msg", PAGES[0], new CmsTextareaWidget(12)));
149    }
150}