KickJava   Java API By Example, From Geeks To Geeks.

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


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.Service;
54 import org.campware.cream.om.ServicePeer;
55 import org.campware.cream.om.ServiceItem;
56 import org.campware.cream.om.ServiceItemPeer;
57
58 /**
59  * This class provides a simple set of methods to
60  * insert/update/delete records in a database.
61  */

62 public class ServiceSQL extends CreamAction
63 {
64     protected void initScreen()
65     {
66         setModuleType(DOCUMENT);
67         setModuleName("SERVICE");
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         Service entry = new Service();
83         data.getParameters().setProperties(entry);
84
85         entry.setServiceCode(getTempCode());
86
87         entry.setIssuedDate(parseDate(data.getParameters().getString("issueddate")));
88         entry.setClosedDate(parseDate(data.getParameters().getString("closeddate")));
89         entry.setCreatedBy(data.getUser().getName());
90         entry.setCreated(new Date JavaDoc());
91         entry.setModifiedBy(data.getUser().getName());
92         entry.setModified(new Date JavaDoc());
93         
94         ParameterParser pp= data.getParameters();
95         Enumeration JavaDoc paramKeys= pp.keys();
96         int sordId = entry.getSorderId();
97         int custId = entry.getCustomerId();
98         int recpId = entry.getRecipientId();
99         int projId = entry.getProjectId();
100         
101         while(paramKeys.hasMoreElements()) {
102             String JavaDoc paramName = paramKeys.nextElement().toString();
103             if(paramName.startsWith("productid")) {
104                 String JavaDoc suffix=paramName.substring(9, paramName.length());
105                 ServiceItem entryItem= new ServiceItem();
106
107                 entryItem.setProductId(pp.getInt("productid" + suffix));
108                 entryItem.setDescription(pp.getString("description" + suffix));
109                 entryItem.setQuantity(pp.getInt("quantity" + suffix));
110
111                 entryItem.setSorderId(sordId);
112                 entryItem.setCustomerId(custId);
113                 entryItem.setRecipientId(recpId);
114                 entryItem.setProjectId(projId);
115
116                 entry.addServiceItem(entryItem);
117             }
118         }
119
120         Connection JavaDoc conn = Transaction.begin(ServicePeer.DATABASE_NAME);
121         boolean success = false;
122         try {
123             entry.save(conn);
124             entry.setServiceCode(getRowCode("SE", entry.getServiceId()));
125             entry.save(conn);
126             Transaction.commit(conn);
127             success = true;
128
129         } finally {
130             if (!success) Transaction.safeRollback(conn);
131         }
132     }
133
134     /**
135      * Update a record in the database with the
136      * information present in the web form.
137      *
138      * Again, this is merely an example. The data
139      * should be checked before being allowed
140      * into the database.
141      */

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

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

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