001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/workplace/broadcast/CmsSendEmailGroupsDialog.java,v $
003: * Date : $Date: 2008-02-27 12:05:53 $
004: * Version: $Revision: 1.4 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.workplace.tools.workplace.broadcast;
033:
034: import org.opencms.file.CmsUser;
035: import org.opencms.jsp.CmsJspActionElement;
036: import org.opencms.main.CmsIllegalStateException;
037: import org.opencms.main.CmsLog;
038: import org.opencms.util.CmsStringUtil;
039: import org.opencms.widgets.CmsDisplayWidget;
040: import org.opencms.widgets.CmsInputWidget;
041: import org.opencms.widgets.CmsTextareaWidget;
042: import org.opencms.workplace.CmsDialog;
043: import org.opencms.workplace.CmsWidgetDialog;
044: import org.opencms.workplace.CmsWidgetDialogParameter;
045: import org.opencms.workplace.CmsWorkplaceSettings;
046: import org.opencms.workplace.list.CmsHtmlList;
047:
048: import java.util.ArrayList;
049: import java.util.Collections;
050: import java.util.Iterator;
051: import java.util.List;
052:
053: import javax.servlet.http.HttpServletRequest;
054: import javax.servlet.http.HttpServletResponse;
055: import javax.servlet.jsp.PageContext;
056:
057: import org.apache.commons.logging.Log;
058:
059: /**
060: * Dialog to send a new email to the selected groups.<p>
061: *
062: * @author Michael Moossen
063: *
064: * @version $Revision: 1.4 $
065: *
066: * @since 6.5.6
067: */
068: public class CmsSendEmailGroupsDialog extends CmsWidgetDialog {
069:
070: /** localized messages Keys prefix. */
071: public static final String KEY_PREFIX = "email";
072:
073: /** Defines which pages are valid for this dialog. */
074: public static final String[] PAGES = { "page1" };
075:
076: /** Parameter name constant. */
077: public static final String PARAM_GROUPS = "groups";
078:
079: /** The static log object for this class. */
080: private static final Log LOG = CmsLog
081: .getLog(CmsSendEmailGroupsDialog.class);
082:
083: /** Message info object. */
084: protected CmsMessageInfo m_msgInfo;
085:
086: /** a warning about excluded users with no email. */
087: private String m_excludedUsers;
088:
089: /** The selected groups. */
090: private String m_paramGroups;
091:
092: /**
093: * Public constructor with JSP action element.<p>
094: *
095: * @param jsp an initialized JSP action element
096: */
097: public CmsSendEmailGroupsDialog(CmsJspActionElement jsp) {
098:
099: super (jsp);
100: }
101:
102: /**
103: * Public constructor with JSP variables.<p>
104: *
105: * @param context the JSP page context
106: * @param req the JSP request
107: * @param res the JSP response
108: */
109: public CmsSendEmailGroupsDialog(PageContext context,
110: HttpServletRequest req, HttpServletResponse res) {
111:
112: this (new CmsJspActionElement(context, req, res));
113: }
114:
115: /**
116: * Commits the edited project to the db.<p>
117: */
118: public void actionCommit() {
119:
120: List errors = new ArrayList();
121:
122: if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_msgInfo.getTo())) {
123: setCommitErrors(Collections
124: .singletonList(new CmsIllegalStateException(
125: Messages
126: .get()
127: .container(
128: Messages.ERR_NO_SELECTED_USER_WITH_EMAIL_0))));
129: return;
130: }
131: try {
132: m_msgInfo.setTo(getEmailAddresses());
133: m_msgInfo.sendEmail(getCms());
134: } catch (Throwable t) {
135: errors.add(t);
136: } finally {
137: m_msgInfo.setTo(getToNames());
138: }
139: // set the list of errors to display when saving failed
140: setCommitErrors(errors);
141: }
142:
143: /**
144: * Returns a warning if users have been excluded.<p>
145: *
146: * @return a warning
147: */
148: public String getExcludedUsers() {
149:
150: if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_excludedUsers)) {
151: getToNames();
152: }
153: if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_excludedUsers)) {
154: return "";
155: }
156: return m_excludedUsers;
157: }
158:
159: /**
160: * Returns the selected groups.<p>
161: *
162: * @return the selected groups
163: */
164: public List getGroups() {
165:
166: return CmsStringUtil.splitAsList(getParamGroups(),
167: CmsHtmlList.ITEM_SEPARATOR);
168: }
169:
170: /**
171: * Returns the list of selected groups.<p>
172: *
173: * @return the list of selected groups
174: */
175: public String getParamGroups() {
176:
177: return m_paramGroups;
178: }
179:
180: /**
181: * Sets the warning message if users have been excluded.<p>
182: *
183: * @param excludedUsers the warning message
184: */
185: public void setExcludedUsers(String excludedUsers) {
186:
187: m_excludedUsers = excludedUsers;
188: }
189:
190: /**
191: * Sets the list of selected groups.<p>
192: *
193: * @param paramGroups the list of selected groups to set
194: */
195: public void setParamGroups(String paramGroups) {
196:
197: m_paramGroups = paramGroups;
198: }
199:
200: /**
201: * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
202: */
203: protected String createDialogHtml(String dialog) {
204:
205: StringBuffer result = new StringBuffer(1024);
206:
207: result.append(createWidgetTableStart());
208: // show error header once if there were validation errors
209: result.append(createWidgetErrorHeader());
210:
211: if (dialog.equals(PAGES[0])) {
212: int n = 4;
213: if (CmsStringUtil
214: .isNotEmptyOrWhitespaceOnly(getExcludedUsers())) {
215: n++;
216: }
217: // create the widgets for the second dialog page
218: result
219: .append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_HEADER_BLOCK_0)));
220: result.append(createWidgetTableStart());
221: result.append(createDialogRowsHtml(0, n - 1));
222: result.append(createWidgetTableEnd());
223: result.append(dialogBlockEnd());
224: result
225: .append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_CONTENT_BLOCK_0)));
226: result.append(createWidgetTableStart());
227: result.append(createDialogRowsHtml(n, n));
228: result.append(createWidgetTableEnd());
229: result.append(dialogBlockEnd());
230: }
231:
232: result.append(createWidgetTableEnd());
233: return result.toString();
234: }
235:
236: /**
237: * Creates the list of widgets for this dialog.<p>
238: */
239: protected void defineWidgets() {
240:
241: // initialize the project object to use for the dialog
242: initMessageObject();
243:
244: setKeyPrefix(KEY_PREFIX);
245:
246: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "from",
247: PAGES[0], new CmsDisplayWidget()));
248: if (CmsStringUtil
249: .isNotEmptyOrWhitespaceOnly(getExcludedUsers())) {
250: addWidget(new CmsWidgetDialogParameter(this ,
251: "excludedUsers", PAGES[0], new CmsDisplayWidget()));
252: }
253: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "to",
254: PAGES[0], new CmsDisplayWidget()));
255: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "cc",
256: PAGES[0], "", new CmsInputWidget(), 0, 1));
257: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "subject",
258: PAGES[0], new CmsInputWidget()));
259: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "msg",
260: PAGES[0], new CmsTextareaWidget(12)));
261: }
262:
263: /**
264: * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
265: */
266: protected String[] getPageArray() {
267:
268: return PAGES;
269: }
270:
271: /**
272: * Returns a semicolon separated list of user names.<p>
273: *
274: * @return a semicolon separated list of user names
275: */
276: protected String getToNames() {
277:
278: List excluded = new ArrayList();
279: List users = new ArrayList();
280: Iterator itGroups = getGroups().iterator();
281: while (itGroups.hasNext()) {
282: String groupName = (String) itGroups.next();
283: try {
284: Iterator itUsers = getCms().getUsersOfGroup(groupName,
285: true).iterator();
286: while (itUsers.hasNext()) {
287: CmsUser user = (CmsUser) itUsers.next();
288: String userName = user.getFullName();
289: if (!users.contains(userName)) {
290: String emailAddress = user.getEmail();
291: if (CmsStringUtil
292: .isNotEmptyOrWhitespaceOnly(emailAddress)) {
293: users.add(userName);
294: } else {
295: excluded.add(userName);
296: }
297: }
298: }
299: } catch (Exception e) {
300: LOG.error(e.getLocalizedMessage(), e);
301: }
302: }
303: if (!excluded.isEmpty()) {
304: StringBuffer text = new StringBuffer(500);
305: text.append(Messages.get().container(
306: Messages.GUI_EXCLUDED_USERS_WARNING_0).key(
307: getLocale()));
308: text.append("<br>");
309: Iterator it = excluded.iterator();
310: while (it.hasNext()) {
311: text.append("- ");
312: text.append(it.next());
313: text.append("<br>");
314: }
315: setExcludedUsers(text.toString());
316: }
317: if (users.isEmpty()) {
318: setCommitErrors(Collections
319: .singletonList(new CmsIllegalStateException(
320: Messages
321: .get()
322: .container(
323: Messages.ERR_NO_SELECTED_USER_WITH_EMAIL_0))));
324: return "";
325: }
326: StringBuffer result = new StringBuffer(256);
327: Iterator itUsers = users.iterator();
328: while (itUsers.hasNext()) {
329: result.append(itUsers.next().toString());
330: if (itUsers.hasNext()) {
331: result.append("; ");
332: }
333: }
334: return result.toString();
335: }
336:
337: /**
338: * Initializes the message info object to work with depending on the dialog state and request parameters.<p>
339: */
340: protected void initMessageObject() {
341:
342: try {
343: if (CmsStringUtil.isEmpty(getParamAction())
344: || CmsDialog.DIALOG_INITIAL
345: .equals(getParamAction())) {
346: // create a new message info object
347: m_msgInfo = new CmsMessageInfo();
348: } else {
349: // this is not the initial call, get the message info object from session
350: m_msgInfo = (CmsMessageInfo) getDialogObject();
351: }
352: } catch (Exception e) {
353: // create a new message info object
354: m_msgInfo = new CmsMessageInfo();
355: }
356: m_msgInfo.setFrom(getCms().getRequestContext().currentUser()
357: .getFullName());
358: m_msgInfo.setTo(getToNames());
359: }
360:
361: /**
362: * @see org.opencms.workplace.CmsWorkplace#initMessages()
363: */
364: protected void initMessages() {
365:
366: // add specific dialog resource bundle
367: addMessages(Messages.get().getBundleName());
368: addMessages(org.opencms.workplace.tools.workplace.Messages
369: .get().getBundleName());
370: // add default resource bundles
371: super .initMessages();
372: }
373:
374: /**
375: * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
376: */
377: protected void initWorkplaceRequestValues(
378: CmsWorkplaceSettings settings, HttpServletRequest request) {
379:
380: // initialize parameters and dialog actions in super implementation
381: super .initWorkplaceRequestValues(settings, request);
382:
383: // save the current state of the message (may be changed because of the widget values)
384: setDialogObject(m_msgInfo);
385: }
386:
387: /**
388: * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
389: */
390: protected void validateParamaters() throws Exception {
391:
392: if ((getGroups() == null) || getGroups().isEmpty()) {
393: throw new Exception();
394: }
395: }
396:
397: /**
398: * Returns a semicolon separated list of email addresses.<p>
399: *
400: * @return a semicolon separated list of email addresses
401: */
402: private String getEmailAddresses() {
403:
404: List emails = new ArrayList();
405: Iterator itGroups = getGroups().iterator();
406: while (itGroups.hasNext()) {
407: String groupName = (String) itGroups.next();
408: try {
409: Iterator itUsers = getCms().getUsersOfGroup(groupName,
410: true).iterator();
411: while (itUsers.hasNext()) {
412: CmsUser user = (CmsUser) itUsers.next();
413: String emailAddress = user.getEmail();
414: if (CmsStringUtil
415: .isNotEmptyOrWhitespaceOnly(emailAddress)
416: && !emails.contains(emailAddress)) {
417: emails.add(emailAddress);
418: }
419: }
420: } catch (Exception e) {
421: LOG.error(e.getLocalizedMessage(), e);
422: }
423: }
424: StringBuffer result = new StringBuffer(256);
425: Iterator itEmails = emails.iterator();
426: while (itEmails.hasNext()) {
427: result.append(itEmails.next().toString());
428: if (itEmails.hasNext()) {
429: result.append("; ");
430: }
431: }
432: return result.toString();
433: }
434: }
|