1 21 22 package org.opensubsystems.blog.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.util.Collection ; 29 import java.util.List ; 30 31 import org.opensubsystems.blog.data.Entry; 32 import org.opensubsystems.blog.persist.EntryFactory; 33 import org.opensubsystems.core.data.DataConstant; 34 import org.opensubsystems.core.data.DataObject; 35 import org.opensubsystems.core.data.ModifiableDataObject; 36 import org.opensubsystems.core.error.OSSDatabaseAccessException; 37 import org.opensubsystems.core.error.OSSException; 38 import org.opensubsystems.core.persist.db.DatabaseCreateMultipleDataObjectsOperation; 39 import org.opensubsystems.core.persist.db.DatabaseCreateSingleDataObjectOperation; 40 import org.opensubsystems.core.persist.db.DatabaseDeleteSingleDataObjectOperation; 41 import org.opensubsystems.core.persist.db.DatabaseFactoryImpl; 42 import org.opensubsystems.core.persist.db.DatabaseReadMultipleOperation; 43 import org.opensubsystems.core.persist.db.DatabaseReadOperation; 44 import org.opensubsystems.core.persist.db.DatabaseReadSingleDataObjectOperation; 45 import org.opensubsystems.core.persist.db.DatabaseSchemaManager; 46 import org.opensubsystems.core.persist.db.DatabaseUpdateOperation; 47 import org.opensubsystems.core.persist.db.DatabaseUpdateSingleDataObjectOperation; 48 import org.opensubsystems.core.persist.db.ModifiableDatabaseFactory; 49 import org.opensubsystems.core.util.CallContext; 50 import org.opensubsystems.core.util.DatabaseUtils; 51 import org.opensubsystems.core.util.GlobalConstants; 52 53 61 public class EntryDatabaseFactory extends DatabaseFactoryImpl 62 implements EntryFactory, 63 ModifiableDatabaseFactory 64 { 65 67 70 protected BlogDatabaseSchema m_schema; 71 72 74 79 public EntryDatabaseFactory( 80 ) throws OSSException 81 { 82 super(DataConstant.BLOGENTRY_DATA_TYPE); 83 84 m_schema = ((BlogDatabaseSchema)DatabaseSchemaManager.getInstance( 85 BlogDatabaseSchema.class)); 86 } 87 88 91 public int setValuesForInsert( 92 PreparedStatement insertStatement, 93 DataObject data, 94 int iIndex 95 ) throws OSSException, 96 SQLException 97 { 98 Entry entry = (Entry)data; 99 100 104 insertStatement.setInt(iIndex++, entry.getDomainId()); 105 insertStatement.setInt(iIndex++, entry.getParentId()); 106 insertStatement.setString(iIndex++, entry.getCaption()); 107 insertStatement.setString(iIndex++, entry.getComments()); 108 insertStatement.setString(iIndex++, entry.getImageURL()); 109 insertStatement.setString(iIndex++, entry.getTargetURL()); 110 111 return iIndex; 112 } 113 114 117 public int setValuesForUpdate( 118 PreparedStatement updateStatement, 119 DataObject data, 120 int iIndex 121 ) throws OSSException, 122 SQLException 123 { 124 Entry entry = (Entry)data; 125 126 updateStatement.setString(iIndex++, entry.getCaption()); 127 updateStatement.setString(iIndex++, entry.getComments()); 128 updateStatement.setString(iIndex++, entry.getImageURL()); 129 updateStatement.setString(iIndex++, entry.getTargetURL()); 130 updateStatement.setInt(iIndex++, entry.getId()); 131 updateStatement.setInt(iIndex++, entry.getDomainId()); 136 updateStatement.setTimestamp(iIndex++, entry.getModificationTimestamp()); 137 138 return iIndex; 139 } 140 141 143 146 public DataObject load( 147 ResultSet rsQueryResults, 148 int initialIndex 149 ) throws OSSDatabaseAccessException 150 { 151 Entry data; 152 153 try 154 { 155 data = new Entry(rsQueryResults.getInt(initialIndex), 157 rsQueryResults.getInt(initialIndex + 1), 158 rsQueryResults.getInt(initialIndex + 2), 159 rsQueryResults.getString(initialIndex + 3), 160 rsQueryResults.getString(initialIndex + 4), 161 rsQueryResults.getString(initialIndex + 5), 162 rsQueryResults.getString(initialIndex + 6), 163 rsQueryResults.getTimestamp(initialIndex + 7), 164 rsQueryResults.getTimestamp(initialIndex + 8)); 165 data.setFromPersistenceStore(); 166 } 167 catch (SQLException sqleExc) 168 { 169 throw new OSSDatabaseAccessException("Failed to load data from the database.", 170 sqleExc); 171 } 172 173 return data; 174 } 175 176 179 public DataObject get( 180 final int iId, 181 final int iDomainId 182 ) throws OSSException 183 { 184 DataObject data = null; 185 186 if (iId == DataObject.NEW_ID) 189 { 190 data = new Entry(iDomainId); 192 } 193 else 194 { 195 DatabaseReadOperation dbop = new DatabaseReadSingleDataObjectOperation( 196 this, m_schema.getSelectEntryById(BlogDatabaseSchema.ENTRY_COLUMNS), 197 m_schema, iId, iDomainId); 198 data = (DataObject)dbop.executeRead(); 199 } 200 201 return data; 202 } 203 204 207 public DataObject create( 208 final DataObject data 209 ) throws OSSException 210 { 211 if (GlobalConstants.ERROR_CHECKING) 212 { 213 assert data.getId() == DataObject.NEW_ID 214 : "Cannot create already created data."; 215 } 216 217 DatabaseUpdateOperation dbop = new DatabaseCreateSingleDataObjectOperation( 218 this, m_schema.getInsertEntryAndFetchGeneratedValues(), m_schema, data); 219 dbop.executeUpdate(); 220 221 return (DataObject)dbop.getReturnData(); 222 } 223 224 227 public int create( 228 Collection colDataObject 229 ) throws OSSException 230 { 231 if (GlobalConstants.ERROR_CHECKING) 232 { 233 assert colDataObject != null && !colDataObject.isEmpty() 234 : "Cannot create empty data list."; 235 } 236 237 DatabaseUpdateOperation dbop = new DatabaseCreateMultipleDataObjectsOperation( 238 this, m_schema.getInsertEntry(), m_schema, colDataObject, false); 239 dbop.executeUpdate(); 240 241 return ((Integer )dbop.getReturnData()).intValue(); 242 } 243 244 247 public void delete( 248 final int iId, 249 final int iDomainId 250 ) throws OSSException 251 { 252 if (GlobalConstants.ERROR_CHECKING) 253 { 254 assert iId != DataObject.NEW_ID 255 : "Cannot delete data, which wasn't created yet."; 256 } 257 258 DatabaseUpdateOperation dbop = new DatabaseDeleteSingleDataObjectOperation( 259 this, m_schema.getDeleteEntryById(), m_schema, iId, iDomainId); 260 dbop.executeUpdate(); 261 } 262 263 265 268 public ModifiableDataObject save( 269 final ModifiableDataObject data 270 ) throws OSSException 271 { 272 if (GlobalConstants.ERROR_CHECKING) 273 { 274 assert data != null 275 : "Data can not be null"; 276 assert data.getId() != DataObject.NEW_ID 277 : "Cannot save data which wasn't created yet."; 278 } 279 280 DatabaseUpdateOperation dbop = new DatabaseUpdateSingleDataObjectOperation( 281 this, m_schema.getUpdateEntryAndFetchGeneratedValues(), m_schema, data); 282 dbop.executeUpdate(); 283 284 return (ModifiableDataObject)dbop.getReturnData(); 285 } 286 287 289 290 293 public List getAll( 294 final int iBlogId 295 ) throws OSSException 296 { 297 DatabaseReadOperation dbop = new DatabaseReadMultipleOperation( 298 this, m_schema.getSelectAllEntries(BlogDatabaseSchema.ENTRY_COLUMNS), m_schema) 299 { 300 protected Object performOperation( 301 DatabaseFactoryImpl dbfactory, 302 Connection cntConnection, 303 PreparedStatement pstmQuery 304 ) throws OSSException, 305 SQLException 306 { 307 int iDomainId = CallContext.getInstance().getCurrentDomainId(); 308 pstmQuery.setInt(1, iBlogId); 309 pstmQuery.setInt(2, iDomainId); 310 return DatabaseUtils.loadMultipleData(dbfactory, pstmQuery); 311 } 312 }; 313 return (List )dbop.executeRead(); 314 } 315 316 319 public Entry getLast( 320 final int iBlogId 321 ) throws OSSException 322 { 323 DatabaseReadOperation dbop = new DatabaseReadOperation( 324 this, m_schema.getSelectLastEntry(BlogDatabaseSchema.ENTRY_COLUMNS), m_schema) 325 { 326 protected Object performOperation( 327 DatabaseFactoryImpl dbfactory, 328 Connection cntConnection, 329 PreparedStatement pstmQuery 330 ) throws OSSException, 331 SQLException 332 { 333 int iDomainId = CallContext.getInstance().getCurrentDomainId(); 334 pstmQuery.setInt(1, iBlogId); 335 pstmQuery.setInt(2, iDomainId); 336 return DatabaseUtils.loadAtMostOneData(dbfactory, pstmQuery, 337 "Multiple records loaded from database for domain Id " 338 + iDomainId + " and blog Id " + iBlogId); 339 } 340 }; 341 return (Entry)dbop.executeRead(); 342 } 343 } 344 | Popular Tags |