KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

82     public void doInsert(RunData data, Context context)
83         throws Exception JavaDoc
84     {
85         TurbineUser entry = new TurbineUser();
86         data.getParameters().setProperties(entry);
87         CreamUser prefs = new CreamUser();
88         data.getParameters().setProperties(prefs);
89
90         ParameterParser pp= data.getParameters();
91         Enumeration JavaDoc paramKeys= pp.keys();
92         
93         while(paramKeys.hasMoreElements()) {
94             String JavaDoc paramName = paramKeys.nextElement().toString();
95             if(paramName.startsWith("roleid")) {
96                 String JavaDoc suffix=paramName.substring(6, paramName.length());
97                 TurbineUserGroupRole entryItem= new TurbineUserGroupRole();
98
99                 entryItem.setGroupId(1);
100                 entryItem.setRoleId(pp.getInt("roleid" + suffix));
101                 entry.addTurbineUserGroupRole(entryItem);
102             }
103         }
104
105         Connection JavaDoc conn = Transaction.begin(TurbineUserPeer.DATABASE_NAME);
106         boolean success = false;
107         try {
108             entry.save(conn);
109             int usrId=entry.getUserId();
110             prefs.setUserId(usrId);
111             prefs.save(conn);
112             Transaction.commit(conn);
113             success = true;
114
115         } finally {
116             if (!success) Transaction.safeRollback(conn);
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         TurbineUser entry = new TurbineUser();
132         data.getParameters().setProperties(entry);
133         CreamUser prefs = new CreamUser();
134         data.getParameters().setProperties(prefs);
135
136         ParameterParser pp= data.getParameters();
137         Enumeration JavaDoc paramKeys= pp.keys();
138
139         while(paramKeys.hasMoreElements()) {
140             String JavaDoc paramName = paramKeys.nextElement().toString();
141             if(paramName.startsWith("roleid")) {
142                 String JavaDoc suffix=paramName.substring(6, paramName.length());
143                 TurbineUserGroupRole entryItem= new TurbineUserGroupRole();
144
145                 entryItem.setGroupId(1);
146                 entryItem.setRoleId(pp.getInt("roleid" + suffix));
147
148                 entry.addTurbineUserGroupRole(entryItem);
149             }
150         }
151
152         entry.setModified(true);
153         entry.setNew(false);
154
155         prefs.setModified(true);
156         prefs.setNew(false);
157
158         Criteria crit = new Criteria();
159         crit.add(TurbineUserGroupRolePeer.USER_ID, entry.getUserId());
160
161         Connection JavaDoc conn = Transaction.begin(TurbineUserPeer.DATABASE_NAME);
162         boolean success = false;
163         try {
164             TurbineUserGroupRolePeer.doDelete(crit, conn);
165             entry.save(conn);
166             prefs.save(conn);
167             Transaction.commit(conn);
168             success = true;
169
170         } finally {
171             if (!success) Transaction.safeRollback(conn);
172         }
173
174     }
175
176     /**
177      * Delete a record from the database using
178      * the unique id gleaned from the web form.
179      */

180     public void doDelete(RunData data, Context context)
181         throws Exception JavaDoc
182     {
183         Criteria criteria = new Criteria();
184         criteria.add(TurbineUserPeer.USER_ID, data.getParameters().getInt("userid"));
185
186         Criteria prefcrit = new Criteria();
187         prefcrit.add(CreamUserPeer.USER_ID, data.getParameters().getInt("userid"));
188
189         Connection JavaDoc conn = Transaction.begin(TurbineUserPeer.DATABASE_NAME);
190         boolean success = false;
191         try {
192             TurbineUserPeer.doDelete(criteria, conn);
193             CreamUserPeer.doDelete(prefcrit, conn);
194             Transaction.commit(conn);
195             success = true;
196
197         } finally {
198             if (!success) Transaction.safeRollback(conn);
199         }
200
201     }
202
203     /**
204      * Delete selected records from the database using
205      * the unique ids gleaned from the web form.
206      */

207     public void doDeleteselected(RunData data, Context context)
208         throws Exception JavaDoc
209     {
210         int[] delIds= data.getParameters().getInts("rowid");
211
212         Criteria criteria = new Criteria();
213         criteria.addIn(TurbineUserPeer.USER_ID, delIds);
214         Criteria prefcrit = new Criteria();
215         prefcrit.addIn(CreamUserPeer.USER_ID, delIds);
216
217         Connection JavaDoc conn = Transaction.begin(TurbineUserPeer.DATABASE_NAME);
218         boolean success = false;
219         try {
220             TurbineUserPeer.doDelete(criteria, conn);
221             CreamUserPeer.doDelete(prefcrit, conn);
222             Transaction.commit(conn);
223             success = true;
224
225         } finally {
226             if (!success) Transaction.safeRollback(conn);
227         }
228
229     }
230
231 }
232
Popular Tags