001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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 jakarta.servlet.http.HttpServletRequest; 055import jakarta.servlet.http.HttpServletResponse; 056import jakarta.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( 121 (Throwable)new CmsIllegalStateException( 122 Messages.get().container(Messages.ERR_NO_SELECTED_GROUP_0)))); 123 return; 124 } 125 126 boolean hasUser = false; 127 Iterator<String> itGroups = getGroups().iterator(); 128 while (!hasUser && itGroups.hasNext()) { 129 String groupName = itGroups.next(); 130 try { 131 Iterator<CmsUser> itUsers = getCms().getUsersOfGroup(groupName, true).iterator(); 132 while (!hasUser && itUsers.hasNext()) { 133 CmsUser user = itUsers.next(); 134 if (!isEmail) { 135 if (!OpenCms.getSessionManager().getSessionInfos(user.getId()).isEmpty()) { 136 hasUser = true; 137 } 138 } else { 139 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(user.getEmail())) { 140 hasUser = true; 141 } 142 } 143 } 144 } catch (CmsException e) { 145 // should never happen 146 } 147 } 148 149 if (!hasUser) { 150 setCommitErrors( 151 Collections.singletonList( 152 (Throwable)new CmsIllegalStateException( 153 Messages.get().container(Messages.ERR_NO_SELECTED_RECEIVERS_0)))); 154 return; 155 } 156 157 try { 158 Set<String> groups = new HashSet<String>(m_groups); 159 Map<String, String[]> params = new HashMap<String, String[]>(); 160 params.put(CmsToolDialog.PARAM_STYLE, new String[] {CmsToolDialog.STYLE_NEW}); 161 params.put( 162 CmsSendEmailGroupsDialog.PARAM_GROUPS, 163 new String[] {CmsStringUtil.collectionAsString(groups, CmsHtmlList.ITEM_SEPARATOR)}); 164 params.put( 165 CmsDialog.PARAM_CLOSELINK, 166 new String[] {CmsToolManager.linkForToolPath(getJsp(), "/workplace/broadcast")}); 167 168 if (isEmail) { 169 getToolManager().jspForwardPage( 170 this, 171 "/system/workplace/admin/workplace/groups_send_email.jsp", 172 params); 173 } else { 174 getToolManager().jspForwardPage( 175 this, 176 "/system/workplace/admin/workplace/groups_send_popup.jsp", 177 params); 178 } 179 } catch (Throwable t) { 180 errors.add(t); 181 } 182 // set the list of errors to display when saving failed 183 setCommitErrors(errors); 184 } 185 186 /** 187 * Returns the selected groups.<p> 188 * 189 * @return the selected groups 190 */ 191 public List<String> getGroups() { 192 193 return m_groups; 194 } 195 196 /** 197 * Returns the message type parameter value.<p> 198 * 199 * @return the message type parameter value 200 */ 201 public String getParamMsgtype() { 202 203 return m_paramMsgtype; 204 } 205 206 /** 207 * Sets the selected groups.<p> 208 * 209 * @param groups the selected groups to set 210 */ 211 public void setGroups(List<String> groups) { 212 213 m_groups = groups; 214 } 215 216 /** 217 * Sets the message type parameter value.<p> 218 * 219 * @param paramMsgtype the message type to set 220 */ 221 public void setParamMsgtype(String paramMsgtype) { 222 223 m_paramMsgtype = paramMsgtype; 224 } 225 226 /** 227 * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String) 228 */ 229 @Override 230 protected String createDialogHtml(String dialog) { 231 232 StringBuffer result = new StringBuffer(1024); 233 234 result.append(createWidgetTableStart()); 235 // show error header once if there were validation errors 236 result.append(createWidgetErrorHeader()); 237 238 if (dialog.equals(PAGES[0])) { 239 // create the widgets for the first dialog page 240 result.append(dialogBlockStart(key(Messages.GUI_SELECT_EDITOR_LABEL_BLOCK_0))); 241 result.append(createWidgetTableStart()); 242 result.append(createDialogRowsHtml(0, 0)); 243 result.append(createWidgetTableEnd()); 244 result.append(dialogBlockEnd()); 245 } 246 247 result.append(createWidgetTableEnd()); 248 return result.toString(); 249 } 250 251 /** 252 * Creates the list of widgets for this dialog.<p> 253 */ 254 @Override 255 protected void defineWidgets() { 256 257 // initialize the project object to use for the dialog 258 initMessageObject(); 259 260 setKeyPrefix(KEY_PREFIX); 261 262 addWidget(new CmsWidgetDialogParameter(this, "groups", "", PAGES[0], new CmsGroupWidget(), 0, 10)); 263 } 264 265 /** 266 * @see org.opencms.workplace.CmsWidgetDialog#getPageArray() 267 */ 268 @Override 269 protected String[] getPageArray() { 270 271 return PAGES; 272 } 273 274 /** 275 * Initializes the message info object to work with depending on the dialog state and request parameters.<p> 276 */ 277 @SuppressWarnings("unchecked") 278 protected void initMessageObject() { 279 280 try { 281 if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) { 282 // create a new list 283 m_groups = new ArrayList<String>(); 284 } else { 285 // this is not the initial call, get the message info object from session 286 m_groups = (List<String>)getDialogObject(); 287 } 288 } catch (Exception e) { 289 // create a new list 290 m_groups = new ArrayList<String>(); 291 } 292 } 293 294 /** 295 * @see org.opencms.workplace.CmsWorkplace#initMessages() 296 */ 297 @Override 298 protected void initMessages() { 299 300 // add specific dialog resource bundle 301 addMessages(Messages.get().getBundleName()); 302 addMessages(org.opencms.workplace.tools.workplace.Messages.get().getBundleName()); 303 // add default resource bundles 304 super.initMessages(); 305 } 306 307 /** 308 * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, jakarta.servlet.http.HttpServletRequest) 309 */ 310 @Override 311 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 312 313 // initialize parameters and dialog actions in super implementation 314 super.initWorkplaceRequestValues(settings, request); 315 316 // save the current state of the message (may be changed because of the widget values) 317 setDialogObject(m_groups); 318 } 319 320}