1 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 ; 38 import java.sql.PreparedStatement ; 39 import java.sql.ResultSet ; 40 import java.sql.SQLException ; 41 import java.util.ArrayList ; 42 import java.util.List ; 43 import java.util.HashMap ; 44 import java.util.Map ; 45 46 52 53 public class JDBCVersionStorage implements VersionStorage { 54 private DataSource ds; 55 56 public static void createStorage() { 57 DataSource 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 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 ds) { 82 this.ds = ds; 83 } 84 85 public Snip loadVersion(final Snip snip, final int version) { 86 final String applicationOid = snip.getApplication(); 87 final String name = snip.getName(); 88 final List snips = new ArrayList (); 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 rs) throws SQLException { 96 snips.add(createSnip(snip, rs)); 97 } 98 }, 99 new PreparedStatementSetter() { 100 public void setValues(PreparedStatement ps) throws SQLException { 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 ps) throws SQLException { 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 139 public List getVersionHistory(final Snip snip) { 140 final List history = new ArrayList (); 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 rs) throws SQLException { 148 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 ps) throws SQLException { 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 rs) throws SQLException { 168 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 |