KickJava   Java API By Example, From Geeks To Geeks.

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


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