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.Blog; 32 import org.opensubsystems.blog.persist.BlogFactory; 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 BlogDatabaseFactory extends DatabaseFactoryImpl 62 implements BlogFactory, 63 ModifiableDatabaseFactory 64 { 65 67 70 protected BlogDatabaseSchema m_schema; 71 72 74 79 public BlogDatabaseFactory( 80 ) throws OSSException 81 { 82 super(DataConstant.BLOG_DATA_TYPE); 83 84 m_schema = ((BlogDatabaseSchema)DatabaseSchemaManager.getInstance( 85 BlogDatabaseSchema.class)); 86 } 87 88 90 93 public DataObject load( 94 ResultSet rsQueryResults, 95 int initialIndex 96 ) throws OSSDatabaseAccessException 97 { 98 Blog data; 99 100 try 101 { 102 data = new Blog(rsQueryResults.getInt(initialIndex), 104 rsQueryResults.getInt(initialIndex + 1), 105 rsQueryResults.getString(initialIndex + 2), 106 rsQueryResults.getString(initialIndex + 3), 107 rsQueryResults.getString(initialIndex + 4), 108 rsQueryResults.getTimestamp(initialIndex + 5), 109 rsQueryResults.getTimestamp(initialIndex + 6)); 110 data.setFromPersistenceStore(); 111 } 112 catch (SQLException sqleExc) 113 { 114 throw new OSSDatabaseAccessException("Failed to load data from the database.", 115 sqleExc); 116 } 117 118 return data; 119 } 120 121 124 public int setValuesForInsert( 125 PreparedStatement insertStatement, 126 DataObject data, 127 int iIndex 128 ) throws OSSException, 129 SQLException 130 { 131 Blog blog = (Blog)data; 132 133 137 insertStatement.setInt(iIndex++, blog.getDomainId()); 138 insertStatement.setString(iIndex++, blog.getFolder()); 139 insertStatement.setString(iIndex++, blog.getCaption()); 140 insertStatement.setString(iIndex++, blog.getComments()); 141 142 return iIndex; 143 } 144 145 148 public int setValuesForUpdate( 149 PreparedStatement updateStatement, 150 DataObject data, 151 int iIndex 152 ) throws OSSException, 153 SQLException 154 { 155 Blog blog = (Blog)data; 156 157 updateStatement.setString(iIndex++, blog.getFolder()); 158 updateStatement.setString(iIndex++, blog.getCaption()); 159 updateStatement.setString(iIndex++, blog.getComments()); 160 updateStatement.setInt(iIndex++, blog.getId()); 161 updateStatement.setInt(iIndex++, blog.getDomainId()); 166 updateStatement.setTimestamp(iIndex++, blog.getModificationTimestamp()); 167 168 return iIndex; 169 } 170 171 174 public DataObject get( 175 final int iId, 176 final int iDomainId 177 ) throws OSSException 178 { 179 DataObject data = null; 180 181 if (iId == DataObject.NEW_ID) 184 { 185 data = new Blog(iDomainId); 187 } 188 else 189 { 190 DatabaseReadOperation dbop = new DatabaseReadSingleDataObjectOperation( 191 this, m_schema.getSelectBlogById(BlogDatabaseSchema.BLOG_COLUMNS), 192 m_schema, iId, iDomainId); 193 data = (DataObject)dbop.executeRead(); 194 } 195 196 return data; 197 } 198 199 202 public DataObject create( 203 final DataObject data 204 ) throws OSSException 205 { 206 if (GlobalConstants.ERROR_CHECKING) 207 { 208 assert data.getId() == DataObject.NEW_ID 209 : "Cannot create already created data."; 210 } 211 DatabaseUpdateOperation dbop = new DatabaseCreateSingleDataObjectOperation( 212 this, m_schema.getInsertBlogAndFetchGeneratedValues(), m_schema, data); 213 dbop.executeUpdate(); 214 215 return (DataObject)dbop.getReturnData(); 216 } 217 218 221 public int create( 222 final Collection colDataObject 223 ) throws OSSException 224 { 225 if (GlobalConstants.ERROR_CHECKING) 226 { 227 assert colDataObject != null && !colDataObject.isEmpty() 228 : "Cannot create empty data list."; 229 } 230 231 DatabaseUpdateOperation dbop = new DatabaseCreateMultipleDataObjectsOperation( 232 this, m_schema.getInsertBlog(), m_schema, colDataObject, false); 233 dbop.executeUpdate(); 234 235 return ((Integer )dbop.getReturnData()).intValue(); 236 } 237 238 241 public void delete( 242 final int iId, 243 final int iDomainId 244 ) throws OSSException 245 { 246 if (GlobalConstants.ERROR_CHECKING) 247 { 248 assert iId != DataObject.NEW_ID 249 : "Cannot delete data, which wasn't created yet."; 250 } 251 252 DatabaseUpdateOperation dbop = new DatabaseDeleteSingleDataObjectOperation( 253 this, m_schema.getDeleteBlogById(), m_schema, iId, iDomainId); 254 dbop.executeUpdate(); 255 } 256 257 259 262 public ModifiableDataObject save( 263 final ModifiableDataObject data 264 ) throws OSSException 265 { 266 if (GlobalConstants.ERROR_CHECKING) 267 { 268 assert data != null 269 : "Data can not be null"; 270 assert data.getId() != DataObject.NEW_ID 271 : "Cannot save data which wasn't created yet."; 272 } 273 274 DatabaseUpdateOperation dbop = new DatabaseUpdateSingleDataObjectOperation( 275 this, m_schema.getUpdateBlogAndFetchGeneratedValues(), m_schema, data); 276 dbop.executeUpdate(); 277 278 return (ModifiableDataObject)dbop.getReturnData(); 279 } 280 281 283 286 public Blog get( 287 final String strFolder 288 ) throws OSSException 289 { 290 if (GlobalConstants.ERROR_CHECKING) 291 { 292 assert strFolder != null : "Blog folder cannot be null"; 293 } 294 295 DatabaseReadOperation dbop = new DatabaseReadOperation( 296 this, m_schema.getSelectBlogByFolder(BlogDatabaseSchema.BLOG_COLUMNS), m_schema) 297 { 298 protected Object performOperation( 299 DatabaseFactoryImpl dbfactory, 300 Connection cntConnection, 301 PreparedStatement pstmQuery 302 ) throws OSSException, 303 SQLException 304 { 305 int iDomainId = CallContext.getInstance().getCurrentDomainId(); 306 pstmQuery.setString(1, strFolder); 307 pstmQuery.setInt(2, iDomainId); 308 return DatabaseUtils.loadAtMostOneData(dbfactory, pstmQuery, 309 "Multiple records loaded from database for domain ID " 310 + iDomainId + " and folder " + strFolder); 311 } 312 }; 313 return (Blog)dbop.executeRead(); 314 } 315 316 319 public List getAll( 320 ) throws OSSException 321 { 322 DatabaseReadOperation dbop = new DatabaseReadMultipleOperation( 323 this, m_schema.getSelectAllBlogs(BlogDatabaseSchema.BLOG_COLUMNS), m_schema) 324 { 325 protected Object performOperation( 326 DatabaseFactoryImpl dbfactory, 327 Connection cntConnection, 328 PreparedStatement pstmQuery 329 ) throws OSSException, 330 SQLException 331 { 332 int iDomainId = CallContext.getInstance().getCurrentDomainId(); 333 pstmQuery.setInt(1, iDomainId); 334 return DatabaseUtils.loadMultipleData(dbfactory, pstmQuery); 335 } 336 }; 337 return (List )dbop.executeRead(); 338 } 339 } 340 | Popular Tags |