1 21 package net.mlw.vlh.adapter.hibernate3; 22 23 import java.lang.reflect.InvocationTargetException ; 24 import java.text.ParseException ; 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 import net.mlw.vlh.DefaultListBackedValueList; 29 import net.mlw.vlh.ValueList; 30 import net.mlw.vlh.ValueListInfo; 31 import net.mlw.vlh.adapter.AbstractValueListAdapter; 32 import net.mlw.vlh.adapter.hibernate3.util.ScrollableResultsDecorator; 33 import net.mlw.vlh.adapter.hibernate3.util.StatementBuilder; 34 import net.mlw.vlh.adapter.util.ObjectValidator; 35 36 import org.apache.commons.beanutils.PropertyUtils; 37 import org.apache.commons.logging.Log; 38 import org.apache.commons.logging.LogFactory; 39 import org.hibernate.HibernateException; 40 import org.hibernate.Query; 41 import org.hibernate.ScrollableResults; 42 import org.hibernate.Session; 43 import org.hibernate.SessionFactory; 44 import org.springframework.orm.hibernate3.SessionFactoryUtils; 45 46 69 public class HibernateAdapter extends AbstractValueListAdapter 70 { 71 72 private SessionFactory sessionFactory; 73 74 85 private ObjectValidator _validator = null; 86 87 88 private static final Log LOGGER = LogFactory.getLog(HibernateAdapter.class); 89 90 91 private boolean allowCreate = true; 92 93 94 private String hql; 95 96 private String namedQuery; 97 98 101 private long maxRowsForFocus = Long.MAX_VALUE; 102 103 108 private String defaultFocusPropertyObjectAlias = ""; 109 110 115 private boolean _isRemoveEmptyStrings = false; 116 117 private StatementBuilder statementBuilder; 118 119 122 private boolean _focusOptimalization = true; 123 124 127 public boolean isFocusOptimalization() 128 { 129 return _focusOptimalization; 130 } 131 132 136 public void setFocusOptimalization(boolean focusOptimalization) 137 { 138 _focusOptimalization = focusOptimalization; 139 } 140 141 153 public void setValidator(ObjectValidator validator) 154 { 155 _validator = validator; 156 } 157 158 161 public boolean isRemoveEmptyStrings() 162 { 163 return _isRemoveEmptyStrings; 164 } 165 166 173 public void setRemoveEmptyStrings(boolean isPrefilterEmpty) 174 { 175 _isRemoveEmptyStrings = isPrefilterEmpty; 176 } 177 178 182 public ValueList getValueList(String name, ValueListInfo info) 183 { 184 185 LOGGER.debug("getValueList(String, ValueListInfo) - start"); 186 187 if (info.getSortingColumn() == null) 188 { 189 info.setPrimarySortColumn(getDefaultSortColumn()); 190 info.setPrimarySortDirection(getDefaultSortDirectionInteger()); 191 if (LOGGER.isDebugEnabled()) 192 { 193 LOGGER.debug("The default sort column '" + getDefaultSortColumn() + "' with direction '" + getDefaultSortDirectionInteger() 194 + "' was set."); 195 } 196 } 197 198 int numberPerPage = info.getPagingNumberPer(); 199 200 if (numberPerPage == Integer.MAX_VALUE) 201 { 202 numberPerPage = getDefaultNumberPerPage(); 203 info.setPagingNumberPer(numberPerPage); 204 if (LOGGER.isDebugEnabled()) 205 { 206 LOGGER.debug("The paging number per page '" + numberPerPage + "' was set."); 207 } 208 } 209 210 Session session = SessionFactoryUtils.getSession(getSessionFactory(), allowCreate); 211 try 212 { 213 Query query; 214 215 boolean doFocus = ((getAdapterType() & DO_FOCUS) == 0) && info.isFocusEnabled() && info.isDoFocus() && (namedQuery == null); 216 217 if (doFocus) 218 { 219 if (LOGGER.isDebugEnabled()) 220 { 221 LOGGER.debug("Start to focusing adapterName '" + name + "', ValueListInfo info = " + info + "'"); 222 } 223 ScrollableResults results = getScrollableResults(getQueryForFocus(info, session), info); 224 results.beforeFirst(); 225 doFocusFor(info, results); 226 227 if (LOGGER.isDebugEnabled()) 228 { 229 LOGGER.debug("Focusing finished for adapterName '" + name + "', ValueListInfo info '" + info + "'"); 230 } 231 } 232 233 query = getQuery(info, session); 234 235 boolean doPaging = ((getAdapterType() & DO_PAGE) == 0); 236 237 List list; 238 239 if (doPaging) 240 { 241 if (LOGGER.isDebugEnabled()) 242 { 243 LOGGER.debug("getValueList(String adapterName = " + name + ", ValueListInfo info = " + info 244 + ") - Start to paging result set"); 245 } 246 247 list = new ArrayList (numberPerPage); 248 ScrollableResults results = getScrollableResults(query, info); 249 250 results.last(); 251 int lastRowNumber = results.getRowNumber(); 252 info.setTotalNumberOfEntries(lastRowNumber + 1); 253 254 if (numberPerPage == 0) 255 { 256 numberPerPage = getDefaultNumberPerPage(); 257 } 258 259 int pageNumber = info.getPagingPage(); 260 boolean isResult; 261 if (pageNumber > 1) 262 { 263 if ((pageNumber - 1) * numberPerPage > lastRowNumber) 264 { 265 pageNumber = (lastRowNumber / numberPerPage) + 1; 266 info.setPagingPage(pageNumber); 267 } 268 } 269 if (pageNumber > 1) 270 { 271 isResult = results.scroll((pageNumber - 1) * numberPerPage - lastRowNumber); 272 } 273 else 274 { 275 isResult = results.first(); 276 } 277 278 for (int i = 0; i < numberPerPage && isResult; i++) 279 { 280 list.add(results.get(0)); 281 isResult = results.next(); 282 } 283 284 LOGGER.debug("Sorting finished."); 285 286 } 287 else 288 { 289 290 LOGGER.debug("Retrieving a list directly from the query."); 291 292 list = query.list(); 293 info.setTotalNumberOfEntries(list.size()); 294 } 295 296 ValueList returnValueList = getListBackedValueList(info, list); 297 if (LOGGER.isDebugEnabled()) 298 { 299 LOGGER.debug("Retrieved list was wrapped in valuelist, info=" + info); 300 } 301 return returnValueList; 302 } 303 catch (HibernateException e) 304 { 305 LOGGER.error("Error getting data in adapater '" + name + "' with info = '" + info + "'", e); 306 throw SessionFactoryUtils.convertHibernateAccessException(e); 307 } 308 catch (Exception e) 309 { 310 LOGGER.fatal("Fatal error getting data in adapater '" + name + "' with info = '" + info + "'", e); 311 return null; 312 } 313 finally 314 { 315 SessionFactoryUtils.releaseSession(session, getSessionFactory()); 316 } 317 } 318 319 324 protected ValueList getListBackedValueList(ValueListInfo info, List list) 325 { 326 return new DefaultListBackedValueList(list, info); 327 } 328 329 337 private void doFocusFor(ValueListInfo info, ScrollableResults results) throws HibernateException 338 { 339 info.setFocusStatus(ValueListInfo.FOCUS_NOT_FOUND); 340 341 int currentRow; 342 if (isFocusOptimalization()) 343 { 344 if (LOGGER.isDebugEnabled()) 345 { 346 LOGGER.debug("Focusing only property '" + info.getFocusProperty() + "' == '" + info.getFocusValue() + "'."); 347 } 348 for (currentRow = 0; ((results.next()) && (currentRow < maxRowsForFocus)); currentRow++) 349 { 350 String value = results.get(0).toString(); 351 if (value.equalsIgnoreCase(info.getFocusValue())) 352 { 353 if (LOGGER.isInfoEnabled()) 354 { 355 LOGGER.info("Focus property '" + info.getFocusProperty() + "' in row '" + currentRow + "'."); 356 } 357 info.setPagingPageFromRowNumber(results.getRowNumber()); 358 info.setFocusedRowNumberInTable(results.getRowNumber()); 359 info.setFocusStatus(ValueListInfo.FOCUS_FOUND); 360 break; 361 } 362 } 363 } 364 else 365 { 366 if (LOGGER.isDebugEnabled()) 367 { 368 LOGGER.debug("Focusing object with the property '" + info.getFocusProperty() + "' == '" + info.getFocusValue() + "'."); 369 } 370 for (currentRow = 0; ((results.next()) && (currentRow < maxRowsForFocus)); currentRow++) 371 { 372 373 Object value; 374 try 375 { 376 value = PropertyUtils.getProperty(results.get(0), info.getFocusProperty()); 377 } 378 catch (HibernateException e) 379 { 380 LOGGER.error("Error getting focus property '" + info.getFocusProperty() + "'", e); 381 throw e; 382 } 383 catch (Exception e) 384 { 385 LOGGER.warn("Ingoring error while getting focus property '" + info.getFocusProperty() + "'", e); 386 continue; 387 } 388 389 if (value.toString().equalsIgnoreCase(info.getFocusValue())) 390 { 391 if (LOGGER.isInfoEnabled()) 392 { 393 LOGGER.info("Focus object's property '" + info.getFocusProperty() + "' was found in the row '" + currentRow + "'."); 394 } 395 info.setPagingPageFromRowNumber(results.getRowNumber()); 396 info.setFocusedRowNumberInTable(results.getRowNumber()); 397 info.setFocusStatus(ValueListInfo.FOCUS_FOUND); 398 break; 399 } 400 } 401 } 402 if (currentRow == maxRowsForFocus) 403 { 404 if (LOGGER.isInfoEnabled()) 405 { 406 LOGGER.info("Focus for property '" + info.getFocusProperty() + "' exceded maximum rows for focus '" + maxRowsForFocus + "'."); 407 } 408 info.setFocusStatus(ValueListInfo.FOCUS_TOO_MANY_ITEMS); 409 } 410 } 411 412 419 private ScrollableResults getScrollableResults(Query query, ValueListInfo info) throws HibernateException 420 { 421 ScrollableResults results; 422 423 if (_validator == null) 424 { 425 LOGGER.debug("Validator is null, using normal ScrollableResults"); 426 results = query.scroll(); 427 428 } 429 else 430 { 431 LOGGER.info("Using decorator of the ScrollableResults with your validator."); 432 _validator.setValueListInfo(info); 433 results = new ScrollableResultsDecorator(query.scroll(), _validator); 434 } 435 436 return results; 437 } 438 439 445 private Query getQuery(ValueListInfo info, Session session) throws HibernateException, ParseException 446 { 447 448 if (getHql() != null) 449 { 450 return getStatementBuilder().generate(session, new StringBuffer (getHql()), info.getFilters(), _isRemoveEmptyStrings); 451 } 452 else 453 { 454 if (namedQuery != null) 455 { 456 return session.getNamedQuery(getNamedQuery()); 457 } 458 else 459 { 460 throw new HibernateException("Please define any QUERY in value list retrieve adpater!"); 461 } 462 } 463 } 464 465 475 private Query getQueryForFocus(ValueListInfo info, Session session) throws HibernateException, ParseException 476 { 477 if (isFocusOptimalization()) 478 { 479 LOGGER.info("Focus will use optimalizated query."); 480 return getOptimizedQuery(info, session); 481 } 482 else 483 { 484 LOGGER.info("Focus will use normal (full) query."); 485 return getQuery(info, session); 486 } 487 } 488 489 497 private Query getOptimizedQuery(ValueListInfo info, Session session) throws HibernateException, ParseException 498 { 499 if (getHql() != null) 500 { 501 return getStatementBuilder().generateForFocus(session, new StringBuffer (getHql()), info.getFilters(), _isRemoveEmptyStrings, 502 defaultFocusPropertyObjectAlias, info.getFocusProperty()); 503 504 } 505 else 506 { 507 throw new HibernateException( 508 "Please define any HQL QUERY in value list retrieve adpater, function is not implemented for NamedQuery!"); 509 } 510 } 511 512 516 public final void setSessionFactory(SessionFactory sessionFactory) 517 { 518 this.sessionFactory = sessionFactory; 519 } 520 521 525 protected final SessionFactory getSessionFactory() 526 { 527 return this.sessionFactory; 528 } 529 530 534 public void setHsql(String hql) 535 { 536 this.hql = hql; 537 } 538 539 542 public void setHql(String hql) 543 { 544 this.hql = hql; 545 } 546 547 550 public String getNamedQuery() 551 { 552 return namedQuery; 553 } 554 555 563 public void setNamedQuery(String namedQuery) 564 { 565 this.namedQuery = namedQuery; 566 } 567 568 571 public String getHql() 572 { 573 return hql; 574 } 575 576 581 public void setAllowCreate(boolean allowCreate) 582 { 583 this.allowCreate = allowCreate; 584 } 585 586 591 public long getMaxRowsForFocus() 592 { 593 return maxRowsForFocus; 594 } 595 596 602 public void setMaxRowsForFocus(long maxRowsForFocus) 603 { 604 this.maxRowsForFocus = maxRowsForFocus; 605 } 606 607 610 public String getDefaultFocusPropertyObjectAlias() 611 { 612 return defaultFocusPropertyObjectAlias; 613 } 614 615 622 public void setDefaultFocusPropertyObjectAlias(String defaultFocusPropertyObjectAlias) 623 { 624 this.defaultFocusPropertyObjectAlias = defaultFocusPropertyObjectAlias + "."; 625 } 626 627 630 public StatementBuilder getStatementBuilder() 631 { 632 if (statementBuilder == null) 633 { 634 statementBuilder = new StatementBuilder(); 635 } 636 return statementBuilder; 637 } 638 639 642 public void setStatementBuilder(StatementBuilder statementBuilder) 643 { 644 this.statementBuilder = statementBuilder; 645 } 646 } | Popular Tags |