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.file.CmsUser;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsIllegalStateException;
034import org.opencms.main.OpenCms;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.widgets.CmsGroupWidget;
037import org.opencms.workplace.CmsDialog;
038import org.opencms.workplace.CmsWidgetDialog;
039import org.opencms.workplace.CmsWidgetDialogParameter;
040import org.opencms.workplace.CmsWorkplaceSettings;
041import org.opencms.workplace.list.CmsHtmlList;
042import org.opencms.workplace.tools.CmsToolDialog;
043import org.opencms.workplace.tools.CmsToolManager;
044
045import java.util.ArrayList;
046import java.util.Collections;
047import java.util.HashMap;
048import java.util.HashSet;
049import java.util.Iterator;
050import java.util.List;
051import java.util.Map;
052import java.util.Set;
053
054import javax.servlet.http.HttpServletRequest;
055import javax.servlet.http.HttpServletResponse;
056import javax.servlet.jsp.PageContext;
057
058/**
059 * Dialog to select the receiver of a new message.<p>
060 *
061 * @since 6.5.6
062 */
063public class CmsSelectReceiverDialog extends CmsWidgetDialog {
064
065    /** Localized messages Keys prefix. */
066    public static final String KEY_PREFIX = "select";
067
068    /** Defines which pages are valid for this dialog. */
069    public static final String[] PAGES = {"page1"};
070
071    /** Parameter name constant. */
072    public static final String PARAM_MSGTYPE = "msgtype";
073
074    /** Message type value constant. */
075    public static final String MSGTYPE_EMAIL = "email";
076
077    /** Message type value constant. */
078    public static final String MSGTYPE_POPUP = "popup";
079
080    /** The selected groups. */
081    private List<String> m_groups;
082
083    /** The message type parameter value. */
084    private String m_paramMsgtype;
085
086    /**
087     * Public constructor with JSP action element.<p>
088     *
089     * @param jsp an initialized JSP action element
090     */
091    public CmsSelectReceiverDialog(CmsJspActionElement jsp) {
092
093        super(jsp);
094    }
095
096    /**
097     * Public constructor with JSP variables.<p>
098     *
099     * @param context the JSP page context
100     * @param req the JSP request
101     * @param res the JSP response
102     */
103    public CmsSelectReceiverDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) {
104
105        this(new CmsJspActionElement(context, req, res));
106    }
107
108    /**
109     * Commits the edited project to the db.<p>
110     */
111    @Override
112    public void actionCommit() {
113
114        List<Throwable> errors = new ArrayList<Throwable>();
115
116        boolean isEmail = (getParamMsgtype() != null) && getParamMsgtype().equals(MSGTYPE_EMAIL);
117
118        if ((m_groups == null) || m_groups.isEmpty()) {
119            setCommitErrors(
120                Collections.singletonList((Throwable)new CmsIllegalStateException(
121                    Messages.get().container(Messages.ERR_NO_SELECTED_GROUP_0))));
122            return;
123        }
124
125        boolean hasUser = false;
126        Iterator<String> itGroups = getGroups().iterator();
127        while (!hasUser && itGroups.hasNext()) {
128            String groupName = itGroups.next();
129            try {
130                Iterator<CmsUser> itUsers = getCms().getUsersOfGroup(groupName, true).iterator();
131                while (!hasUser && itUsers.hasNext()) {
132                    CmsUser user = itUsers.next();
133                    if (!isEmail) {
134                        if (!OpenCms.getSessionManager().getSessionInfos(user.getId()).isEmpty()) {
135                            hasUser = true;
136                        }
137                    } else {
138                        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(user.getEmail())) {
139                            hasUser = true;
140                        }
141                    }
142                }
143            } catch (CmsException e) {
144                // should never happen
145            }
146        }
147
148        if (!hasUser) {
149            setCommitErrors(
150                Collections.singletonList((Throwable)new CmsIllegalStateException(
151                    Messages.get().container(Messages.ERR_NO_SELECTED_RECEIVERS_0))));
152            return;
153        }
154
155        try {
156            Set<String> groups = new HashSet<String>(m_groups);
157            Map<String, String[]> params = new HashMap<String, String[]>();
158            params.put(CmsToolDialog.PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW});
159            params.put(
160                CmsSendEmailGroupsDialog.PARAM_GROUPS,
161                new String[] {CmsStringUtil.collectionAsString(groups, CmsHtmlList.ITEM_SEPARATOR)});
162            params.put(
163                CmsDialog.PARAM_CLOSELINK,
164                new String[] {CmsToolManager.linkForToolPath(getJsp(), "/workplace/broadcast")});
165
166            if (isEmail) {
167                getToolManager().jspForwardPage(
168                    this,
169                    "/system/workplace/admin/workplace/groups_send_email.jsp",
170                    params);
171            } else {
172                getToolManager().jspForwardPage(
173                    this,
174                    "/system/workplace/admin/workplace/groups_send_popup.jsp",
175                    params);
176            }
177        } catch (Throwable t) {
178            errors.add(t);
179        }
180        // set the list of errors to display when saving failed
181        setCommitErrors(errors);
182    }
183
184    /**
185     * Returns the selected groups.<p>
186     *
187     * @return the selected groups
188     */
189    public List<String> getGroups() {
190
191        return m_groups;
192    }
193
194    /**
195     * Returns the message type parameter value.<p>
196     *
197     * @return the message type parameter value
198     */
199    public String getParamMsgtype() {
200
201        return m_paramMsgtype;
202    }
203
204    /**
205     * Sets the selected groups.<p>
206     *
207     * @param groups the selected groups to set
208     */
209    public void setGroups(List<String> groups) {
210
211        m_groups = groups;
212    }
213
214    /**
215     * Sets the message type parameter value.<p>
216     *
217     * @param paramMsgtype the message type to set
218     */
219    public void setParamMsgtype(String paramMsgtype) {
220
221        m_paramMsgtype = paramMsgtype;
222    }
223
224    /**
225     * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
226     */
227    @Override
228    protected String createDialogHtml(String dialog) {
229
230        StringBuffer result = new StringBuffer(1024);
231
232        result.append(createWidgetTableStart());
233        // show error header once if there were validation errors
234        result.append(createWidgetErrorHeader());
235
236        if (dialog.equals(PAGES[0])) {
237            // create the widgets for the first dialog page
238            result.append(dialogBlockStart(key(Messages.GUI_SELECT_EDITOR_LABEL_BLOCK_0)));
239            result.append(createWidgetTableStart());
240            result.append(createDialogRowsHtml(0, 0));
241            result.append(createWidgetTableEnd());
242            result.append(dialogBlockEnd());
243        }
244
245        result.append(createWidgetTableEnd());
246        return result.toString();
247    }
248
249    /**
250     * Creates the list of widgets for this dialog.<p>
251     */
252    @Override
253    protected void defineWidgets() {
254
255        // initialize the project object to use for the dialog
256        initMessageObject();
257
258        setKeyPrefix(KEY_PREFIX);
259
260        addWidget(new CmsWidgetDialogParameter(this, "groups", "", PAGES[0], new CmsGroupWidget(), 0, 10));
261    }
262
263    /**
264     * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
265     */
266    @Override
267    protected String[] getPageArray() {
268
269        return PAGES;
270    }
271
272    /**
273     * Initializes the message info object to work with depending on the dialog state and request parameters.<p>
274     */
275    @SuppressWarnings("unchecked")
276    protected void initMessageObject() {
277
278        try {
279            if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
280                // create a new list
281                m_groups = new ArrayList<String>();
282            } else {
283                // this is not the initial call, get the message info object from session
284                m_groups = (List<String>)getDialogObject();
285            }
286        } catch (Exception e) {
287            // create a new list
288            m_groups = new ArrayList<String>();
289        }
290    }
291
292    /**
293     * @see org.opencms.workplace.CmsWorkplace#initMessages()
294     */
295    @Override
296    protected void initMessages() {
297
298        // add specific dialog resource bundle
299        addMessages(Messages.get().getBundleName());
300        addMessages(org.opencms.workplace.tools.workplace.Messages.get().getBundleName());
301        // add default resource bundles
302        super.initMessages();
303    }
304
305    /**
306     * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
307     */
308    @Override
309    protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) {
310
311        // initialize parameters and dialog actions in super implementation
312        super.initWorkplaceRequestValues(settings, request);
313
314        // save the current state of the message (may be changed because of the widget values)
315        setDialogObject(m_groups);
316    }
317
318}