1 18 package sync4j.server.store; 19 20 import java.util.Map ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 24 import java.sql.*; 25 26 import sync4j.framework.tools.DBTools; 27 28 import sync4j.framework.server.ClientMapping; 29 import sync4j.framework.server.store.Clause; 30 import sync4j.framework.server.store.PersistentStore; 31 import sync4j.framework.server.store.BasePersistentStore; 32 import sync4j.framework.server.store.NotFoundException; 33 import sync4j.framework.server.store.PersistentStoreException; 34 35 52 public class ClientMappingPersistentStore 53 extends BasePersistentStore 54 implements PersistentStore, java.io.Serializable { 55 56 58 private String sqlInsertClientMapping = 61 "insert into sync4j_client_mapping (principal, sync_source, guid, luid) values(?, ?, ?, ?)"; 62 private String sqlDeleteClientMapping = 63 "delete from sync4j_client_mapping where principal=? and sync_source=? and luid=?"; 64 private String sqlUpdateClientMapping = 65 "update sync4j_client_mapping set luid=? where principal=? and sync_source=? and guid=?"; 66 private String sqlSelectClientMapping = 67 "select luid,guid from sync4j_client_mapping where principal=? and sync_source=?"; 68 69 73 public String getSqlInsertClientMapping() { 74 return sqlInsertClientMapping; 75 } 76 77 81 public void setSqlInsertClientMapping(String sqlInsertClientMapping) { 82 this.sqlInsertClientMapping = sqlInsertClientMapping; 83 } 84 85 89 public String getSqlDeleteClientMapping() { 90 return sqlDeleteClientMapping; 91 } 92 93 97 public void setSqlDeleteClientMapping(String sqlDeleteClientMapping) { 98 this.sqlDeleteClientMapping = sqlDeleteClientMapping; 99 } 100 101 105 public String getSqlUpdateClientMapping() { 106 return sqlUpdateClientMapping; 107 } 108 109 113 public void setSqlUpdateClientMapping(String sqlUpdateClientMapping) { 114 this.sqlUpdateClientMapping = sqlUpdateClientMapping; 115 } 116 117 121 public String getSqlSelectClientMapping() { 122 return sqlSelectClientMapping; 123 } 124 125 129 public void setSqlSelectClientMapping(String sqlSelectClientMapping) { 130 this.sqlSelectClientMapping = sqlSelectClientMapping; 131 } 132 133 135 137 139 public boolean store(Object o) 140 throws PersistentStoreException { 141 if (o instanceof ClientMapping) { 142 ClientMapping clientMapping = (ClientMapping) o; 143 144 Connection conn = null; 145 PreparedStatement stmt = null, stmtIns = null; 146 147 long principal = -1; 148 String dbURI = clientMapping.getDbURI(); 149 150 principal = Long.parseLong(clientMapping.getPrincipal().getId()); 151 152 assert ((principal >= 0) && (dbURI != null)); 153 154 try { 155 conn = dataSource.getConnection(); 156 157 if (clientMapping.isDeleted()) { 158 stmt = conn.prepareStatement(sqlDeleteClientMapping); 159 stmt.setLong(1, principal); 160 stmt.setString(2, dbURI ); 161 String [] luids = clientMapping.getDeletedLuids(); 162 for (int i=0; i < luids.length; ++i) { 163 stmt.setString(3, luids[i]); 164 stmt.executeUpdate(); 165 } 166 stmt.close(); stmt = null; 167 } 168 169 if (clientMapping.isModified()) { 170 stmt = conn.prepareStatement(sqlUpdateClientMapping); 171 stmt.setLong(2, principal); 172 stmt.setString(3, dbURI ); 173 String [] luids = clientMapping.getModifiedLuids(); 174 175 String guid = null; 176 for (int i=0; i < luids.length; ++i) { 177 int n = -1; 178 179 guid = clientMapping.getMappedValueForLuid(luids[i]); 180 stmt.setString(1, luids[i]); 181 stmt.setString(4, guid ); 182 n = stmt.executeUpdate(); 183 184 if (n == 0) { 185 stmtIns = conn.prepareStatement(sqlInsertClientMapping); 189 stmtIns.setLong(1, principal); 190 stmtIns.setString(2, dbURI ); 191 stmtIns.setString(3, guid); 192 stmtIns.setString(4, luids[i]); 193 stmtIns.executeUpdate(); 194 stmtIns.close(); stmtIns = null; 195 } 196 } 197 stmt.close(); stmt = null; 198 } 199 } catch (SQLException e) { 200 throw new PersistentStoreException("Error storing client mapping", e); 201 } finally { 202 DBTools.close(conn, stmt, null); 203 } 204 return true; 205 } 206 return false; 207 } 208 209 public boolean read(Object o) 210 throws PersistentStoreException { 211 if (o instanceof ClientMapping) { 212 ClientMapping clientMapping = (ClientMapping) o; 213 214 Connection conn = null; 215 PreparedStatement stmt = null; 216 ResultSet rs = null; 217 218 try { 219 conn = dataSource.getConnection(); 220 221 stmt = conn.prepareStatement(sqlSelectClientMapping); 222 stmt.setLong(1, Long.parseLong(clientMapping.getPrincipal().getId())); 223 stmt.setString(2, clientMapping.getDbURI() ); 224 rs = stmt.executeQuery(); 225 226 HashMap mapping = new HashMap (); 227 while (rs.next()) { 228 mapping.put(rs.getString("guid"), rs.getString("luid")); 229 } 230 clientMapping.initializeFromMapping(mapping); 231 } catch (SQLException e) { 232 throw new PersistentStoreException("Error reading mapping", e); 233 } finally { 234 DBTools.close(conn, stmt, rs); 235 } 236 return true; 237 } 238 return false; 239 } 240 241 252 public Object [] read(Class objClass) throws PersistentStoreException { 253 return null; 257 } 258 259 public boolean delete(Object o) throws PersistentStoreException 260 { 261 return false; 262 } 263 264 public Object [] read(Object o, Clause clause) throws PersistentStoreException 265 { 266 return null; 267 } 268 269 public int count(Object o, Clause clause) throws PersistentStoreException 270 { 271 return -1; 272 } 273 274 public boolean store(Object o, String operation) throws PersistentStoreException 275 { 276 return false; 277 } 278 279 } 280 | Popular Tags |