KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.velocity.context.Context;
45
46 import org.apache.turbine.util.RunData;
47 import org.apache.torque.util.Criteria;
48 import org.apache.torque.util.Transaction;
49 import java.sql.Connection JavaDoc;
50
51 import org.campware.cream.om.OnlineSubscription;
52 import org.campware.cream.om.OnlineSubscriptionPeer;
53 import org.apache.turbine.util.velocity.VelocityHtmlEmail;
54 import org.apache.turbine.Turbine;
55
56 import org.campware.cream.om.Notification;
57 import org.campware.cream.om.NotificationPeer;
58
59 /**
60  * This class provides a simple set of methods to
61  * insert/update/delete records in a database.
62  */

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

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

132     public void doUpdate(RunData data, Context context)
133         throws Exception JavaDoc
134     {
135         OnlineSubscription entry = new OnlineSubscription();
136         data.getParameters().setProperties(entry);
137
138         int myStatus= data.getParameters().getInt("status");
139         int notify= data.getParameters().getInt("notify", 10);
140         
141         entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate")));
142         entry.setClosedDate(parseDate(data.getParameters().getString("closeddate")));
143         entry.setStartDate(parseDate(data.getParameters().getString("startdate")));
144         entry.setEndDate(parseDate(data.getParameters().getString("enddate")));
145         entry.setCreated(parseDateTime(data.getParameters().getString("created")));
146         entry.setModifiedBy(data.getUser().getName());
147         entry.setModified(new Date JavaDoc());
148
149         boolean bSave=true;
150
151         if (myStatus!=10 && notify==20){
152             bSave= sendEmail(data, context, entry);
153         }
154
155         if (bSave){
156             entry.setModified(true);
157             entry.setNew(false);
158             entry.save();
159         }
160     }
161
162     /**
163      * Send email to customer
164      */

165     private boolean sendEmail(RunData data, Context context, OnlineSubscription emailEntry)
166     throws Exception JavaDoc
167     {
168         int notifid;
169         int status= emailEntry.getStatus();
170         
171         if (status==30){
172             notifid=1001;
173         }else{
174             notifid=1003;
175         }
176
177         Criteria criteria = new Criteria();
178         criteria.add(NotificationPeer.NOTIFICATION_ID, new Integer JavaDoc(notifid), Criteria.EQUAL);
179         Notification myNotif = (Notification) NotificationPeer.doSelect(criteria).get(0);
180         VelocityTool velTool= new VelocityTool(context);
181
182           String JavaDoc sEmailAddress=emailEntry.getCustomerRelatedByCustomerId().getEmail();
183
184           if (sEmailAddress.length()>1){
185             VelocityHtmlEmail ve = new VelocityHtmlEmail(data);
186             ve.setCharset("UTF-8");
187             ve.addTo( sEmailAddress, "");
188             ve.setFrom(Turbine.getConfiguration().getString("mail.smtp.from"), Turbine.getConfiguration().getString("mail.smtp.from.name"));
189             ve.setSubject(myNotif.getSubject());
190             context.put("name", emailEntry.getCustomerRelatedByCustomerId().getCustomerName1());
191             context.put("display", emailEntry.getCustomerRelatedByCustomerId().getCustomerDisplay());
192             context.put("dear", emailEntry.getCustomerRelatedByCustomerId().getDear());
193             context.put("email", emailEntry.getCustomerRelatedByCustomerId().getEmail());
194             context.put("custom1", emailEntry.getCustomerRelatedByCustomerId().getCustom1());
195             context.put("custom2", emailEntry.getCustomerRelatedByCustomerId().getCustom2());
196             context.put("custom3", emailEntry.getCustomerRelatedByCustomerId().getCustom3());
197             context.put("custom4", emailEntry.getCustomerRelatedByCustomerId().getCustom4());
198             context.put("custom5", emailEntry.getCustomerRelatedByCustomerId().getCustom5());
199             context.put("custom6", emailEntry.getCustomerRelatedByCustomerId().getCustom6());
200             context.put("productdisplay", emailEntry.getProduct().getProductDisplay());
201             context.put("productdescription", emailEntry.getProduct().getProductDescription());
202             context.put("startdate", formatDate(emailEntry.getStartDate()));
203             context.put("enddate", formatDate(emailEntry.getEndDate()));
204             
205             context.put("emailbody", velTool.evaluate(myNotif.getBody()));
206             ve.setTextTemplate("screens/SendEmail.vm");
207             ve.send();
208             
209             return true;
210             
211           }else{
212             data.setMessage("Sorry, this customer has empty email address!");
213             data.setScreenTemplate("CreamError.vm");
214             return false;
215           }
216     }
217
218
219
220     /**
221      * Delete a record from the database using
222      * the unique id gleaned from the web form.
223      */

224     public void doDelete(RunData data, Context context)
225         throws Exception JavaDoc
226     {
227         Criteria criteria = new Criteria();
228         criteria.add(OnlineSubscriptionPeer.ONLINE_SUBS_ID, data.getParameters().getInt("onlinesubsid"));
229         OnlineSubscriptionPeer.doDelete(criteria);
230     }
231
232     /**
233      * Delete selected records from the database using
234      * the unique ids gleaned from the web form.
235      */

236     public void doDeleteselected(RunData data, Context context)
237         throws Exception JavaDoc
238     {
239         int[] delIds= data.getParameters().getInts("rowid");
240         Criteria criteria = new Criteria();
241         criteria.addIn(OnlineSubscriptionPeer.ONLINE_SUBS_ID, delIds);
242         OnlineSubscriptionPeer.doDelete(criteria);
243     }
244
245 }
246
Popular Tags