1 25 26 29 package net.killingar.forum.internal.managers; 30 31 import net.killingar.forum.internal.AccessDeniedException; 32 import net.killingar.forum.internal.AccessLevel; 33 import net.killingar.forum.internal.Utils; 34 35 import java.sql.Connection ; 36 import java.sql.ResultSet ; 37 import java.sql.SQLException ; 38 import java.sql.Statement ; 39 import java.sql.PreparedStatement ; 40 import java.util.HashMap ; 41 import java.util.Properties ; 42 43 public class OptionsManager extends AbstractManager implements java.io.Serializable 44 { 45 private HashMap cache; 46 static private Properties defaults; 47 48 51 public void initialize(ForumManager p) 52 { 53 super.initialize(p); 54 55 try 56 { 57 loadSettings(); 58 } 59 catch (SQLException e) 60 { 61 throw new RuntimeException (e.toString()); 62 } 63 } 64 65 70 public void loadSettings() throws SQLException 71 { 72 if (!manager.isLoggedIn()) 73 { 74 cache = null; 75 return; 76 } 77 78 if (cache == null) 79 cache = new HashMap (); 80 else 81 cache.clear(); 82 83 if (defaults == null) 84 defaults = net.killingar.Utils.getProperties("net.killingar.forum.OptionsManager"); 85 86 Connection c = null; 87 Statement statement = null; 88 ResultSet result = null; 89 90 try 91 { 92 c = getNewConnection(); 93 statement = c.createStatement(); 94 result = statement.executeQuery("select Name, Setting from Options where User = "+manager.getUserID()); 95 96 while (result.next()) 97 cache.put(result.getString(1), result.getString(2)); 98 } 99 finally { closeAll(c, statement, result); } 100 } 101 102 105 public String get(String name) throws SQLException 106 { 107 return get(name, null); 108 } 109 110 113 public String get(String name, String defaultSetting) throws SQLException 114 { 115 if (cache == null) 116 loadSettings(); 117 118 if (cache == null) return defaultSetting; 120 121 String s = (String )cache.get(name); 123 if (s == null) 124 s = defaults.getProperty(name, defaultSetting); 125 126 133 134 return s; 135 } 136 137 140 public void set(String name, String setting) throws SQLException , AccessDeniedException 141 { 142 if (name == null)return; 143 144 if (!manager.isLoggedIn()) 145 return; 146 147 manager.checkMyAccess(AccessLevel.options); 148 149 Connection c = null; 150 PreparedStatement statement = null; 151 ResultSet result = null; 152 153 try 154 { 155 c = getNewConnection(); 156 157 if (setting != null) 159 { 160 statement = c.prepareStatement("delete from Options where User = ? AND Name = ?"); 161 statement.setLong(1, manager.getUserID()); 162 statement.setString(2, name); 163 statement.executeUpdate(); 164 statement.close(); 165 166 statement = c.prepareStatement("insert into Options (Name, User, Setting) values (?, ?, ?)"); 167 statement.setString(1, name); 168 statement.setLong(2, manager.getUserID()); 169 statement.setString(3, setting); 170 171 statement.executeUpdate(); 172 } 173 else 174 { 175 statement = c.prepareStatement("delete from Options where User = ? AND Name = ?"); 176 statement.setLong(1, manager.getUserID()); 177 statement.setString(2, name); 178 179 statement.executeUpdate(); 180 } 181 182 cache.put(name, setting); 184 } 185 finally { closeAll(c, statement, result); } 186 } 187 } | Popular Tags |