KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.apache.torque.util.Transaction;
48 import java.sql.Connection JavaDoc;
49 import org.apache.turbine.util.parser.ParameterParser;
50 import java.util.Enumeration JavaDoc;
51
52 import org.campware.cream.om.TurbineRole;
53 import org.campware.cream.om.TurbineRolePeer;
54 import org.campware.cream.om.TurbineRolePermission;
55 import org.campware.cream.om.TurbineRolePermissionPeer;
56
57 /**
58  * This class provides a simple set of methods to
59  * insert/update/delete records in a database.
60  */

61 public class RoleSQL extends CreamAction
62 {
63     protected void initScreen()
64     {
65         setModuleType(LOOKUP);
66         setModuleName("TURBINE_ROLE");
67     }
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         TurbineRole entry = new TurbineRole();
83         data.getParameters().setProperties(entry);
84
85         ParameterParser pp= data.getParameters();
86         Enumeration JavaDoc paramKeys= pp.keys();
87         
88         while(paramKeys.hasMoreElements()) {
89             String JavaDoc paramName = paramKeys.nextElement().toString();
90             if(paramName.startsWith("permissionid")) {
91                 String JavaDoc suffix=paramName.substring(12, paramName.length());
92                 TurbineRolePermission entryItem= new TurbineRolePermission();
93
94                 entryItem.setPermissionId(pp.getInt("permissionid" + suffix));
95
96                 entry.addTurbineRolePermission(entryItem);
97             }
98         }
99
100         entry.save();
101     }
102
103     /**
104      * Update a record in the database with the
105      * information present in the web form.
106      *
107      * Again, this is merely an example. The data
108      * should be checked before being allowed
109      * into the database.
110      */

111     public void doUpdate(RunData data, Context context)
112         throws Exception JavaDoc
113     {
114         TurbineRole entry = new TurbineRole();
115         data.getParameters().setProperties(entry);
116
117         ParameterParser pp= data.getParameters();
118         Enumeration JavaDoc paramKeys= pp.keys();
119         
120         while(paramKeys.hasMoreElements()) {
121             String JavaDoc paramName = paramKeys.nextElement().toString();
122             if(paramName.startsWith("permissionid")) {
123                 String JavaDoc suffix=paramName.substring(12, paramName.length());
124                 TurbineRolePermission entryItem= new TurbineRolePermission();
125
126                 entryItem.setPermissionId(pp.getInt("permissionid" + suffix));
127
128                 entry.addTurbineRolePermission(entryItem);
129             }
130         }
131
132         entry.setModified(true);
133         entry.setNew(false);
134
135         Criteria crit = new Criteria();
136         crit.add(TurbineRolePermissionPeer.ROLE_ID, entry.getRoleId());
137
138         Connection JavaDoc conn = Transaction.begin(TurbineRolePeer.DATABASE_NAME);
139         boolean success = false;
140         try {
141             TurbineRolePermissionPeer.doDelete(crit, conn);
142             entry.save(conn);
143             Transaction.commit(conn);
144             success = true;
145
146         } finally {
147             if (!success) Transaction.safeRollback(conn);
148         }
149
150     }
151
152     /**
153      * Delete a record from the database using
154      * the unique id gleaned from the web form.
155      */

156     public void doDelete(RunData data, Context context)
157         throws Exception JavaDoc
158     {
159         Criteria criteria = new Criteria();
160         criteria.add(TurbineRolePeer.ROLE_ID, data.getParameters().getInt("roleid"));
161         TurbineRolePeer.doDelete(criteria);
162     }
163
164     /**
165      * Delete selected records from the database using
166      * the unique ids gleaned from the web form.
167      */

168     public void doDeleteselected(RunData data, Context context)
169         throws Exception JavaDoc
170     {
171         int[] delIds= data.getParameters().getInts("rowid");
172         Criteria criteria = new Criteria();
173         criteria.addIn(TurbineRolePeer.ROLE_ID, delIds);
174         TurbineRolePeer.doDelete(criteria);
175     }
176
177 }
178
Popular Tags