KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.velocity.context.Context;
46
47 import org.apache.turbine.util.RunData;
48 import org.apache.turbine.util.parser.ParameterParser;
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.Shipment;
54 import org.campware.cream.om.ShipmentPeer;
55 import org.campware.cream.om.ShipmentItem;
56 import org.campware.cream.om.ShipmentItemPeer;
57
58 /**
59  * This class provides a simple set of methods to
60  * insert/update/delete records in a database.
61  */

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

80     public void doInsert(RunData data, Context context)
81         throws Exception JavaDoc
82     {
83         Shipment entry = new Shipment();
84         data.getParameters().setProperties(entry);
85
86         entry.setShipmentCode(getTempCode());
87
88         entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate")));
89         entry.setClosedDate(parseDate(data.getParameters().getString("closeddate")));
90         entry.setCreatedBy(data.getUser().getName());
91         entry.setCreated(new Date JavaDoc());
92         entry.setModifiedBy(data.getUser().getName());
93         entry.setModified(new Date JavaDoc());
94         
95         ParameterParser pp= data.getParameters();
96         Enumeration JavaDoc paramKeys= pp.keys();
97         int sordId = entry.getSorderId();
98         int custId = entry.getCustomerId();
99         int recpId = entry.getRecipientId();
100         int projId = entry.getProjectId();
101         
102         while(paramKeys.hasMoreElements()) {
103             String JavaDoc paramName = paramKeys.nextElement().toString();
104             if(paramName.startsWith("productid")) {
105                 String JavaDoc suffix=paramName.substring(9, paramName.length());
106                 ShipmentItem entryItem= new ShipmentItem();
107
108                 entryItem.setProductId(pp.getInt("productid" + suffix));
109                 entryItem.setDescription(pp.getString("description" + suffix));
110                 entryItem.setQuantity(pp.getInt("quantity" + suffix));
111
112                 entryItem.setSorderId(sordId);
113                 entryItem.setCustomerId(custId);
114                 entryItem.setRecipientId(recpId);
115                 entryItem.setProjectId(projId);
116
117                 entry.addShipmentItem(entryItem);
118             }
119         }
120
121         Connection JavaDoc conn = Transaction.begin(ShipmentPeer.DATABASE_NAME);
122         boolean success = false;
123         try {
124             entry.save(conn);
125             entry.setShipmentCode(getRowCode("SH", entry.getShipmentId()));
126             entry.save(conn);
127             Transaction.commit(conn);
128             success = true;
129
130         } finally {
131             if (!success) Transaction.safeRollback(conn);
132         }
133     }
134
135     /**
136      * Update a record in the database with the
137      * information present in the web form.
138      *
139      * Again, this is merely an example. The data
140      * should be checked before being allowed
141      * into the database.
142      */

143     public void doUpdate(RunData data, Context context)
144         throws Exception JavaDoc
145     {
146         Shipment entry = new Shipment();
147         data.getParameters().setProperties(entry);
148
149         entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate")));
150         entry.setClosedDate(parseDate(data.getParameters().getString("closeddate")));
151         entry.setCreated(parseDateTime(data.getParameters().getString("created")));
152         entry.setModifiedBy(data.getUser().getName());
153         entry.setModified(new Date JavaDoc());
154
155         ParameterParser pp= data.getParameters();
156         Enumeration JavaDoc paramKeys= pp.keys();
157         int sordId = entry.getSorderId();
158         int custId = entry.getCustomerId();
159         int recpId = entry.getRecipientId();
160         int projId = entry.getProjectId();
161         
162         while(paramKeys.hasMoreElements()) {
163             String JavaDoc paramName = paramKeys.nextElement().toString();
164             if(paramName.startsWith("productid")) {
165                 String JavaDoc suffix=paramName.substring(9, paramName.length());
166                 ShipmentItem entryItem= new ShipmentItem();
167
168                 entryItem.setProductId(pp.getInt("productid" + suffix));
169                 entryItem.setDescription(pp.getString("description" + suffix));
170                 entryItem.setQuantity(pp.getInt("quantity" + suffix));
171
172                 entryItem.setSorderId(sordId);
173                 entryItem.setCustomerId(custId);
174                 entryItem.setRecipientId(recpId);
175                 entryItem.setProjectId(projId);
176
177                 entry.addShipmentItem(entryItem);
178             }
179         }
180
181         entry.setModified(true);
182         entry.setNew(false);
183
184         Criteria crit = new Criteria();
185         crit.add(ShipmentItemPeer.SHIPMENT_ID, entry.getShipmentId());
186
187         Connection JavaDoc conn = Transaction.begin(ShipmentPeer.DATABASE_NAME);
188         boolean success = false;
189         try {
190             ShipmentItemPeer.doDelete(crit, conn);
191             entry.save(conn);
192             Transaction.commit(conn);
193             success = true;
194
195         } finally {
196             if (!success) Transaction.safeRollback(conn);
197         }
198     }
199
200     /**
201      * Delete a record from the database using
202      * the unique id gleaned from the web form.
203      */

204     public void doDelete(RunData data, Context context)
205         throws Exception JavaDoc
206     {
207         Criteria criteria = new Criteria();
208         criteria.add(ShipmentPeer.SHIPMENT_ID, data.getParameters().getInt("shipmentid"));
209         ShipmentPeer.doDelete(criteria);
210     }
211
212     /**
213      * Delete selected records from the database using
214      * the unique ids gleaned from the web form.
215      */

216     public void doDeleteselected(RunData data, Context context)
217         throws Exception JavaDoc
218     {
219         int[] delIds= data.getParameters().getInts("rowid");
220         Criteria criteria = new Criteria();
221         criteria.addIn(ShipmentPeer.SHIPMENT_ID, delIds);
222         ShipmentPeer.doDelete(criteria);
223     }
224
225 }
226
Popular Tags