1 16 package com.blandware.atleap.persistence.hibernate.core; 17 18 import com.blandware.atleap.common.util.PartialCollection; 19 import com.blandware.atleap.common.util.QueryInfo; 20 import com.blandware.atleap.model.core.ContentField; 21 import com.blandware.atleap.model.core.ContentFieldValue; 22 import com.blandware.atleap.model.core.ContentLocale; 23 import com.blandware.atleap.model.core.ContentPage; 24 import com.blandware.atleap.model.core.Layout; 25 import com.blandware.atleap.model.core.Role; 26 import com.blandware.atleap.persistence.core.ContentPageDAO; 27 import com.blandware.atleap.persistence.exception.DeleteException; 28 29 import java.util.ArrayList ; 30 import java.util.Date ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.ListIterator ; 34 import java.util.Map ; 35 36 44 public class ContentPageDAOHibernate extends PageDAOHibernate implements ContentPageDAO { 45 46 49 public ContentPageDAOHibernate() { 50 } 51 52 55 public Long createContentPage(ContentPage contentPage, Layout layout) { 56 contentPage.setUsageCounter(new Integer (0)); 57 layout.addContentPage(contentPage); 58 Long contentPageId = (Long ) getHibernateTemplate().save(contentPage); 59 60 ContentField fTitle = new ContentField(); 62 fTitle.setIdentifier("title"); 63 fTitle.setType(ContentField.LINE_TYPE); 64 fTitle.setInternal(Boolean.TRUE); 65 contentPage.addContentField(fTitle); 66 getHibernateTemplate().save(fTitle); 67 68 ContentField fDescription = new ContentField(); 70 fDescription.setIdentifier("description"); 71 fDescription.setType(ContentField.LINE_TYPE); 72 fDescription.setInternal(Boolean.TRUE); 73 contentPage.addContentField(fDescription); 74 getHibernateTemplate().save(fDescription); 75 76 ContentField fKeywords = new ContentField(); 78 fKeywords.setIdentifier("keywords"); 79 fKeywords.setType(ContentField.LINE_TYPE); 80 fKeywords.setInternal(Boolean.TRUE); 81 contentPage.addContentField(fKeywords); 82 getHibernateTemplate().save(fKeywords); 83 84 List contentLocales = executeFind("from ContentLocale l", true, "query.listContentLocales"); 87 for ( int i = 0; i < contentLocales.size(); i++ ) { 88 ContentLocale contentLocale = (ContentLocale) contentLocales.get(i); 89 String localeIdentifier = contentLocale.getIdentifier(); 90 91 String valueTitle = (String ) contentPage.getTitle().get(localeIdentifier); 93 String valueDescription = (String ) contentPage.getDescription().get(localeIdentifier); 94 String valueKeywords = (String ) contentPage.getKeywords().get(localeIdentifier); 95 96 ContentFieldValue fvTitle = new ContentFieldValue(); 98 fvTitle.setContentLocale(contentLocale); 99 if ( valueTitle != null ) { 100 fvTitle.setSimpleValue(valueTitle); 101 } else { 102 fvTitle.setSimpleValue(""); 103 } 104 fvTitle.setLastUpdatedDatetime(new Date ()); 105 fTitle.addContentFieldValue(fvTitle); 106 getHibernateTemplate().save(fvTitle); 107 108 ContentFieldValue fvDescription = new ContentFieldValue(); 110 fvDescription.setContentLocale(contentLocale); 111 if ( valueDescription != null ) { 112 fvDescription.setSimpleValue(valueDescription); 113 } else { 114 fvDescription.setSimpleValue(""); 115 } 116 fvDescription.setLastUpdatedDatetime(new Date ()); 117 fDescription.addContentFieldValue(fvDescription); 118 getHibernateTemplate().save(fvDescription); 119 120 ContentFieldValue fvKeywords = new ContentFieldValue(); 122 fvKeywords.setContentLocale(contentLocale); 123 if ( valueKeywords != null ) { 124 fvKeywords.setSimpleValue(valueKeywords); 125 } else { 126 fvKeywords.setSimpleValue(""); 127 } 128 fvKeywords.setLastUpdatedDatetime(new Date ()); 129 fKeywords.addContentFieldValue(fvKeywords); 130 getHibernateTemplate().save(fvKeywords); 131 } 132 133 return contentPageId; 134 } 135 136 139 public ContentPage retrieveContentPage(Long contentPageId) { 140 String hql = new StringBuffer ("select distinct page, title.contentLocale.identifier, title.simpleValue, description.simpleValue, keywords.simpleValue ") 141 .append("from ContentPage as page left outer join ") 142 .append("page.contentFields titleField left outer join titleField.contentFieldValues as title left outer join ") 143 .append("page.contentFields descriptionField left outer join descriptionField.contentFieldValues as description ") 144 .append("left outer join page.contentFields keywordsField left outer join keywordsField.contentFieldValues as keywords ") 145 .append("where ") 146 .append("title.contentLocale = description.contentLocale and title.contentLocale = keywords.contentLocale ") 147 .append("and titleField.identifier = 'title' ") 148 .append("and descriptionField.identifier = 'description' and keywordsField.identifier = 'keywords' and page.id = ?") 149 .toString(); 150 151 List list = executeFind(hql, new Object []{contentPageId}); 154 List result = setLocalizableFields(list); 157 if ( result == null || result.size() <= 0 ) { 158 return null; 159 } else { 160 return (ContentPage) result.get(0); 161 } 162 } 163 164 167 public void updateContentPage(ContentPage contentPage, Layout layout) { 168 ContentPage old = layout.updateContentPage(contentPage); 169 removeFromCache(old); 170 getHibernateTemplate().update(contentPage); 171 172 for ( int i = 0; i < contentPage.getContentFields().size(); i++ ) { 174 ContentField contentField = (ContentField) contentPage.getContentFields().get(i); 175 Map values = null; 176 if ( "title".equalsIgnoreCase(contentField.getIdentifier()) ) { 177 values = contentPage.getTitle(); 178 } else if ( "description".equalsIgnoreCase(contentField.getIdentifier()) ) { 179 values = contentPage.getDescription(); 180 } else if ( "keywords".equalsIgnoreCase(contentField.getIdentifier()) ) { 181 values = contentPage.getKeywords(); 182 } else { 183 continue; 184 } 185 for ( int j = 0; j < contentField.getContentFieldValues().size(); j++ ) { 186 ContentFieldValue contentFieldValue = (ContentFieldValue) contentField.getContentFieldValues().get(j); 187 String localeIdentifier = contentFieldValue.getContentLocale().getIdentifier(); 188 String value = (String ) values.get(localeIdentifier); 189 if ( value != null ) { 190 contentFieldValue.setSimpleValue(value); 191 } else { 192 contentFieldValue.setSimpleValue(""); 193 } 194 contentFieldValue.setLastUpdatedDatetime(new Date ()); 195 contentFieldValue.getContentField().updateContentFieldValue(contentFieldValue); 196 getHibernateTemplate().update(contentFieldValue); 197 } 198 } 199 } 200 201 204 public void deleteContentPage(ContentPage contentPage) throws DeleteException { 205 getHibernateTemplate().delete(contentPage); 206 contentPage.getLayout().removeContentPage(contentPage); 207 208 List roles = new ArrayList (contentPage.getRoles()); 210 for ( int i = 0; i < roles.size(); i++ ) { 211 Role role = (Role) roles.get(i); 212 contentPage.removeRole(role); 213 } 214 } 215 216 218 221 public PartialCollection listContentPages(QueryInfo queryInfo) { 222 String whereClause = new String (); 223 String orderByClause = new String (); 224 if ( queryInfo != null ) { 225 whereClause = queryInfo.getWhereClause(); 226 orderByClause = queryInfo.getOrderByClause(); 227 if ( whereClause == null || whereClause.length() == 0 ) { 228 whereClause = new String (); 229 } 230 if ( orderByClause != null && orderByClause.length() != 0 ) { 231 orderByClause = " order by " + orderByClause; 232 } else { 233 orderByClause = new String (); 234 } 235 } 236 237 List list = null; 238 Integer total = null; 239 240 String localeIdentifier = null; 241 if ( queryInfo != null ) { 242 if ( queryInfo.getQueryParameters() != null ) { 243 localeIdentifier = (String ) queryInfo.getQueryParameters().get("localeIdentifier"); 244 } 245 } 246 247 boolean localeIdentifierPresent = localeIdentifier != null && localeIdentifier.length() > 0; 248 249 String hqlPart = new String (); 250 ArrayList args = new ArrayList (); 251 if ( localeIdentifierPresent ) { 252 args.add(localeIdentifier); 253 hqlPart = new StringBuffer ("from ContentPage as page ") 254 .append("left outer join page.contentFields as titleField ") 255 .append("left outer join page.contentFields as descriptionField ") 256 .append("left outer join page.contentFields as keywordsField ") 257 .append("left outer join titleField.contentFieldValues as title ") 258 .append("left outer join descriptionField.contentFieldValues as description ") 259 .append("left outer join keywordsField.contentFieldValues as keywords ") 260 .append("left outer join page.roles as role ") 261 .append("where ") 262 .append("titleField.identifier = 'title' and descriptionField.identifier = 'description' and keywordsField.identifier = 'keywords' ") 263 .append("and title.contentLocale = description.contentLocale and title.contentLocale = keywords.contentLocale ") 264 .append("and title.contentLocale.identifier = ? ") 265 .toString(); 266 if ( whereClause.length() > 0 ) { 267 hqlPart += "and " + whereClause; 268 } 269 } else { 270 hqlPart = "from ContentPage page left outer join page.roles as role "; 271 if ( whereClause.length() > 0 ) { 272 hqlPart += "where " + whereClause; 273 } 274 } 275 276 if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) { 277 String hqlForTotal = "select count(distinct page.id) " + hqlPart; 279 total = (Integer ) findUniqueResult(hqlForTotal, args.toArray()); 280 if ( total == null ) { 281 total = new Integer (0); 282 } 283 } 284 285 if ( total == null || total.intValue() > 0 ) { 288 String hql = new String (); 289 if ( localeIdentifierPresent ) { 290 hql = "select distinct page, title.simpleValue, description.simpleValue, keywords.simpleValue " + hqlPart + orderByClause; 291 } else { 292 hql = "select distinct page " + hqlPart + orderByClause; 293 } 294 list = executeFind(hql, queryInfo, args.toArray()); 295 if ( total == null ) { 296 total = new Integer (list.size()); 297 } 298 if ( localeIdentifierPresent ) { 299 for ( ListIterator i = list.listIterator(); i.hasNext(); ) { 302 Object [] objects = (Object []) i.next(); 303 ContentPage contentPage = (ContentPage) objects[0]; 304 contentPage.getTitle().put(localeIdentifier, objects[1]); 305 contentPage.getDescription().put(localeIdentifier, objects[2]); 306 contentPage.getKeywords().put(localeIdentifier, objects[3]); 307 i.set(contentPage); 308 } 309 } 310 } else { 311 list = new ArrayList (); 312 } 313 314 return new PartialCollection(list, total); 315 316 } 317 318 320 323 public ContentPage findContentPageByUri(String contentPageUri) { 324 return (ContentPage) findUniqueResult("from ContentPage page where page.uri = ?", new Object []{contentPageUri}, true, "query.findContentPageByUri"); 325 } 326 327 329 336 protected ContentPage findContentPageById(List contentPages, Long id) { 337 for ( int i = 0; i < contentPages.size(); i++ ) { 338 ContentPage contentPage = (ContentPage) contentPages.get(i); 339 if ( contentPage.getId().equals(id) ) { 340 return contentPage; 341 } 342 } 343 return null; 344 } 345 346 352 protected List setLocalizableFields(List queryResult) { 353 if ( queryResult == null || queryResult.size() <= 0 ) { 354 return null; 355 } 356 357 List contentPages = new ArrayList (queryResult.size()); 360 for ( int i = 0; i < queryResult.size(); i++ ) { 361 Object [] objects = (Object []) queryResult.get(i); 362 ContentPage page = (ContentPage) objects[0]; 363 String locale = (String ) objects[1]; 364 if ( locale != null && locale.trim().length() > 0 ) { 365 page.getTitle().put(locale, objects[2]); 366 page.getDescription().put(locale, objects[3]); 367 page.getKeywords().put(locale, objects[4]); 368 } 369 contentPages.add(page); 370 } 371 372 List fullContentPages = new ArrayList (contentPages.size()); 376 for ( int i = 0; i < contentPages.size(); i++ ) { 377 ContentPage newContentPage = (ContentPage) contentPages.get(i); 378 ContentPage existingContentPage = findContentPageById(fullContentPages, newContentPage.getId()); 379 if ( existingContentPage != null ) { 380 Iterator iterator = newContentPage.getTitle().keySet().iterator(); 382 while ( iterator.hasNext() ) { 383 String localeIdentifier = (String ) iterator.next(); 384 String value = (String ) newContentPage.getTitle().get(localeIdentifier); 385 if ( value == null ) { 386 continue; 387 } 388 existingContentPage.getTitle().put(localeIdentifier, value); 389 } 390 391 iterator = newContentPage.getDescription().keySet().iterator(); 393 while ( iterator.hasNext() ) { 394 String localeIdentifier = (String ) iterator.next(); 395 String value = (String ) newContentPage.getDescription().get(localeIdentifier); 396 if ( value == null ) { 397 continue; 398 } 399 existingContentPage.getDescription().put(localeIdentifier, value); 400 } 401 402 iterator = newContentPage.getKeywords().keySet().iterator(); 404 while ( iterator.hasNext() ) { 405 String localeIdentifier = (String ) iterator.next(); 406 String value = (String ) newContentPage.getKeywords().get(localeIdentifier); 407 if ( value == null ) { 408 continue; 409 } 410 existingContentPage.getKeywords().put(localeIdentifier, value); 411 } 412 413 } else { 414 fullContentPages.add(newContentPage); 415 } 416 } 417 418 return fullContentPages; 419 } 420 421 } 422 | Popular Tags |