KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > workplace > broadcast > CmsSendEmailDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/workplace/broadcast/CmsSendEmailDialog.java,v $
3  * Date : $Date: 2005/06/29 20:16:25 $
4  * Version: $Revision: 1.10 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools.workplace.broadcast;
33
34 import org.opencms.jsp.CmsJspActionElement;
35 import org.opencms.main.CmsIllegalStateException;
36 import org.opencms.main.CmsSessionInfo;
37 import org.opencms.main.OpenCms;
38 import org.opencms.util.CmsStringUtil;
39 import org.opencms.widgets.CmsDisplayWidget;
40 import org.opencms.widgets.CmsInputWidget;
41 import org.opencms.widgets.CmsTextareaWidget;
42 import org.opencms.workplace.CmsWidgetDialogParameter;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48
49 import javax.servlet.http.HttpServletRequest JavaDoc;
50 import javax.servlet.http.HttpServletResponse JavaDoc;
51 import javax.servlet.jsp.PageContext JavaDoc;
52
53 /**
54  * Dialog to edit an email to send in the administration view.<p>
55  *
56  * @author Michael Moossen
57  *
58  * @version $Revision: 1.10 $
59  *
60  * @since 6.0.0
61  */

62 public class CmsSendEmailDialog extends A_CmsMessageDialog {
63
64     /** localized messages Keys prefix. */
65     public static final String JavaDoc KEY_PREFIX = "email";
66
67     /** a warning about excluded users with no email. */
68     private String JavaDoc m_excludedUsers = "";
69
70     /**
71      * Public constructor with JSP action element.<p>
72      *
73      * @param jsp an initialized JSP action element
74      */

75     public CmsSendEmailDialog(CmsJspActionElement jsp) {
76
77         super(jsp);
78     }
79
80     /**
81      * Public constructor with JSP variables.<p>
82      *
83      * @param context the JSP page context
84      * @param req the JSP request
85      * @param res the JSP response
86      */

87     public CmsSendEmailDialog(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
88
89         this(new CmsJspActionElement(context, req, res));
90     }
91
92     /**
93      * Commits the edited project to the db.<p>
94      */

95     public void actionCommit() {
96
97         List JavaDoc errors = new ArrayList JavaDoc();
98
99         if (CmsStringUtil.isEmptyOrWhitespaceOnly(m_msgInfo.getTo())) {
100             setCommitErrors(Collections.singletonList(new CmsIllegalStateException(Messages.get().container(
101                 Messages.ERR_NO_SELECTED_USER_WITH_EMAIL_0))));
102             return;
103         }
104         try {
105             m_msgInfo.setTo(getEmailAddresses());
106             m_msgInfo.sendEmail(getCms());
107         } catch (Throwable JavaDoc t) {
108             errors.add(t);
109         } finally {
110             m_msgInfo.setTo(getToNames());
111         }
112         // set the list of errors to display when saving failed
113
setCommitErrors(errors);
114     }
115
116     /**
117      * Returns a warning if users have been excluded.<p>
118      *
119      * @return a warning
120      */

121     public String JavaDoc getExcludedUsers() {
122
123         return m_excludedUsers;
124     }
125
126     /**
127      * Sets the warning message if users have been excluded.<p>
128      *
129      * @param excludedUsers the warning message
130      */

131     public void setExcludedUsers(String JavaDoc excludedUsers) {
132
133         m_excludedUsers = excludedUsers;
134     }
135
136     /**
137      * @see org.opencms.workplace.CmsWidgetDialog#createDialogHtml(java.lang.String)
138      */

139     protected String JavaDoc createDialogHtml(String JavaDoc dialog) {
140
141         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
142
143         result.append(createWidgetTableStart());
144         // show error header once if there were validation errors
145
result.append(createWidgetErrorHeader());
146
147         int n = 4;
148         getToNames(); // need it to fill the exclude users property
149
if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getExcludedUsers())) {
150             n++;
151         }
152         if (dialog.equals(PAGES[0])) {
153             // create the widgets for the first dialog page
154
result.append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_HEADER_BLOCK_0)));
155             result.append(createWidgetTableStart());
156             result.append(createDialogRowsHtml(0, n - 1));
157             result.append(createWidgetTableEnd());
158             result.append(dialogBlockEnd());
159             result.append(dialogBlockStart(key(Messages.GUI_MESSAGE_EDITOR_LABEL_CONTENT_BLOCK_0)));
160             result.append(createWidgetTableStart());
161             result.append(createDialogRowsHtml(n, n));
162             result.append(createWidgetTableEnd());
163             result.append(dialogBlockEnd());
164         }
165
166         result.append(createWidgetTableEnd());
167         return result.toString();
168     }
169
170     /**
171      * Creates the list of widgets for this dialog.<p>
172      */

173     protected void defineWidgets() {
174
175         // initialize the project object to use for the dialog
176
initMessageObject();
177
178         setKeyPrefix(KEY_PREFIX);
179
180         addWidget(new CmsWidgetDialogParameter(m_msgInfo, "from", PAGES[0], new CmsDisplayWidget()));
181         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getExcludedUsers())) {
182             addWidget(new CmsWidgetDialogParameter(this, "excludedUsers", PAGES[0], new CmsDisplayWidget()));
183         }
184         addWidget(new CmsWidgetDialogParameter(m_msgInfo, "to", PAGES[0], new CmsDisplayWidget()));
185         addWidget(new CmsWidgetDialogParameter(m_msgInfo, "cc", PAGES[0], "", new CmsInputWidget(), 0, 1));
186         addWidget(new CmsWidgetDialogParameter(m_msgInfo, "subject", PAGES[0], new CmsInputWidget()));
187         addWidget(new CmsWidgetDialogParameter(m_msgInfo, "msg", PAGES[0], new CmsTextareaWidget(12)));
188     }
189
190     /**
191      * Returns a semicolon separated list of user names.<p>
192      *
193      * @return a semicolon separated list of user names
194      */

195     protected String JavaDoc getToNames() {
196
197         List JavaDoc excluded = new ArrayList JavaDoc();
198         List JavaDoc users = new ArrayList JavaDoc();
199         Iterator JavaDoc itIds = idsList().iterator();
200         while (itIds.hasNext()) {
201             String JavaDoc id = itIds.next().toString();
202             CmsSessionInfo session = OpenCms.getSessionManager().getSessionInfo(id);
203             if (session != null) {
204                 String JavaDoc userName = session.getUser().getFullName();
205                 String JavaDoc emailAddress = session.getUser().getEmail();
206                 if (!users.contains(userName)) {
207                     if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(emailAddress)) {
208                         users.add(userName);
209                     } else {
210                         excluded.add(userName);
211                     }
212                 }
213             }
214         }
215         if (!excluded.isEmpty()) {
216             StringBuffer JavaDoc text = new StringBuffer JavaDoc(500);
217             text.append(Messages.get().container(Messages.GUI_EXCLUDED_USERS_WARNING_0).key(getLocale()));
218             text.append("\n");
219             Iterator JavaDoc it = excluded.iterator();
220             while (it.hasNext()) {
221                 text.append("- ");
222                 text.append(it.next());
223                 text.append("\n");
224             }
225             setExcludedUsers(text.toString());
226         }
227         if (users.isEmpty()) {
228             setCommitErrors(Collections.singletonList(new CmsIllegalStateException(Messages.get().container(
229                 Messages.ERR_NO_SELECTED_USER_WITH_EMAIL_0))));
230             return "";
231         }
232         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
233         Iterator JavaDoc itUsers = users.iterator();
234         while (itUsers.hasNext()) {
235             result.append(itUsers.next().toString());
236             if (itUsers.hasNext()) {
237                 result.append("; ");
238             }
239         }
240         return result.toString();
241     }
242
243     /**
244      * Returns a semicolon separated list of email addresses.<p>
245      *
246      * @return a semicolon separated list of email addresses
247      */

248     private String JavaDoc getEmailAddresses() {
249
250         List JavaDoc emails = new ArrayList JavaDoc();
251         Iterator JavaDoc itIds = idsList().iterator();
252         while (itIds.hasNext()) {
253             String JavaDoc id = itIds.next().toString();
254             CmsSessionInfo session = OpenCms.getSessionManager().getSessionInfo(id);
255             if (session != null) {
256                 String JavaDoc emailAddress = session.getUser().getEmail();
257                 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(emailAddress) && !emails.contains(emailAddress)) {
258                     emails.add(emailAddress);
259                 }
260             }
261         }
262         StringBuffer JavaDoc result = new StringBuffer JavaDoc(256);
263         Iterator JavaDoc itEmails = emails.iterator();
264         while (itEmails.hasNext()) {
265             result.append(itEmails.next().toString());
266             if (itEmails.hasNext()) {
267                 result.append("; ");
268             }
269         }
270         return result.toString();
271     }
272 }
Popular Tags