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.CmsBroadcast.ContentMode; 033import org.opencms.main.CmsException; 034import org.opencms.main.CmsIllegalStateException; 035import org.opencms.main.CmsLog; 036import org.opencms.main.OpenCms; 037import org.opencms.util.CmsStringUtil; 038import org.opencms.widgets.CmsDisplayWidget; 039import org.opencms.widgets.CmsTextareaWidget; 040import org.opencms.workplace.CmsDialog; 041import org.opencms.workplace.CmsWidgetDialog; 042import org.opencms.workplace.CmsWidgetDialogParameter; 043import org.opencms.workplace.CmsWorkplaceSettings; 044import org.opencms.workplace.list.CmsHtmlList; 045 046import java.util.ArrayList; 047import java.util.Collections; 048import java.util.Iterator; 049import java.util.List; 050 051import javax.servlet.http.HttpServletRequest; 052import javax.servlet.http.HttpServletResponse; 053import javax.servlet.jsp.PageContext; 054 055import org.apache.commons.logging.Log; 056 057/** 058 * Dialog to send a new email to the selected groups.<p> 059 * 060 * @since 6.5.6 061 */ 062public class CmsSendPopupGroupsDialog extends CmsWidgetDialog { 063 064 /** localized messages Keys prefix. */ 065 public static final String KEY_PREFIX = "message"; 066 067 /** Defines which pages are valid for this dialog. */ 068 public static final String[] PAGES = {"page1"}; 069 070 /** Parameter name constant. */ 071 public static final String PARAM_GROUPS = "groups"; 072 073 /** The static log object for this class. */ 074 private static final Log LOG = CmsLog.getLog(CmsSendPopupGroupsDialog.class); 075 076 /** Message info object. */ 077 protected CmsMessageInfo m_msgInfo; 078 079 /** The selected groups. */ 080 private String m_paramGroups; 081 082 /** The list of all members of the selected groups. */ 083 private List<CmsUser> m_users; 084 085 /** 086 * Public constructor with JSP action element.<p> 087 * 088 * @param jsp an initialized JSP action element 089 */ 090 public CmsSendPopupGroupsDialog(CmsJspActionElement jsp) { 091 092 super(jsp); 093 } 094 095 /** 096 * Public constructor with JSP variables.<p> 097 * 098 * @param context the JSP page context 099 * @param req the JSP request 100 * @param res the JSP response 101 */ 102 public CmsSendPopupGroupsDialog(PageContext context, HttpServletRequest req, HttpServletResponse res) { 103 104 this(new CmsJspActionElement(context, req, res)); 105 } 106 107 /** 108 * Commits the edited project to the db.<p> 109 */ 110 @Override 111 public void actionCommit() { 112 113 List<Throwable> errors = new ArrayList<Throwable>(); 114 115 if (getUsers().isEmpty()) { 116 setCommitErrors( 117 Collections.singletonList( 118 (Throwable)new CmsIllegalStateException( 119 Messages.get().container(Messages.ERR_NO_SELECTED_USER_WITH_EMAIL_0)))); 120 return; 121 } 122 try { 123 Iterator<CmsUser> itUsers = getUsers().iterator(); 124 while (itUsers.hasNext()) { 125 CmsUser user = itUsers.next(); 126 OpenCms.getSessionManager().sendBroadcast( 127 getCms().getRequestContext().getCurrentUser(), 128 m_msgInfo.getMsg(), 129 user, 130 ContentMode.html); 131 } 132 } catch (Throwable t) { 133 errors.add(t); 134 } 135 // set the list of errors to display when saving failed 136 setCommitErrors(errors); 137 } 138 139 /** 140 * Returns the selected groups.<p> 141 * 142 * @return the selected groups 143 */ 144 public List<String> getGroups() { 145 146 return CmsStringUtil.splitAsList(getParamGroups(), CmsHtmlList.ITEM_SEPARATOR); 147 } 148 149 /** 150 * Returns the list of selected groups.<p> 151 * 152 * @return the list of selected groups 153 */ 154 public String getParamGroups() { 155 156 return m_paramGroups; 157 } 158 159 /** 160 * Sets the list of selected groups.<p> 161 * 162 * @param paramGroups the list of selected groups to set 163 */ 164 public void setParamGroups(String paramGroups) { 165 166 m_paramGroups = paramGroups; 167 } 168 169 /** 170 * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String) 171 */ 172 @Override 173 protected String createDialogHtml(String dialog) { 174 175 StringBuffer result = new StringBuffer(1024); 176 177 result.append(createWidgetTableStart()); 178 // show error header once if there were validation errors 179 result.append(createWidgetErrorHeader()); 180 181 if (dialog.equals(PAGES[0])) { 182 // create the widgets for the second dialog page 183 result.append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_HEADER_BLOCK_0))); 184 result.append(createWidgetTableStart()); 185 result.append(createDialogRowsHtml(0, 1)); 186 result.append(createWidgetTableEnd()); 187 result.append(dialogBlockEnd()); 188 result.append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_CONTENT_BLOCK_0))); 189 result.append(createWidgetTableStart()); 190 result.append(createDialogRowsHtml(2, 2)); 191 result.append(createWidgetTableEnd()); 192 result.append(dialogBlockEnd()); 193 } 194 195 result.append(createWidgetTableEnd()); 196 return result.toString(); 197 } 198 199 /** 200 * Creates the list of widgets for this dialog.<p> 201 */ 202 @Override 203 protected void defineWidgets() { 204 205 // initialize the project object to use for the dialog 206 initMessageObject(); 207 208 setKeyPrefix(KEY_PREFIX); 209 210 addWidget(new CmsWidgetDialogParameter(m_msgInfo, "from", PAGES[0], new CmsDisplayWidget())); 211 addWidget(new CmsWidgetDialogParameter(m_msgInfo, "to", PAGES[0], new CmsDisplayWidget())); 212 addWidget(new CmsWidgetDialogParameter(m_msgInfo, "msg", PAGES[0], new CmsTextareaWidget(12))); 213 } 214 215 /** 216 * @see org.opencms.workplace.CmsWidgetDialog#getPageArray() 217 */ 218 @Override 219 protected String[] getPageArray() { 220 221 return PAGES; 222 } 223 224 /** 225 * Returns a semicolon separated list of user names.<p> 226 * 227 * @return a semicolon separated list of user names 228 */ 229 protected String getToNames() { 230 231 StringBuffer result = new StringBuffer(256); 232 Iterator<CmsUser> itUsers = getUsers().iterator(); 233 while (itUsers.hasNext()) { 234 CmsUser user = itUsers.next(); 235 result.append(user.getFullName()); 236 if (itUsers.hasNext()) { 237 result.append("; "); 238 } 239 } 240 return result.toString(); 241 } 242 243 /** 244 * Initializes the message info object to work with depending on the dialog state and request parameters.<p> 245 */ 246 protected void initMessageObject() { 247 248 try { 249 if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) { 250 // create a new message info object 251 m_msgInfo = new CmsMessageInfo(); 252 } else { 253 // this is not the initial call, get the message info object from session 254 m_msgInfo = (CmsMessageInfo)getDialogObject(); 255 } 256 } catch (Exception e) { 257 // create a new message info object 258 m_msgInfo = new CmsMessageInfo(); 259 } 260 m_msgInfo.setFrom(getCms().getRequestContext().getCurrentUser().getFullName()); 261 m_msgInfo.setTo(getToNames()); 262 } 263 264 /** 265 * @see org.opencms.workplace.CmsWorkplace#initMessages() 266 */ 267 @Override 268 protected void initMessages() { 269 270 // add specific dialog resource bundle 271 addMessages(Messages.get().getBundleName()); 272 addMessages(org.opencms.workplace.tools.workplace.Messages.get().getBundleName()); 273 // add default resource bundles 274 super.initMessages(); 275 } 276 277 /** 278 * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest) 279 */ 280 @Override 281 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 282 283 // initialize parameters and dialog actions in super implementation 284 super.initWorkplaceRequestValues(settings, request); 285 286 // save the current state of the message (may be changed because of the widget values) 287 setDialogObject(m_msgInfo); 288 } 289 290 /** 291 * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters() 292 */ 293 @Override 294 protected void validateParamaters() throws Exception { 295 296 if ((getUsers() == null) || getUsers().isEmpty()) { 297 throw new Exception(); 298 } 299 } 300 301 /** 302 * Returns a list of all members of the selected groups.<p> 303 * 304 * @return a list of user objects 305 */ 306 private List<CmsUser> getUsers() { 307 308 if (m_users == null) { 309 m_users = new ArrayList<CmsUser>(); 310 List<CmsUser> manageableUsers = new ArrayList<CmsUser>(); 311 try { 312 manageableUsers = OpenCms.getRoleManager().getManageableUsers(getCms(), "", true); 313 } catch (CmsException e) { 314 if (LOG.isErrorEnabled()) { 315 LOG.error(e.getLocalizedMessage(), e); 316 } 317 } 318 Iterator<String> itGroups = getGroups().iterator(); 319 while (itGroups.hasNext()) { 320 String groupName = itGroups.next(); 321 try { 322 Iterator<CmsUser> itUsers = getCms().getUsersOfGroup(groupName, true).iterator(); 323 while (itUsers.hasNext()) { 324 CmsUser user = itUsers.next(); 325 if (OpenCms.getSessionManager().getSessionInfos(user.getId()).isEmpty()) { 326 continue; 327 } 328 if (!manageableUsers.contains(user)) { 329 continue; 330 } 331 m_users.add(user); 332 } 333 } catch (CmsException e) { 334 // should never happen 335 } 336 } 337 } 338 return m_users; 339 } 340}