1 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 ; 35 import java.sql.PreparedStatement ; 36 import java.sql.ResultSet ; 37 import java.sql.SQLException ; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 import java.util.Properties ; 41 42 48 49 public class JDBCApplicationStorage implements ApplicationStorage { 50 private DataSource ds; 51 52 public static void createStorage() { 53 DataSource 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 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 ds) { 72 this.ds = ds; 73 } 74 75 public Map getApplications() { 76 JDBCTemplate template = new JDBCTemplate(ds); 77 final Map applications = new HashMap (); 78 template.query("SELECT applicationOid, name, prefix FROM Application", 79 new RowCallbackHandler() { 80 public void processRow(ResultSet rs) throws SQLException { 81 Properties prefixProps = new Properties (); 82 String 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 oid) { 93 JDBCTemplate template = new JDBCTemplate(ds); 94 template.update("DELETE FROM Application WHERE applicationOid=?", new PreparedStatementSetter() { 95 public void setValues(PreparedStatement ps) throws SQLException { 96 ps.setString(1, oid); 97 } 98 }); 99 return; 100 } 101 102 103 public Properties createApplication(final String name, final String prefix) { 104 final String 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 ps) throws SQLException { 108 ps.setString(1, oid); 109 ps.setString(2, name); 110 ps.setString(3, prefix); 111 } 112 }); 113 Properties prefixProps = new Properties (); 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 |