1 16 package com.blandware.atleap.persistence.hibernate.news; 17 18 import com.blandware.atleap.common.Constants; 19 import com.blandware.atleap.common.parsers.exception.PlainTextExtractorException; 20 import com.blandware.atleap.common.parsers.html.HTMLPlainTextExtractor; 21 import com.blandware.atleap.common.util.ConvertUtil; 22 import com.blandware.atleap.common.util.PartialCollection; 23 import com.blandware.atleap.common.util.QueryInfo; 24 import com.blandware.atleap.model.core.BaseObject; 25 import com.blandware.atleap.model.core.ContentField; 26 import com.blandware.atleap.model.core.ContentFieldValue; 27 import com.blandware.atleap.model.core.ContentLocale; 28 import com.blandware.atleap.model.core.ContentResource; 29 import com.blandware.atleap.model.core.Page; 30 import com.blandware.atleap.model.core.Role; 31 import com.blandware.atleap.model.news.NewsItem; 32 import com.blandware.atleap.persistence.exception.DeleteException; 33 import com.blandware.atleap.persistence.hibernate.core.PageDAOHibernate; 34 import com.blandware.atleap.persistence.news.NewsDAO; 35 36 import java.io.ByteArrayInputStream ; 37 import java.io.PrintWriter ; 38 import java.io.StringWriter ; 39 import java.util.ArrayList ; 40 import java.util.Date ; 41 import java.util.HashSet ; 42 import java.util.Iterator ; 43 import java.util.List ; 44 import java.util.ListIterator ; 45 import java.util.Map ; 46 import java.util.Set ; 47 48 import org.hibernate.Hibernate; 49 import org.hibernate.type.Type; 50 51 58 public class NewsDAOHibernate extends PageDAOHibernate implements NewsDAO { 59 60 63 public NewsDAOHibernate() { 64 } 65 66 69 public Long createNewsItem(NewsItem newsItem, Map linkedObjects) { 70 newsItem.setUsageCounter(new Integer (0)); 71 Long newsItemId = (Long ) getHibernateTemplate().save(newsItem); 72 73 ContentField fTitle = new ContentField(); 75 fTitle.setIdentifier("title"); 76 fTitle.setType(ContentField.LINE_TYPE); 77 fTitle.setInternal(Boolean.TRUE); 78 newsItem.addContentField(fTitle); 79 getHibernateTemplate().save(fTitle); 80 81 ContentField fAnnotation = new ContentField(); 83 fAnnotation.setIdentifier("annotation"); 84 fAnnotation.setType(ContentField.HTML_TYPE); 85 fAnnotation.setInternal(Boolean.TRUE); 86 newsItem.addContentField(fAnnotation); 87 getHibernateTemplate().save(fAnnotation); 88 89 ContentField fBody = new ContentField(); 91 fBody.setIdentifier("body"); 92 fBody.setType(ContentField.HTML_TYPE); 93 fBody.setInternal(Boolean.TRUE); 94 newsItem.addContentField(fBody); 95 getHibernateTemplate().save(fBody); 96 97 List contentLocales = executeFind("from ContentLocale l", true, "query.listContentLocales"); 100 HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor(); 101 for ( int i = 0; i < contentLocales.size(); i++ ) { 102 ContentLocale contentLocale = (ContentLocale) contentLocales.get(i); 103 String localeIdentifier = contentLocale.getIdentifier(); 104 105 String valueTitle = (String ) newsItem.getTitle().get(localeIdentifier); 107 String valueAnnotation = (String ) newsItem.getAnnotation().get(localeIdentifier); 108 String valueBody = (String ) newsItem.getBody().get(localeIdentifier); 109 110 if ( (valueTitle == null || valueTitle.trim().length() == 0) && (valueAnnotation == null || valueAnnotation.trim().length() == 0) && (valueBody == null || valueBody.trim().length() == 0) ) { 112 continue; 113 } 114 115 ContentFieldValue fvTitle = new ContentFieldValue(); 117 fvTitle.setContentLocale(contentLocale); 118 if ( valueTitle != null ) { 119 fvTitle.setSimpleValue(valueTitle); 120 } else { 121 fvTitle.setSimpleValue(""); 122 } 123 fvTitle.setLastUpdatedDatetime(new Date ()); 124 fTitle.addContentFieldValue(fvTitle); 125 getHibernateTemplate().save(fvTitle); 126 127 ContentFieldValue fvAnnotation = new ContentFieldValue(); 129 fvAnnotation.setContentLocale(contentLocale); 130 if ( valueAnnotation != null ) { 131 byte[] annotationBytes = ConvertUtil.convertToByteArray(valueAnnotation); 132 fvAnnotation.setValue(annotationBytes); 133 try { 135 Set refs = new HashSet (extractor.extractAllRefs(new ByteArrayInputStream (annotationBytes), Constants.DEFAULT_ENCODING)); 136 List linkedPages = new ArrayList (); 137 List linkedResources = new ArrayList (); 138 for ( Iterator j = refs.iterator(); j.hasNext(); ) { 139 String ref = (String ) j.next(); 140 BaseObject linkedObject = (BaseObject) linkedObjects.get(ref); 141 if ( linkedObject != null ) { 142 if ( linkedObject instanceof ContentResource ) { 143 linkedResources.add(linkedObject); 144 } else if ( linkedObject instanceof Page ) { 145 linkedPages.add(linkedObject); 146 } 147 } 148 } 149 fvAnnotation.setLinkedResources(linkedResources); 150 fvAnnotation.setLinkedPages(linkedPages); 151 } catch ( PlainTextExtractorException e ) { 152 if ( log.isErrorEnabled() ) { 154 StringWriter sw = new StringWriter (); 155 e.printStackTrace(new PrintWriter (sw)); 156 log.error(sw.toString()); 157 } 158 } 159 } else { 160 fvAnnotation.setValue(new byte[0]); 161 } 162 fvAnnotation.setLastUpdatedDatetime(new Date ()); 163 fAnnotation.addContentFieldValue(fvAnnotation); 164 getHibernateTemplate().save(fvAnnotation); 165 166 ContentFieldValue fvBody = new ContentFieldValue(); 168 fvBody.setContentLocale(contentLocale); 169 if ( valueBody != null ) { 170 byte[] bodyBytes = ConvertUtil.convertToByteArray(valueBody); 171 fvBody.setValue(bodyBytes); 172 try { 174 Set refs = new HashSet (extractor.extractAllRefs(new ByteArrayInputStream (bodyBytes), Constants.DEFAULT_ENCODING)); 175 List linkedPages = new ArrayList (); 176 List linkedResources = new ArrayList (); 177 for ( Iterator j = refs.iterator(); j.hasNext(); ) { 178 String ref = (String ) j.next(); 179 BaseObject linkedObject = (BaseObject) linkedObjects.get(ref); 180 if ( linkedObject != null ) { 181 if ( linkedObject instanceof ContentResource ) { 182 linkedResources.add(linkedObject); 183 } else if ( linkedObject instanceof Page ) { 184 linkedPages.add(linkedObject); 185 } 186 } 187 } 188 fvBody.setLinkedResources(linkedResources); 189 fvBody.setLinkedPages(linkedPages); 190 } catch ( PlainTextExtractorException e ) { 191 if ( log.isErrorEnabled() ) { 193 StringWriter sw = new StringWriter (); 194 e.printStackTrace(new PrintWriter (sw)); 195 log.error(sw.toString()); 196 } 197 } 198 } else { 199 fvBody.setValue(new byte[0]); 200 } 201 fvBody.setLastUpdatedDatetime(new Date ()); 202 fBody.addContentFieldValue(fvBody); 203 getHibernateTemplate().save(fvBody); 204 } 205 206 return newsItemId; 207 } 208 209 212 public NewsItem retrieveNewsItem(Long newsItemId) { 213 String hql = new StringBuffer ("select newsItem, title.contentLocale.identifier, title.simpleValue, annotation.value, body.value ") 214 .append("from NewsItem as newsItem left outer join ") 215 .append("newsItem.contentFields titleField left outer join titleField.contentFieldValues as title left outer join ") 216 .append("newsItem.contentFields annotationField left outer join annotationField.contentFieldValues as annotation ") 217 .append("left outer join newsItem.contentFields bodyField left outer join bodyField.contentFieldValues as body ") 218 .append("where ") 219 .append("((title.contentLocale = annotation.contentLocale and title.contentLocale = body.contentLocale) or (title is null and annotation is null and body is null)) ") 220 .append("and titleField.identifier = 'title' ") 221 .append("and annotationField.identifier = 'annotation' and bodyField.identifier = 'body' and newsItem.id = ?") 222 .toString(); 223 224 List list = executeFind(hql, new Object []{newsItemId}); 227 List result = setLocalizableFields(list); 230 if ( result == null || result.size() <= 0 ) { 231 return null; 232 } else { 233 return (NewsItem) result.get(0); 234 } 235 } 236 237 240 public NewsItem retrieveNewsItemLite(Long newsItemId) { 241 return (NewsItem) getHibernateTemplate().get(NewsItem.class, newsItemId); 242 } 243 244 247 public void updateNewsItem(NewsItem newsItem, Map linkedObjects) { 248 249 getHibernateTemplate().update(newsItem); 250 251 HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor(); 252 253 for ( int i = 0; i < newsItem.getContentFields().size(); i++ ) { 255 ContentField contentField = (ContentField) newsItem.getContentFields().get(i); 256 Map values = null; 257 boolean blob = true; 258 if ( "title".equalsIgnoreCase(contentField.getIdentifier()) ) { 259 values = newsItem.getTitle(); 260 blob = false; 261 } else if ( "annotation".equalsIgnoreCase(contentField.getIdentifier()) ) { 262 values = newsItem.getAnnotation(); 263 } else if ( "body".equalsIgnoreCase(contentField.getIdentifier()) ) { 264 values = newsItem.getBody(); 265 } else { 266 continue; 267 } 268 for ( int j = 0; j < contentField.getContentFieldValues().size(); j++ ) { 269 ContentFieldValue contentFieldValue = (ContentFieldValue) contentField.getContentFieldValues().get(j); 270 String localeIdentifier = contentFieldValue.getContentLocale().getIdentifier(); 271 String value = (String ) values.get(localeIdentifier); 272 if ( value != null ) { 273 if ( !blob ) { 274 contentFieldValue.setSimpleValue(value); 275 } else { 276 byte[] valueBytes = ConvertUtil.convertToByteArray(value); 277 contentFieldValue.setValue(valueBytes); 278 if ( linkedObjects != null ) { 279 try { 281 Set refs = new HashSet (extractor.extractAllRefs(new ByteArrayInputStream (valueBytes), Constants.DEFAULT_ENCODING)); 282 List linkedPages = new ArrayList (); 283 List linkedResources = new ArrayList (); 284 for ( Iterator k = refs.iterator(); k.hasNext(); ) { 285 String ref = (String ) k.next(); 286 BaseObject linkedObject = (BaseObject) linkedObjects.get(ref); 287 if ( linkedObject != null ) { 288 if ( linkedObject instanceof ContentResource ) { 289 linkedResources.add(linkedObject); 290 } else if ( linkedObject instanceof Page ) { 291 linkedPages.add(linkedObject); 292 } 293 } 294 } 295 contentFieldValue.setLinkedResources(linkedResources); 296 contentFieldValue.setLinkedPages(linkedPages); 297 } catch ( PlainTextExtractorException e ) { 298 if ( log.isErrorEnabled() ) { 300 StringWriter sw = new StringWriter (); 301 e.printStackTrace(new PrintWriter (sw)); 302 log.error(sw.toString()); 303 } 304 } 305 } 306 } 307 } else { 308 if ( !blob ) { 309 contentFieldValue.setSimpleValue(""); 310 } else { 311 contentFieldValue.setValue(new byte[0]); 312 } 313 } 314 contentFieldValue.setLastUpdatedDatetime(new Date ()); 315 contentFieldValue.getContentField().updateContentFieldValue(contentFieldValue); 316 getHibernateTemplate().update(contentFieldValue); 317 } 318 } 319 } 320 321 324 public void deleteNewsItem(NewsItem newsItem) throws DeleteException { 325 getHibernateTemplate().delete(newsItem); 326 327 List roles = new ArrayList (newsItem.getRoles()); 329 for ( int i = 0; i < roles.size(); i++ ) { 330 Role role = (Role) roles.get(i); 331 newsItem.removeRole(role); 332 } 333 } 334 335 337 340 public PartialCollection listNewsItems(QueryInfo queryInfo) { 341 String whereClause = new String (); 342 String orderByClause = new String (); 343 if ( queryInfo != null ) { 344 whereClause = queryInfo.getWhereClause(); 345 orderByClause = queryInfo.getOrderByClause(); 346 } 347 348 if ( whereClause == null || whereClause.length() == 0 ) { 349 whereClause = new String (); 350 } 351 if ( orderByClause != null && orderByClause.length() != 0 ) { 352 orderByClause = " order by " + orderByClause; 353 } else { 354 orderByClause = " order by item.publicationDate desc, title.simpleValue asc"; 355 } 356 357 List list = null; 358 Integer total = null; 359 360 String localeIdentifier = null; 361 if ( queryInfo != null ) { 362 if ( queryInfo.getQueryParameters() != null ) { 363 localeIdentifier = (String ) queryInfo.getQueryParameters().get("localeIdentifier"); 364 } 365 } 366 367 boolean localeIdentifierPresent = localeIdentifier != null && localeIdentifier.length() > 0; 368 369 String hqlPart = new String (); 370 ArrayList args = new ArrayList (); 371 if ( localeIdentifierPresent ) { 372 args.add(localeIdentifier); 373 hqlPart = new StringBuffer ("from NewsItem as item ") 374 .append("left outer join item.contentFields as titleField ") 375 .append("left outer join titleField.contentFieldValues as title ") 376 .append("left outer join item.roles as role ") 377 .append("where ") 378 .append("titleField.identifier = 'title' ") 379 .append("and title.contentLocale.identifier = ? ") 380 .toString(); 381 if ( whereClause.length() > 0 ) { 382 hqlPart += "and " + whereClause; 383 } 384 } else { 385 hqlPart = "from NewsItem item left outer join item.roles as role "; 386 if ( whereClause.length() > 0 ) { 387 hqlPart += "where " + whereClause; 388 } 389 } 390 391 if ( queryInfo != null && queryInfo.getLimit() != null && queryInfo.getOffset() != null ) { 392 String hqlForTotal = "select count(distinct item.id) " + hqlPart; 394 total = (Integer ) findUniqueResult(hqlForTotal, args.toArray()); 395 if ( total == null ) { 396 total = new Integer (0); 397 } 398 } 399 400 if ( total == null || total.intValue() > 0 ) { 403 String hql = new String (); 404 if ( localeIdentifierPresent ) { 405 hql = "select distinct item, title.simpleValue " + hqlPart + orderByClause; 406 } else { 407 hql = "select distinct item " + hqlPart + orderByClause; 408 } 409 list = executeFind(hql, queryInfo, args.toArray()); 410 if ( total == null ) { 411 total = new Integer (list.size()); 412 } 413 if ( localeIdentifierPresent ) { 414 for ( ListIterator i = list.listIterator(); i.hasNext(); ) { 415 Object [] objects = (Object []) i.next(); 416 NewsItem newsItem = (NewsItem) objects[0]; 417 newsItem.getTitle().put(localeIdentifier, objects[1]); 418 i.set(newsItem); 419 } 420 } 421 } else { 422 list = new ArrayList (); 423 } 424 425 426 return new PartialCollection(list, total); 427 428 } 429 430 433 public List getLastNews(QueryInfo queryInfo) { 434 435 String whereClause = "item.active = 'T'"; 436 String orderByClause = " order by item.publicationDate desc, title.simpleValue asc"; 437 438 List list = new ArrayList (); 439 440 String localeIdentifier = null; 441 List roles = null; 442 Integer limit = queryInfo.getLimit(); 443 if ( queryInfo != null ) { 444 if ( queryInfo.getQueryParameters() != null ) { 445 localeIdentifier = (String ) queryInfo.getQueryParameters().get("localeIdentifier"); 446 roles = (List ) queryInfo.getQueryParameters().get("roles"); 447 } 448 } 449 450 boolean localeIdentifierPresent = localeIdentifier != null && localeIdentifier.length() > 0; 451 452 String hqlPart = new String (); 453 ArrayList args = new ArrayList (); 454 if ( localeIdentifierPresent ) { 455 args.add(localeIdentifier); 456 hqlPart = new StringBuffer ("from NewsItem as item ") 457 .append("left outer join item.contentFields as titleField ") 458 .append("left outer join item.contentFields as annotationField ") 459 .append("left outer join titleField.contentFieldValues as title ") 460 .append("left outer join annotationField.contentFieldValues as annotation ") 461 .append("where ") 462 .append("titleField.identifier = 'title' and annotationField.identifier = 'annotation' ") 463 .append("and title.contentLocale = annotation.contentLocale ") 464 .append("and title.contentLocale.identifier = ? ") 465 .append("and ").append(whereClause) 466 .toString(); 467 } else { 468 hqlPart = "from NewsItem item where " + whereClause; 469 } 470 471 String hql = new String (); 472 if ( localeIdentifierPresent ) { 473 hql = "select item, title.simpleValue, annotation.value " + hqlPart + orderByClause; 474 } else { 475 hql = "select item " + hqlPart + orderByClause; 476 } 477 478 479 Integer offset = new Integer (0); 480 do { 485 List tmp = executeFind(hql, queryInfo, args.toArray(), true, "query.getLastNews"); 486 487 if ( tmp.isEmpty() ) { 489 break; 490 } 491 492 if ( localeIdentifierPresent ) { 493 for ( ListIterator i = tmp.listIterator(); i.hasNext(); ) { 494 Object [] objects = (Object []) i.next(); 495 NewsItem newsItem = (NewsItem) objects[0]; 496 List itemRoles = newsItem.getRoles(); 497 boolean accessAllowed = true; 498 if ( !itemRoles.isEmpty() ) { 499 accessAllowed = false; 500 if ( roles != null ) { 501 for ( Iterator j = itemRoles.iterator(); j.hasNext() && !accessAllowed; ) { 502 Role role = (Role) j.next(); 503 accessAllowed = roles.contains(role); 504 } 505 } 506 } 507 if ( !accessAllowed ) { 508 i.remove(); 509 } else { 510 byte[] annotation = (byte[]) objects[2]; 511 newsItem.getTitle().put(localeIdentifier, objects[1]); 512 newsItem.getAnnotation().put(localeIdentifier, ConvertUtil.convertToString(annotation)); 513 i.set(newsItem); 514 } 515 } 516 } else { 517 for ( Iterator i = tmp.iterator(); i.hasNext(); ) { 519 NewsItem item = (NewsItem) i.next(); 520 if ( !item.getRoles().equals(roles) ) { 521 i.remove(); 522 } 523 } 524 } 525 526 list.addAll(tmp); 527 528 offset = new Integer (offset.intValue() + limit.intValue()); 529 queryInfo.setOffset(offset); 530 } while ( list.size() < limit.intValue() ); 531 532 if ( list.size() > limit.intValue() ) { 533 list = list.subList(0, limit.intValue()); 534 } 535 536 return list; 537 538 } 539 540 541 543 546 public NewsItem findNewsItemByUri(String newsItemUri) { 547 return (NewsItem) findUniqueResult("from NewsItem newsItem where newsItem.uri = ?", new Object []{newsItemUri}); 548 } 549 550 553 public List findUnpublishedNews(Date date) { 554 String hql = "from NewsItem item where item.active = 'F' and item.publicationDate <= ? and item.expirationDate > ?"; 555 return executeFind(hql, new Object []{date, date}, new Type[]{Hibernate.DATE, Hibernate.DATE}); 556 } 557 558 561 public List findExpiredNews(Date date) { 562 String hql = "from NewsItem item where item.active = 'T' and item.expirationDate <= ?"; 563 return executeFind(hql, new Object []{date}, new Type[]{Hibernate.DATE}); 564 } 565 566 567 569 576 protected NewsItem findNewsItemById(List newsItems, Long id) { 577 for ( int i = 0; i < newsItems.size(); i++ ) { 578 NewsItem newsItem = (NewsItem) newsItems.get(i); 579 if ( newsItem.getId().equals(id) ) { 580 return newsItem; 581 } 582 } 583 return null; 584 } 585 586 592 protected List setLocalizableFields(List queryResult) { 593 if ( queryResult == null || queryResult.size() <= 0 ) { 594 return null; 595 } 596 597 List newsItems = new ArrayList (queryResult.size()); 600 Set processedLocales = new HashSet (); 601 for ( int i = 0; i < queryResult.size(); i++ ) { 602 Object [] objects = (Object []) queryResult.get(i); 603 NewsItem newsItem = (NewsItem) objects[0]; 604 String locale = (String ) objects[1]; 605 if ( locale != null && locale.trim().length() > 0 && !processedLocales.contains(locale) ) { 606 processedLocales.add(locale); 607 newsItem.getTitle().put(locale, objects[2]); 608 newsItem.getAnnotation().put(locale, ConvertUtil.convertToString((byte[]) objects[3])); 609 newsItem.getBody().put(locale, ConvertUtil.convertToString((byte[]) objects[4])); 610 } 611 612 newsItems.add(newsItem); 613 } 614 615 List fullNewsItems = new ArrayList (newsItems.size()); 619 for ( int i = 0; i < newsItems.size(); i++ ) { 620 NewsItem newItem = (NewsItem) newsItems.get(i); 621 NewsItem existingItem = findNewsItemById(fullNewsItems, newItem.getId()); 622 if ( existingItem != null ) { 623 Iterator iterator = newItem.getTitle().keySet().iterator(); 625 while ( iterator.hasNext() ) { 626 String localeIdentifier = (String ) iterator.next(); 627 Object value = newItem.getTitle().get(localeIdentifier); 628 if ( value == null ) { 629 continue; 630 } 631 existingItem.getTitle().put(localeIdentifier, value); 632 } 633 634 iterator = newItem.getAnnotation().keySet().iterator(); 636 while ( iterator.hasNext() ) { 637 String localeIdentifier = (String ) iterator.next(); 638 Object value = newItem.getAnnotation().get(localeIdentifier); 639 if ( value == null ) { 640 continue; 641 } 642 existingItem.getAnnotation().put(localeIdentifier, value); 643 } 644 645 iterator = newItem.getBody().keySet().iterator(); 647 while ( iterator.hasNext() ) { 648 String localeIdentifier = (String ) iterator.next(); 649 Object value = newItem.getBody().get(localeIdentifier); 650 if ( value == null ) { 651 continue; 652 } 653 existingItem.getBody().put(localeIdentifier, value); 654 } 655 656 } else { 657 fullNewsItems.add(newItem); 658 } 659 } 660 661 return fullNewsItems; 662 } 663 664 } 665 | Popular Tags |