KickJava   Java API By Example, From Geeks To Geeks.

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


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

63 public class PaymentSQL extends CreamAction
64 {
65     protected void initScreen()
66     {
67         setModuleType(DOCUMENT);
68         setModuleName("PAYMENT");
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         Payment entry = new Payment();
85         data.getParameters().setProperties(entry);
86
87         entry.setPaymentCode(getTempCode());
88
89         entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate")));
90         entry.setClosedDate(parseDate(data.getParameters().getString("closeddate")));
91         entry.setCreatedBy(data.getUser().getName());
92         entry.setCreated(new Date JavaDoc());
93         entry.setModifiedBy(data.getUser().getName());
94         entry.setModified(new Date JavaDoc());
95         
96         ParameterParser pp= data.getParameters();
97         Enumeration JavaDoc paramKeys= pp.keys();
98         BigDecimal JavaDoc currTotal = new BigDecimal JavaDoc(0);
99         BigDecimal JavaDoc locTotal = new BigDecimal JavaDoc(0);
100         BigDecimal JavaDoc currRate = entry.getCurrencyRate();
101         int currId = entry.getCurrencyId();
102         int sordId = entry.getSorderId();
103         int custId = entry.getCustomerId();
104         int projId = entry.getProjectId();
105         
106         while(paramKeys.hasMoreElements()) {
107             String JavaDoc paramName = paramKeys.nextElement().toString();
108             if(paramName.startsWith("productid")) {
109                 String JavaDoc suffix=paramName.substring(9, paramName.length());
110                 PaymentItem entryItem= new PaymentItem();
111
112                 entryItem.setProductId(pp.getInt("productid" + suffix));
113                 entryItem.setDescription(pp.getString("description" + suffix));
114                 BigDecimal JavaDoc itmUnitPrice= pp.getBigDecimal("unitprice" + suffix);
115                 int itmQuantity= pp.getInt("quantity" + suffix);
116                 BigDecimal JavaDoc itmCurrTotal= itmUnitPrice.multiply(BigDecimal.valueOf(itmQuantity));
117                 BigDecimal JavaDoc itmTotal= itmCurrTotal.multiply(currRate);
118
119                 entryItem.setUnitPrice(itmUnitPrice);
120                 entryItem.setQuantity(itmQuantity);
121                 entryItem.setItemCurrTotal(itmCurrTotal);
122                 entryItem.setItemTotal(itmTotal);
123
124                 entryItem.setCurrencyId(currId);
125                 entryItem.setSorderId(sordId);
126                 entryItem.setCustomerId(custId);
127                 entryItem.setProjectId(projId);
128
129                 currTotal= currTotal.add(itmCurrTotal);
130                 locTotal= locTotal.add(itmTotal);
131                 entry.addPaymentItem(entryItem);
132             }
133         }
134
135         entry.setCurrencyAmount(currTotal);
136         entry.setTotalAmount(locTotal);
137
138         Connection JavaDoc conn = Transaction.begin(PaymentPeer.DATABASE_NAME);
139         boolean success = false;
140         try {
141             entry.save(conn);
142             entry.setPaymentCode(getRowCode("PA", entry.getPaymentId()));
143             entry.save(conn);
144             Transaction.commit(conn);
145             success = true;
146
147         } finally {
148             if (!success) Transaction.safeRollback(conn);
149         }
150     }
151
152     /**
153      * Update a record in the database with the
154      * information present in the web form.
155      *
156      * Again, this is merely an example. The data
157      * should be checked before being allowed
158      * into the database.
159      */

160     public void doUpdate(RunData data, Context context)
161         throws Exception JavaDoc
162     {
163         Payment entry = new Payment();
164         data.getParameters().setProperties(entry);
165
166         entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate")));
167         entry.setClosedDate(parseDate(data.getParameters().getString("closeddate")));
168         entry.setCreated(parseDateTime(data.getParameters().getString("created")));
169         entry.setModifiedBy(data.getUser().getName());
170         entry.setModified(new Date JavaDoc());
171
172         ParameterParser pp= data.getParameters();
173         Enumeration JavaDoc paramKeys= pp.keys();
174         BigDecimal JavaDoc currTotal = new BigDecimal JavaDoc(0);
175         BigDecimal JavaDoc locTotal = new BigDecimal JavaDoc(0);
176         BigDecimal JavaDoc currRate = entry.getCurrencyRate();
177         int currId = entry.getCurrencyId();
178         int sordId = entry.getSorderId();
179         int custId = entry.getCustomerId();
180         int projId = entry.getProjectId();
181         
182         while(paramKeys.hasMoreElements()) {
183             String JavaDoc paramName = paramKeys.nextElement().toString();
184             if(paramName.startsWith("productid")) {
185                 String JavaDoc suffix=paramName.substring(9, paramName.length());
186                 PaymentItem entryItem= new PaymentItem();
187
188                 entryItem.setProductId(pp.getInt("productid" + suffix));
189                 entryItem.setDescription(pp.getString("description" + suffix));
190                 BigDecimal JavaDoc itmUnitPrice= pp.getBigDecimal("unitprice" + suffix);
191                 int itmQuantity= pp.getInt("quantity" + suffix);
192                 BigDecimal JavaDoc itmCurrTotal= itmUnitPrice.multiply(BigDecimal.valueOf(itmQuantity));
193                 BigDecimal JavaDoc itmTotal= itmCurrTotal.multiply(currRate);
194
195                 entryItem.setUnitPrice(itmUnitPrice);
196                 entryItem.setQuantity(itmQuantity);
197                 entryItem.setItemCurrTotal(itmCurrTotal);
198                 entryItem.setItemTotal(itmTotal);
199
200                 entryItem.setCurrencyId(currId);
201                 entryItem.setSorderId(sordId);
202                 entryItem.setCustomerId(custId);
203                 entryItem.setProjectId(projId);
204
205                 currTotal= currTotal.add(itmCurrTotal);
206                 locTotal= locTotal.add(itmTotal);
207                 entry.addPaymentItem(entryItem);
208             }
209         }
210
211         entry.setCurrencyAmount(currTotal);
212         entry.setTotalAmount(locTotal);
213
214         entry.setModified(true);
215         entry.setNew(false);
216
217         Criteria crit = new Criteria();
218         crit.add(PaymentItemPeer.PAYMENT_ID, entry.getPaymentId());
219
220         Connection JavaDoc conn = Transaction.begin(PaymentPeer.DATABASE_NAME);
221         boolean success = false;
222         try {
223             PaymentItemPeer.doDelete(crit, conn);
224             entry.save(conn);
225             Transaction.commit(conn);
226             success = true;
227
228         } finally {
229             if (!success) Transaction.safeRollback(conn);
230         }
231     }
232
233     /**
234      * Delete a record from the database using
235      * the unique id gleaned from the web form.
236      */

237     public void doDelete(RunData data, Context context)
238         throws Exception JavaDoc
239     {
240         Criteria criteria = new Criteria();
241         criteria.add(PaymentPeer.PAYMENT_ID, data.getParameters().getInt("paymentid"));
242         PaymentPeer.doDelete(criteria);
243     }
244
245     /**
246      * Delete selected records from the database using
247      * the unique ids gleaned from the web form.
248      */

249     public void doDeleteselected(RunData data, Context context)
250         throws Exception JavaDoc
251     {
252         int[] delIds= data.getParameters().getInts("rowid");
253         Criteria criteria = new Criteria();
254         criteria.addIn(PaymentPeer.PAYMENT_ID, delIds);
255         PaymentPeer.doDelete(criteria);
256     }
257
258
259 }
260
Popular Tags