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