KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > app > JDBCApplicationStorage


1 /*
2  * This file is part of "SnipSnap Wiki/Weblog".
3  *
4  * Copyright (c) 2002 Stephan J. Schmidt, Matthias L. Jugel
5  * All Rights Reserved.
6  *
7  * Please visit http://snipsnap.org/ for updates and contact.
8  *
9  * --LICENSE NOTICE--
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  * --LICENSE NOTICE--
24  */

25 package org.snipsnap.app;
26
27 import org.snipsnap.jdbc.JDBCTemplate;
28 import org.snipsnap.jdbc.PreparedStatementSetter;
29 import org.snipsnap.jdbc.RowCallbackHandler;
30 import org.snipsnap.jdbc.UIDGenerator;
31 import org.snipsnap.util.ConnectionManager;
32 import org.snipsnap.util.log.SQLLogger;
33
34 import javax.sql.DataSource JavaDoc;
35 import java.sql.PreparedStatement JavaDoc;
36 import java.sql.ResultSet JavaDoc;
37 import java.sql.SQLException JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Properties JavaDoc;
41
42 /**
43  * ApplicationStorage is a DAO for applications.
44  *
45  * @author Stephan J. Schmidt
46  * @version $Id: JDBCApplicationStorage.java 1257 2003-12-11 13:36:55Z leo $
47  */

48
49 public class JDBCApplicationStorage implements ApplicationStorage {
50   private DataSource JavaDoc ds;
51
52   public static void createStorage() {
53     DataSource JavaDoc datasource = ConnectionManager.getDataSource();
54     System.err.println("JDBCApplicationManager: dropping application SQL table");
55     JDBCTemplate dropTemplate = new JDBCTemplate(datasource);
56     try {
57       dropTemplate.update("DROP TABLE Application");
58     } catch (Exception JavaDoc e) {
59       SQLLogger.warn("JDBCApplicationStorage: unable to drop table (new install?)", e);
60     }
61     System.err.println("JDBCApplicationManager: creating application SQL table");
62     JDBCTemplate template = new JDBCTemplate(datasource);
63     template.update(
64       "CREATE TABLE Application ( " +
65       " applicationOid VARCHAR(100) NOT NULL," +
66       " name VARCHAR(100) NOT NULL, " +
67       " prefix VARCHAR(100) )");
68     return;
69   }
70
71   public JDBCApplicationStorage(DataSource JavaDoc ds) {
72     this.ds = ds;
73   }
74
75   public Map JavaDoc getApplications() {
76     JDBCTemplate template = new JDBCTemplate(ds);
77     final Map JavaDoc applications = new HashMap JavaDoc();
78     template.query("SELECT applicationOid, name, prefix FROM Application",
79                    new RowCallbackHandler() {
80                      public void processRow(ResultSet JavaDoc rs) throws SQLException JavaDoc {
81                        Properties JavaDoc prefixProps = new Properties JavaDoc();
82                        String JavaDoc prefix = rs.getString(3);
83                        prefixProps.setProperty(ApplicationStorage.OID, rs.getString(1));
84                        prefixProps.setProperty(ApplicationStorage.NAME, rs.getString(2));
85                        prefixProps.setProperty(ApplicationStorage.PREFIX, prefix);
86                        applications.put(prefix, prefixProps);
87                      }
88                    });
89     return applications;
90   }
91
92   public void removeApplication(final String JavaDoc oid) {
93     JDBCTemplate template = new JDBCTemplate(ds);
94     template.update("DELETE FROM Application WHERE applicationOid=?", new PreparedStatementSetter() {
95       public void setValues(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
96         ps.setString(1, oid);
97       }
98     });
99     return;
100   }
101
102
103   public Properties JavaDoc createApplication(final String JavaDoc name, final String JavaDoc prefix) {
104     final String JavaDoc oid = UIDGenerator.generate(ApplicationStorage.class);
105     JDBCTemplate template = new JDBCTemplate(ds);
106     template.update("INSERT INTO Application (applicationOid,name,prefix) VALUES (?,?,?)", new PreparedStatementSetter() {
107       public void setValues(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
108         ps.setString(1, oid);
109         ps.setString(2, name);
110         ps.setString(3, prefix);
111       }
112     });
113     Properties JavaDoc prefixProps = new Properties JavaDoc();
114     prefixProps.setProperty(ApplicationStorage.OID, oid);
115     prefixProps.setProperty(ApplicationStorage.NAME, name);
116     prefixProps.setProperty(ApplicationStorage.PREFIX, prefix);
117     return prefixProps;
118   }
119 }
120
Popular Tags