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.ActionPage; 21 import com.blandware.atleap.model.core.ContentField; 22 import com.blandware.atleap.model.core.ContentFieldValue; 23 import com.blandware.atleap.model.core.ContentLocale; 24 import com.blandware.atleap.persistence.core.ActionPageDAO; 25 import com.blandware.atleap.persistence.exception.DeleteException; 26 27 import java.util.ArrayList ; 28 import java.util.Date ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.ListIterator ; 32 import java.util.Map ; 33 34 41 42 public class ActionPageDAOHibernate extends PageDAOHibernate implements ActionPageDAO { 43 44 47 public ActionPageDAOHibernate() { 48 } 49 50 52 55 public Long createActionPage(ActionPage actionPage) { 56 actionPage.setUsageCounter(new Integer (0)); 57 Long actionPageId = (Long ) getHibernateTemplate().save(actionPage); 58 59 ContentField fTitle = new ContentField(); 61 fTitle.setIdentifier("title"); 62 fTitle.setType(ContentField.LINE_TYPE); 63 fTitle.setInternal(Boolean.TRUE); 64 actionPage.addContentField(fTitle); 65 getHibernateTemplate().save(fTitle); 66 67 ContentField fDescription = new ContentField(); 69 fDescription.setIdentifier("description"); 70 fDescription.setType(ContentField.LINE_TYPE); 71 fDescription.setInternal(Boolean.TRUE); 72 actionPage.addContentField(fDescription); 73 getHibernateTemplate().save(fDescription); 74 75 ContentField fKeywords = new ContentField(); 77 fKeywords.setIdentifier("keywords"); 78 fKeywords.setType(ContentField.LINE_TYPE); 79 fKeywords.setInternal(Boolean.TRUE); 80 actionPage.addContentField(fKeywords); 81 getHibernateTemplate().save(fKeywords); 82 83 List contentLocales = executeFind("from ContentLocale l", true, "query.listContentLocales"); 86 for ( int i = 0; i < contentLocales.size(); i++ ) { 87 ContentLocale contentLocale = (ContentLocale) contentLocales.get(i); 88 String localeIdentifier = contentLocale.getIdentifier(); 89 90 String valueTitle = (String ) actionPage.getTitle().get(localeIdentifier); 92 String valueDescription = (String ) actionPage.getDescription().get(localeIdentifier); 93 String valueKeywords = (String ) actionPage.getKeywords().get(localeIdentifier); 94 95 ContentFieldValue fvTitle = new ContentFieldValue(); 97 fvTitle.setContentLocale(contentLocale); 98 if ( valueTitle != null ) { 99 fvTitle.setSimpleValue(valueTitle); 100 } else { 101 fvTitle.setSimpleValue(""); 102 } 103 fvTitle.setLastUpdatedDatetime(new Date ()); 104 fTitle.addContentFieldValue(fvTitle); 105 getHibernateTemplate().save(fvTitle); 106 107 ContentFieldValue fvDescription = new ContentFieldValue(); 109 fvDescription.setContentLocale(contentLocale); 110 if ( valueDescription != null ) { 111 fvDescription.setSimpleValue(valueDescription); 112 } else { 113 fvDescription.setSimpleValue(""); 114 } 115 fvDescription.setLastUpdatedDatetime(new Date ()); 116 fDescription.addContentFieldValue(fvDescription); 117 getHibernateTemplate().save(fvDescription); 118 119 ContentFieldValue fvKeywords = new ContentFieldValue(); 121 fvKeywords.setContentLocale(contentLocale); 122 if ( valueKeywords != null ) { 123 fvKeywords.setSimpleValue(valueKeywords); 124 } else { 125 fvKeywords.setSimpleValue(""); 126 } 127 fvKeywords.setLastUpdatedDatetime(new Date ()); 128 fKeywords.addContentFieldValue(fvKeywords); 129 getHibernateTemplate().save(fvKeywords); 130 } 131 132 return actionPageId; 133 134 } 135 136 139 public ActionPage retrieveActionPage(Long actionPageId) { 140 String hql = new StringBuffer ("select distinct page, title.contentLocale.identifier, title.simpleValue, description.simpleValue, keywords.simpleValue ") 141 .append("from ActionPage 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) or (title is null and description is null and keywords is null)) ") 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 []{actionPageId}); 154 List result = setLocalizableFields(list); 157 if ( result == null || result.size() <= 0 ) { 158 return null; 159 } else { 160 return (ActionPage) result.get(0); 161 } 162 } 163 164 167 public void updateActionPage(ActionPage actionPage) { 168 getHibernateTemplate().update(actionPage); 169 for ( int i = 0; i < actionPage.getContentFields().size(); i++ ) { 171 ContentField contentField = (ContentField) actionPage.getContentFields().get(i); 172 Map values = null; 173 if ( "title".equalsIgnoreCase(contentField.getIdentifier()) ) { 174 values = actionPage.getTitle(); 175 } else if ( "description".equalsIgnoreCase(contentField.getIdentifier()) ) { 176 values = actionPage.getDescription(); 177 } else if ( "keywords".equalsIgnoreCase(contentField.getIdentifier()) ) { 178 values = actionPage.getKeywords(); 179 } else { 180 continue; 181 } 182 for ( int j = 0; j < contentField.getContentFieldValues().size(); j++ ) { 183 ContentFieldValue contentFieldValue = (ContentFieldValue) contentField.getContentFieldValues().get(j); 184 String localeIdentifier = contentFieldValue.getContentLocale().getIdentifier(); 185 String value = (String ) values.get(localeIdentifier); 186 if ( value != null ) { 187 contentFieldValue.setSimpleValue(value); 188 } else { 189 contentFieldValue.setSimpleValue(""); 190 } 191 contentFieldValue.setLastUpdatedDatetime(new Date ()); 192 contentFieldValue.getContentField().updateContentFieldValue(contentFieldValue); 193 getHibernateTemplate().update(contentFieldValue); 194 } 195 } 196 } 197 198 201 public void deleteActionPage(ActionPage actionPage) throws DeleteException { 202 getHibernateTemplate().delete(actionPage); 203 } 204 205 207 210 public PartialCollection listActionPages(QueryInfo queryInfo) { 211 String whereClause = new String (); 212 String orderByClause = new String (); 213 if ( queryInfo != null ) { 214 whereClause = queryInfo.getWhereClause(); 215 orderByClause = queryInfo.getOrderByClause(); 216 if ( whereClause == null || whereClause.length() == 0 ) { 217 whereClause = new String (); 218 } 219 if ( orderByClause != null && orderByClause.length() != 0 ) { 220 orderByClause = " order by " + orderByClause; 221 } else { 222 orderByClause = new String (); 223 } 224 } 225 226 List list = null; 227 Integer total = null; 228 229 String localeIdentifier = null; 230 if ( queryInfo != null ) { 231 if ( queryInfo.getQueryParameters() != null ) { 232 localeIdentifier = (String ) queryInfo.getQueryParameters().get("localeIdentifier"); 233 } 234 } 235 236 boolean localeIdentifierPresent = localeIdentifier != null && localeIdentifier.length() > 0; 237 238 String hqlPart = new String (); 239 ArrayList args = new ArrayList (); 240 if ( localeIdentifierPresent ) { 241 args.add(localeIdentifier); 242 hqlPart = new StringBuffer ("from ActionPage as page ") 243 .append("left outer join page.contentFields as titleField ") 244 .append("left outer join page.contentFields as descriptionField ") 245 .append("left outer join page.contentFields as keywordsField ") 246 .append("left outer join titleField.contentFieldValues as title ") 247 .append("left outer join descriptionField.contentFieldValues as description ") 248 .append("left outer join keywordsField.contentFieldValues as keywords ") 249 .append("where ") 250 .append("titleField.identifier = 'title' and descriptionField.identifier = 'description' and keywordsField.identifier = 'keywords' ") 251 .append("and title.contentLocale = description.contentLocale and title.contentLocale = keywords.contentLocale ") 252 .append("and title.contentLocale.identifier = ? ") 253 .toString(); 254 if ( whereClause.length() > 0 ) { 255 hqlPart += "and " + whereClause; 256 } 257 } else { 258 hqlPart = "from ActionPage page "; 259 if ( whereClause.length() > 0 ) { 260 hqlPart += "where " + whereClause; 261 } 262 } 263 264 if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) { 265 String hqlForTotal = "select count(distinct page.id) " + hqlPart; 267 total = (Integer ) findUniqueResult(hqlForTotal, args.toArray()); 268 if ( total == null ) { 269 total = new Integer (0); 270 } 271 } 272 273 if ( total == null || total.intValue() > 0 ) { 276 String hql = new String (); 277 if ( localeIdentifierPresent ) { 278 hql = "select distinct page, title.simpleValue, description.simpleValue, keywords.simpleValue " + hqlPart + orderByClause; 279 } else { 280 hql = "select page " + hqlPart + orderByClause; 281 } 282 list = executeFind(hql, queryInfo, args.toArray()); 283 if ( total == null ) { 284 total = new Integer (list.size()); 285 } 286 if ( localeIdentifierPresent ) { 287 for ( ListIterator i = list.listIterator(); i.hasNext(); ) { 290 Object [] objects = (Object []) i.next(); 291 ActionPage actionPage = (ActionPage) objects[0]; 292 actionPage.getTitle().put(localeIdentifier, objects[1]); 293 actionPage.getDescription().put(localeIdentifier, objects[2]); 294 actionPage.getKeywords().put(localeIdentifier, objects[3]); 295 i.set(actionPage); 296 } 297 } 298 } else { 299 list = new ArrayList (); 300 } 301 302 return new PartialCollection(list, total); 303 304 } 305 307 310 public ActionPage findActionPageByUri(String actionPageUri) { 311 return (ActionPage) findUniqueResult("from ActionPage page where page.uri = ?", new Object []{actionPageUri}); 312 } 313 314 316 323 protected ActionPage findActionPageById(List actionPages, Long id) { 324 for ( int i = 0; i < actionPages.size(); i++ ) { 325 ActionPage actionPage = (ActionPage) actionPages.get(i); 326 if ( actionPage.getId().equals(id) ) { 327 return actionPage; 328 } 329 } 330 return null; 331 } 332 333 339 protected List setLocalizableFields(List queryResult) { 340 if ( queryResult == null || queryResult.size() <= 0 ) { 341 return null; 342 } 343 344 List actionPages = new ArrayList (queryResult.size()); 347 for ( int i = 0; i < queryResult.size(); i++ ) { 348 Object [] objects = (Object []) queryResult.get(i); 349 ActionPage page = (ActionPage) objects[0]; 350 String locale = (String ) objects[1]; 351 if ( locale != null && locale.trim().length() > 0 ) { 352 page.getTitle().put(locale, objects[2]); 353 page.getDescription().put(locale, objects[3]); 354 page.getKeywords().put(locale, objects[4]); 355 } 356 actionPages.add(page); 357 } 358 359 List fullActionPages = new ArrayList (actionPages.size()); 363 for ( int i = 0; i < actionPages.size(); i++ ) { 364 ActionPage newActionPage = (ActionPage) actionPages.get(i); 365 ActionPage existingActionPage = findActionPageById(fullActionPages, newActionPage.getId()); 366 if ( existingActionPage != null ) { 367 Iterator iterator = newActionPage.getTitle().keySet().iterator(); 369 while ( iterator.hasNext() ) { 370 String localeIdentifier = (String ) iterator.next(); 371 String value = (String ) newActionPage.getTitle().get(localeIdentifier); 372 if ( value == null ) { 373 continue; 374 } 375 existingActionPage.getTitle().put(localeIdentifier, value); 376 } 377 378 iterator = newActionPage.getDescription().keySet().iterator(); 380 while ( iterator.hasNext() ) { 381 String localeIdentifier = (String ) iterator.next(); 382 String value = (String ) newActionPage.getDescription().get(localeIdentifier); 383 if ( value == null ) { 384 continue; 385 } 386 existingActionPage.getDescription().put(localeIdentifier, value); 387 } 388 389 iterator = newActionPage.getKeywords().keySet().iterator(); 391 while ( iterator.hasNext() ) { 392 String localeIdentifier = (String ) iterator.next(); 393 String value = (String ) newActionPage.getKeywords().get(localeIdentifier); 394 if ( value == null ) { 395 continue; 396 } 397 existingActionPage.getKeywords().put(localeIdentifier, value); 398 } 399 400 } else { 401 fullActionPages.add(newActionPage); 402 } 403 } 404 405 return fullActionPages; 406 } 407 408 } 409 | Popular Tags |