1 18 19 package sync4j.server.store; 20 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 25 import java.sql.*; 26 27 import sync4j.framework.tools.DBTools; 28 29 import sync4j.framework.server.LastTimestamp; 30 import sync4j.framework.server.store.Clause; 31 import sync4j.framework.server.store.PersistentStore; 32 import sync4j.framework.server.store.BasePersistentStore; 33 import sync4j.framework.server.store.NotFoundException; 34 import sync4j.framework.server.store.PersistentStoreException; 35 36 48 public class LastTimestampPersistentStore 49 extends BasePersistentStore 50 implements PersistentStore, java.io.Serializable { 51 52 54 57 60 private String sqlInsertLastTimestamp = 61 "insert into sync4j_last_sync (principal, sync_source, last_anchor_server, last_anchor_client, start_sync, end_sync) values(?, ?, ?, ?, ?, ?)"; 62 63 67 public String getSqlInsertLastTimestamp() { 68 return sqlInsertLastTimestamp; 69 } 70 71 75 public void setSqlInsertLastTimestamp(String sqlInsertLastTimestamp) { 76 this.sqlInsertLastTimestamp = sqlInsertLastTimestamp; 77 } 78 79 82 private String sqlUpdateLastTimestamp = 83 "update sync4j_last_sync set last_anchor_server=?,last_anchor_client=?,start_sync=?,end_sync=? where principal=? and sync_source=?"; 84 85 89 public String getSqlUpdateLastTimestamp() { 90 return sqlUpdateLastTimestamp; 91 } 92 93 97 public void setSqlUpdateLastTimestamp(String sqlUpdateLastTimestamp) { 98 this.sqlUpdateLastTimestamp = sqlUpdateLastTimestamp; 99 } 100 101 104 private String sqlSelectLastTimestamp = 105 "select last_anchor_server, last_anchor_client,start_sync,end_sync from sync4j_last_sync where principal=? and sync_source=?"; 106 107 111 public String getSqlSelectLastTimestamp() { 112 return sqlSelectLastTimestamp; 113 } 114 115 119 public void setSqlSelectLastTimestamp(String sqlSelectLastTimestamp) { 120 this.sqlSelectLastTimestamp = sqlSelectLastTimestamp; 121 } 122 123 125 127 129 141 public boolean store(Object o) 142 throws PersistentStoreException { 143 if (o instanceof LastTimestamp) { 144 LastTimestamp l = (LastTimestamp) o; 145 146 Connection conn = null; 147 PreparedStatement stmt = null; 148 149 try { 150 conn = dataSource.getConnection(); 151 152 stmt = conn.prepareStatement(sqlUpdateLastTimestamp); 153 stmt.setString (1, l.tagServer ); 154 stmt.setString (2, l.tagClient ); 155 stmt.setTimestamp(3, new Timestamp(l.start) ); 156 stmt.setTimestamp(4, new Timestamp(l.end) ); 157 stmt.setLong (5, Long.parseLong(l.principal)); 158 stmt.setString (6, l.database ); 159 int n = stmt.executeUpdate(); 160 161 if (n == 0) { 162 stmt.close(); 166 stmt = conn.prepareStatement(sqlInsertLastTimestamp); 167 stmt.setLong (1, Long.parseLong(l.principal)); 168 stmt.setString (2, l.database ); 169 stmt.setString (3, l.tagServer ); 170 stmt.setString (4, l.tagClient ); 171 stmt.setTimestamp(5, new Timestamp(l.start) ); 172 stmt.setTimestamp(6, new Timestamp(l.end) ); 173 stmt.executeUpdate(); 174 } 175 } catch (SQLException e) { 176 throw new PersistentStoreException("Error storing last timestamp", e); 177 } finally { 178 DBTools.close(conn, stmt, null); 179 } 180 return true; 181 } 182 return false; 183 } 184 185 public boolean read(Object o) 186 throws PersistentStoreException { 187 if (o instanceof LastTimestamp) { 188 LastTimestamp l = (LastTimestamp) o; 189 190 Connection conn = null; 191 PreparedStatement stmt = null; 192 ResultSet rs = null; 193 194 try { 195 conn = dataSource.getConnection(); 196 197 stmt = conn.prepareStatement(sqlSelectLastTimestamp); 198 stmt.setLong(1, Long.parseLong(l.principal)); 199 stmt.setString(2, l.database); 200 rs = stmt.executeQuery(); 201 202 if (rs.next() == false) { 203 throw new NotFoundException("Last timestamp not found for " 204 + l.toString() 205 ); 206 } 207 l.tagServer = rs.getString (1) ; 208 l.tagClient = rs.getString (2) ; 209 l.start = rs.getTimestamp(3).getTime(); 210 l.end = rs.getTimestamp(4).getTime(); 211 } catch (SQLException e) { 212 throw new PersistentStoreException("Error reading last timestamp", e); 213 } finally { 214 DBTools.close(conn, stmt, rs); 215 } 216 return true; 217 } 218 return false; 219 } 220 221 232 public Object [] read(Class objClass) throws PersistentStoreException { 233 return null; 237 } 238 239 240 public boolean delete(Object o) throws PersistentStoreException 241 { 242 return false; 243 } 244 245 public Object [] read(Object o, Clause clause) throws PersistentStoreException 246 { 247 return null; 248 } 249 250 public int count(Object o, Clause clause) throws PersistentStoreException 251 { 252 return -1; 253 } 254 255 public boolean store(Object o, String operation) throws PersistentStoreException 256 { 257 return false; 258 } 259 260 } 261 | Popular Tags |