| 1 21 22 package org.opensubsystems.blog.persist.db; 23 24 import java.sql.Connection ; 25 import java.sql.SQLException ; 26 import java.util.HashMap ; 27 import java.util.Map ; 28 29 import org.opensubsystems.blog.data.Blog; 30 import org.opensubsystems.blog.data.Entry; 31 import org.opensubsystems.core.data.DataConstant; 32 import org.opensubsystems.core.error.OSSException; 33 import org.opensubsystems.core.error.OSSInvalidContextException; 34 import org.opensubsystems.core.error.OSSInvalidDataException; 35 import org.opensubsystems.core.persist.db.DatabaseImpl; 36 import org.opensubsystems.core.persist.db.ModifiableDatabaseSchemaImpl; 37 import org.opensubsystems.core.util.Messages; 38 39 47 public abstract class BlogDatabaseSchema extends ModifiableDatabaseSchemaImpl 48 { 49 51 54 public static final String BLOG_SCHEMA_NAME = "BLOG"; 55 56 59 public static final int BLOG_SCHEMA_VERSION = 1; 60 61 63 66 public static final String BLOG_TABLE_NAME = "BF_BLOG"; 67 68 71 public static final String BLOG_COLUMNS = "ID, DOMAIN_ID, FOLDER, CAPTION," + 72 " COMMENTS, CREATION_DATE," + 73 " MODIFICATION_DATE"; 74 75 78 public static final int BLOG_FOLDER_MAXLENGTH = 50; 79 80 83 public static final int BLOG_CAPTION_MAXLENGTH = 1024; 84 85 88 public static final int BLOG_COMMENTS_MAXLENGTH = 4000; 89 90 92 95 public static final String BLOGENTRY_TABLE_NAME = "BF_BLOG_ENTRY"; 96 97 100 public static final String ENTRY_COLUMNS = "ID, DOMAIN_ID, BLOG_ID, CAPTION," + 101 " COMMENTS, IMAGEURL, TARGETURL," + 102 " CREATION_DATE, MODIFICATION_DATE"; 103 106 public static final int BLOGENTRY_CAPTION_MAXLENGTH = 1024; 107 108 111 public static final int BLOGENTRY_COMMENTS_MAXLENGTH = 4000; 112 113 116 public static final int BLOGENTRY_IMAGEURL_MAXLENGTH = 1024; 117 118 121 public static final int BLOGENTRY_TARGETURL_MAXLENGTH = 1024; 122 123 125 128 public static final Map TABLE_NAMES; 129 130 132 135 static 136 { 137 TABLE_NAMES = new HashMap (); 140 TABLE_NAMES.put(DataConstant.BLOG_DATA_TYPE_OBJ, "BF_BLOG"); 141 TABLE_NAMES.put(DataConstant.BLOGENTRY_DATA_TYPE_OBJ, "BF_BLOG_ENTRY"); 142 143 Blog.setFolderMaxLength(BLOG_FOLDER_MAXLENGTH); 145 Blog.setCaptionMaxLength(BLOG_CAPTION_MAXLENGTH); 146 Blog.setCommentsMaxLength(BLOG_COMMENTS_MAXLENGTH); 147 Entry.setCaptionMaxLength(BLOGENTRY_CAPTION_MAXLENGTH); 148 Entry.setCommentsMaxLength(BLOGENTRY_COMMENTS_MAXLENGTH); 149 Entry.setImageURLMaxLength(BLOGENTRY_IMAGEURL_MAXLENGTH); 150 Entry.setTargetURLMaxLength(BLOGENTRY_TARGETURL_MAXLENGTH); 151 } 152 153 158 public BlogDatabaseSchema( 159 ) throws OSSException 160 { 161 super(null, BLOG_SCHEMA_NAME, BLOG_SCHEMA_VERSION, true, TABLE_NAMES); 162 } 163 164 166 173 public String getSelectBlogById( 174 String strColumns 175 ) throws OSSException 176 { 177 StringBuffer buffer = new StringBuffer (); 178 179 buffer.append("select "); 180 buffer.append(strColumns); 181 buffer.append(" from BF_BLOG where ID = ? and DOMAIN_ID = ?"); 182 183 return buffer.toString(); 184 } 185 186 193 public String getSelectBlogByFolder( 194 String strColumns 195 ) throws OSSException 196 { 197 StringBuffer buffer = new StringBuffer (); 198 199 buffer.append("select "); 200 buffer.append(strColumns); 201 buffer.append(" from BF_BLOG where FOLDER = ? and DOMAIN_ID = ?"); 202 203 return buffer.toString(); 204 } 205 206 213 public String getSelectAllBlogs( 214 String strColumns 215 ) throws OSSException 216 { 217 StringBuffer buffer = new StringBuffer (); 218 219 buffer.append("select "); 220 buffer.append(strColumns); 221 buffer.append(" from BF_BLOG where DOMAIN_ID = ? order by CREATION_DATE"); 222 223 return buffer.toString(); 224 } 225 226 233 public abstract String getInsertBlogAndFetchGeneratedValues( 234 ) throws OSSException; 235 236 243 public abstract String getUpdateBlogAndFetchGeneratedValues( 244 ) throws OSSException; 245 246 253 public String getInsertBlog( 254 ) throws OSSException 255 { 256 StringBuffer buffer = new StringBuffer (); 257 258 buffer.append("insert into BF_BLOG(DOMAIN_ID, FOLDER, CAPTION, COMMENTS," + 261 " CREATION_DATE, MODIFICATION_DATE)" + 262 " values (?, ?, ?, ?, "); 263 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 264 buffer.append(","); 265 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 266 buffer.append(")"); 267 268 return buffer.toString(); 269 } 270 271 276 public String getDeleteBlogById( 277 ) 278 { 279 return "delete from BF_BLOG where ID = ? and DOMAIN_ID = ?"; 280 } 281 282 289 public String getSelectEntryById( 290 String strColumns 291 ) throws OSSException 292 { 293 StringBuffer buffer = new StringBuffer (); 294 295 buffer.append("select "); 296 buffer.append(strColumns); 297 buffer.append(" from BF_BLOG_ENTRY where ID = ? and DOMAIN_ID = ?"); 298 299 return buffer.toString(); 300 } 301 302 309 public String getSelectAllEntries( 310 String strColumns 311 ) throws OSSException 312 { 313 StringBuffer buffer = new StringBuffer (); 314 315 buffer.append("select "); 316 buffer.append(strColumns); 317 buffer.append(" from BF_BLOG_ENTRY where BLOG_ID = ? and DOMAIN_ID = ?" + 318 " order by CREATION_DATE desc"); 319 320 return buffer.toString(); 321 322 } 323 324 331 public String getSelectLastEntry( 332 String strColumns 333 ) throws OSSException 334 { 335 StringBuffer buffer = new StringBuffer (); 336 337 buffer.append("select "); 338 buffer.append(strColumns); 339 buffer.append(" from BF_BLOG_ENTRY where ID = (select max(ID)" + 341 " from BF_BLOG_ENTRY where BLOG_ID = ? and DOMAIN_ID = ?)"); 342 343 return buffer.toString(); 344 345 } 346 347 354 public abstract String getInsertEntryAndFetchGeneratedValues( 355 ) throws OSSException; 356 357 364 public abstract String getUpdateEntryAndFetchGeneratedValues( 365 ) throws OSSException; 366 367 374 public String getInsertEntry( 375 ) throws OSSException 376 { 377 StringBuffer buffer = new StringBuffer (); 378 379 buffer.append("insert into BF_BLOG_ENTRY(DOMAIN_ID, BLOG_ID, CAPTION," + 382 " COMMENTS, IMAGEURL, TARGETURL, CREATION_DATE, MODIFICATION_DATE)" + 383 " values (?, ?, ?, ?, ?, ?, "); 384 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 385 buffer.append(","); 386 buffer.append(DatabaseImpl.getInstance().getCurrentTimestampFunctionCall()); 387 buffer.append(")"); 388 389 return buffer.toString(); 390 } 391 392 397 public String getDeleteEntryById( 398 ) 399 { 400 return "delete from BF_BLOG_ENTRY where ID = ? and DOMAIN_ID = ?"; 401 } 402 403 406 public void handleSQLException( 407 SQLException exc, 408 Connection dbConnection, 409 int iOperationType, 410 int iDataType, 411 Object data 412 ) throws OSSException 413 { 414 OSSInvalidDataException ideException = null; 415 416 switch(iOperationType) 417 { 418 case DBOP_INSERT: 419 { 420 if (iDataType == DataConstant.BLOG_DATA_TYPE) 421 { 422 if ((exc.getMessage().toUpperCase().indexOf("BF_BLOG_FLDR_UQ") > -1) 423 || ((exc.getMessage().toUpperCase()).endsWith("KEY 2")) 425 || ((exc.getMessage().toUpperCase()).indexOf("\"2\"") > -1) 427 ) 428 { 429 ideException = OSSInvalidDataException.addException(ideException, 430 Messages.NONSPECIFIC_ERRORS, 431 "Folder has to be unique.", 432 exc); 433 } 434 } 435 else if (iDataType == DataConstant.BLOGENTRY_DATA_TYPE) 436 { 437 if (exc.getMessage().toUpperCase().indexOf("BF_BLOGENTR_FK") > -1) 438 { 439 throw new OSSInvalidContextException( 440 "Blog to create entry in does not exist.", 441 exc); 442 } 443 } 444 break; 445 } 446 case DBOP_UPDATE: 447 { 448 if (iDataType == DataConstant.BLOG_DATA_TYPE) 449 { 450 if ((exc.getMessage().toUpperCase().indexOf("BF_BLOG_FLDR_UQ") > -1) 451 || ((exc.getMessage().toUpperCase()).endsWith("KEY 2")) 453 || ((exc.getMessage().toUpperCase()).indexOf("\"2\"") > -1) 455 ) 456 { 457 ideException = OSSInvalidDataException.addException(ideException, 458 Messages.NONSPECIFIC_ERRORS, 459 "Folder has to be unique.", 460 exc); 461 } 462 } 463 else if (iDataType == DataConstant.BLOGENTRY_DATA_TYPE) 464 { 465 } 467 break; 468 } 469 default: 470 { 471 } 473 } 474 if (ideException != null) 475 { 476 throw ideException; 478 } 479 else 480 { 481 super.handleSQLException(exc, dbConnection, iOperationType, iDataType, data); 483 } 484 } 485 } 486 | Popular Tags |