001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2003 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.server.store.sql;
020:
021: import java.util.*;
022: import java.sql.*;
023:
024: import org.lucane.server.*;
025: import org.lucane.common.concepts.*;
026: import org.lucane.server.database.*;
027: import org.lucane.server.store.*;
028:
029: public class SqlGroupStore extends GroupStore {
030: private static final String TABLENAME = "groups";
031: protected static final String GROUPLINKS = "groupLinks";
032: protected static final String PLUGINLINKS = "pluginLinks";
033: protected static final String SERVICELINKS = "serviceLinks";
034: protected static final String USERLINKS = "userLinks";
035:
036: private DatabaseAbstractionLayer layer;
037: private Store store;
038:
039: public SqlGroupStore(Store store) throws Exception {
040: this .layer = Server.getInstance().getDBLayer();
041: this .store = store;
042:
043: if (!layer.hasTable(TABLENAME)) {
044: String dbDescription = "jar:file:///"
045: + Server.getInstance().getWorkingDirectory()
046: + "lib/lucane-server-" + Server.VERSION + ".jar!/"
047: + "db-sqlstore.xml";
048:
049: layer.getTableCreator().createFromXml(dbDescription);
050: }
051: }
052:
053: //-- interface
054:
055: /**
056: * Does the store has any data ?
057: *
058: * @return true if the store is already created
059: */
060: public boolean isInitialized() throws Exception {
061: return getAllGroups().hasNext();
062: }
063:
064: public void storeGroup(GroupConcept group) throws SQLException {
065: Connection c = layer.getConnection();
066: PreparedStatement insert;
067: Iterator i;
068:
069: //store group
070: insert = c.prepareStatement("INSERT INTO " + TABLENAME
071: + " VALUES(?, ?)");
072: insert.setString(1, group.getName());
073: insert.setString(2, group.getDescription());
074: insert.execute();
075: insert.close();
076:
077: //store group links
078: i = group.getParents();
079: while (i.hasNext()) {
080: GroupConcept parent = (GroupConcept) i.next();
081: insert = c.prepareStatement("INSERT INTO " + GROUPLINKS
082: + " VALUES(?, ?)");
083: insert.setString(1, parent.getName());
084: insert.setString(2, group.getName());
085: insert.execute();
086: insert.close();
087: }
088:
089: //store user links
090: i = group.getUsers();
091: while (i.hasNext()) {
092: UserConcept user = (UserConcept) i.next();
093: insert = c.prepareStatement("INSERT INTO " + USERLINKS
094: + " VALUES(?, ?)");
095: insert.setString(1, group.getName());
096: insert.setString(2, user.getName());
097: insert.execute();
098: insert.close();
099: }
100:
101: //store services links
102: i = group.getServices();
103: while (i.hasNext()) {
104: ServiceConcept service = (ServiceConcept) i.next();
105: insert = c.prepareStatement("INSERT INTO " + SERVICELINKS
106: + " VALUES(?, ?)");
107: insert.setString(1, group.getName());
108: insert.setString(2, service.getName());
109: insert.execute();
110: insert.close();
111: }
112:
113: //store plugins links
114: i = group.getPlugins();
115: while (i.hasNext()) {
116: PluginConcept plugin = (PluginConcept) i.next();
117: insert = c.prepareStatement("INSERT INTO " + PLUGINLINKS
118: + " VALUES(?, ?)");
119: insert.setString(1, group.getName());
120: insert.setString(2, plugin.getName());
121: insert.execute();
122: insert.close();
123: }
124:
125: c.close();
126: }
127:
128: public void updateGroup(GroupConcept group) throws SQLException {
129: removeGroupOnly(group);
130: storeGroup(group);
131: }
132:
133: public void removeGroup(GroupConcept group) throws SQLException {
134: //-- delete basic infos
135: removeGroupOnly(group);
136:
137: Connection c = layer.getConnection();
138:
139: //delete group links
140: PreparedStatement delete = c.prepareStatement("DELETE FROM "
141: + GROUPLINKS + " WHERE parent=? OR child=?");
142: delete.setString(1, group.getName());
143: delete.setString(2, group.getName());
144: delete.execute();
145:
146: delete.close();
147: c.close();
148: }
149:
150: public GroupConcept getGroup(String name) throws Exception {
151: GroupConcept group = null;
152:
153: Connection c = layer.getConnection();
154: PreparedStatement select = c.prepareStatement("SELECT * FROM "
155: + TABLENAME + " WHERE name=?");
156: select.setString(1, name);
157: ResultSet rs = select.executeQuery();
158:
159: if (rs.next()) {
160: name = rs.getString(1);
161: String description = rs.getString(2);
162:
163: group = new GroupConcept(name);
164: group.setDescription(description);
165: setGroupLinks(c, group);
166: setUserLinks(c, group);
167: setPluginLinks(c, group);
168: setServiceLinks(c, group);
169: }
170:
171: rs.close();
172: select.close();
173: c.close();
174:
175: return group;
176: }
177:
178: public Iterator getAllGroups() throws Exception {
179: ArrayList all = new ArrayList();
180:
181: Connection c = layer.getConnection();
182: PreparedStatement select = c.prepareStatement("SELECT * FROM "
183: + TABLENAME);
184: ResultSet rs = select.executeQuery();
185:
186: while (rs.next()) {
187: String name = rs.getString(1);
188: String description = rs.getString(2);
189:
190: GroupConcept group = new GroupConcept(name);
191: group.setDescription(description);
192: setGroupLinks(c, group);
193: setUserLinks(c, group);
194: setPluginLinks(c, group);
195: setServiceLinks(c, group);
196: all.add(group);
197: }
198:
199: rs.close();
200: select.close();
201: c.close();
202:
203: return all.iterator();
204: }
205:
206: //-- private methods
207:
208: private void setServiceLinks(Connection c, GroupConcept group)
209: throws Exception {
210: PreparedStatement select = c
211: .prepareStatement("SELECT service FROM " + SERVICELINKS
212: + " WHERE groupName=?");
213: select.setString(1, group.getName());
214: ResultSet rs = select.executeQuery();
215:
216: while (rs.next()) {
217: String name = rs.getString(1);
218: ServiceConcept service = store.getServiceStore()
219: .getService(name);
220: group.addService(service);
221: }
222:
223: rs.close();
224: select.close();
225: }
226:
227: private void setPluginLinks(Connection c, GroupConcept group)
228: throws Exception {
229: PreparedStatement select = c
230: .prepareStatement("SELECT plugin FROM " + PLUGINLINKS
231: + " WHERE groupName=?");
232: select.setString(1, group.getName());
233: ResultSet rs = select.executeQuery();
234:
235: while (rs.next()) {
236: String name = rs.getString(1);
237: PluginConcept plugin = store.getPluginStore().getPlugin(
238: name);
239: group.addPlugin(plugin);
240: }
241:
242: rs.close();
243: select.close();
244: }
245:
246: private void setUserLinks(Connection c, GroupConcept group)
247: throws Exception {
248: PreparedStatement select = c
249: .prepareStatement("SELECT userName FROM " + USERLINKS
250: + " WHERE groupName=?");
251: select.setString(1, group.getName());
252: ResultSet rs = select.executeQuery();
253:
254: while (rs.next()) {
255: String name = rs.getString(1);
256: UserConcept user = store.getUserStore().getUser(name);
257: group.addUser(user);
258: }
259:
260: rs.close();
261: select.close();
262: }
263:
264: private void setGroupLinks(Connection c, GroupConcept group)
265: throws Exception {
266: PreparedStatement select = c
267: .prepareStatement("SELECT parent FROM " + GROUPLINKS
268: + " WHERE child=?");
269: select.setString(1, group.getName());
270: ResultSet rs = select.executeQuery();
271:
272: while (rs.next()) {
273: String name = rs.getString(1);
274: GroupConcept parent = store.getGroupStore().getGroup(name);
275: group.addParent(parent);
276: }
277:
278: rs.close();
279: select.close();
280: }
281:
282: private void removeGroupOnly(GroupConcept group)
283: throws SQLException {
284: Connection c = layer.getConnection();
285: PreparedStatement delete;
286:
287: //delete group
288: delete = c.prepareStatement("DELETE FROM " + TABLENAME
289: + " WHERE name=?");
290: delete.setString(1, group.getName());
291: delete.execute();
292: delete.close();
293:
294: //delete group links
295: delete = c.prepareStatement("DELETE FROM " + GROUPLINKS
296: + " WHERE child=?");
297: delete.setString(1, group.getName());
298: delete.execute();
299: delete.close();
300:
301: //delete user links
302: delete = c.prepareStatement("DELETE FROM " + USERLINKS
303: + " WHERE groupName=?");
304: delete.setString(1, group.getName());
305: delete.execute();
306: delete.close();
307:
308: //delete services links
309: delete = c.prepareStatement("DELETE FROM " + SERVICELINKS
310: + " WHERE groupName=?");
311: delete.setString(1, group.getName());
312: delete.execute();
313: delete.close();
314:
315: //delete plugins links
316: delete = c.prepareStatement("DELETE FROM " + PLUGINLINKS
317: + " WHERE groupName=?");
318: delete.setString(1, group.getName());
319: delete.execute();
320: delete.close();
321:
322: c.close();
323: }
324:
325: }
|