KickJava   Java API By Example, From Geeks To Geeks.

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


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.common.concepts.PluginConcept;
25 import org.lucane.server.store.PluginStore;
26 import org.lucane.server.database.*;
27 import org.lucane.server.*;
28
29 public class SqlPluginStore extends PluginStore
30 {
31     private static final String JavaDoc TABLENAME = "plugins";
32     private DatabaseAbstractionLayer layer;
33     
34     public SqlPluginStore()
35     throws Exception JavaDoc
36     {
37         this.layer = Server.getInstance().getDBLayer();
38     }
39     
40     //-- interface
41
public void storePlugin(PluginConcept plugin)
42     throws SQLException
43     {
44         //store plugin
45
Connection c = layer.getConnection();
46         PreparedStatement insert = c.prepareStatement("INSERT INTO " + TABLENAME
47             + " VALUES(?, ?, ?)");
48         insert.setString(1, plugin.getName());
49         insert.setString(2, plugin.getVersion());
50         insert.setString(3, plugin.getDescription());
51         insert.execute();
52         insert.close();
53         c.close();
54     }
55     
56     public void updatePlugin(PluginConcept plugin)
57     throws SQLException
58     {
59         Connection c = layer.getConnection();
60         
61         //try to delete plugin
62
try {
63             PreparedStatement delete = c.prepareStatement("DELETE FROM " + TABLENAME
64                 + " WHERE name=?");
65             delete.setString(1, plugin.getName());
66             delete.execute();
67             delete.close();
68         } catch(SQLException e) {
69             //no such plugin
70
}
71         
72         //store plugin
73
PreparedStatement insert = c.prepareStatement("INSERT INTO " + TABLENAME
74             + " VALUES(?, ?, ?)");
75         insert.setString(1, plugin.getName());
76         insert.setString(2, plugin.getVersion());
77         insert.setString(3, plugin.getDescription());
78         insert.execute();
79         insert.close();
80         
81         c.close();
82     }
83
84     public void removePlugin(PluginConcept plugin)
85     throws SQLException
86     {
87         Connection c = layer.getConnection();
88         Statement s = c.createStatement();
89         
90         PreparedStatement delete = c.prepareStatement("DELETE FROM " + TABLENAME
91             + " WHERE name=?");
92         delete.setString(1, plugin.getName());
93         delete.execute();
94         delete.close();
95             
96         delete = c.prepareStatement("DELETE FROM " + SqlGroupStore.PLUGINLINKS
97             + " WHERE plugin=?");
98         delete.setString(1, plugin.getName());
99         delete.execute();
100         delete.close();
101                         
102         s.close();
103         c.close();
104     }
105
106     public PluginConcept getPlugin(String JavaDoc name)
107     throws SQLException
108     {
109         PluginConcept plugin = null;
110
111         Connection c = layer.getConnection();
112         PreparedStatement select = c.prepareStatement("SELECT * FROM " + TABLENAME
113             + " WHERE name=?");
114         select.setString(1, name);
115         ResultSet rs = select.executeQuery();
116                     
117         if(rs.next())
118         {
119             name = rs.getString(1);
120             String JavaDoc version = rs.getString(2);
121             String JavaDoc description = rs.getString(3);
122             
123             plugin = new PluginConcept(name, version);
124             plugin.setDescription(description);
125         }
126     
127         rs.close();
128         select.close();
129         c.close();
130             
131         return plugin;
132     }
133
134     public Iterator getAllPlugins()
135     throws SQLException
136     {
137         ArrayList all = new ArrayList();
138         
139         Connection c = layer.getConnection();
140         PreparedStatement select = c.prepareStatement("SELECT * FROM " + TABLENAME);
141         ResultSet rs = select.executeQuery();
142                     
143         while(rs.next())
144         {
145             String JavaDoc name = rs.getString(1);
146             String JavaDoc version = rs.getString(2);
147             String JavaDoc description = rs.getString(3);
148             
149             PluginConcept plugin = new PluginConcept(name, version);
150             plugin.setDescription(description);
151             all.add(plugin);
152         }
153     
154         rs.close();
155         select.close();
156         c.close();
157         
158         return all.iterator();
159     }
160 }
Popular Tags