KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > server > store > sql > SqlGroupStore


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.server.store.sql;
20
21 import java.util.*;
22 import java.sql.*;
23
24 import org.lucane.server.*;
25 import org.lucane.common.concepts.*;
26 import org.lucane.server.database.*;
27 import org.lucane.server.store.*;
28
29 public class SqlGroupStore extends GroupStore
30 {
31     private static final String JavaDoc TABLENAME = "groups";
32     protected static final String JavaDoc GROUPLINKS = "groupLinks";
33     protected static final String JavaDoc PLUGINLINKS = "pluginLinks";
34     protected static final String JavaDoc SERVICELINKS = "serviceLinks";
35     protected static final String JavaDoc USERLINKS = "userLinks";
36     
37     private DatabaseAbstractionLayer layer;
38     private Store store;
39     
40     public SqlGroupStore(Store store)
41     throws Exception JavaDoc
42     {
43         this.layer = Server.getInstance().getDBLayer();
44         this.store = store;
45         
46         if(!layer.hasTable(TABLENAME))
47         {
48             String JavaDoc dbDescription = "jar:file:///"
49                 + Server.getInstance().getWorkingDirectory()
50                 + "lib/lucane-server-" +Server.VERSION+ ".jar!/"
51                 + "db-sqlstore.xml";
52
53             layer.getTableCreator().createFromXml(dbDescription);
54         }
55     }
56     
57     //-- interface
58

59     /**
60      * Does the store has any data ?
61      *
62      * @return true if the store is already created
63      */

64     public boolean isInitialized()
65     throws Exception JavaDoc
66     {
67         return getAllGroups().hasNext();
68     }
69     
70     public void storeGroup(GroupConcept group)
71     throws SQLException
72     {
73         Connection c = layer.getConnection();
74         PreparedStatement insert;
75         Iterator i;
76                
77         //store group
78
insert = c.prepareStatement("INSERT INTO " + TABLENAME + " VALUES(?, ?)");
79         insert.setString(1, group.getName());
80         insert.setString(2, group.getDescription());
81         insert.execute();
82         insert.close();
83         
84         //store group links
85
i = group.getParents();
86         while(i.hasNext())
87         {
88             GroupConcept parent = (GroupConcept)i.next();
89             insert = c.prepareStatement("INSERT INTO " + GROUPLINKS + " VALUES(?, ?)");
90             insert.setString(1, parent.getName());
91             insert.setString(2, group.getName());
92             insert.execute();
93             insert.close();
94         }
95         
96         //store user links
97
i = group.getUsers();
98         while(i.hasNext())
99         {
100             UserConcept user = (UserConcept)i.next();
101             insert = c.prepareStatement("INSERT INTO " + USERLINKS + " VALUES(?, ?)");
102             insert.setString(1, group.getName());
103             insert.setString(2, user.getName());
104             insert.execute();
105             insert.close();
106         }
107         
108         //store services links
109
i = group.getServices();
110         while(i.hasNext())
111         {
112             ServiceConcept service = (ServiceConcept)i.next();
113             insert = c.prepareStatement("INSERT INTO " + SERVICELINKS + " VALUES(?, ?)");
114             insert.setString(1, group.getName());
115             insert.setString(2, service.getName());
116             insert.execute();
117             insert.close();
118         }
119         
120         //store plugins links
121
i = group.getPlugins();
122         while(i.hasNext())
123         {
124             PluginConcept plugin = (PluginConcept)i.next();
125             insert = c.prepareStatement("INSERT INTO " + PLUGINLINKS + " VALUES(?, ?)");
126             insert.setString(1, group.getName());
127             insert.setString(2, plugin.getName());
128             insert.execute();
129             insert.close();
130         }
131         
132         c.close();
133     }
134     
135     public void updateGroup(GroupConcept group)
136     throws SQLException
137     {
138         removeGroupOnly(group);
139         storeGroup(group);
140     }
141
142     public void removeGroup(GroupConcept group)
143     throws SQLException
144     {
145         //-- delete basic infos
146
removeGroupOnly(group);
147         
148         Connection c = layer.getConnection();
149         
150         //delete group links
151
PreparedStatement delete = c.prepareStatement("DELETE FROM " + GROUPLINKS
152             + " WHERE parent=? OR child=?");
153         delete.setString(1, group.getName());
154         delete.setString(2, group.getName());
155         delete.execute();
156         
157         delete.close();
158         c.close();
159     }
160
161     public GroupConcept getGroup(String JavaDoc name)
162     throws Exception JavaDoc
163     {
164         GroupConcept group = null;
165
166         Connection c = layer.getConnection();
167         PreparedStatement select = c.prepareStatement("SELECT * FROM " + TABLENAME
168             + " WHERE name=?");
169         select.setString(1, name);
170         ResultSet rs = select.executeQuery();
171                     
172         if(rs.next())
173         {
174             name = rs.getString(1);
175             String JavaDoc description = rs.getString(2);
176             
177             group = new GroupConcept(name);
178             group.setDescription(description);
179             setGroupLinks(c, group);
180             setUserLinks(c, group);
181             setPluginLinks(c, group);
182             setServiceLinks(c, group);
183         }
184     
185         rs.close();
186         select.close();
187         c.close();
188             
189         return group;
190     }
191     
192     public Iterator getAllGroups()
193     throws Exception JavaDoc
194     {
195         ArrayList all = new ArrayList();
196         
197         Connection c = layer.getConnection();
198         PreparedStatement select = c.prepareStatement("SELECT * FROM " + TABLENAME);
199         ResultSet rs = select.executeQuery();
200                     
201         while(rs.next())
202         {
203             String JavaDoc name = rs.getString(1);
204             String JavaDoc description = rs.getString(2);
205             
206             GroupConcept group = new GroupConcept(name);
207             group.setDescription(description);
208             setGroupLinks(c, group);
209             setUserLinks(c, group);
210             setPluginLinks(c, group);
211             setServiceLinks(c, group);
212             all.add(group);
213         }
214     
215         rs.close();
216         select.close();
217         c.close();
218         
219         return all.iterator();
220     }
221     
222     //-- private methods
223

224     private void setServiceLinks(Connection c, GroupConcept group)
225     throws Exception JavaDoc
226     {
227         PreparedStatement select = c.prepareStatement("SELECT service FROM " + SERVICELINKS
228             + " WHERE groupName=?");
229         select.setString(1, group.getName());
230         ResultSet rs = select.executeQuery();
231                     
232         while(rs.next())
233         {
234             String JavaDoc name = rs.getString(1);
235             ServiceConcept service = store.getServiceStore().getService(name);
236             group.addService(service);
237         }
238     
239         rs.close();
240         select.close();
241     }
242
243     private void setPluginLinks(Connection c, GroupConcept group)
244     throws Exception JavaDoc
245     {
246         PreparedStatement select = c.prepareStatement("SELECT plugin FROM " + PLUGINLINKS
247             + " WHERE groupName=?");
248         select.setString(1, group.getName());
249         ResultSet rs = select.executeQuery();
250                     
251         while(rs.next())
252         {
253             String JavaDoc name = rs.getString(1);
254             PluginConcept plugin = store.getPluginStore().getPlugin(name);
255             group.addPlugin(plugin);
256         }
257     
258         rs.close();
259         select.close();
260     }
261
262     private void setUserLinks(Connection c, GroupConcept group)
263     throws Exception JavaDoc
264     {
265         PreparedStatement select = c.prepareStatement("SELECT userName FROM " + USERLINKS
266             + " WHERE groupName=?");
267         select.setString(1, group.getName());
268         ResultSet rs = select.executeQuery();
269                     
270         while(rs.next())
271         {
272             String JavaDoc name = rs.getString(1);
273             UserConcept user = store.getUserStore().getUser(name);
274             group.addUser(user);
275         }
276     
277         rs.close();
278         select.close();
279     }
280
281     private void setGroupLinks(Connection c, GroupConcept group)
282     throws Exception JavaDoc
283     {
284         PreparedStatement select = c.prepareStatement("SELECT parent FROM " + GROUPLINKS
285             + " WHERE child=?");
286         select.setString(1, group.getName());
287         ResultSet rs = select.executeQuery();
288                     
289         while(rs.next())
290         {
291             String JavaDoc name = rs.getString(1);
292             GroupConcept parent = store.getGroupStore().getGroup(name);
293             group.addParent(parent);
294         }
295     
296         rs.close();
297         select.close();
298     }
299     
300     private void removeGroupOnly(GroupConcept group)
301     throws SQLException
302     {
303         Connection c = layer.getConnection();
304         PreparedStatement delete;
305                
306         //delete group
307
delete = c.prepareStatement("DELETE FROM " + TABLENAME + " WHERE name=?");
308         delete.setString(1, group.getName());
309         delete.execute();
310         delete.close();
311         
312         //delete group links
313
delete = c.prepareStatement("DELETE FROM " + GROUPLINKS + " WHERE child=?");
314         delete.setString(1, group.getName());
315         delete.execute();
316         delete.close();
317         
318         //delete user links
319
delete = c.prepareStatement("DELETE FROM " + USERLINKS + " WHERE groupName=?");
320         delete.setString(1, group.getName());
321         delete.execute();
322         delete.close();
323         
324         //delete services links
325
delete = c.prepareStatement("DELETE FROM " + SERVICELINKS + " WHERE groupName=?");
326         delete.setString(1, group.getName());
327         delete.execute();
328         delete.close();
329         
330         //delete plugins links
331
delete = c.prepareStatement("DELETE FROM " + PLUGINLINKS + " WHERE groupName=?");
332         delete.setString(1, group.getName());
333         delete.execute();
334         delete.close();
335                
336         c.close();
337     }
338
339 }
340
Popular Tags