KickJava   Java API By Example, From Geeks To Geeks.

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


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.OutboxEvent;
52 import org.campware.cream.om.OutboxEventPeer;
53 import org.apache.turbine.util.velocity.VelocityHtmlEmail;
54 import org.apache.turbine.Turbine;
55  
56  
57 /**
58  * This class provides a simple set of methods to
59  * insert/update/delete records in a database.
60  */

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

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

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

159     private boolean sendEmail(RunData data, Context context, OutboxEvent emailEntry)
160     throws Exception JavaDoc
161     {
162 // String sEmailAddress=emailEntry.getCustomer().getEmail();
163
String JavaDoc sEmailAddress=emailEntry.getReceiverTo();
164
165         if (sEmailAddress.length()>2){
166 // Context emailContext = new org.apache.velocity.VelocityContext();
167
// emailContext.put("emailbody", emailEntry.getBody());
168
// VelocityEmail ve = new VelocityEmail(emailContext);
169
// ve.setTo("", sEmailAddress);
170
// ve.setFrom("","");
171
// ve.setSubject(emailEntry.getSubject());
172
// ve.setTemplate("screens/SendEmail.vm");
173
// ve.setWordWrap(72);
174
// ve.send();
175

176 // Context emailContext = new org.apache.velocity.VelocityContext();
177
// emailContext.put("emailbody", emailEntry.getBody());
178

179             try{
180                 VelocityHtmlEmail ve = new VelocityHtmlEmail(data);
181     // ve.setMailServer(TurbineResources.getString("mail.server"));
182
ve.setCharset("UTF-8");
183                 ve.addTo( sEmailAddress, "");
184                 ve.setFrom(Turbine.getConfiguration().getString("mail.smtp.from"), Turbine.getConfiguration().getString("mail.smtp.from.name"));
185                 ve.setSubject(emailEntry.getSubject());
186     // ve.setTextMsg(emailEntry.getBody());
187
context.put("emailbody", emailEntry.getBody());
188     // ve.setTextTemplate("screens/SendEmail.vm");
189
ve.setHtmlTemplate("screens/SendEmail.vm");
190                 ve.send();
191     
192                 return true;
193             }catch(Exception JavaDoc e){
194                 data.setScreenTemplate("CreamError.vm");
195                 return false;
196             }
197             
198         }else{
199             data.setScreenTemplate("CreamError.vm");
200             return false;
201         }
202         
203     }
204
205
206
207     /**
208      * Delete a record from the database using
209      * the unique id gleaned from the web form.
210      */

211     public void doDelete(RunData data, Context context)
212         throws Exception JavaDoc
213     {
214         Criteria criteria = new Criteria();
215         criteria.add(OutboxEventPeer.OUTBOX_EVENT_ID, data.getParameters().getInt("outboxeventid"));
216         OutboxEventPeer.doDelete(criteria);
217     }
218
219     /**
220      * Delete selected records from the database using
221      * the unique ids gleaned from the web form.
222      */

223     public void doDeleteselected(RunData data, Context context)
224         throws Exception JavaDoc
225     {
226         int[] delIds= data.getParameters().getInts("rowid");
227         Criteria criteria = new Criteria();
228         criteria.addIn(OutboxEventPeer.OUTBOX_EVENT_ID, delIds);
229         OutboxEventPeer.doDelete(criteria);
230     }
231
232 }
233
Popular Tags