KickJava   Java API By Example, From Geeks To Geeks.

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


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.Sorder;
55 import org.campware.cream.om.SorderPeer;
56 import org.campware.cream.om.SorderItem;
57 import org.campware.cream.om.SorderItemPeer;
58
59 /**
60  * This class provides a simple set of methods to
61  * insert/update/delete records in a database.
62  */

63 public class OrderSQL extends CreamAction
64 {
65     protected void initScreen()
66     {
67         setModuleType(DOCUMENT);
68         setModuleName("SORDER");
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         Sorder entry = new Sorder();
85         data.getParameters().setProperties(entry);
86
87         entry.setSorderCode(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         int currId = entry.getCurrencyId();
100         int custId = entry.getCustomerId();
101         int recpId = entry.getRecipientId();
102         int projId = entry.getProjectId();
103         
104         while(paramKeys.hasMoreElements()) {
105             String JavaDoc paramName = paramKeys.nextElement().toString();
106             if(paramName.startsWith("productid")) {
107                 String JavaDoc suffix=paramName.substring(9, paramName.length());
108                 SorderItem entryItem= new SorderItem();
109
110                 entryItem.setProductId(pp.getInt("productid" + suffix));
111                 entryItem.setDescription(pp.getString("description" + suffix));
112                 BigDecimal JavaDoc itmUnitPrice= pp.getBigDecimal("unitprice" + suffix);
113                 int itmQuantity= pp.getInt("quantity" + suffix);
114                 BigDecimal JavaDoc itmCurrTotal= itmUnitPrice.multiply(BigDecimal.valueOf(itmQuantity));
115                 entryItem.setUnitPrice(itmUnitPrice);
116                 entryItem.setQuantity(itmQuantity);
117                 entryItem.setItemCurrTotal(itmCurrTotal);
118
119                 entryItem.setCurrencyId(currId);
120                 entryItem.setCustomerId(custId);
121                 entryItem.setRecipientId(recpId);
122                 entryItem.setProjectId(projId);
123
124                 currTotal= currTotal.add(itmCurrTotal);
125                 entry.addSorderItem(entryItem);
126             }
127         }
128
129         entry.setCurrencyAmount(currTotal);
130
131         Connection JavaDoc conn = Transaction.begin(SorderPeer.DATABASE_NAME);
132         boolean success = false;
133         try {
134             entry.save(conn);
135             entry.setSorderCode(getRowCode("SO", entry.getSorderId()));
136             entry.save(conn);
137             Transaction.commit(conn);
138             success = true;
139
140         } finally {
141             if (!success) Transaction.safeRollback(conn);
142         }
143
144     }
145
146     /**
147      * Update a record in the database with the
148      * information present in the web form.
149      *
150      * Again, this is merely an example. The data
151      * should be checked before being allowed
152      * into the database.
153      */

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

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

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