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.security.Sync4jPrincipal; 28 29 import sync4j.server.tools.IdSpaceGenerator; 30 31 43 public class PrincipalPersistentStore 44 extends BasePersistentStore 45 implements PersistentStore, java.io.Serializable { 46 47 49 private static final String OPT_INSERT = "INSERT"; 50 51 public static final int SQL_INSERT_PRINCIPAL = 0; 52 public static final int SQL_GET_PRINCIPAL = 1; 53 public static final int SQL_SELECT_PRINCIPAL = 2; 54 public static final int SQL_UPDATE_PRINCIPAL = 3; 55 public static final int SQL_SELECT_ALL_PRINCIPALS = 4; 56 public static final int SQL_DELETE_PRINCIPAL = 5; 57 public static final int SQL_DELETE_CLIENT_MAPPING = 6; 58 public static final int SQL_DELETE_LAST_SYNC = 7; 59 public static final int SQL_COUNT_PRINCIPALS = 8; 60 61 public static final String NS_PRINCIPAL = "principal"; 62 63 65 protected String [] sql = null; 66 67 public void setSql(String [] sql) { 68 this.sql = sql; 69 } 70 71 public String [] getSql() { 72 return this.sql; 73 } 74 75 private static final String SEARCH_COUNT_PRINCIPALS = "SCP"; 77 78 81 88 public boolean delete(Object o) throws PersistentStoreException { 89 if (o instanceof Sync4jPrincipal) { 90 Sync4jPrincipal p = (Sync4jPrincipal) o; 91 92 Connection conn = null; 93 PreparedStatement stmt = null; 94 95 try { 96 conn = dataSource.getConnection(); 97 98 long pid = Long.parseLong(p.getId()); 99 100 stmt = conn.prepareStatement(sql[SQL_DELETE_CLIENT_MAPPING]); 101 stmt.setLong(1, pid); 102 stmt.executeUpdate(); 103 stmt.close(); 104 105 stmt = conn.prepareStatement(sql[SQL_DELETE_LAST_SYNC]); 106 stmt.setLong(1, pid); 107 stmt.executeUpdate(); 108 stmt.close(); 109 110 stmt = conn.prepareStatement(sql[SQL_DELETE_PRINCIPAL]); 111 stmt.setLong(1, pid); 112 stmt.executeUpdate(); 113 114 } catch (SQLException e) { 115 throw new PersistentStoreException("Error deleting the principal " + p, e); 116 } finally { 117 DBTools.close(conn, stmt, null); 118 } 119 return true; 120 } 121 return false; 122 } 123 124 public boolean store(Object o, String operation) throws PersistentStoreException { 125 return false; 126 } 127 128 public boolean store(Object o) throws PersistentStoreException { 129 if (o instanceof Sync4jPrincipal) { 130 Sync4jPrincipal p = (Sync4jPrincipal) o; 131 132 Connection conn = null; 133 PreparedStatement stmt = null; 134 ResultSet rs = null; 135 int n = 0; 136 137 try { 138 conn = dataSource.getConnection(); 139 140 if (p.getId() != null && !p.getId().equals("")) { 141 stmt = conn.prepareStatement(sql[SQL_UPDATE_PRINCIPAL]); 142 stmt.setString(1, p.getUsername()); 143 stmt.setString(2, p.getDeviceId()); 144 stmt.setLong(3, Long.parseLong(p.getId())); 145 n = stmt.executeUpdate(); 146 stmt.close(); stmt = null; 147 } else { 148 stmt = conn.prepareStatement(sql[SQL_SELECT_PRINCIPAL]); 150 stmt.setString(1, p.getUsername()); 151 stmt.setString(2, p.getDeviceId()); 152 rs = stmt.executeQuery(); 153 154 if (rs.next() == false) { 155 n = 0; 156 } else { 157 n = 1; 158 p.setId (rs.getString(1)); 159 p.setUsername (rs.getString(2)); 160 p.setDeviceId (rs.getString(3)); 161 } 162 rs.close(); rs = null; 163 stmt.close(); stmt = null; 164 } 165 166 if (n == 0) { 167 int principalId = getPrincipalId(); 169 p.setId(String.valueOf(principalId)); 170 171 stmt = conn.prepareStatement(sql[SQL_INSERT_PRINCIPAL]); 172 stmt.setLong(1, principalId); 173 stmt.setString(2, p.getUsername()); 174 stmt.setString(3, p.getDeviceId()); 175 stmt.executeUpdate(); 176 } 177 return true; 178 } catch (SQLException e) { 179 throw new PersistentStoreException("Error storing principal " + p, e); 180 } finally { 181 DBTools.close(conn, stmt, rs); 182 } 183 } 184 return false; 185 } 186 187 194 public boolean read(Object o) throws PersistentStoreException { 195 if (o instanceof Sync4jPrincipal) { 196 Sync4jPrincipal p = (Sync4jPrincipal) o; 197 198 Connection conn = null; 199 PreparedStatement stmt = null; 200 ResultSet rs = null; 201 202 try { 203 conn = dataSource.getConnection(); 204 205 if (p.getId() == null) { 206 stmt = conn.prepareStatement(sql[SQL_SELECT_PRINCIPAL]); 207 stmt.setString(1, p.getUsername()); 208 stmt.setString(2, p.getDeviceId()); 209 } else { 210 stmt = conn.prepareStatement(sql[SQL_GET_PRINCIPAL]); 211 stmt.setLong(1, Long.parseLong(p.getId())); 212 } 213 rs = stmt.executeQuery(); 214 215 if (rs.next() == false) { 216 throw new NotFoundException("Principal not found for " 217 + p.toString()); 218 } 219 p.setId (String.valueOf(rs.getLong(1))); 220 p.setUsername (rs.getString(2)); 221 p.setDeviceId (rs.getString(3)); 222 223 } catch (SQLException e) { 224 throw new PersistentStoreException("Error reading principal " + p, e); 225 } finally { 226 DBTools.close(conn, stmt, rs); 227 } 228 return true; 229 } 230 return false; 231 } 232 233 public Object [] read(Class objClass) throws PersistentStoreException { 234 return null; 235 } 236 237 244 public Object [] read(Object o, Clause clause) throws PersistentStoreException { 245 if (!(o instanceof Sync4jPrincipal)) { 246 return null; } 248 Connection conn = null; 249 PreparedStatement stmt = null; 250 ResultSet rs = null; 251 252 ArrayList ret = new ArrayList (); 253 254 try { 255 conn = dataSource.getConnection(); 256 257 PreparedWhere where = clause.getPreparedWhere(); 258 String query = sql[SQL_SELECT_ALL_PRINCIPALS]; 259 if (where.sql.length() > 0){ 260 query += " where " + where.sql; 261 } 262 stmt = conn.prepareStatement(query); 263 for (int i=0; i<where.parameters.length; ++i) { 264 stmt.setObject(i+1, where.parameters[i]); 265 } 266 rs = stmt.executeQuery(); 267 268 while (rs.next()) { 269 ret.add( 270 new Sync4jPrincipal( 271 String.valueOf(rs.getLong(1)), 272 rs.getString(2), 273 rs.getString(3))); 274 } 275 276 return ret.toArray(new Sync4jPrincipal[ret.size()]); 277 } catch (SQLException e) { 278 throw new PersistentStoreException("Error reading principals", e); 279 } finally { 280 DBTools.close(conn, stmt, rs); 281 } 282 } 283 284 285 286 292 public int count(Object o, Clause clause) throws PersistentStoreException { 293 if (!(o instanceof Sync4jPrincipal)) { 294 return -1; } 296 Connection conn = null; 297 PreparedStatement stmt = null; 298 ResultSet rs = null; 299 int n = 0; 300 301 try { 302 conn = dataSource.getConnection(); 303 304 PreparedWhere where = clause.getPreparedWhere(); 305 String query = sql[SQL_COUNT_PRINCIPALS]; 306 if (where.sql.length()>0) { 307 query += " where " + where.sql; 308 } 309 stmt = conn.prepareStatement(query); 310 for (int i=0; i<where.parameters.length; ++i) { 311 stmt.setObject(i+1, where.parameters[i]); 312 } 313 rs = stmt.executeQuery(); 314 315 while (rs.next()) { 316 n = rs.getInt(1); 317 } 318 return n; 319 } catch (SQLException e) { 320 throw new PersistentStoreException("Error reading count principals ", e); 321 } finally { 322 DBTools.close(conn, stmt, rs); 323 } 324 } 325 326 328 332 private int getPrincipalId() { 333 IdSpaceGenerator idGenerator = new IdSpaceGenerator(NS_PRINCIPAL); 334 return Integer.parseInt(idGenerator.next()); 335 } 336 } 337 338 | Popular Tags |