KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > snipsnap > versioning > JDBCVersionStorage


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
26 package org.snipsnap.versioning;
27
28 import org.snipsnap.jdbc.JDBCTemplate;
29 import org.snipsnap.jdbc.PreparedStatementSetter;
30 import org.snipsnap.jdbc.RowCallbackHandler;
31 import org.snipsnap.snip.Snip;
32 import org.snipsnap.snip.SnipImpl;
33 import org.snipsnap.snip.label.Labels;
34 import org.snipsnap.util.ConnectionManager;
35 import org.snipsnap.util.log.SQLLogger;
36
37 import javax.sql.DataSource JavaDoc;
38 import java.sql.PreparedStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40 import java.sql.SQLException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.HashMap JavaDoc;
44 import java.util.Map JavaDoc;
45
46 /**
47  * Stores versions of snips with JDBC
48  *
49  * @author Stephan J. Schmidt
50  * @version $Id: JDBCVersionStorage.java 1262 2003-12-12 08:51:11Z stephan $
51  */

52
53 public class JDBCVersionStorage implements VersionStorage {
54   private DataSource JavaDoc ds;
55
56   public static void createStorage() {
57     DataSource JavaDoc datasource = ConnectionManager.getDataSource();
58     System.err.println("JDBCVersionStorage: dropping version SQL table");
59     JDBCTemplate dropTemplate = new JDBCTemplate(datasource);
60     try {
61       dropTemplate.update("DROP TABLE SnipVersion");
62     } catch (Exception JavaDoc e) {
63       SQLLogger.warn("JDBCVersionStorage: unable to drop table (new install?)", e);
64     }
65     System.err.println("JDBCVersionStorage: creating version SQL table");
66     JDBCTemplate template = new JDBCTemplate(datasource);
67     template.update(
68       "CREATE TABLE SnipVersion ( " +
69       " version INTEGER," +
70       " name VARCHAR(100) NOT NULL, " +
71       " applicationOid VARCHAR(100) NOT NULL, " +
72       " content TEXT, " +
73       " mTime TIMESTAMP, " +
74       " mUser VARCHAR(55), " +
75       " labels TEXT, " +
76       " viewCount INTEGER" +
77       ")");
78     return;
79   }
80
81   public JDBCVersionStorage(DataSource JavaDoc ds) {
82     this.ds = ds;
83   }
84
85   public Snip loadVersion(final Snip snip, final int version) {
86     final String JavaDoc applicationOid = snip.getApplication();
87     final String JavaDoc name = snip.getName();
88     final List JavaDoc snips = new ArrayList JavaDoc();
89     JDBCTemplate template = new JDBCTemplate(ds);
90     template.query(
91       "SELECT applicationOid, name, version, content, mTime, mUser, labels, viewCount" +
92       " FROM SnipVersion " +
93       " WHERE name=? AND applicationOid=? AND version=?",
94       new RowCallbackHandler() {
95         public void processRow(ResultSet JavaDoc rs) throws SQLException JavaDoc {
96           snips.add(createSnip(snip, rs));
97         }
98       },
99       new PreparedStatementSetter() {
100         public void setValues(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
101           ps.setString(1, name);
102           ps.setString(2, applicationOid);
103           ps.setInt(3, version);
104         }
105       });
106     return (snips.size() > 0 ? (Snip) snips.get(0) : null);
107   }
108
109   public void storeVersion(final Snip snip) {
110     synchronized(snip) {
111     JDBCTemplate template = new JDBCTemplate(ds);
112     template.update(
113       "INSERT INTO SnipVersion (" +
114       " applicationOid, name, version, content, mTime, mUser, labels, viewCount" +
115       " ) VALUES (?,?,?,?,?, ?,?,?)",
116       new PreparedStatementSetter() {
117         public void setValues(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
118           ps.setString(1, snip.getApplication());
119           ps.setString(2, snip.getName());
120           ps.setInt(3, snip.getVersion());
121           ps.setString(4, snip.getContent());
122           ps.setTimestamp(5, snip.getMTime());
123           ps.setString(6, snip.getMUser());
124           ps.setString(7, snip.getLabels().toString());
125           ps.setInt(8, snip.getViewCount());
126         }
127       });
128     }
129     return;
130   }
131
132   /**
133    * Return a list of VersionInfo objects.
134    * Reads all snips from the JDBC datasource.
135    *
136    * @param snip Snip to get the version history for.
137    * @return
138    */

139   public List JavaDoc getVersionHistory(final Snip snip) {
140     final List JavaDoc history = new ArrayList JavaDoc();
141     JDBCTemplate template = new JDBCTemplate(ds);
142     template.query(
143       "SELECT version, mTime, mUser, viewCount, content" +
144       " FROM SnipVersion " +
145       " WHERE name=? AND applicationOid=? ORDER BY version DESC",
146       new RowCallbackHandler() {
147         public void processRow(ResultSet JavaDoc rs) throws SQLException JavaDoc {
148           // Replace with version object?
149
VersionInfo info = new VersionInfo();
150           info.setVersion(rs.getInt("version"));
151           info.setMTime( rs.getTimestamp("mTime"));
152           info.setMUser( rs.getString("mUser"));
153           info.setViewCount(rs.getInt("viewCount"));
154           info.setSize(rs.getString("content").length());
155           history.add(info);
156         }
157       },
158       new PreparedStatementSetter() {
159         public void setValues(PreparedStatement JavaDoc ps) throws SQLException JavaDoc {
160           ps.setString(1, snip.getName());
161           ps.setString(2, snip.getApplication());
162         }
163       });
164     return history;
165   }
166
167   private Snip createSnip(Snip snip, ResultSet JavaDoc rs) throws SQLException JavaDoc {
168 // "SELECT applicationOid, name, version, content, mTime, mUser, labels, viewCount" +
169
Snip newSnip = new SnipImpl(snip.getName(), "");
170     newSnip.setApplication(snip.getApplication());
171     newSnip.setVersion(rs.getInt("version"));
172     newSnip.setContent(rs.getString("content"));
173     newSnip.setMTime(rs.getTimestamp("mTime"));
174     newSnip.setMUser(rs.getString("mUser"));
175     newSnip.setLabels(new Labels(newSnip, rs.getString("labels")));
176     newSnip.setViewCount(rs.getInt("viewCount"));
177     return newSnip;
178   }
179 }
180
Popular Tags