KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/frontend/templateone/CmsTemplateFormLetter.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 "letter to the editor" form.<p>
51  *
52  * @author Andreas Zahner
53  *
54  * @version $Revision: 1.14 $
55  *
56  * @since 6.0.0
57  */

58 public class CmsTemplateFormLetter extends CmsTemplateForm {
59
60     /** The log object for this class. */
61     private static final Log LOG = CmsLog.getLog(CmsTemplateFormLetter.class);
62
63     /** Stores the type of concern.<p> */
64     private String JavaDoc m_concern;
65     /** Stores the details of the concern.<p> */
66     private String JavaDoc m_concernDetail;
67     /** Stores the contact city.<p> */
68     private String JavaDoc m_contactCity;
69     /** Stores the contact country.<p> */
70     private String JavaDoc m_contactCountry;
71     /** Stores the contact email address.<p> */
72     private String JavaDoc m_contactEmail;
73     /** Stores the contacts first name.<p> */
74     private String JavaDoc m_contactFirstName;
75     /** Stores the contacts last name.<p> */
76     private String JavaDoc m_contactLastName;
77     /** Stores the contact street number.<p> */
78     private String JavaDoc m_contactNumber;
79     /** Stores the contact phone number.<p> */
80     private String JavaDoc m_contactPhone;
81     /** Stores the contact salutation.<p> */
82     private String JavaDoc m_contactSalutation;
83     /** Stores the contact street.<p> */
84     private String JavaDoc m_contactStreet;
85     /** Stores the contact title.<p> */
86     private String JavaDoc m_contactTitle;
87     /** Stores the contact zip code.<p> */
88     private String JavaDoc m_contactZip;
89     /** Stores the send copy to sender flag.<p> */
90     private String JavaDoc m_copy;
91     /** Stores the message for the recipient.<p> */
92     private String JavaDoc m_message;
93
94     /**
95      * Empty constructor, required for every JavaBean.<p>
96      */

97     public CmsTemplateFormLetter() {
98
99         super();
100         // set the members to empty Strings
101
m_concern = "";
102         m_concernDetail = "";
103         m_contactCity = "";
104         m_contactCountry = "";
105         m_contactEmail = "";
106         m_contactFirstName = "";
107         m_contactLastName = "";
108         m_contactNumber = "";
109         m_contactPhone = "";
110         m_contactSalutation = "";
111         m_contactStreet = "";
112         m_contactTitle = "";
113         m_contactZip = "";
114         m_copy = "";
115         m_message = "";
116
117     }
118
119     /**
120      * Constructor, with parameters.<p>
121      *
122      * Use this constructor for the template.<p>
123      *
124      * @param context the JSP page context object
125      * @param req the JSP request
126      * @param res the JSP response
127      */

128     public CmsTemplateFormLetter(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
129
130         super();
131         super.init(context, req, res);
132     }
133
134     /**
135      * Returns the concern of contact.<p>
136      *
137      * @return the concern of contact
138      */

139     public String JavaDoc getConcern() {
140
141         return m_concern;
142     }
143
144     /**
145      * Returns the concern details if specified.<p>
146      *
147      * @return the concern details if specified
148      */

149     public String JavaDoc getConcernDetail() {
150
151         return m_concernDetail;
152     }
153
154     /**
155      * Returns the contact city.<p>
156      *
157      * @return the contact city
158      */

159     public String JavaDoc getContactCity() {
160
161         return m_contactCity;
162     }
163
164     /**
165      * Returns the contact country.<p>
166      *
167      * @return the contact country
168      */

169     public String JavaDoc getContactCountry() {
170
171         return m_contactCountry;
172     }
173
174     /**
175      * Returns the contact email address.<p>
176      *
177      * @return the contact email address
178      */

179     public String JavaDoc getContactEmail() {
180
181         return m_contactEmail;
182     }
183
184     /**
185      * Returns the contact first name.<p>
186      *
187      * @return the contact first name
188      */

189     public String JavaDoc getContactFirstName() {
190
191         return m_contactFirstName;
192     }
193
194     /**
195      * Returns the contact last name.<p>
196      *
197      * @return the contact last name
198      */

199     public String JavaDoc getContactLastName() {
200
201         return m_contactLastName;
202     }
203
204     /**
205      * Returns the contact street number.<p>
206      *
207      * @return the contact street number
208      */

209     public String JavaDoc getContactNumber() {
210
211         return m_contactNumber;
212     }
213
214     /**
215      * Returns the contact phone number.<p>
216      *
217      * @return the contact phone number
218      */

219     public String JavaDoc getContactPhone() {
220
221         return m_contactPhone;
222     }
223
224     /**
225      * Returns the contact salutation.<p>
226      *
227      * @return the contact salutation
228      */

229     public String JavaDoc getContactSalutation() {
230
231         return m_contactSalutation;
232     }
233
234     /**
235      * Returns the contact street.<p>
236      *
237      * @return the contact street
238      */

239     public String JavaDoc getContactStreet() {
240
241         return m_contactStreet;
242     }
243
244     /**
245      * Returns the contact title.<p>
246      *
247      * @return the contact title
248      */

249     public String JavaDoc getContactTitle() {
250
251         return m_contactTitle;
252     }
253
254     /**
255      * Returns the contact zip code.<p>
256      *
257      * @return the contact zip code
258      */

259     public String JavaDoc getContactZip() {
260
261         return m_contactZip;
262     }
263
264     /**
265      * Returns the send copy to sender flag.<p>
266      *
267      * @return the send copy to sender flag
268      */

269     public String JavaDoc getCopy() {
270
271         return m_copy;
272     }
273
274     /**
275      * Returns the message for the recipient.<p>
276      *
277      * @return the message for the recipient
278      */

279     public String JavaDoc getMessage() {
280
281         return m_message;
282     }
283
284     /**
285      * Returns the "checked" attribute if the current "concern" radio button is checked.<p>
286      *
287      * @param currentValue the value of the current radio button to check
288      * @return the "checked" attribute if the current "concern" radio button is checked
289      */

290     public String JavaDoc isConcernChecked(String JavaDoc currentValue) {
291
292         if (isSelected(currentValue, getConcern())) {
293             return " checked=\"checked\"";
294         }
295         return "";
296     }
297
298     /**
299      * Examines the value of the send copy checkbox and returns the "checked" attribute.<p>
300      *
301      * @return the "checked" attribute or an empty String
302      */

303     public String JavaDoc isCopyChecked() {
304
305         return isChecked(getCopy());
306     }
307
308     /**
309      * Returns the "selected" attribute if the current "contact option" is selected.<p>
310      *
311      * @param currentValue the value of the current radio button to check
312      * @return the "selected" attribute if the current "contact option" is selected
313      */

314     public String JavaDoc isSalutationSelected(String JavaDoc currentValue) {
315
316         if (isSelected(currentValue, getContactSalutation())) {
317             return " selected=\"selected\"";
318         }
319         return "";
320     }
321
322     /**
323      * Sends the recommendation email(s) to the recipient and/or the sender.<p>
324      *
325      * @return true if the emails were successfully sent, otherwise false;
326      */

327     public boolean sendMail() {
328
329         // create the new mail message
330
CmsHtmlMail theMail = new CmsHtmlMail();
331         theMail.setSubject(key("letter.mail.subject.prefix") + getPageTitle());
332         theMail.setCharset(getRequestContext().getEncoding());
333         theMail.setHtmlMsg(getContent("letter_mail.html", "html", getRequestContext().getLocale()));
334         theMail.setTextMsg(getContent("letter_mail.html", "text", getRequestContext().getLocale()));
335         try {
336             // store the uri
337
String JavaDoc uri = getRequestContext().getUri();
338             // set the recipient from imprint information
339
try {
340                 // create an instance of imprint bean
341
CmsTemplateImprint imprint = new CmsTemplateImprint(getJspContext(), getRequest(), getResponse());
342                 // get the author email address
343
String JavaDoc receiver = imprint.getEmail(null);
344                 theMail.addTo(receiver);
345             } finally {
346                 // set request context uri back because this is changed in imprint bean
347
getRequestContext().setUri(uri);
348             }
349             // set the recipient and the reply to address
350
String JavaDoc sender = OpenCms.getSystemInfo().getMailSettings().getMailFromDefault();
351             String JavaDoc contactMail = getContactEmail();
352             if (CmsStringUtil.isEmptyOrWhitespaceOnly(contactMail)) {
353                 contactMail = sender;
354             }
355             theMail.setFrom(sender);
356             theMail.addReplyTo(contactMail);
357             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getCopy())) {
358                 // send a copy of the mail to the sender
359
theMail.addCc(contactMail);
360             }
361             // send the mail
362
theMail.send();
363         } catch (Exception JavaDoc e) {
364             if (LOG.isWarnEnabled()) {
365                 LOG.warn(e);
366             } else if (LOG.isErrorEnabled()) {
367                 LOG.error(Messages.get().getBundle().key(Messages.LOG_SEND_MAIL_CONTACT_1));
368             }
369             return false;
370         }
371         return true;
372     }
373
374     /**
375      * Sets the concern of contact.<p>
376      *
377      * @param concern the concern of contact
378      */

379     public void setConcern(String JavaDoc concern) {
380
381         m_concern = concern;
382     }
383
384     /**
385      * Sets the concern details if specified.<p>
386      *
387      * @param concernDetail the concern details if specified
388      */

389     public void setConcernDetail(String JavaDoc concernDetail) {
390
391         m_concernDetail = concernDetail;
392     }
393
394     /**
395      * Sets the contact city.<p>
396      *
397      * @param contactCity the contact city
398      */

399     public void setContactCity(String JavaDoc contactCity) {
400
401         m_contactCity = contactCity;
402     }
403
404     /**
405      * Sets the contact country.<p>
406      *
407      * @param contactCountry the contact country
408      */

409     public void setContactCountry(String JavaDoc contactCountry) {
410
411         m_contactCountry = contactCountry;
412     }
413
414     /**
415      * Sets the contact email address.<p>
416      *
417      * @param email the contact email address
418      */

419     public void setContactEmail(String JavaDoc email) {
420
421         m_contactEmail = email;
422     }
423
424     /**
425      * Sets the contact first name.<p>
426      *
427      * @param contactFirstName the contact first name
428      */

429     public void setContactFirstName(String JavaDoc contactFirstName) {
430
431         m_contactFirstName = contactFirstName;
432     }
433
434     /**
435      * Sets the contact last name.<p>
436      *
437      * @param contactLastName the contact last name
438      */

439     public void setContactLastName(String JavaDoc contactLastName) {
440
441         m_contactLastName = contactLastName;
442     }
443
444     /**
445      * Sets the contact street number.<p>
446      *
447      * @param contactNumber the contact street number
448      */

449     public void setContactNumber(String JavaDoc contactNumber) {
450
451         m_contactNumber = contactNumber;
452     }
453
454     /**
455      * Sets the contact phone number.<p>
456      *
457      * @param contactPhone the contact phone number
458      */

459     public void setContactPhone(String JavaDoc contactPhone) {
460
461         m_contactPhone = contactPhone;
462     }
463
464     /**
465      * Sets the contact salutation.<p>
466      *
467      * @param contactSalutation the contact salutation
468      */

469     public void setContactSalutation(String JavaDoc contactSalutation) {
470
471         m_contactSalutation = contactSalutation;
472     }
473
474     /**
475      * Sets the contact street.<p>
476      *
477      * @param contactStreet the contact street
478      */

479     public void setContactStreet(String JavaDoc contactStreet) {
480
481         m_contactStreet = contactStreet;
482     }
483
484     /**
485      * Sets the contact title.<p>
486      *
487      * @param contactTitle the contact title
488      */

489     public void setContactTitle(String JavaDoc contactTitle) {
490
491         m_contactTitle = contactTitle;
492     }
493
494     /**
495      * Sets the contact zip code.<p>
496      *
497      * @param contactZip the contact zip code
498      */

499     public void setContactZip(String JavaDoc contactZip) {
500
501         m_contactZip = contactZip;
502     }
503
504     /**
505      * Sets the send copy to sender flag.<p>
506      *
507      * @param copy the send copy to sender flag
508      */

509     public void setCopy(String JavaDoc copy) {
510
511         m_copy = copy;
512     }
513
514     /**
515      * Sets the message for the recipient.<p>
516      *
517      * @param message the message for the recipient
518      */

519     public void setMessage(String JavaDoc message) {
520
521         m_message = message;
522     }
523
524     /**
525      * Validates the values of the input fields and creates error messages, if necessary.<p>
526      *
527      * @return true if all checked input values are valid, otherwise false
528      */

529     public boolean validate() {
530
531         boolean allOk = true;
532         setErrors(new HashMap JavaDoc());
533         // check concern
534
if (CmsStringUtil.isEmptyOrWhitespaceOnly(getConcern())) {
535             getErrors().put("concern", key("letter.error.concern.empty"));
536             allOk = false;
537         } else {
538             // concern given, check if "other" is selected
539
if ("other".equals(getConcern())) {
540                 if (CmsStringUtil.isEmptyOrWhitespaceOnly(getConcernDetail())) {
541                     // details not given
542
getErrors().put("concern", key("letter.error.concerndetails.empty"));
543                     allOk = false;
544                 }
545             }
546         }
547         // check message
548
if (CmsStringUtil.isEmptyOrWhitespaceOnly(getMessage())) {
549             getErrors().put("message", key("letter.error.message.empty"));
550             allOk = false;
551         }
552         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getCopy())) {
553             // send copy to sender is checked, check email address
554
if (CmsStringUtil.isEmptyOrWhitespaceOnly(getContactEmail())) {
555                 // email address is empty
556
getErrors().put("email", key("letter.error.email.empty"));
557                 allOk = false;
558             }
559         }
560         if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getContactEmail()) && !isValidEmailAddress(getContactEmail())) {
561             // email address is not valid
562
getErrors().put("email", key("letter.error.email.wrong"));
563             allOk = false;
564         }
565         return allOk;
566     }
567
568     /**
569      * @see org.opencms.frontend.templateone.CmsTemplateForm#checkTextsUri()
570      */

571     protected String JavaDoc checkTextsUri() {
572
573         String JavaDoc fileUri = getConfigurationValue("page.form.letter", null);
574         if (fileUri != null) {
575             fileUri = getRequestContext().removeSiteRoot(fileUri);
576             try {
577                 getCmsObject().readResource(fileUri);
578                 return fileUri;
579             } catch (CmsException e) {
580                 // file not found, use default texts page file
581
}
582         }
583         return CmsWorkplace.VFS_PATH_MODULES + MODULE_NAME + "/pages/letter_content.html";
584     }
585
586 }
587
Popular Tags