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.ContentPage; 22 import com.blandware.atleap.model.core.Page; 23 import com.blandware.atleap.persistence.core.PageDAO; 24 25 import java.util.*; 26 27 34 public class PageDAOHibernate extends LocalizableDAOHibernate implements PageDAO { 35 36 39 public PageDAOHibernate() { 40 } 41 42 45 public Page retrievePage(Long pageId) { 46 return (Page) getHibernateTemplate().get(Page.class, pageId); 47 } 48 49 52 public Page findPageByUri(String uri) { 53 return (Page) findUniqueResult("from Page p where p.uri = ?", new Object []{uri}); 54 } 55 56 59 public void updatePage(Page page) { 60 getHibernateTemplate().update(page); 61 } 62 63 66 public PartialCollection listLinkableItems(QueryInfo queryInfo) { 67 String whereClause = new String (); 68 String orderByClause = new String (); 69 if ( queryInfo != null ) { 70 whereClause = queryInfo.getWhereClause(); 71 orderByClause = queryInfo.getOrderByClause(); 72 if ( whereClause == null || whereClause.length() == 0 ) { 73 whereClause = new String (); 74 } 75 if ( orderByClause != null && orderByClause.length() != 0 ) { 76 orderByClause = " order by " + orderByClause; 77 } else { 78 orderByClause = new String (); 79 } 80 } 81 82 List list = null; 83 Integer total = null; 84 String localeIdentifier = null; 85 if ( queryInfo != null ) { 86 if ( queryInfo.getQueryParameters() != null ) { 87 localeIdentifier = (String ) queryInfo.getQueryParameters().get("localeIdentifier"); 88 } 89 } 90 91 boolean localeIdentifierPresent = localeIdentifier != null && localeIdentifier.length() > 0; 92 93 String hqlPart = new String (); 94 ArrayList args = new ArrayList(); 95 if ( localeIdentifierPresent ) { 96 args.add(localeIdentifier); 97 hqlPart = new StringBuffer ("from Page as page ") 98 .append("left outer join page.contentFields as titleField ") 99 .append("left outer join titleField.contentFieldValues as title ") 100 .append("left outer join page.roles as role ").append("where ((page.className != '").append(ActionPage.class.getName()).append("') and (page.className != '").append(ContentPage.class.getName()).append("')) and ") 101 .append("titleField.identifier = 'title' ") 102 .append("and title.contentLocale.identifier = ? ") 103 .toString(); 104 if ( whereClause.length() > 0 ) { 105 hqlPart += "and " + whereClause; 106 } 107 } else { 108 hqlPart = new StringBuffer ("from Page page left outer join page.roles as role ").append("where ((page.className != '").append(ActionPage.class.getName()).append("') and (page.className != '").append(ContentPage.class.getName()).append("')) ") 109 .toString(); 110 if ( whereClause.length() > 0 ) { 111 hqlPart += "and " + whereClause; 112 } 113 } 114 115 if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) { 116 String hqlForTotal = "select count(distinct page.id) " + hqlPart; 118 total = (Integer ) findUniqueResult(hqlForTotal, args.toArray()); 119 if ( total == null ) { 120 total = new Integer (0); 121 } 122 } 123 124 if ( total == null || total.intValue() > 0 ) { 127 String hql = new String (); 128 if ( localeIdentifierPresent ) { 129 hql = "select distinct page, title.simpleValue " + hqlPart + orderByClause; 130 } else { 131 hql = "select page " + hqlPart + orderByClause; 132 } 133 list = executeFind(hql, queryInfo, args.toArray()); 134 if ( total == null ) { 135 total = new Integer (list.size()); 136 } 137 if ( localeIdentifierPresent ) { 138 for ( ListIterator i = list.listIterator(); i.hasNext(); ) { 139 Object [] objects = (Object []) i.next(); 140 Page page = (Page) objects[0]; 141 page.getTitle().put(localeIdentifier, objects[1]); 142 i.set(page); 143 } 144 } 145 } else { 146 list = new ArrayList(); 147 } 148 149 150 return new PartialCollection(list, total); 151 } 152 153 156 public Collection listPagesFetching() { 157 String hql = new StringBuffer ("from Page as page ") 158 .append("left outer join fetch page.contentFields as field ") 159 .append("left outer join fetch field.contentFieldValues as value where page.active = 'T'").toString(); 160 return new HashSet(executeFind(hql)); 161 } 162 163 166 public boolean hasDuplicates(Page page) { 167 168 ArrayList args = new ArrayList(); 169 args.add(page.getUri()); 170 String hql = "select count(p.id) from Page p where p.uri = ?"; 171 if ( page.getId() != null ) { 172 hql += " and p.id != ?"; 173 args.add(page.getId()); 174 } 175 176 int count = ((Integer ) findUniqueResult(hql, args.toArray())).intValue(); 177 178 return count > 0; 179 180 } 181 182 } 183 | Popular Tags |