1 16 package com.blandware.atleap.persistence.hibernate.core; 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.MailTemplate; 30 import com.blandware.atleap.model.core.Page; 31 import com.blandware.atleap.persistence.core.MailTemplateDAO; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.PrintWriter ; 35 import java.io.StringWriter ; 36 import java.util.ArrayList ; 37 import java.util.Date ; 38 import java.util.HashSet ; 39 import java.util.Iterator ; 40 import java.util.List ; 41 import java.util.ListIterator ; 42 import java.util.Map ; 43 import java.util.Set ; 44 45 52 public class MailTemplateDAOHibernate extends LocalizableDAOHibernate implements MailTemplateDAO { 53 54 57 public MailTemplateDAOHibernate() { 58 } 59 60 62 65 public Long createMailTemplate(MailTemplate template) { 66 Long templateId = (Long ) getHibernateTemplate().save(template); 67 68 ContentField fFrom = new ContentField(); 70 fFrom.setIdentifier("from"); 71 fFrom.setType(ContentField.LINE_TYPE); 72 fFrom.setInternal(Boolean.TRUE); 73 template.addContentField(fFrom); 74 getHibernateTemplate().save(fFrom); 75 76 ContentField fSubject = new ContentField(); 78 fSubject.setIdentifier("subject"); 79 fSubject.setType(ContentField.LINE_TYPE); 80 fSubject.setInternal(Boolean.TRUE); 81 template.addContentField(fSubject); 82 getHibernateTemplate().save(fSubject); 83 84 ContentField fBody = new ContentField(); 86 fBody.setIdentifier("body"); 87 fBody.setType(ContentField.MULTILINE_TYPE); 88 fBody.setInternal(Boolean.TRUE); 89 template.addContentField(fBody); 90 getHibernateTemplate().save(fBody); 91 92 ContentField fCharset = new ContentField(); 94 fCharset.setIdentifier("charset"); 95 fCharset.setType(ContentField.LINE_TYPE); 96 fCharset.setInternal(Boolean.TRUE); 97 template.addContentField(fCharset); 98 getHibernateTemplate().save(fCharset); 99 100 List contentLocales = executeFind("from ContentLocale l", true, "query.listContentLocales"); 103 for ( int i = 0; i < contentLocales.size(); i++ ) { 104 ContentLocale contentLocale = (ContentLocale) contentLocales.get(i); 105 String localeIdentifier = contentLocale.getIdentifier(); 106 107 String valueFrom = (String ) template.getFrom().get(localeIdentifier); 109 String valueSubject = (String ) template.getSubject().get(localeIdentifier); 110 String valueBody = (String ) template.getBody().get(localeIdentifier); 111 String valueCharset = (String ) template.getCharset().get(localeIdentifier); 112 113 ContentFieldValue fvFrom = new ContentFieldValue(); 115 fvFrom.setContentLocale(contentLocale); 116 if ( valueFrom != null ) { 117 fvFrom.setSimpleValue(valueFrom); 118 } else { 119 fvFrom.setSimpleValue(""); 120 } 121 fvFrom.setLastUpdatedDatetime(new Date ()); 122 fFrom.addContentFieldValue(fvFrom); 123 getHibernateTemplate().save(fvFrom); 124 125 ContentFieldValue fvSubject = new ContentFieldValue(); 127 fvSubject.setContentLocale(contentLocale); 128 if ( valueSubject != null ) { 129 fvSubject.setSimpleValue(valueSubject); 130 } else { 131 fvSubject.setSimpleValue(""); 132 } 133 fvSubject.setLastUpdatedDatetime(new Date ()); 134 fSubject.addContentFieldValue(fvSubject); 135 getHibernateTemplate().save(fvSubject); 136 137 ContentFieldValue fvBody = new ContentFieldValue(); 139 fvBody.setContentLocale(contentLocale); 140 if ( valueBody != null ) { 141 fvBody.setValue(ConvertUtil.convertToByteArray(valueBody)); 142 } else { 143 fvBody.setValue(new byte[0]); 144 } 145 fvBody.setLastUpdatedDatetime(new Date ()); 146 fBody.addContentFieldValue(fvBody); 147 getHibernateTemplate().save(fvBody); 148 149 ContentFieldValue fvCharset = new ContentFieldValue(); 151 fvCharset.setContentLocale(contentLocale); 152 if ( valueCharset != null ) { 153 fvCharset.setSimpleValue(valueCharset); 154 } else { 155 fvCharset.setSimpleValue(""); 156 } 157 fvCharset.setLastUpdatedDatetime(new Date ()); 158 fCharset.addContentFieldValue(fvCharset); 159 getHibernateTemplate().save(fvCharset); 160 } 161 162 return templateId; 163 } 164 165 168 public MailTemplate retrieveMailTemplate(Long templateId) { 169 String hql = new StringBuffer ("select template, fromValue.contentLocale.identifier, fromValue.simpleValue, subject.simpleValue, charset.simpleValue, body.value ") 170 .append("from MailTemplate as template left outer join ") 171 .append("template.contentFields fromField left outer join fromField.contentFieldValues as fromValue left outer join ") 172 .append("template.contentFields subjectField left outer join subjectField.contentFieldValues as subject ") 173 .append("left outer join template.contentFields bodyField left outer join bodyField.contentFieldValues as body ") 174 .append("left outer join template.contentFields charsetField left outer join charsetField.contentFieldValues as charset ") 175 .append("where ") 176 .append("((fromValue.contentLocale = subject.contentLocale and fromValue.contentLocale = body.contentLocale and fromValue.contentLocale = charset.contentLocale) or (fromValue is null and subject is null and body is null and charset is null)) ") 177 .append("and fromField.identifier = 'from' and charsetField.identifier = 'charset' ") 178 .append("and subjectField.identifier = 'subject' and bodyField.identifier = 'body' and template.id = ?") 179 .toString(); 180 181 List list = executeFind(hql, new Object []{templateId}); 184 List result = setLocalizableFields(list); 187 if ( result == null || result.size() <= 0 ) { 188 return null; 189 } else { 190 return (MailTemplate) result.get(0); 191 } 192 } 193 194 197 public void updateMailTemplate(MailTemplate template, Map linkedObjects) { 198 getHibernateTemplate().update(template); 199 HTMLPlainTextExtractor extractor = new HTMLPlainTextExtractor(); 200 for ( int i = 0; i < template.getContentFields().size(); i++ ) { 202 ContentField contentField = (ContentField) template.getContentFields().get(i); 203 Map values = null; 204 boolean blob = false; 205 if ( "from".equalsIgnoreCase(contentField.getIdentifier()) ) { 206 values = template.getFrom(); 207 } else if ( "subject".equalsIgnoreCase(contentField.getIdentifier()) ) { 208 values = template.getSubject(); 209 } else if ( "body".equalsIgnoreCase(contentField.getIdentifier()) ) { 210 blob = true; 211 values = template.getBody(); 212 byte type = template.isPlain() ? ContentField.MULTILINE_TYPE : ContentField.HTML_TYPE; 213 if ( contentField.getType() != type ) { 214 contentField.setType(type); 215 getHibernateTemplate().update(contentField); 216 } 217 } else if ( "charset".equalsIgnoreCase(contentField.getIdentifier()) ) { 218 values = template.getCharset(); 219 } else { 220 continue; 221 } 222 for ( int j = 0; j < contentField.getContentFieldValues().size(); j++ ) { 223 ContentFieldValue contentFieldValue = (ContentFieldValue) contentField.getContentFieldValues().get(j); 224 String localeIdentifier = contentFieldValue.getContentLocale().getIdentifier(); 225 String value = (String ) values.get(localeIdentifier); 226 if ( value != null ) { 227 if ( !blob ) { 228 contentFieldValue.setSimpleValue(value); 229 } else { 230 byte[] valueBytes = ConvertUtil.convertToByteArray(value); 231 contentFieldValue.setValue(valueBytes); 232 if ( linkedObjects != null ) { 233 try { 235 Set refs = new HashSet (extractor.extractAllRefs(new ByteArrayInputStream (valueBytes), Constants.DEFAULT_ENCODING)); 236 List linkedPages = new ArrayList (); 237 List linkedResources = new ArrayList (); 238 for ( Iterator k = refs.iterator(); k.hasNext(); ) { 239 String ref = (String ) k.next(); 240 BaseObject linkedObject = (BaseObject) linkedObjects.get(ref); 241 if ( linkedObject != null ) { 242 if ( linkedObject instanceof ContentResource ) { 243 linkedResources.add(linkedObject); 244 } else if ( linkedObject instanceof Page ) { 245 linkedPages.add(linkedObject); 246 } 247 } 248 } 249 contentFieldValue.setLinkedResources(linkedResources); 250 contentFieldValue.setLinkedPages(linkedPages); 251 } catch ( PlainTextExtractorException e ) { 252 if ( log.isErrorEnabled() ) { 254 StringWriter sw = new StringWriter (); 255 e.printStackTrace(new PrintWriter (sw)); 256 log.error(sw.toString()); 257 } 258 } 259 } 260 } 261 } else { 262 if ( !blob ) { 263 contentFieldValue.setSimpleValue(""); 264 } else { 265 contentFieldValue.setValue(new byte[0]); 266 } 267 } 268 contentFieldValue.setLastUpdatedDatetime(new Date ()); 269 contentFieldValue.getContentField().updateContentFieldValue(contentFieldValue); 270 getHibernateTemplate().update(contentFieldValue); 271 } 272 } 273 } 274 275 278 public void deleteMailTemplate(MailTemplate template) { 279 getHibernateTemplate().delete(template); 280 } 281 282 285 public PartialCollection listMailTemplates(QueryInfo queryInfo) { 286 String whereClause = new String (); 287 String orderByClause = new String (); 288 if ( queryInfo != null ) { 289 whereClause = queryInfo.getWhereClause(); 290 orderByClause = queryInfo.getOrderByClause(); 291 if ( whereClause == null || whereClause.length() == 0 ) { 292 whereClause = new String (); 293 } 294 if ( orderByClause != null && orderByClause.length() != 0 ) { 295 orderByClause = " order by " + orderByClause; 296 } else { 297 orderByClause = new String (); 298 } 299 } 300 301 List list = null; 302 Integer total = null; 303 304 String localeIdentifier = null; 305 if ( queryInfo != null ) { 306 if ( queryInfo.getQueryParameters() != null ) { 307 localeIdentifier = (String ) queryInfo.getQueryParameters().get("localeIdentifier"); 308 } 309 } 310 311 boolean localeIdentifierPresent = localeIdentifier != null && localeIdentifier.length() > 0; 312 313 String hqlPart = new String (); 314 ArrayList args = new ArrayList (); 315 if ( localeIdentifierPresent ) { 316 args.add(localeIdentifier); 317 hqlPart = new StringBuffer ("from MailTemplate as template ") 318 .append("left outer join template.contentFields as fromField ") 319 .append("left outer join template.contentFields as subjectField ") 320 .append("left outer join template.contentFields as bodyField ") 321 .append("left outer join template.contentFields as charsetField ") 322 .append("left outer join fromField.contentFieldValues as fromValue ") 323 .append("left outer join subjectField.contentFieldValues as subject ") 324 .append("left outer join bodyField.contentFieldValues as body ") 325 .append("left outer join charsetField.contentFieldValues as charset ") 326 .append("where ") 327 .append("fromField.identifier = 'from' and subjectField.identifier = 'subject'") 328 .append("and bodyField.identifier = 'body' and charsetField.identifier = 'charset' ") 329 .append("and fromValue.contentLocale = subject.contentLocale and fromValue.contentLocale = body.contentLocale ") 330 .append("and fromValue.contentLocale = charset.contentLocale ") 331 .append("and fromValue.contentLocale.identifier = ? ") 332 .toString(); 333 if ( whereClause.length() > 0 ) { 334 hqlPart += "and " + whereClause; 335 } 336 } else { 337 hqlPart = "from MailTemplate template "; 338 if ( whereClause.length() > 0 ) { 339 hqlPart += "where " + whereClause; 340 } 341 } 342 343 if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) { 344 String hqlForTotal = "select count(distinct template.id) " + hqlPart; 346 total = (Integer ) findUniqueResult(hqlForTotal, args.toArray()); 347 if ( total == null ) { 348 total = new Integer (0); 349 } 350 } 351 352 if ( total == null || total.intValue() > 0 ) { 355 String hql = new String (); 356 if ( localeIdentifierPresent ) { 357 hql = "select distinct template, fromValue.simpleValue, subject.simpleValue, charset.simpleValue " + hqlPart + orderByClause; 358 } else { 359 hql = "select distinct template " + hqlPart + orderByClause; 360 } 361 list = executeFind(hql, queryInfo, args.toArray()); 362 if ( total == null ) { 363 total = new Integer (list.size()); 364 } 365 if ( localeIdentifierPresent ) { 366 for ( ListIterator i = list.listIterator(); i.hasNext(); ) { 367 Object [] objects = (Object []) i.next(); 368 MailTemplate mailTemplate = (MailTemplate) objects[0]; 369 mailTemplate.getFrom().put(localeIdentifier, objects[1]); 370 mailTemplate.getSubject().put(localeIdentifier, objects[2]); 371 mailTemplate.getCharset().put(localeIdentifier, objects[3]); 372 i.set(mailTemplate); 373 } 374 } 375 } else { 376 list = new ArrayList (); 377 } 378 379 return new PartialCollection(list, total); 380 381 } 382 383 385 388 public MailTemplate findMailTemplateByIdentifier(String identifier) { 389 String hql = new StringBuffer ("select template, fromValue.contentLocale.identifier, fromValue.simpleValue, subject.simpleValue, charset.simpleValue, body.value ") 390 .append("from MailTemplate as template left outer join ") 391 .append("template.contentFields fromField left outer join fromField.contentFieldValues as fromValue left outer join ") 392 .append("template.contentFields subjectField left outer join subjectField.contentFieldValues as subject ") 393 .append("left outer join template.contentFields bodyField left outer join bodyField.contentFieldValues as body ") 394 .append("left outer join template.contentFields charsetField left outer join charsetField.contentFieldValues as charset ") 395 .append("where ") 396 .append("((fromValue.contentLocale = subject.contentLocale and fromValue.contentLocale = body.contentLocale and fromValue.contentLocale = charset.contentLocale) or (fromValue is null and subject is null and body is null and charset is null)) ") 397 .append("and fromField.identifier = 'from' and charsetField.identifier = 'charset' ") 398 .append("and subjectField.identifier = 'subject' and bodyField.identifier = 'body' and template.identifier = ?") 399 .toString(); 400 401 List list = executeFind(hql, new Object []{identifier}); 404 List result = setLocalizableFields(list); 407 if ( result == null || result.size() <= 0 ) { 408 return null; 409 } else { 410 return (MailTemplate) result.get(0); 411 } 412 } 413 414 415 422 protected MailTemplate findMailTemplateById(List mailMessageTemplates, Long id) { 423 for ( int i = 0; i < mailMessageTemplates.size(); i++ ) { 424 MailTemplate mailTemplate = (MailTemplate) mailMessageTemplates.get(i); 425 if ( mailTemplate.getId().equals(id) ) { 426 return mailTemplate; 427 } 428 } 429 return null; 430 } 431 432 434 440 protected List setLocalizableFields(List queryResult) { 441 if ( queryResult == null || queryResult.size() <= 0 ) { 442 return null; 443 } 444 445 List mailMessageTemplates = new ArrayList (queryResult.size()); 448 for ( int i = 0; i < queryResult.size(); i++ ) { 449 Object [] objects = (Object []) queryResult.get(i); 450 MailTemplate template = (MailTemplate) objects[0]; 451 String locale = (String ) objects[1]; 452 if ( locale != null && locale.trim().length() > 0 ) { 453 template.getFrom().put(locale, objects[2]); 454 template.getSubject().put(locale, objects[3]); 455 template.getCharset().put(locale, objects[4]); 456 template.getBody().put(locale, ConvertUtil.convertToString((byte[]) objects[5])); 457 } 458 mailMessageTemplates.add(template); 459 } 460 461 List fullMailMessageTemplates = new ArrayList (mailMessageTemplates.size()); 465 for ( int i = 0; i < mailMessageTemplates.size(); i++ ) { 466 MailTemplate newMailTemplate = (MailTemplate) mailMessageTemplates.get(i); 467 MailTemplate existingMailTemplate = findMailTemplateById(fullMailMessageTemplates, newMailTemplate.getId()); 468 if ( existingMailTemplate != null ) { 469 Iterator iterator = newMailTemplate.getFrom().keySet().iterator(); 471 while ( iterator.hasNext() ) { 472 String localeIdentifier = (String ) iterator.next(); 473 String value = (String ) newMailTemplate.getFrom().get(localeIdentifier); 474 if ( value == null ) { 475 continue; 476 } 477 existingMailTemplate.getFrom().put(localeIdentifier, value); 478 } 479 480 iterator = newMailTemplate.getSubject().keySet().iterator(); 482 while ( iterator.hasNext() ) { 483 String localeIdentifier = (String ) iterator.next(); 484 String value = (String ) newMailTemplate.getSubject().get(localeIdentifier); 485 if ( value == null ) { 486 continue; 487 } 488 existingMailTemplate.getSubject().put(localeIdentifier, value); 489 } 490 491 iterator = newMailTemplate.getBody().keySet().iterator(); 493 while ( iterator.hasNext() ) { 494 String localeIdentifier = (String ) iterator.next(); 495 Object value = newMailTemplate.getBody().get(localeIdentifier); 496 if ( value == null ) { 497 continue; 498 } 499 existingMailTemplate.getBody().put(localeIdentifier, value); 500 } 501 502 iterator = newMailTemplate.getCharset().keySet().iterator(); 504 while ( iterator.hasNext() ) { 505 String localeIdentifier = (String ) iterator.next(); 506 String value = (String ) newMailTemplate.getCharset().get(localeIdentifier); 507 if ( value == null ) { 508 continue; 509 } 510 existingMailTemplate.getCharset().put(localeIdentifier, value); 511 } 512 513 } else { 514 fullMailMessageTemplates.add(newMailTemplate); 515 } 516 } 517 518 return fullMailMessageTemplates; 519 } 520 521 522 } 523 | Popular Tags |