1 21 22 package org.opensubsystems.patterns.mappeddata.persist.db; 23 24 import java.sql.Connection ; 25 import java.sql.PreparedStatement ; 26 import java.sql.ResultSet ; 27 import java.sql.SQLException ; 28 import java.sql.Types ; 29 import java.util.Collection ; 30 31 import org.opensubsystems.core.data.DataConstant; 32 import org.opensubsystems.core.data.DataObject; 33 import org.opensubsystems.core.data.ModifiableDataObject; 34 import org.opensubsystems.core.error.OSSDataNotFoundException; 35 import org.opensubsystems.core.error.OSSDatabaseAccessException; 36 import org.opensubsystems.core.error.OSSException; 37 import org.opensubsystems.core.error.OSSInconsistentDataException; 38 import org.opensubsystems.core.persist.db.DatabaseCreateMultipleDataObjectsOperation; 39 import org.opensubsystems.core.persist.db.DatabaseDeleteSingleDataObjectOperation; 40 import org.opensubsystems.core.persist.db.DatabaseFactoryImpl; 41 import org.opensubsystems.core.persist.db.DatabaseReadMultipleOperation; 42 import org.opensubsystems.core.persist.db.DatabaseReadOperation; 43 import org.opensubsystems.core.persist.db.DatabaseReadSingleDataObjectOperation; 44 import org.opensubsystems.core.persist.db.DatabaseUpdateOperation; 45 import org.opensubsystems.core.util.DatabaseUtils; 46 import org.opensubsystems.core.util.GlobalConstants; 47 import org.opensubsystems.core.util.ThreeIntStruct; 48 import org.opensubsystems.patterns.mappeddata.data.MappedData; 49 import org.opensubsystems.patterns.mappeddata.persist.MappingFactory; 50 51 64 public class MappingDatabaseFactory extends DatabaseFactoryImpl 65 implements MappingFactory 66 { 67 69 72 protected MappingDatabaseSchema m_schema; 73 74 76 83 public MappingDatabaseFactory( 84 MappingDatabaseSchema schema 85 ) throws OSSException 86 { 87 super(DataConstant.MAPPEDDATA_DATA_TYPE); 88 89 m_schema = schema; 90 } 91 92 94 97 public void create( 98 final ThreeIntStruct tisIDs 99 ) throws OSSException 100 { 101 if (GlobalConstants.ERROR_CHECKING) 102 { 103 assert tisIDs != null : "Cannot create unspecified mapping values."; 104 } 105 106 DatabaseUpdateOperation dbop = new DatabaseUpdateOperation( 107 this, m_schema.getInsertMappingRecord(), m_schema, 108 DatabaseUpdateOperation.DBOP_INSERT, null) 109 { 110 protected void performOperation( 111 DatabaseFactoryImpl dbfactory, 112 Connection cntConnection, 113 PreparedStatement pstmQuery 114 ) throws OSSException, 115 SQLException 116 { 117 pstmQuery.setInt(1, tisIDs.getFirst()); 118 pstmQuery.setInt(2, tisIDs.getSecond()); 119 pstmQuery.setInt(3, tisIDs.getThird()); 120 pstmQuery.setNull(4, Types.INTEGER); 121 pstmQuery.executeUpdate(); 122 } 123 }; 124 dbop.executeUpdate(); 125 } 126 127 130 public void delete( 131 final ThreeIntStruct tisIDs 132 ) throws OSSException 133 { 134 if (GlobalConstants.ERROR_CHECKING) 135 { 136 assert tisIDs != null : "Cannot delete unspecified mapping values."; 137 } 138 139 DatabaseUpdateOperation dbop = new DatabaseUpdateOperation( 140 this, m_schema.getDeleteMappingRecord(), m_schema, 141 DatabaseUpdateOperation.DBOP_DELETE, null) 142 { 143 protected void performOperation( 144 DatabaseFactoryImpl dbfactory, 145 Connection cntConnection, 146 PreparedStatement pstmQuery 147 ) throws OSSException, 148 SQLException 149 { 150 int iDeleted; 151 152 pstmQuery.setInt(1, tisIDs.getFirst()); 153 pstmQuery.setInt(2, tisIDs.getSecond()); 154 pstmQuery.setInt(3, tisIDs.getThird()); 155 iDeleted = pstmQuery.executeUpdate(); 156 157 if (iDeleted == 0) 158 { 159 throw new OSSDataNotFoundException( 160 "Data to delete cannot be found in the database"); 161 } 162 else if (iDeleted != 1) 163 { 164 throw new OSSInconsistentDataException("Inconsistent database contains multiple (" 165 + iDeleted + ") data object with the same ID"); 166 } 167 } 168 }; 169 dbop.executeUpdate(); 170 } 171 172 175 public int[] getMappedData( 176 final int iId, 177 final int iMappingType 178 ) throws OSSException 179 { 180 int[] arrMappedData = null; 181 182 if (iId > DataObject.NEW_ID) 183 { 184 DatabaseReadOperation dbop = new DatabaseReadMultipleOperation( 185 this, m_schema.geSelectMappingRecords(), m_schema) 186 { 187 protected Object performOperation( 188 DatabaseFactoryImpl dbfactory, 189 Connection cntConnection, 190 PreparedStatement pstmQuery 191 ) throws OSSException, 192 SQLException 193 { 194 pstmQuery.setInt(1, iId); 195 pstmQuery.setInt(2, iMappingType); 196 return DatabaseUtils.loadMultipleIntsAsArray(pstmQuery); 197 } 198 }; 199 arrMappedData = (int[])dbop.executeRead(); 200 } 201 202 return arrMappedData; 203 } 204 205 208 public int[] getMappedData( 209 final String strColumnIDs, 210 final int iMappingType 211 ) throws OSSException 212 { 213 215 int[] arrMappedData = null; 216 217 if ((strColumnIDs != null) && (strColumnIDs.length() > 0) 218 && !(strColumnIDs.equals("-1"))) 219 { 220 DatabaseReadOperation dbop = new DatabaseReadMultipleOperation( 221 this, m_schema.geSelectMappingMultipleRecords(strColumnIDs), m_schema) 222 { 223 protected Object performOperation( 224 DatabaseFactoryImpl dbfactory, 225 Connection cntConnection, 226 PreparedStatement pstmQuery 227 ) throws OSSException, 228 SQLException 229 { 230 pstmQuery.setInt(1, iMappingType); 231 return DatabaseUtils.loadMultipleIntsAsArray(pstmQuery); 232 } 233 }; 234 arrMappedData = (int[])dbop.executeRead(); 235 } 236 237 return arrMappedData; 238 } 239 240 243 public DataObject load( 244 ResultSet rsQueryResults, 245 int initialIndex 246 ) throws OSSDatabaseAccessException 247 { 248 MappedData data; 249 250 try 251 { 252 data = new MappedData(rsQueryResults.getInt(initialIndex), 254 rsQueryResults.getInt(initialIndex + 1), 255 rsQueryResults.getInt(initialIndex + 2), 256 rsQueryResults.getInt(initialIndex + 3), 257 rsQueryResults.getString(initialIndex + 4), 258 rsQueryResults.getTimestamp(initialIndex + 5), 259 rsQueryResults.getTimestamp(initialIndex + 6)); 260 data.setFromPersistenceStore(); 261 } 262 catch (SQLException sqleExc) 263 { 264 throw new OSSDatabaseAccessException("Failed to load data from the database.", 265 sqleExc); 266 } 267 268 return data; 269 } 270 271 274 public DataObject get( 275 final int iId, 276 final int iDomainId 277 ) throws OSSException 278 { 279 DataObject data = null; 280 281 if (iId == DataObject.NEW_ID) 284 { 285 data = new MappedData(); 287 } 288 else 289 { 290 DatabaseReadOperation dbop = new DatabaseReadSingleDataObjectOperation( 291 this, 292 m_schema.getSelectMappedDataById(MappedData.ALL_MAPPEDDATA_COLUMNS), 293 m_schema, iId, iDomainId); 294 data = (DataObject)dbop.executeRead(); 295 } 296 297 return data; 298 } 299 300 303 public DataObject create( 304 final DataObject data 305 ) throws OSSException 306 { 307 if (GlobalConstants.ERROR_CHECKING) 308 { 309 assert data.getId() == DataObject.NEW_ID 310 : "Cannot create already created data."; 311 } 312 313 DatabaseUpdateOperation dbop = new DatabaseUpdateOperation( 314 this, DatabaseUpdateOperation.DBOP_INSERT) 315 { 316 protected void performOperation( 317 DatabaseFactoryImpl dbfactory, 318 Connection cntConnection, 319 PreparedStatement pstmQuery 320 ) throws OSSException, 321 SQLException 322 { 323 setReturnData(m_schema.insertMappedData(cntConnection, (MappedData)data)); 324 } 325 }; 326 dbop.executeUpdate(); 327 328 return (DataObject)dbop.getReturnData(); 329 } 330 331 334 public int create( 335 Collection colDataObject 336 ) throws OSSException 337 { 338 if (GlobalConstants.ERROR_CHECKING) 339 { 340 assert colDataObject != null && !colDataObject.isEmpty() 341 : "Cannot create empty data list."; 342 } 343 344 DatabaseUpdateOperation dbop = new DatabaseCreateMultipleDataObjectsOperation( 345 this, m_schema.getInsertMappingRecord(), m_schema, colDataObject, false); 346 dbop.executeUpdate(); 347 348 return ((Integer )dbop.getReturnData()).intValue(); 349 } 350 351 354 public void delete( 355 final int iId, 356 final int iDomainId 357 ) throws OSSException 358 { 359 if (GlobalConstants.ERROR_CHECKING) 360 { 361 assert iId != DataObject.NEW_ID 362 : "Cannot delete data, which wasn't created yet."; 363 } 364 365 DatabaseUpdateOperation dbop = new DatabaseDeleteSingleDataObjectOperation( 366 this, m_schema.getDeleteMappedDataById(), m_schema, iId, iDomainId); 367 dbop.executeUpdate(); 368 } 369 370 372 375 public ModifiableDataObject save( 376 final ModifiableDataObject data 377 ) throws OSSException 378 { 379 if (GlobalConstants.ERROR_CHECKING) 380 { 381 assert data != null 382 : "Data can not be null"; 383 assert data.getId() != DataObject.NEW_ID 384 : "Cannot save data which wasn't created yet."; 385 } 386 387 DatabaseUpdateOperation dbop = new DatabaseUpdateOperation( 388 this, DatabaseUpdateOperation.DBOP_UPDATE) 389 { 390 protected void performOperation( 391 DatabaseFactoryImpl dbfactory, 392 Connection cntConnection, 393 PreparedStatement pstmQuery 394 ) throws OSSException, 395 SQLException 396 { 397 setReturnData(m_schema.updateMappedData(cntConnection, (MappedData)data)); 398 } 399 }; 400 dbop.executeUpdate(); 401 402 return (ModifiableDataObject)dbop.getReturnData(); 403 } 404 } 405 | Popular Tags |