KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > frontend > templateone > CmsTemplateFormRecommend


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/CmsTemplateFormRecommend.java,v $
3  * Date : $Date: 2006/03/27 14:52:51 $
4  * Version: $Revision: 1.14 $
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.frontend.templateone;
33
34 import org.opencms.mail.CmsHtmlMail;
35 import org.opencms.main.CmsException;
36 import org.opencms.main.CmsLog;
37 import org.opencms.main.OpenCms;
38 import org.opencms.util.CmsStringUtil;
39 import org.opencms.workplace.CmsWorkplace;
40
41 import java.util.HashMap JavaDoc;
42
43 import javax.servlet.http.HttpServletRequest JavaDoc;
44 import javax.servlet.http.HttpServletResponse JavaDoc;
45 import javax.servlet.jsp.PageContext JavaDoc;
46
47 import org.apache.commons.logging.Log;
48
49 /**
50  * Provides methods to build the page recommendation form.<p>
51  *
52  * @author Andreas Zahner
53  *
54  * @version $Revision: 1.14 $
55  *
56  * @since 6.0.0
57  */

58 public class CmsTemplateFormRecommend extends CmsTemplateForm {
59
60     /** The log object for this class. */
61     private static final Log LOG = CmsLog.getLog(CmsTemplateFormRecommend.class);
62
63     /** Stores the send copy to sender flag.<p> */
64     private String JavaDoc m_copy;
65     /** Stores the email recipient address.<p> */
66     private String JavaDoc m_emailRecipient;
67     /** Stores the email sender address.<p> */
68     private String JavaDoc m_emailSender;
69     /** Stores the message for the recipient.<p> */
70     private String JavaDoc m_message;
71
72     /**
73      * Empty constructor, required for every JavaBean.<p>
74      */

75     public CmsTemplateFormRecommend() {
76
77         super();
78         // set the members to empty Strings
79
m_emailRecipient = "";
80         m_emailSender = "";
81         m_message = "";
82         m_copy = "";
83     }
84
85     /**
86      * Constructor, with parameters.<p>
87      *
88      * Use this constructor for the template.<p>
89      *
90      * @param context the JSP page context object
91      * @param req the JSP request
92      * @param res the JSP response
93      */

94     public CmsTemplateFormRecommend(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
95
96         super();
97         super.init(context, req, res);
98     }
99
100     /**
101      * Returns the send copy to sender flag.<p>
102      *
103      * @return the send copy to sender flag
104      */

105     public String JavaDoc getCopy() {
106
107         return m_copy;
108     }
109
110     /**
111      * Returns the email recipient address.<p>
112      *
113      * @return the email recipient address
114      */

115     public String JavaDoc getEmailRecipient() {
116
117         return m_emailRecipient;
118     }
119
120     /**
121      * Returns the email sender address.<p>
122      *
123      * @return the email sender address
124      */

125     public String JavaDoc getEmailSender() {
126
127         return m_emailSender;
128     }
129
130     /**
131      * Returns the message for the recipient.<p>
132      *
133      * @return the message for the recipient
134      */

135     public String JavaDoc getMessage() {
136
137         return m_message;
138     }
139
140     /**
141      * Examines the value of the send copy checkbox and returns the "checked" attribute.<p>
142      *
143      * @return the "checked" attribute or an empty String
144      */

145     public String JavaDoc isCopyChecked() {
146
147         return isChecked(getCopy());
148     }
149
150     /**
151      * Sends the recommendation email(s) to the recipient and/or the sender.<p>
152      *
153      * @return true if the emails were successfully sent, otherwise false;
154      */

155     public boolean sendMail() {
156
157         // create the new mail message
158
CmsHtmlMail theMail = new CmsHtmlMail();
159         theMail.setSubject(key("recommend.mail.subject.prefix") + getPageTitle());
160         theMail.setCharset(getRequestContext().getEncoding());
161         theMail.setHtmlMsg(getContent("recommend_mail.html", "html", getRequestContext().getLocale()));
162         theMail.setTextMsg(getContent("recommend_mail.html", "text", getRequestContext().getLocale()));
163         try {
164             // set the recipient and the reply to address
165
theMail.addTo(getEmailRecipient());
166             String JavaDoc sender = OpenCms.getSystemInfo().getMailSettings().getMailFromDefault();
167             String JavaDoc replyTo = getEmailSender();
168             if (CmsStringUtil.isEmptyOrWhitespaceOnly(replyTo)) {
169                 replyTo = sender;
170             }
171             theMail.setFrom(sender);
172             theMail.addReplyTo(replyTo);
173             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getCopy())) {
174                 // send a copy of the mail to the sender
175
theMail.addCc(replyTo);
176             }
177             // send the mail
178
theMail.send();
179         } catch (Exception JavaDoc e) {
180             if (LOG.isWarnEnabled()) {
181                 LOG.warn(e);
182             } else if (LOG.isErrorEnabled()) {
183                 LOG.error(Messages.get().getBundle().key(
184                     Messages.LOG_SEND_MAIL_RECOMMENDPAGE_1,
185                     getRequestContext().getUri()));
186             }
187             return false;
188         }
189         return true;
190     }
191
192     /**
193      * Sets the send copy to sender flag.<p>
194      *
195      * @param copy the send copy to sender flag
196      */

197     public void setCopy(String JavaDoc copy) {
198
199         m_copy = copy;
200     }
201
202     /**
203      * Sets the email recipient address.<p>
204      *
205      * @param emailRecipient email recipient address
206      */

207     public void setEmailRecipient(String JavaDoc emailRecipient) {
208
209         m_emailRecipient = emailRecipient;
210     }
211
212     /**
213      * Sets the email sender address.<p>
214      *
215      * @param emailSender the email sender address
216      */

217     public void setEmailSender(String JavaDoc emailSender) {
218
219         m_emailSender = emailSender;
220     }
221
222     /**
223      * Sets the message for the recipient.<p>
224      *
225      * @param message the message for the recipient
226      */

227     public void setMessage(String JavaDoc message) {
228
229         m_message = message;
230     }
231
232     /**
233      * Validates the values of the input fields and creates error messages, if necessary.<p>
234      *
235      * @return true if all checked input values are valid, otherwise false
236      */

237     public boolean validate() {
238
239         boolean allOk = true;
240         setErrors(new HashMap JavaDoc());
241
242         // check email recipient
243
if (CmsStringUtil.isEmptyOrWhitespaceOnly(getEmailRecipient())) {
244             // recipient is empty
245
getErrors().put("recipient", key("recommend.error.recipient.empty"));
246             allOk = false;
247         } else if (!isValidEmailAddress(getEmailRecipient())) {
248             // recipient is not valid
249
getErrors().put("recipient", key("recommend.error.recipient.wrong"));
250             allOk = false;
251         }
252         // check message
253
if (CmsStringUtil.isEmptyOrWhitespaceOnly(getMessage())) {
254             getErrors().put("message", key("recommend.error.message.empty"));
255             allOk = false;
256         }
257         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getCopy())) {
258             // send copy to sender is checked, check sender address
259
if (CmsStringUtil.isEmptyOrWhitespaceOnly(getEmailSender())) {
260                 // sender is empty
261
getErrors().put("sender", key("recommend.error.sender.empty"));
262                 allOk = false;
263             }
264         }
265         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getEmailSender()) && !isValidEmailAddress(getEmailSender())) {
266             // sender is not valid
267
getErrors().put("sender", key("recommend.error.sender.wrong"));
268             allOk = false;
269         }
270         return allOk;
271     }
272
273     /**
274      * @see org.opencms.frontend.templateone.CmsTemplateForm#checkTextsUri()
275      */

276     protected String JavaDoc checkTextsUri() {
277
278         String JavaDoc fileUri = getConfigurationValue("page.form.recommend", null);
279         if (fileUri != null) {
280             fileUri = getRequestContext().removeSiteRoot(fileUri);
281             try {
282                 getCmsObject().readResource(fileUri);
283                 return fileUri;
284             } catch (CmsException e) {
285                 // file not found, use default texts page file
286
}
287         }
288         return CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/pages/recommend_content.html";
289     }
290
291 }
292
Popular Tags