001: /*
002: * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/workplace/broadcast/CmsSendEmailDialog.java,v $
003: * Date : $Date: 2008-02-27 12:05:53 $
004: * Version: $Revision: 1.13 $
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.main.CmsSessionInfo;
039: import org.opencms.main.OpenCms;
040: import org.opencms.util.CmsStringUtil;
041: import org.opencms.widgets.CmsDisplayWidget;
042: import org.opencms.widgets.CmsInputWidget;
043: import org.opencms.widgets.CmsTextareaWidget;
044: import org.opencms.workplace.CmsWidgetDialogParameter;
045:
046: import java.util.ArrayList;
047: import java.util.Collections;
048: import java.util.Iterator;
049: import java.util.List;
050:
051: import javax.servlet.http.HttpServletRequest;
052: import javax.servlet.http.HttpServletResponse;
053: import javax.servlet.jsp.PageContext;
054:
055: import org.apache.commons.logging.Log;
056:
057: /**
058: * Dialog to edit an email to send in the administration view.<p>
059: *
060: * @author Michael Moossen
061: *
062: * @version $Revision: 1.13 $
063: *
064: * @since 6.0.0
065: */
066: public class CmsSendEmailDialog extends A_CmsMessageDialog {
067:
068: /** localized messages Keys prefix. */
069: public static final String KEY_PREFIX = "email";
070:
071: /** The static log object for this class. */
072: private static final Log LOG = CmsLog
073: .getLog(CmsSendEmailDialog.class);
074:
075: /** a warning about excluded users with no email. */
076: private String m_excludedUsers = "";
077:
078: /**
079: * Public constructor with JSP action element.<p>
080: *
081: * @param jsp an initialized JSP action element
082: */
083: public CmsSendEmailDialog(CmsJspActionElement jsp) {
084:
085: super (jsp);
086: }
087:
088: /**
089: * Public constructor with JSP variables.<p>
090: *
091: * @param context the JSP page context
092: * @param req the JSP request
093: * @param res the JSP response
094: */
095: public CmsSendEmailDialog(PageContext context,
096: HttpServletRequest req, HttpServletResponse res) {
097:
098: this (new CmsJspActionElement(context, req, res));
099: }
100:
101: /**
102: * Commits the edited project to the db.<p>
103: */
104: public void actionCommit() {
105:
106: List errors = new ArrayList();
107:
108: if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_msgInfo.getTo())) {
109: setCommitErrors(Collections
110: .singletonList(new CmsIllegalStateException(
111: Messages
112: .get()
113: .container(
114: Messages.ERR_NO_SELECTED_USER_WITH_EMAIL_0))));
115: return;
116: }
117: try {
118: m_msgInfo.setTo(getEmailAddresses());
119: m_msgInfo.sendEmail(getCms());
120: } catch (Throwable t) {
121: errors.add(t);
122: } finally {
123: m_msgInfo.setTo(getToNames());
124: }
125: // set the list of errors to display when saving failed
126: setCommitErrors(errors);
127: }
128:
129: /**
130: * Returns a warning if users have been excluded.<p>
131: *
132: * @return a warning
133: */
134: public String getExcludedUsers() {
135:
136: return m_excludedUsers;
137: }
138:
139: /**
140: * Sets the warning message if users have been excluded.<p>
141: *
142: * @param excludedUsers the warning message
143: */
144: public void setExcludedUsers(String excludedUsers) {
145:
146: m_excludedUsers = excludedUsers;
147: }
148:
149: /**
150: * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
151: */
152: protected String createDialogHtml(String dialog) {
153:
154: StringBuffer result = new StringBuffer(1024);
155:
156: result.append(createWidgetTableStart());
157: // show error header once if there were validation errors
158: result.append(createWidgetErrorHeader());
159:
160: int n = 4;
161: getToNames(); // need it to fill the exclude users property
162: if (CmsStringUtil
163: .isNotEmptyOrWhitespaceOnly(getExcludedUsers())) {
164: n++;
165: }
166: if (dialog.equals(PAGES[0])) {
167: // create the widgets for the first dialog page
168: result
169: .append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_HEADER_BLOCK_0)));
170: result.append(createWidgetTableStart());
171: result.append(createDialogRowsHtml(0, n - 1));
172: result.append(createWidgetTableEnd());
173: result.append(dialogBlockEnd());
174: result
175: .append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_CONTENT_BLOCK_0)));
176: result.append(createWidgetTableStart());
177: result.append(createDialogRowsHtml(n, n));
178: result.append(createWidgetTableEnd());
179: result.append(dialogBlockEnd());
180: }
181:
182: result.append(createWidgetTableEnd());
183: return result.toString();
184: }
185:
186: /**
187: * Creates the list of widgets for this dialog.<p>
188: */
189: protected void defineWidgets() {
190:
191: // initialize the project object to use for the dialog
192: initMessageObject();
193:
194: setKeyPrefix(KEY_PREFIX);
195:
196: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "from",
197: PAGES[0], new CmsDisplayWidget()));
198: if (CmsStringUtil
199: .isNotEmptyOrWhitespaceOnly(getExcludedUsers())) {
200: addWidget(new CmsWidgetDialogParameter(this ,
201: "excludedUsers", PAGES[0], new CmsDisplayWidget()));
202: }
203: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "to",
204: PAGES[0], new CmsDisplayWidget()));
205: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "cc",
206: PAGES[0], "", new CmsInputWidget(), 0, 1));
207: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "subject",
208: PAGES[0], new CmsInputWidget()));
209: addWidget(new CmsWidgetDialogParameter(m_msgInfo, "msg",
210: PAGES[0], new CmsTextareaWidget(12)));
211: }
212:
213: /**
214: * Returns a semicolon separated list of user names.<p>
215: *
216: * @return a semicolon separated list of user names
217: */
218: protected String getToNames() {
219:
220: List excluded = new ArrayList();
221: List users = new ArrayList();
222: Iterator itIds = idsList().iterator();
223: while (itIds.hasNext()) {
224: String id = itIds.next().toString();
225: CmsSessionInfo session = OpenCms.getSessionManager()
226: .getSessionInfo(id);
227: if (session != null) {
228: try {
229: CmsUser user = getCms().readUser(
230: session.getUserId());
231: String userName = user.getFullName();
232: if (!users.contains(userName)) {
233: String emailAddress = user.getEmail();
234: if (CmsStringUtil
235: .isNotEmptyOrWhitespaceOnly(emailAddress)) {
236: users.add(userName);
237: } else {
238: excluded.add(userName);
239: }
240: }
241: } catch (Exception e) {
242: LOG.error(e.getLocalizedMessage(), e);
243: }
244: }
245: }
246: if (!excluded.isEmpty()) {
247: StringBuffer text = new StringBuffer(500);
248: text.append(Messages.get().container(
249: Messages.GUI_EXCLUDED_USERS_WARNING_0).key(
250: getLocale()));
251: text.append("\n");
252: Iterator it = excluded.iterator();
253: while (it.hasNext()) {
254: text.append("- ");
255: text.append(it.next());
256: text.append("\n");
257: }
258: setExcludedUsers(text.toString());
259: }
260: if (users.isEmpty()) {
261: setCommitErrors(Collections
262: .singletonList(new CmsIllegalStateException(
263: Messages
264: .get()
265: .container(
266: Messages.ERR_NO_SELECTED_USER_WITH_EMAIL_0))));
267: return "";
268: }
269: StringBuffer result = new StringBuffer(256);
270: Iterator itUsers = users.iterator();
271: while (itUsers.hasNext()) {
272: result.append(itUsers.next().toString());
273: if (itUsers.hasNext()) {
274: result.append("; ");
275: }
276: }
277: return result.toString();
278: }
279:
280: /**
281: * Returns a semicolon separated list of email addresses.<p>
282: *
283: * @return a semicolon separated list of email addresses
284: */
285: private String getEmailAddresses() {
286:
287: List emails = new ArrayList();
288: Iterator itIds = idsList().iterator();
289: while (itIds.hasNext()) {
290: String id = itIds.next().toString();
291: CmsSessionInfo session = OpenCms.getSessionManager()
292: .getSessionInfo(id);
293: if (session != null) {
294: try {
295: String emailAddress = getCms().readUser(
296: session.getUserId()).getEmail();
297: if (CmsStringUtil
298: .isNotEmptyOrWhitespaceOnly(emailAddress)
299: && !emails.contains(emailAddress)) {
300: emails.add(emailAddress);
301: }
302: } catch (Exception e) {
303: LOG.error(e.getLocalizedMessage(), e);
304: }
305: }
306: }
307: StringBuffer result = new StringBuffer(256);
308: Iterator itEmails = emails.iterator();
309: while (itEmails.hasNext()) {
310: result.append(itEmails.next().toString());
311: if (itEmails.hasNext()) {
312: result.append("; ");
313: }
314: }
315: return result.toString();
316: }
317: }
|