KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > QuestionnaireAction


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.modules.actions.portlets;
18
19 // Turbine stuff
20
import org.apache.turbine.util.RunData;
21
22 // Jetspeed stuff
23
import org.apache.jetspeed.portal.Portlet;
24 import org.apache.jetspeed.om.registry.Parameter;
25 import org.apache.jetspeed.services.Registry;
26 import org.apache.jetspeed.om.registry.PortletEntry;
27 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
28 import org.apache.jetspeed.services.logging.JetspeedLogger;
29 import org.apache.jetspeed.services.resources.JetspeedResources;
30
31 // Java stuff
32
import java.util.Hashtable JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.io.File JavaDoc;
35
36 import javax.mail.Session JavaDoc;
37 import javax.mail.Address JavaDoc;
38 import javax.mail.Message JavaDoc;
39 import javax.mail.Multipart JavaDoc;
40 import javax.mail.Transport JavaDoc;
41 import javax.mail.internet.MimeMessage JavaDoc;
42 import javax.mail.internet.InternetAddress JavaDoc;
43 import javax.mail.internet.MimeBodyPart JavaDoc;
44 import javax.mail.internet.MimeMultipart JavaDoc;
45 import javax.activation.FileDataSource JavaDoc;
46 import javax.activation.DataHandler JavaDoc;
47
48 /**
49  * This action sets up the template context for retrieving stock quotes.
50  *
51  * @author <a HREF="mailto:morciuch@apache.org">Mark Orciuch</a>
52  * @author <a HREF="mailto:weaver@apache.org">Scott T. Weaver</a>
53  * @version $Id: QuestionnaireAction.java,v 1.7 2004/02/23 02:56:58 jford Exp $
54  */

55
56 public class QuestionnaireAction extends JspPortletAction
57 {
58
59     /**
60      * Static initialization of the logger for this class
61      */

62     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(QuestionnaireAction.class.getName());
63     
64     /**
65      * Build the normal state content for this portlet.
66      *
67      * @param portlet The jsp-based portlet that is being built.
68      * @param rundata The turbine rundata context for this request.
69      */

70     protected void buildNormalContext(Portlet portlet, RunData rundata)
71     {
72         PortletEntry entry = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
73         Iterator JavaDoc i = entry.getParameterNames();
74         Hashtable JavaDoc qa = new Hashtable JavaDoc();
75
76         while (i.hasNext())
77         {
78             String JavaDoc name = (String JavaDoc) i.next();
79             Parameter param = entry.getParameter(name);
80             if (param.isHidden() == false)
81             {
82                 String JavaDoc title = param.getTitle();
83                 String JavaDoc value = portlet.getPortletConfig().getInitParameter(name);
84                 qa.put(title, value);
85             }
86         }
87
88         rundata.getRequest().setAttribute("questions", qa);
89
90         // After successful send, the user may or may not click the Continue button so
91
// reset to default template here
92
if (rundata.getRequest().getAttribute("email") == null)
93         {
94             //this.setTemplate(rundata, portlet, null);
95
resetTemplate(rundata);
96         }
97
98     }
99
100     /**
101      * Continue event handler.
102      *
103      * @param portlet The jsp-based portlet that is being built.
104      * @param rundata The turbine rundata context for this request.
105      */

106     public void doContinue(RunData rundata, Portlet portlet)
107     {
108         // this.setTemplate(rundata, portlet, null);
109
resetTemplate(rundata);
110     }
111
112     /**
113      * Sort the quotes.
114      *
115      * @param portlet The jsp-based portlet that is being built.
116      * @param rundata The turbine rundata context for this request.
117      */

118     public void doEmail(RunData rundata, Portlet portlet)
119     {
120         StringBuffer JavaDoc emailBody = new StringBuffer JavaDoc();
121         PortletEntry entry = (PortletEntry) Registry.getEntry(Registry.PORTLET, portlet.getName());
122         Iterator JavaDoc i = entry.getParameterNames();
123
124         while (i.hasNext())
125         {
126             String JavaDoc name = (String JavaDoc) i.next();
127             Parameter param = entry.getParameter(name);
128             if (param.isHidden() == false)
129             {
130                 String JavaDoc title = param.getTitle();
131                 String JavaDoc value = portlet.getPortletConfig().getInitParameter(name);
132                 value = value == null || value.length() == 0 ? "NOT PROVIDED" : value;
133                 emailBody.append(title);
134                 emailBody.append(" ===> ");
135                 emailBody.append(value);
136                 emailBody.append("\n\n");
137
138             }
139         }
140
141         String JavaDoc emailSmtp = JetspeedResources.getString(JetspeedResources.MAIL_SERVER_KEY);
142         String JavaDoc emailFrom = JetspeedResources.getString("mail.support", "david@bluesunrise.com");
143         String JavaDoc emailTo = rundata.getParameters().getString("emailTo", "jetspeed-dev@jakarta.apache.org");
144         String JavaDoc emailAttachment = rundata.getRequest().getParameter("emailAttachment");
145         try
146         {
147             String JavaDoc emailText = emailBody.toString();
148
149             // Create the JavaMail session
150
java.util.Properties JavaDoc properties = System.getProperties();
151             properties.put("mail.smtp.host", emailSmtp);
152             Session JavaDoc emailSession = Session.getInstance(properties, null);
153
154             // Construct the message
155
MimeMessage JavaDoc message = new MimeMessage JavaDoc(emailSession);
156
157             // Set the from address
158
Address JavaDoc fromAddress = new InternetAddress JavaDoc(emailFrom);
159             message.setFrom(fromAddress);
160
161             // Parse and set the recipient addresses
162
Address JavaDoc[] toAddresses = InternetAddress.parse(emailTo);
163             message.setRecipients(Message.RecipientType.TO, toAddresses);
164
165             // Set the subject and text
166
message.setSubject("Jetspeed Questionnaire from " + rundata.getUser().getEmail());
167             message.setText(emailText);
168
169             // Attach file with message
170
if (emailAttachment != null)
171             {
172                 File JavaDoc file = new File JavaDoc(emailAttachment);
173                 if (file.exists())
174                 {
175                     // create and fill the first message part
176
MimeBodyPart JavaDoc mbp1 = new MimeBodyPart JavaDoc();
177                     mbp1.setText(emailText);
178
179                     // create the second message part
180
MimeBodyPart JavaDoc mbp2 = new MimeBodyPart JavaDoc();
181
182                     // attach the file to the message
183
FileDataSource JavaDoc fds = new FileDataSource JavaDoc(emailAttachment);
184                     mbp2.setDataHandler(new DataHandler JavaDoc(fds));
185                     mbp2.setFileName(fds.getName());
186
187                     // create the Multipart and its parts to it
188
Multipart JavaDoc mp = new MimeMultipart JavaDoc();
189                     mp.addBodyPart(mbp1);
190                     mp.addBodyPart(mbp2);
191
192                     // add the Multipart to the message
193
message.setContent(mp);
194                 }
195                 else
196                 {
197                     message.setText(emailBody.toString());
198                 }
199             }
200
201             // send the message
202
Transport.send(message);
203
204             // Display confirmation
205
rundata.getRequest().setAttribute("email", emailBody.toString());
206             String JavaDoc confirmTemplate = portlet.getPortletConfig().getInitParameter("confirm.template",
207                                                                                  "JetspeedQuestionnaireConfirmation.jsp");
208            // this.setTemplate(rundata, portlet, confirmTemplate);
209
setTemplate(rundata, confirmTemplate, true);
210
211             rundata.setMessage("Email successfully sent");
212         }
213         catch (Exception JavaDoc e)
214         {
215             logger.error("Exception", e);
216             rundata.setMessage("Error sending email: " + e);
217         }
218
219         //buildNormalContext(portlet, rundata);
220

221     }
222
223
224 }
225
226
Popular Tags