1 18 19 package sync4j.server.store; 20 21 import java.util.Map ; 22 import java.util.HashMap ; 23 import java.util.ArrayList ; 24 import java.util.Set ; 25 import java.util.Iterator ; 26 27 import java.sql.*; 28 29 import sync4j.framework.tools.DBTools; 30 import sync4j.framework.engine.source.SyncSource; 31 import sync4j.framework.server.Sync4jSource; 32 import sync4j.framework.server.Sync4jSourceType; 33 import sync4j.framework.server.store.*; 34 import sync4j.framework.server.store.PersistentStoreException; 35 36 import org.apache.commons.lang.StringUtils; 37 38 import sync4j.framework.server.Sync4jModule; 39 import sync4j.framework.server.Sync4jConnector; 40 41 48 public class ModulesPersistentStore 49 extends BasePersistentStore 50 implements PersistentStore, java.io.Serializable { 51 52 public static final int SQL_SELECT_ALL_MODULES_NAME = 0; 54 public static final int SQL_GET_MODULE = 1; 55 public static final int SQL_SELECT_MODULE_CONNECTOR = 2; 56 public static final int SQL_SELECT_CONNECTOR_SYNCSOURCETYPE = 3; 57 public static final int SQL_SELECT_SYNCSOURCETYPE_SYNCSOURCE = 4; 58 59 61 private String [] sql = null; 62 63 public void setSql(String [] sql) { 64 this.sql = sql; 65 } 66 67 public String [] getSql() { 68 return this.sql; 69 } 70 71 75 public boolean delete(Object o) 76 throws PersistentStoreException { 77 return false; 78 } 79 80 public Object [] delete(Class objClass) throws PersistentStoreException 81 { 82 return new Object [0]; 83 } 84 85 public boolean store(Object o, String operation) throws PersistentStoreException { 86 return false; 87 } 88 89 public boolean store(Object o) 90 throws PersistentStoreException { 91 return false; 92 } 93 94 public boolean read(Object o) 95 throws PersistentStoreException { 96 if (o instanceof Sync4jModule) { 97 readModule((Sync4jModule) o); 98 return true; 99 } 100 101 return false; 102 } 103 104 public Object [] read(Class objClass) throws PersistentStoreException { 105 if (objClass.getName().equals(Sync4jModule.class.getName())) { 106 return readModulesName(); 107 } 108 return null; 109 } 110 111 118 public Object [] read(Object o, Clause clause) throws PersistentStoreException { 119 if (o instanceof Sync4jSourceType) { 120 return readSyncSource((Sync4jSourceType) o); 121 } 122 123 return null; 124 } 125 126 public int count(Object o, Clause clause) throws PersistentStoreException { 127 return -1; 128 } 129 130 private Object [] readModulesName() 132 throws PersistentStoreException { 133 Connection conn = null; 134 PreparedStatement stmt = null; 135 ResultSet rs = null; 136 137 ArrayList ret = new ArrayList (); 138 139 try { 140 conn = dataSource.getConnection(); 141 142 stmt = conn.prepareStatement(sql[SQL_SELECT_ALL_MODULES_NAME]); 143 144 rs = stmt.executeQuery(); 145 146 while (rs.next()) { 147 ret.add( 148 new Sync4jModule( 149 rs.getString(1), 150 rs.getString(2), 151 rs.getString(3) 152 ) 153 ); 154 } 155 156 return ret.toArray(new Sync4jModule[ret.size()]); 157 } catch (SQLException e) { 158 throw new PersistentStoreException("Error reading modules name", e); 159 } finally { 160 DBTools.close(conn, stmt, rs); 161 } 162 } 163 164 private void readModule(Sync4jModule module) 165 throws PersistentStoreException { 166 Connection conn = null; 167 PreparedStatement stmt = null; 168 ResultSet rs = null; 169 170 try { 171 conn = dataSource.getConnection(); 172 173 stmt = conn.prepareStatement(sql[SQL_GET_MODULE]); 174 stmt.setString(1, module.getModuleId()); 175 176 rs = stmt.executeQuery(); 177 178 if (rs.next() == false) { 179 throw new NotFoundException("Module not found for " 180 + module.getModuleId() 181 ); 182 } 183 module.setModuleId(rs.getString(1)); 184 module.setModuleName(rs.getString(2)); 185 module.setDescription(rs.getString(3)); 186 187 Sync4jConnector[] syncConnectors = (Sync4jConnector[])this.readModuleConnectors(module); 189 module.setConnectors(syncConnectors); 190 191 for (int i=0; (syncConnectors != null) && i<syncConnectors.length; i++) { 193 Sync4jConnector sc = syncConnectors[i]; 194 Sync4jSourceType[] sst = (Sync4jSourceType[])this.readSyncSourceType(sc); 195 196 sc.setSourceTypes(sst); 197 } 198 199 } catch (SQLException e) { 200 throw new PersistentStoreException("Error reading the module " + module, e); 201 } finally { 202 DBTools.close(conn, stmt, rs); 203 } 204 } 205 206 private Object [] readModuleConnectors(Sync4jModule module) 207 throws PersistentStoreException { 208 Connection conn = null; 209 PreparedStatement stmt = null; 210 ResultSet rs = null; 211 ArrayList ret = new ArrayList (); 212 213 try { 214 conn = dataSource.getConnection(); 215 216 stmt = conn.prepareStatement(sql[SQL_SELECT_MODULE_CONNECTOR]); 217 stmt.setString(1, module.getModuleId()); 218 219 rs = stmt.executeQuery(); 220 221 while (rs.next()) { 222 ret.add( 223 new Sync4jConnector( 224 rs.getString(1), 225 rs.getString(2), 226 rs.getString(3), 227 rs.getString(4) 228 ) 229 ); 230 } 231 232 return ret.toArray(new Sync4jConnector[ret.size()]); 233 234 } catch (SQLException e) { 235 throw new PersistentStoreException("Error reading the connectors ", e); 236 } finally { 237 DBTools.close(conn, stmt, rs); 238 } 239 } 240 241 private Object [] readSyncSourceType(Sync4jConnector sc) 242 throws PersistentStoreException { 243 Connection conn = null; 244 PreparedStatement stmt = null; 245 ResultSet rs = null; 246 ArrayList ret = new ArrayList (); 247 248 try { 249 conn = dataSource.getConnection(); 250 251 stmt = conn.prepareStatement(sql[SQL_SELECT_CONNECTOR_SYNCSOURCETYPE]); 252 stmt.setString(1, sc.getConnectorId()); 253 254 rs = stmt.executeQuery(); 255 256 while (rs.next()) { 257 ret.add( 258 new Sync4jSourceType( 259 rs.getString(1), 260 rs.getString(2), 261 rs.getString(3), 262 rs.getString(4) 263 ) 264 ); 265 } 266 267 return ret.toArray(new Sync4jSourceType[ret.size()]); 268 269 } catch (SQLException e) { 270 throw new PersistentStoreException("Error reading the SyncSourceType ", e); 271 } finally { 272 DBTools.close(conn, stmt, rs); 273 } 274 } 275 276 private Object [] readSyncSource(Sync4jSourceType sst) 277 throws PersistentStoreException { 278 Connection conn = null; 279 PreparedStatement stmt = null; 280 ResultSet rs = null; 281 ArrayList ret = new ArrayList (); 282 283 try { 284 conn = dataSource.getConnection(); 285 286 stmt = conn.prepareStatement(sql[SQL_SELECT_SYNCSOURCETYPE_SYNCSOURCE]); 287 stmt.setString(1, sst.getSourceTypeId()); 288 289 rs = stmt.executeQuery(); 290 291 while (rs.next()) { 292 ret.add( 293 new Sync4jSource( 294 rs.getString(1), 295 rs.getString(2), 296 rs.getString(3), 297 rs.getString(4) 298 ) 299 ); 300 } 301 302 return ret.toArray(new Sync4jSource[ret.size()]); 303 304 } catch (SQLException e) { 305 throw new PersistentStoreException("Error reading the SyncSource ", e); 306 } finally { 307 DBTools.close(conn, stmt, rs); 308 } 309 } 310 } 311 | Popular Tags |