1 18 19 package sync4j.server.store; 20 21 import java.util.ArrayList ; 22 import java.sql.*; 23 24 import sync4j.framework.tools.Base64; 25 import sync4j.framework.tools.DBTools; 26 import sync4j.framework.server.store.*; 27 import sync4j.framework.server.Sync4jSource; 28 29 38 public class SyncSourcePersistentStore 39 extends BasePersistentStore 40 implements PersistentStore, java.io.Serializable { 41 42 44 public static final int SQL_GET_SOURCE = 0; 45 public static final int SQL_SELECT_ALL_SOURCES = 1; 46 public static final int SQL_DELETE_CLIENT_MAPPING = 2; 47 public static final int SQL_DELETE_LAST_SYNC = 3; 48 public static final int SQL_DELETE_SOURCE = 4; 49 public static final int SQL_INSERT_SYNCSOURCE = 5; 50 public static final int SQL_UPDATE_SYNCSOURCE = 6; 51 public static final int SQL_DELETE_SOURCE_CLIENT_MAPPING = 7; 52 public static final int SQL_DELETE_SOURCE_LAST_SYNC = 8; 53 54 protected String [] sql = null; 56 57 public void setSql(String [] sql) { 58 this.sql = sql; 59 } 60 61 public String [] getSql() { 62 return this.sql; 63 } 64 65 69 public boolean delete(Object o) 70 throws PersistentStoreException { 71 if (o instanceof Sync4jSource) { 72 Sync4jSource s = (Sync4jSource) o; 73 74 Connection conn = null; 75 PreparedStatement stmt = null; 76 77 try { 78 conn = dataSource.getConnection(); 79 80 stmt = conn.prepareStatement(sql[SQL_DELETE_SOURCE_CLIENT_MAPPING]); 81 stmt.setString(1, s.getUri()); 82 stmt.executeUpdate(); 83 stmt.close(); 84 85 stmt = conn.prepareStatement(sql[SQL_DELETE_SOURCE_LAST_SYNC]); 86 stmt.setString(1, s.getUri()); 87 stmt.executeUpdate(); 88 stmt.close(); 89 90 stmt = conn.prepareStatement(sql[SQL_DELETE_SOURCE]); 91 stmt.setString(1, s.getUri()); 92 stmt.executeUpdate(); 93 stmt.close(); 94 95 } catch (SQLException e) { 96 throw new PersistentStoreException("Error deleting the source " + s, e); 97 } finally { 98 DBTools.close(conn, stmt, null); 99 } 100 return true; 101 } 102 return false; 103 } 104 public boolean store(Object o) throws PersistentStoreException { 105 if (!(o instanceof Sync4jSource)) { 106 return false; } 108 Sync4jSource ss = (Sync4jSource) o; 109 110 Connection conn = null; 111 PreparedStatement stmt = null; 112 int n = 0; 113 114 try { 115 conn = dataSource.getConnection(); 116 117 stmt = conn.prepareStatement(sql[SQL_UPDATE_SYNCSOURCE]); 118 119 stmt.setString(1, ss.getConfig()); 120 stmt.setString(2, ss.getSourceName()); 121 stmt.setString(3, ss.getSourceTypeId()); 122 stmt.setString(4, ss.getUri()); 123 124 n = stmt.executeUpdate(); 125 stmt.close(); 126 127 if (n == 0) { 128 stmt = conn.prepareStatement(sql[SQL_INSERT_SYNCSOURCE]); 129 stmt.setString(1, ss.getUri()); 130 stmt.setString(2, ss.getConfig()); 131 stmt.setString(3, ss.getSourceName()); 132 stmt.setString(4, ss.getSourceTypeId()); 133 stmt.executeUpdate(); 134 stmt.close(); 135 } 136 137 } catch (SQLException e) { 138 throw new PersistentStoreException("Error updating the syncsource " + ss, e); 139 } finally { 140 DBTools.close(conn, stmt, null); 141 } 142 return true; 143 } 144 145 152 public boolean read(Object o) 153 throws PersistentStoreException { 154 if (o instanceof Sync4jSource) { 155 Sync4jSource s = (Sync4jSource) o; 156 157 Connection conn = null; 158 PreparedStatement stmt = null; 159 ResultSet rs = null; 160 161 try { 162 conn = dataSource.getConnection(); 163 164 stmt = conn.prepareStatement(sql[SQL_GET_SOURCE]); 165 stmt.setString(1, s.getUri()); 166 rs = stmt.executeQuery(); 167 if (rs.next() == false) { 168 throw new NotFoundException("Source not found for " 169 + s.getUri()); 170 } 171 172 s.setUri (rs.getString(1)); 173 s.setConfig(rs.getString(2)); 174 } catch (SQLException e) { 175 throw new PersistentStoreException("Error reading the source " + s, e); 176 } finally { 177 DBTools.close(conn, stmt, rs); 178 } 179 return true; 180 } 181 return false; 182 } 183 184 191 public Object [] read(Class objClass) throws PersistentStoreException { 192 if (objClass.getName().equals(Sync4jSource.class.getName())) { 193 194 Connection conn = null; 195 PreparedStatement stmt = null; 196 ResultSet rs = null; 197 198 ArrayList ret = new ArrayList (); 199 200 try { 201 conn = dataSource.getConnection(); 202 203 stmt = conn.prepareStatement(sql[SQL_SELECT_ALL_SOURCES]); 204 rs = stmt.executeQuery(); 205 while (rs.next()) { 206 ret.add(new Sync4jSource(rs.getString(1), 207 rs.getString(2))); 208 } 209 210 return ret.toArray(new Sync4jSource[ret.size()]); 211 } catch (SQLException e) { 212 throw new PersistentStoreException("Error reading sources", e); 213 } finally { 214 DBTools.close(conn, stmt, rs); 215 } 216 } 217 return null; 218 } 219 220 227 public Object [] read(Object o, Clause clause) throws PersistentStoreException { 228 if (o instanceof Sync4jSource) { 229 Sync4jSource s = (Sync4jSource)o; 230 231 Connection conn = null; 232 PreparedStatement stmt = null; 233 ResultSet rs = null; 234 235 ArrayList ret = new ArrayList (); 236 237 try { 238 conn = dataSource.getConnection(); 239 240 PreparedWhere where = clause.getPreparedWhere(); 241 242 String query = sql[SQL_SELECT_ALL_SOURCES]; 243 if (where.sql.length() > 0) { 244 query += " where " + where.sql; 245 } 246 247 stmt = conn.prepareStatement(query); 248 249 for (int i=0; i<where.parameters.length; ++i) { 250 stmt.setObject(i+1, where.parameters[i]); 251 } 252 253 rs = stmt.executeQuery(); 254 255 while (rs.next()) { 256 ret.add( 257 new Sync4jSource( 258 rs.getString(1), 259 rs.getString(2) 260 ) 261 ); 262 } 263 264 return ret.toArray(new Sync4jSource[ret.size()]); 265 } catch (SQLException e) { 266 throw new PersistentStoreException("Error reading sources", e); 267 } finally { 268 DBTools.close(conn, stmt, rs); 269 } 270 } 271 return null; 272 } 273 274 277 protected void insertSyncSource(String sourcetypeid, Sync4jSource ss) 278 throws PersistentStoreException { 279 Connection conn = null; 280 PreparedStatement stmt = null; 281 282 try { 283 conn = dataSource.getConnection(); 284 285 stmt = conn.prepareStatement(sql[SQL_INSERT_SYNCSOURCE]); 286 287 stmt.setString(1, ss.getUri()); 288 stmt.setString(2, ss.getConfig()); 289 stmt.setString(3, ss.getSourceName()); 290 stmt.setString(4, sourcetypeid); 291 292 stmt.executeUpdate(); 293 stmt.close(); 294 295 } catch (SQLException e) { 296 throw new PersistentStoreException("Error storing the syncsource " + ss, e); 297 } finally { 298 DBTools.close(conn, stmt, null); 299 } 300 } 301 302 305 protected void updateSyncSource(String sourcetypeid, Sync4jSource ss) 306 throws PersistentStoreException { 307 Connection conn = null; 308 PreparedStatement stmt = null; 309 310 try { 311 conn = dataSource.getConnection(); 312 313 stmt = conn.prepareStatement(sql[SQL_UPDATE_SYNCSOURCE]); 314 315 stmt.setString(1, ss.getConfig()); 316 stmt.setString(2, ss.getSourceName()); 317 stmt.setString(3, ss.getSourceTypeId()); 318 stmt.setString(4, ss.getUri()); 319 320 stmt.executeUpdate(); 321 stmt.close(); 322 323 } catch (SQLException e) { 324 throw new PersistentStoreException("Error updating the syncsource " + ss, e); 325 } finally { 326 DBTools.close(conn, stmt, null); 327 } 328 } 329 330 public int count(Object o, Clause clause) { 331 return -1; 332 } 333 } 334 335 | Popular Tags |