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 import sync4j.framework.server.ID; 36 37 46 public class IDPersistentStore extends BasePersistentStore 47 implements PersistentStore, java.io.Serializable { 48 49 51 public static final int SQL_GET_ID = 0; 52 public static final int SQL_UPDATE_ID = 1; 53 54 56 protected String [] sql = null; 57 58 public void setSql(String [] sql) { 59 this.sql = sql; 60 } 61 62 public String [] getSql() { 63 return this.sql; 64 } 65 66 67 70 public boolean delete(Object o) throws PersistentStoreException { 71 return false; 72 } 73 74 public boolean store(Object o, String operation) throws PersistentStoreException { 75 return false; 76 } 77 78 84 public boolean store(Object o) throws PersistentStoreException { 85 if (o instanceof ID) { 86 87 ID id = (ID) o; 88 String idSpace = id.getIdSpace(); 89 int counter = id.getValue(); 90 91 if (idSpace == null) { 92 throw new PersistentStoreException("Id space must be not null"); 93 } 94 95 Connection conn = null; 96 PreparedStatement stmt = null; 97 ResultSet rs = null; 98 99 try { 100 conn = dataSource.getConnection(); 101 102 stmt = conn.prepareStatement(sql[SQL_UPDATE_ID]); 103 stmt.setInt (1, counter); 104 stmt.setString(2, idSpace); 105 stmt.executeUpdate(); 106 107 } catch (SQLException e) { 108 e.printStackTrace(); 109 throw new PersistentStoreException("Error storing the counter " + idSpace, e); 110 } finally { 111 DBTools.close(conn, stmt, rs); 112 } 113 114 return true; 115 } 116 return false; 117 118 } 119 120 125 public boolean read(Object o) throws PersistentStoreException { 126 if (o instanceof ID) { 127 128 ID id = (ID) o; 129 String idSpace = id.getIdSpace(); 130 131 if (idSpace == null) { 132 throw new PersistentStoreException("Id space must be not null"); 133 } 134 135 Connection conn = null; 136 PreparedStatement stmt = null; 137 ResultSet rs = null; 138 139 try { 140 conn = dataSource.getConnection(); 141 142 int counter = 0; 143 144 stmt = conn.prepareStatement(sql[SQL_GET_ID]); 145 stmt.setString(1, id.getIdSpace()); 146 rs = stmt.executeQuery(); 147 148 if (rs.next() == false) { 149 throw new NotFoundException("Counter not found for " 150 + idSpace); 151 } 152 153 counter = rs.getInt(1) + 1; 154 155 DBTools.close(null, stmt, rs); 156 157 stmt = conn.prepareStatement(sql[SQL_UPDATE_ID]); 158 stmt.setInt (1, counter); 159 stmt.setString(2, idSpace); 160 stmt.executeUpdate(); 161 162 id.setValue(counter); 163 164 } catch (SQLException e) { 165 e.printStackTrace(); 166 throw new PersistentStoreException("Error reading the counter " + idSpace, e); 167 } finally { 168 DBTools.close(conn, stmt, rs); 169 } 170 171 return true; 172 } 173 return false; 174 } 175 176 public Object [] read(Class objClass) throws PersistentStoreException { 177 return null; 178 } 179 180 public Object [] read(Object o, Clause clause) throws PersistentStoreException { 181 return new Object [0]; 182 } 183 184 public int count(Object o, Clause clause) throws PersistentStoreException { 185 return 0; 186 } 187 188 } 189 190 | Popular Tags |