KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > campware > cream > modules > actions > NewsletterSQL


1 package org.campware.cream.modules.actions;
2
3 /* ====================================================================
4  * Copyright (C) 2003-2005 Media Development Loan Fund
5  *
6  * * contact: contact@campware.org - http://www.campware.org
7  * Campware encourages further development. Please let us know.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
24  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
27  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  * ====================================================================
36  *
37  * This software consists of voluntary contributions made by many
38  * individuals on behalf of the Apache Software Foundation. For more
39  * information on the Apache Software Foundation, please see
40  * <http://www.apache.org/>.
41  */

42
43 import java.util.Date JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import org.apache.velocity.context.Context;
47
48 import org.apache.turbine.util.RunData;
49 import org.apache.torque.util.Criteria;
50 import org.apache.torque.util.Transaction;
51 import java.sql.Connection JavaDoc;
52
53 import org.campware.cream.om.Newsletter;
54 import org.campware.cream.om.NewsletterPeer;
55 import org.apache.turbine.util.velocity.VelocityHtmlEmail;
56 import org.apache.turbine.Turbine;
57
58 import org.campware.cream.om.Customer;
59 import org.campware.cream.om.CustomerPeer;
60 import org.campware.cream.om.NewsSubscription;
61 import org.campware.cream.om.NewsSubscriptionPeer;
62
63 /**
64  * This class provides a simple set of methods to
65  * insert/update/delete records in a database.
66  */

67 public class NewsletterSQL extends CreamAction
68 {
69     protected void initScreen()
70     {
71         setModuleType(DOCUMENT);
72         setModuleName("NEWSLETTER");
73     }
74     /**
75      * This simply takes an entry from the web form and
76      * inserts it directly into the database.
77      *
78      * This would not be good in practice as the
79      * data should be verified before being allowed
80      * into the database. This is merely an
81      * example of how to use peers, this certainly
82      * wouldn't be secure.
83      */

84     public void doInsert(RunData data, Context context)
85         throws Exception JavaDoc
86     {
87         Newsletter entry = new Newsletter();
88         data.getParameters().setProperties(entry);
89
90         String JavaDoc myCode=data.getParameters().getString("newslettercode");
91         int myStatus= data.getParameters().getInt("status");
92         
93         boolean bSave=true;
94
95         if (myStatus==30){
96             bSave= sendNewsletters(data, context, entry);
97             if (bSave) entry.setStatus(50);
98         }
99
100         if (bSave){
101
102             entry.setNewsletterCode(getTempCode());
103     
104             entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate")));
105             entry.setClosedDate(parseDate(data.getParameters().getString("closeddate")));
106             entry.setCreatedBy(data.getUser().getName());
107             entry.setCreated(new Date JavaDoc());
108             entry.setModifiedBy(data.getUser().getName());
109             entry.setModified(new Date JavaDoc());
110             
111             Connection JavaDoc conn = Transaction.begin(NewsletterPeer.DATABASE_NAME);
112             boolean success = false;
113             try {
114                 entry.save(conn);
115                 entry.setNewsletterCode(getRowCode("NL", entry.getNewsletterId()));
116                 entry.save(conn);
117                 Transaction.commit(conn);
118                 success = true;
119     
120             } finally {
121                 if (!success) Transaction.safeRollback(conn);
122             }
123         }
124     }
125
126     /**
127      * Update a record in the database with the
128      * information present in the web form.
129      *
130      * Again, this is merely an example. The data
131      * should be checked before being allowed
132      * into the database.
133      */

134     public void doUpdate(RunData data, Context context)
135         throws Exception JavaDoc
136     {
137         Newsletter entry = new Newsletter();
138         data.getParameters().setProperties(entry);
139
140         int myStatus= data.getParameters().getInt("status");
141         
142         boolean bSave=true;
143
144         if (myStatus==30){
145             bSave= sendNewsletters(data, context, entry);
146             if (bSave) entry.setStatus(50);
147         }
148
149         if (bSave){
150             entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate")));
151             entry.setClosedDate(parseDate(data.getParameters().getString("closeddate")));
152             entry.setCreated(parseDateTime(data.getParameters().getString("created")));
153             entry.setModifiedBy(data.getUser().getName());
154             entry.setModified(new Date JavaDoc());
155     
156             entry.setModified(true);
157             entry.setNew(false);
158             entry.save();
159         }
160     }
161
162     /**
163      * Send newsletters to recipients
164      */

165     private boolean sendNewsletters(RunData data, Context context, Newsletter newsletterEntry)
166     throws Exception JavaDoc
167     {
168
169         int relDocument= newsletterEntry.getRelDocument();
170         String JavaDoc mailSmtpFrom= Turbine.getConfiguration().getString("mail.smtp.from");
171         String JavaDoc mailSmtpFromName= Turbine.getConfiguration().getString("mail.smtp.from.name");
172         boolean bHasBadEmails= false;
173         
174         if (relDocument==20){
175             int customerCatId= newsletterEntry.getCustomerCatId();
176             int customerType= newsletterEntry.getCustomerType();
177             int custLanguageId= newsletterEntry.getCustLanguageId();
178             int custCountryId= newsletterEntry.getCustCountryId();
179             
180             Criteria criteria = new Criteria();
181             
182             if (customerCatId>999){
183                 criteria.add(CustomerPeer.CUSTOMER_CAT_ID, new Integer JavaDoc(customerCatId), Criteria.EQUAL);
184             }
185             if (customerType>1){
186                 criteria.add(CustomerPeer.CUSTOMER_TYPE, new Integer JavaDoc(customerType), Criteria.EQUAL);
187             }
188             if (custLanguageId>999){
189                 criteria.add(CustomerPeer.LANGUAGE_ID, new Integer JavaDoc(custLanguageId), Criteria.EQUAL);
190             }
191             if (custCountryId>999){
192                 criteria.add(CustomerPeer.COUNTRY_ID, new Integer JavaDoc(custCountryId), Criteria.EQUAL);
193             }
194             criteria.add(CustomerPeer.SEND_NEWS, new Integer JavaDoc(20), Criteria.EQUAL);
195             criteria.add(CustomerPeer.EMAIL, (Object JavaDoc)"EMAIL is NOT NULL", Criteria.CUSTOM);
196             
197     
198     
199             List JavaDoc receivers = CustomerPeer.doSelect(criteria);
200             Iterator JavaDoc i = receivers.iterator();
201             VelocityTool velTool= new VelocityTool(context);
202     
203             while (i.hasNext())
204             {
205               Customer cust = (Customer) i.next();
206     
207               String JavaDoc sEmailAddress=cust.getEmail();
208     
209               if (sEmailAddress.length()>1){
210                 context.put("customerid", cust.getCustomerCode());
211                 context.put("name", cust.getCustomerName1());
212                 context.put("display", cust.getCustomerDisplay());
213                 context.put("dear", cust.getDear());
214                 context.put("email", cust.getEmail());
215                 context.put("custom1", cust.getCustom1());
216                 context.put("custom2", cust.getCustom2());
217                 context.put("custom3", cust.getCustom3());
218                 context.put("custom4", cust.getCustom4());
219                 context.put("custom5", cust.getCustom5());
220                 context.put("custom6", cust.getCustom6());
221                 context.put("emailbody", velTool.evaluate(newsletterEntry.getBody()));
222                 try{
223                     VelocityHtmlEmail ve = new VelocityHtmlEmail(data);
224                     ve.setCharset("UTF-8");
225                     ve.addTo( sEmailAddress, "");
226                     ve.setFrom(mailSmtpFrom, mailSmtpFromName);
227                     ve.setSubject(newsletterEntry.getSubject());
228                     ve.setHtmlTemplate("screens/SendEmail.vm");
229                     ve.send();
230                 }catch(Exception JavaDoc e){
231                     if (!bHasBadEmails){
232                         bHasBadEmails=true;
233                         newsletterEntry.setNotes("Not sent to these addresses:");
234                         
235                     }
236                     newsletterEntry.setNotes(newsletterEntry.getNotes() + "\n" + sEmailAddress);
237                 }
238               }
239             }
240         } else if (relDocument==10){
241             
242             int relDocStatus= newsletterEntry.getRelDocStatus();
243             int relProductId= newsletterEntry.getRelProductId();
244             int relProjectId= newsletterEntry.getRelProjectId();
245             
246             Criteria criteria = new Criteria();
247             
248             if (relDocStatus>1){
249                 criteria.add(NewsSubscriptionPeer.STATUS, new Integer JavaDoc(relDocStatus), Criteria.EQUAL);
250             }
251             if (relProductId>999){
252                 criteria.add(NewsSubscriptionPeer.PRODUCT_ID, new Integer JavaDoc(relProductId), Criteria.EQUAL);
253             }
254             if (relProjectId>999){
255                 criteria.add(NewsSubscriptionPeer.PROJECT_ID, new Integer JavaDoc(relProjectId), Criteria.EQUAL);
256             }
257             criteria.add(NewsSubscriptionPeer.EMAIL, (Object JavaDoc)"EMAIL is NOT NULL", Criteria.CUSTOM);
258     
259             List JavaDoc receivers = NewsSubscriptionPeer.doSelect(criteria);
260             Iterator JavaDoc i = receivers.iterator();
261             VelocityTool velTool= new VelocityTool(context);
262     
263             while (i.hasNext())
264             {
265                 NewsSubscription cust = (NewsSubscription) i.next();
266                 String JavaDoc sEmailAddress=cust.getEmail();
267     
268                 if (sEmailAddress.length()>1){
269 // context.put("product", cust.getProduct().getProductDescription());
270
// context.put("project", cust.getProject().getProjectName());
271
context.put("subscriptionid", cust.getNewsSubsCode());
272                     context.put("email", cust.getEmail());
273                     context.put("emailbody", velTool.evaluate(newsletterEntry.getBody()));
274                     try{
275                         VelocityHtmlEmail ve = new VelocityHtmlEmail(data);
276                         ve.setCharset("UTF-8");
277                         ve.addTo( sEmailAddress, "");
278                         ve.setFrom(mailSmtpFrom, mailSmtpFromName);
279                         ve.setSubject(newsletterEntry.getSubject());
280                         ve.setHtmlTemplate("screens/SendEmail.vm");
281                         ve.send();
282                     }catch(Exception JavaDoc e){
283                         if (!bHasBadEmails){
284                             bHasBadEmails=true;
285                             newsletterEntry.setNotes("Not sent to these addresses:");
286                             
287                         }
288                         newsletterEntry.setNotes(newsletterEntry.getNotes() + "\n" + sEmailAddress);
289                     }
290                 }
291             }
292         } else{
293             return false;
294         }
295         
296         return true;
297     }
298
299     /**
300      * Delete a record from the database using
301      * the unique id gleaned from the web form.
302      */

303     public void doDelete(RunData data, Context context)
304         throws Exception JavaDoc
305     {
306         Criteria criteria = new Criteria();
307         criteria.add(NewsletterPeer.NEWSLETTER_ID, data.getParameters().getInt("newsletterid"));
308         NewsletterPeer.doDelete(criteria);
309     }
310
311     /**
312      * Delete selected records from the database using
313      * the unique ids gleaned from the web form.
314      */

315     public void doDeleteselected(RunData data, Context context)
316         throws Exception JavaDoc
317     {
318         int[] delIds= data.getParameters().getInts("rowid");
319         Criteria criteria = new Criteria();
320         criteria.addIn(NewsletterPeer.NEWSLETTER_ID, delIds);
321         NewsletterPeer.doDelete(criteria);
322     }
323
324 }
325
Popular Tags