KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > applications > managementtool > actions > CreateEmailAction


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23
24 package org.infoglue.cms.applications.managementtool.actions;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.infoglue.cms.applications.common.VisualFormatter;
30 import org.infoglue.cms.applications.common.actions.InfoGlueAbstractAction;
31 import org.infoglue.cms.controllers.kernel.impl.simple.GroupControllerProxy;
32 import org.infoglue.cms.controllers.kernel.impl.simple.RoleControllerProxy;
33 import org.infoglue.cms.controllers.kernel.impl.simple.UserControllerProxy;
34 import org.infoglue.cms.security.InfoGluePrincipal;
35 import org.infoglue.cms.util.CmsPropertyHandler;
36 import org.infoglue.cms.util.mail.MailServiceFactory;
37
38 public class CreateEmailAction extends InfoGlueAbstractAction
39 {
40     private static final long serialVersionUID = 1L;
41     
42     private List JavaDoc users = null;
43     private List JavaDoc roles = null;
44     private List JavaDoc groups = null;
45     
46     private String JavaDoc[] userNames;
47     private String JavaDoc[] roleNames;
48     private String JavaDoc[] groupNames;
49     
50     private String JavaDoc usersAddresses = "";
51     
52     private String JavaDoc recipients;
53     private String JavaDoc from;
54     private String JavaDoc subject;
55     private String JavaDoc message;
56     
57     private String JavaDoc errorMessage = "";
58     
59     public String JavaDoc doExecute() throws Exception JavaDoc
60     {
61         if(recipients != null && recipients.length() > 0 && subject != null && subject.length() > 0 && message != null && message.length() > 0)
62         {
63             if(from == null || from.length() == 0)
64             {
65                 String JavaDoc systemEmailSender = CmsPropertyHandler.getSystemEmailSender();
66                 if(systemEmailSender == null || systemEmailSender.equalsIgnoreCase(""))
67                     systemEmailSender = "InfoGlueCMS@" + CmsPropertyHandler.getMailSmtpHost();
68
69                 from = systemEmailSender;
70             }
71
72             String JavaDoc contentType = CmsPropertyHandler.getMailContentType();
73             if(contentType == null || contentType.length() == 0)
74                 contentType = "text/html";
75
76             if(contentType.equalsIgnoreCase("text/html"))
77             {
78                 VisualFormatter ui = new VisualFormatter();
79                 message = ui.escapeHTMLforXMLService(message);
80                 message = "<div>" + message.replaceAll("\n", "<br/>\n") + "</div>";
81             }
82             
83             MailServiceFactory.getService().sendEmail(from, from, recipients, subject, message, "utf-8");
84         }
85         else
86         {
87             errorMessage = "Must enter information in all fields below.";
88             return "inputCreateEmail";
89         }
90         
91         return "success";
92     }
93         
94     public String JavaDoc doInputChooseRecipients() throws Exception JavaDoc
95     {
96         users = UserControllerProxy.getController().getAllUsers();
97         roles = RoleControllerProxy.getController().getAllRoles();
98         groups = GroupControllerProxy.getController().getAllGroups();
99         
100         return "inputChooseRecipients";
101     }
102
103     public String JavaDoc doInputCreateEmail() throws Exception JavaDoc
104     {
105         userNames = getRequest().getParameterValues("userName");
106         roleNames = getRequest().getParameterValues("roleName");
107         groupNames = getRequest().getParameterValues("groupName");
108
109         if(userNames != null)
110         {
111             for(int i=0; i<userNames.length; i++)
112             {
113                 String JavaDoc userName = userNames[i];
114                 InfoGluePrincipal principal = UserControllerProxy.getController().getUser(userName);
115                 if(usersAddresses.indexOf(principal.getEmail()) == -1)
116                 {
117                     if(usersAddresses.length() > 0)
118                         usersAddresses += ";";
119                     
120                     usersAddresses += principal.getEmail();
121                 }
122             }
123         }
124         
125         if(roleNames != null)
126         {
127             for(int i=0; i<roleNames.length; i++)
128             {
129                 String JavaDoc roleName = roleNames[i];
130                 
131                 List JavaDoc principals = RoleControllerProxy.getController().getInfoGluePrincipals(roleName);
132                 Iterator JavaDoc principalsIterator = principals.iterator();
133                 while(principalsIterator.hasNext())
134                 {
135                     InfoGluePrincipal principal = (InfoGluePrincipal)principalsIterator.next();
136                     if(usersAddresses.indexOf(principal.getEmail()) == -1)
137                     {
138                         if(usersAddresses.length() > 0)
139                             usersAddresses += ";";
140                         
141                         usersAddresses += principal.getEmail();
142                     }
143                 }
144             }
145         }
146         
147         if(groupNames != null)
148         {
149             for(int i=0; i<groupNames.length; i++)
150             {
151                 String JavaDoc groupName = groupNames[i];
152                 
153                 List JavaDoc principals = GroupControllerProxy.getController().getInfoGluePrincipals(groupName);
154                 Iterator JavaDoc principalsIterator = principals.iterator();
155                 while(principalsIterator.hasNext())
156                 {
157                     InfoGluePrincipal principal = (InfoGluePrincipal)principalsIterator.next();
158                     if(usersAddresses.indexOf(principal.getEmail()) == -1)
159                     {
160                         if(usersAddresses.length() > 0)
161                             usersAddresses += ";";
162                         
163                         usersAddresses += principal.getEmail();
164                     }
165                 }
166             }
167         }
168         
169         return "inputCreateEmail";
170     }
171
172     public List JavaDoc getGroups()
173     {
174         return groups;
175     }
176
177     public List JavaDoc getRoles()
178     {
179         return roles;
180     }
181
182     public List JavaDoc getUsers()
183     {
184         return users;
185     }
186
187     public String JavaDoc getUsersAddresses()
188     {
189         return usersAddresses;
190     }
191
192     public String JavaDoc getMessage()
193     {
194         return message;
195     }
196
197     public void setMessage(String JavaDoc message)
198     {
199         this.message = message;
200     }
201
202     public String JavaDoc getRecipients()
203     {
204         return recipients;
205     }
206
207     public void setRecipients(String JavaDoc recipients)
208     {
209         this.recipients = recipients;
210     }
211
212     public String JavaDoc getSubject()
213     {
214         return subject;
215     }
216
217     public void setSubject(String JavaDoc subject)
218     {
219         this.subject = subject;
220     }
221
222     public String JavaDoc getFrom() {
223         return from;
224     }
225
226     public void setFrom(String JavaDoc from) {
227         this.from = from;
228     }
229
230     public String JavaDoc getErrorMessage() {
231         return errorMessage;
232     }
233 }
234
Popular Tags