| 1 16 package com.blandware.atleap.persistence.hibernate.testimonials; 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.testimonials.Testimonial; 31 import com.blandware.atleap.persistence.exception.DeleteException; 32 import com.blandware.atleap.persistence.hibernate.core.PageDAOHibernate; 33 import com.blandware.atleap.persistence.testimonials.TestimonialDAO; 34 35 import java.io.ByteArrayInputStream ; 36 import java.io.PrintWriter ; 37 import java.io.StringWriter ; 38 import java.util.ArrayList ; 39 import java.util.Date ; 40 import java.util.HashSet ; 41 import java.util.Iterator ; 42 import java.util.List ; 43 import java.util.ListIterator ; 44 import java.util.Map ; 45 import java.util.Set ; 46 47 54 public class TestimonialDAOHibernate extends PageDAOHibernate implements TestimonialDAO { 55 56 59 public TestimonialDAOHibernate() { 60 } 61 62 65 public Long createTestimonial(Testimonial testimonial, Map linkedObjects) { 66 testimonial.setUsageCounter(new Integer (0)); 67 Long testimonialId = (Long ) getHibernateTemplate().save(testimonial); 68 69 ContentField fTitle = new ContentField(); 71 fTitle.setIdentifier("title"); 72 fTitle.setType(ContentField.LINE_TYPE); 73 fTitle.setInternal(Boolean.TRUE); 74 testimonial.addContentField(fTitle); 75 getHibernateTemplate().save(fTitle); 76 77 ContentField fBody = new ContentField(); 79 fBody.setIdentifier("body"); 80 fBody.setType(ContentField.HTML_TYPE); 81 fBody.setInternal(Boolean.TRUE); 82 testimonial.addContentField(fBody); 83 getHibernateTemplate().save(fBody); 84 85 List contentLocales = executeFind("from ContentLocale l", true, "query.listContentLocales"); 88 HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor(); 89 for ( int i = 0; i < contentLocales.size(); i++ ) { 90 ContentLocale contentLocale = (ContentLocale) contentLocales.get(i); 91 String localeIdentifier = contentLocale.getIdentifier(); 92 93 String valueTitle = (String ) testimonial.getTitle().get(localeIdentifier); 95 String valueBody = (String ) testimonial.getBody().get(localeIdentifier); 96 97 if ( (valueTitle == null || valueTitle.trim().length() == 0) && (valueBody == null || valueBody.trim().length() == 0) ) { 98 continue; 99 } 100 101 ContentFieldValue fvTitle = new ContentFieldValue(); 103 fvTitle.setContentLocale(contentLocale); 104 if ( valueTitle != null ) { 105 fvTitle.setSimpleValue(valueTitle); 106 } else { 107 fvTitle.setSimpleValue(""); 108 } 109 fvTitle.setLastUpdatedDatetime(new Date ()); 110 fTitle.addContentFieldValue(fvTitle); 111 getHibernateTemplate().save(fvTitle); 112 113 ContentFieldValue fvBody = new ContentFieldValue(); 115 fvBody.setContentLocale(contentLocale); 116 if ( valueBody != null ) { 117 byte[] bodyBytes = ConvertUtil.convertToByteArray(valueBody); 118 fvBody.setValue(bodyBytes); 119 try { 121 Set refs = new HashSet (extractor.extractAllRefs(new ByteArrayInputStream (bodyBytes), Constants.DEFAULT_ENCODING)); 122 List linkedPages = new ArrayList (); 123 List linkedResources = new ArrayList (); 124 for ( Iterator j = refs.iterator(); j.hasNext(); ) { 125 String ref = (String ) j.next(); 126 BaseObject linkedObject = (BaseObject) linkedObjects.get(ref); 127 if ( linkedObject != null ) { 128 if ( linkedObject instanceof ContentResource ) { 129 linkedResources.add(linkedObject); 130 } else if ( linkedObject instanceof Page ) { 131 linkedPages.add(linkedObject); 132 } 133 } 134 } 135 fvBody.setLinkedResources(linkedResources); 136 fvBody.setLinkedPages(linkedPages); 137 } catch ( PlainTextExtractorException e ) { 138 if ( log.isErrorEnabled() ) { 140 StringWriter sw = new StringWriter (); 141 e.printStackTrace(new PrintWriter (sw)); 142 log.error(sw.toString()); 143 } 144 } 145 } else { 146 fvBody.setValue(new byte[0]); 147 } 148 fvBody.setLastUpdatedDatetime(new Date ()); 149 fBody.addContentFieldValue(fvBody); 150 getHibernateTemplate().save(fvBody); 151 } 152 153 return testimonialId; 154 } 155 156 159 public Testimonial retrieveTestimonial(Long testimonialId) { 160 String hql = new StringBuffer ("select testimonial, title.contentLocale.identifier, title.simpleValue, body.value ") 161 .append("from Testimonial as testimonial left outer join ") 162 .append("testimonial.contentFields titleField left outer join titleField.contentFieldValues as title left outer join ") 163 .append("testimonial.contentFields bodyField left outer join bodyField.contentFieldValues as body ") 164 .append("where ") 165 .append("((title.contentLocale = body.contentLocale) or (title is null and body is null)) ") 166 .append("and titleField.identifier = 'title' ") 167 .append("and bodyField.identifier = 'body' and testimonial.id = ?") 168 .toString(); 169 170 List list = executeFind(hql, new Object []{testimonialId}); 173 List result = setLocalizableFields(list); 176 if ( result == null || result.size() <= 0 ) { 177 return null; 178 } else { 179 return (Testimonial) result.get(0); 180 } 181 } 182 183 186 public Testimonial retrieveTestimonialLite(Long testimonialId) { 187 return (Testimonial) getHibernateTemplate().get(Testimonial.class, testimonialId); 188 } 189 190 193 public void updateTestimonial(Testimonial testimonial, Map linkedObjects) { 194 195 getHibernateTemplate().update(testimonial); 196 197 HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor(); 198 199 for ( int i = 0; i < testimonial.getContentFields().size(); i++ ) { 201 ContentField contentField = (ContentField) testimonial.getContentFields().get(i); 202 Map values = null; 203 boolean blob = true; 204 if ( "title".equalsIgnoreCase(contentField.getIdentifier()) ) { 205 values = testimonial.getTitle(); 206 blob = false; 207 } else if ( "body".equalsIgnoreCase(contentField.getIdentifier()) ) { 208 values = testimonial.getBody(); 209 } else { 210 continue; 211 } 212 for ( int j = 0; j < contentField.getContentFieldValues().size(); j++ ) { 213 ContentFieldValue contentFieldValue = (ContentFieldValue) contentField.getContentFieldValues().get(j); 214 String localeIdentifier = contentFieldValue.getContentLocale().getIdentifier(); 215 String value = (String ) values.get(localeIdentifier); 216 if ( value != null ) { 217 if ( !blob ) { 218 contentFieldValue.setSimpleValue(value); 219 } else { 220 byte[] valueBytes = ConvertUtil.convertToByteArray(value); 221 contentFieldValue.setValue(valueBytes); 222 if ( linkedObjects != null ) { 223 try { 225 Set refs = new HashSet (extractor.extractAllRefs(new ByteArrayInputStream (valueBytes), Constants.DEFAULT_ENCODING)); 226 List linkedPages = new ArrayList (); 227 List linkedResources = new ArrayList (); 228 for ( Iterator k = refs.iterator(); k.hasNext(); ) { 229 String ref = (String ) k.next(); 230 BaseObject linkedObject = (BaseObject) linkedObjects.get(ref); 231 if ( linkedObject != null ) { 232 if ( linkedObject instanceof ContentResource ) { 233 linkedResources.add(linkedObject); 234 } else if ( linkedObject instanceof Page ) { 235 linkedPages.add(linkedObject); 236 } 237 } 238 } 239 contentFieldValue.setLinkedResources(linkedResources); 240 contentFieldValue.setLinkedPages(linkedPages); 241 } catch ( PlainTextExtractorException e ) { 242 if ( log.isErrorEnabled() ) { 244 StringWriter sw = new StringWriter (); 245 e.printStackTrace(new PrintWriter (sw)); 246 log.error(sw.toString()); 247 } 248 } 249 } 250 } 251 } else { 252 if ( !blob ) { 253 contentFieldValue.setSimpleValue(""); 254 } else { 255 contentFieldValue.setValue(new byte[0]); 256 } 257 } 258 contentFieldValue.setLastUpdatedDatetime(new Date ()); 259 contentFieldValue.getContentField().updateContentFieldValue(contentFieldValue); 260 getHibernateTemplate().update(contentFieldValue); 261 } 262 } 263 } 264 265 268 public void deleteTestimonial(Testimonial testimonial) throws DeleteException { 269 getHibernateTemplate().delete(testimonial); 270 } 271 272 274 277 public PartialCollection listTestimonials(QueryInfo queryInfo) { 278 String whereClause = new String (); 279 String orderByClause = new String (); 280 if ( queryInfo != null ) { 281 whereClause = queryInfo.getWhereClause(); 282 orderByClause = queryInfo.getOrderByClause(); 283 if ( whereClause == null || whereClause.length() == 0 ) { 284 whereClause = new String (); 285 } 286 if ( orderByClause != null && orderByClause.length() != 0 ) { 287 orderByClause = " order by " + orderByClause; 288 } else { 289 orderByClause = new String (); 290 } 291 } 292 293 List list = null; 294 Integer total = null; 295 296 String localeIdentifier = null; 297 if ( queryInfo != null ) { 298 if ( queryInfo.getQueryParameters() != null ) { 299 localeIdentifier = (String ) queryInfo.getQueryParameters().get("localeIdentifier"); 300 } 301 } 302 303 boolean localeIdentifierPresent = localeIdentifier != null && localeIdentifier.length() > 0; 304 305 String hqlPart = new String (); 306 ArrayList args = new ArrayList (); 307 if ( localeIdentifierPresent ) { 308 args.add(localeIdentifier); 309 hqlPart = new StringBuffer ("from Testimonial as testimonial ") 310 .append("left outer join testimonial.contentFields as titleField ") 311 .append("left outer join testimonial.contentFields as bodyField ") 312 .append("left outer join titleField.contentFieldValues as title ") 313 .append("left outer join bodyField.contentFieldValues as body ") 314 .append("left outer join testimonial.roles as role ") 315 .append("where ") 316 .append("titleField.identifier = 'title' and bodyField.identifier = 'body' ") 317 .append("and title.contentLocale = body.contentLocale ") 318 .append("and title.contentLocale.identifier = ? ") 319 .toString(); 320 if ( whereClause.length() > 0 ) { 321 hqlPart += "and " + whereClause; 322 } 323 } else { 324 hqlPart = "from Testimonial testimonial "; 325 if ( whereClause.length() > 0 ) { 326 hqlPart += "where " + whereClause; 327 } 328 } 329 330 if ( queryInfo != null && queryInfo.getLimit() != null && queryInfo.getOffset() != null ) { 331 String hqlForTotal = "select count(testimonial.id) " + hqlPart; 333 total = (Integer ) findUniqueResult(hqlForTotal, args.toArray()); 334 if ( total == null ) { 335 total = new Integer (0); 336 } 337 } 338 339 if ( total == null || total.intValue() > 0 ) { 342 String hql = new String (); 343 if ( localeIdentifierPresent ) { 344 hql = "select testimonial, title.simpleValue, body.value " + hqlPart + orderByClause; 345 } else { 346 hql = "select testimonial " + hqlPart + orderByClause; 347 } 348 list = executeFind(hql, queryInfo, args.toArray()); 349 if ( total == null ) { 350 total = new Integer (list.size()); 351 } 352 if ( localeIdentifierPresent ) { 353 for ( ListIterator i = list.listIterator(); i.hasNext(); ) { 354 Object [] objects = (Object []) i.next(); 355 Testimonial testimonial = (Testimonial) objects[0]; 356 testimonial.getTitle().put(localeIdentifier, objects[1]); 357 byte[] body = (byte[]) objects[2]; 358 testimonial.getBody().put(localeIdentifier, ConvertUtil.convertToString(body)); 359 i.set(testimonial); 360 } 361 } 362 } else { 363 list = new ArrayList (); 364 } 365 366 367 return new PartialCollection(list, total); 368 369 } 370 371 374 public Testimonial getRandomTestimonial() { 375 String hql = "select testimonial.id from Testimonial testimonial where testimonial.active = 'T'"; 376 List list = executeFind(hql); 377 if ( list != null && list.size() > 0 ) { 378 int position = (int) Math.floor(Math.random() * list.size()); 379 Long id = (Long ) list.get(position); 380 return retrieveTestimonial(id); 381 } else { 382 return null; 383 } 384 } 385 386 387 389 392 public Testimonial findTestimonialByUri(String testimonialUri) { 393 return (Testimonial) findUniqueResult("from Testimonial testimonial where testimonial.uri = ?", new Object []{testimonialUri}); 394 } 395 396 398 405 protected Testimonial findTestimonialById(List testimonials, Long id) { 406 for ( int i = 0; i < testimonials.size(); i++ ) { 407 Testimonial testimonial = (Testimonial) testimonials.get(i); 408 if ( testimonial.getId().equals(id) ) { 409 return testimonial; 410 } 411 } 412 return null; 413 } 414 415 421 protected List setLocalizableFields(List queryResult) { 422 if ( queryResult == null || queryResult.size() <= 0 ) { 423 return null; 424 } 425 426 List testimonials = new ArrayList (queryResult.size()); 429 Set processedLocales = new HashSet (); 430 for ( int i = 0; i < queryResult.size(); i++ ) { 431 Object [] objects = (Object []) queryResult.get(i); 432 Testimonial testimonial = (Testimonial) objects[0]; 433 String locale = (String ) objects[1]; 434 if ( locale != null && locale.trim().length() > 0 && !processedLocales.contains(locale) ) { 435 processedLocales.add(locale); 436 testimonial.getTitle().put(locale, objects[2]); 437 testimonial.getBody().put(locale, ConvertUtil.convertToString((byte[]) objects[3])); 438 } 439 440 testimonials.add(testimonial); 441 } 442 443 List fullTestimonials = new ArrayList (testimonials.size()); 447 for ( int i = 0; i < testimonials.size(); i++ ) { 448 Testimonial newItem = (Testimonial) testimonials.get(i); 449 Testimonial existingItem = findTestimonialById(fullTestimonials, newItem.getId()); 450 if ( existingItem != null ) { 451 Iterator iterator = newItem.getTitle().keySet().iterator(); 453 while ( iterator.hasNext() ) { 454 String localeIdentifier = (String ) iterator.next(); 455 Object value = newItem.getTitle().get(localeIdentifier); 456 if ( value == null ) { 457 continue; 458 } 459 existingItem.getTitle().put(localeIdentifier, value); 460 } 461 462 iterator = newItem.getBody().keySet().iterator(); 464 while ( iterator.hasNext() ) { 465 String localeIdentifier = (String ) iterator.next(); 466 Object value = newItem.getBody().get(localeIdentifier); 467 if ( value == null ) { 468 continue; 469 } 470 existingItem.getBody().put(localeIdentifier, value); 471 } 472 473 } else { 474 fullTestimonials.add(newItem); 475 } 476 } 477 478 return fullTestimonials; 479 } 480 481 } 482 | Popular Tags |