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.common.util.StringUtil; 21 import com.blandware.atleap.model.core.ContentField; 22 import com.blandware.atleap.model.core.Localizable; 23 import com.blandware.atleap.persistence.core.ContentFieldDAO; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 35 public class ContentFieldDAOHibernate extends BaseDAOHibernate implements ContentFieldDAO { 36 37 39 42 public Long createContentField(ContentField contentField, Localizable localizable) { 43 localizable.addContentField(contentField); 44 return (Long ) getHibernateTemplate().save(contentField); 45 } 46 47 50 public ContentField retrieveContentField(Long contentFieldId) { 51 return (ContentField) getHibernateTemplate().get(ContentField.class, contentFieldId); 52 } 53 54 57 public void updateContentField(ContentField contentField, Localizable localizable) { 58 ContentField oldContentField = localizable.updateContentField(contentField); 59 removeFromCache(oldContentField); 60 getHibernateTemplate().update(contentField); 61 } 62 63 66 public void deleteContentField(ContentField contentField) { 67 getHibernateTemplate().delete(contentField); 68 contentField.getOwner().removeContentField(contentField); 69 } 70 71 73 74 77 public PartialCollection listContentFields(QueryInfo queryInfo) { 78 String whereClause = new String (); 79 String orderByClause = new String (); 80 if ( queryInfo != null ) { 81 whereClause = queryInfo.getWhereClause(); 82 orderByClause = queryInfo.getOrderByClause(); 83 if ( whereClause != null && whereClause.length() != 0 ) { 84 whereClause = " and " + whereClause; 85 } else { 86 whereClause = new String (); 87 } 88 if ( orderByClause != null && orderByClause.length() != 0 ) { 89 orderByClause = " order by " + orderByClause; 90 } else { 91 orderByClause = new String (); 92 } 93 } 94 95 Long ownerId = null; 96 if ( queryInfo != null ) { 97 if ( queryInfo.getQueryParameters() != null ) { 98 ownerId = (Long ) queryInfo.getQueryParameters().get("ownerId"); 99 } 100 } 101 List list = null; 102 Integer total = null; 103 ArrayList args = new ArrayList (); 104 String hqlPart = new String (); 105 if ( ownerId != null ) { 106 hqlPart = "from ContentField f where (f.owner.id = ?) and (f.internal = 'F')" + whereClause; 107 args.add(ownerId); 108 } else { 109 hqlPart = "from ContentField f where (f.internal = 'T')" + whereClause; 110 } 111 if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) { 112 String hqlForTotal = "select count(f.id) " + hqlPart; 113 total = (Integer ) findUniqueResult(hqlForTotal, args.toArray()); 114 if ( total == null ) { 115 total = new Integer (0); 116 } 117 } 118 if ( total == null || total.intValue() > 0 ) { 121 String hql = "select f " + hqlPart + orderByClause; 122 list = executeFind(hql, queryInfo, args.toArray()); 123 if ( total == null ) { 124 total = new Integer (list.size()); 125 } 126 } else { 127 list = new ArrayList (); 128 } 129 return new PartialCollection(list, total); 130 131 } 132 133 136 public boolean hasDuplicates(ContentField contentField, Long ownerId) { 137 138 ArrayList args = new ArrayList (); 139 args.add(contentField.getIdentifier()); 140 args.add(ownerId); 141 142 String hql = "select count(f.id) from ContentField f where f.identifier = ? and f.owner.id = ?"; 143 if ( contentField.getId() != null ) { 144 hql += " and f.id != ?"; 145 args.add(contentField.getId()); 146 } 147 148 int count = ((Integer ) findUniqueResult(hql, args.toArray())).intValue(); 149 150 return count > 0; 151 } 152 153 155 158 public ContentField findContentFieldByLayoutDefinition(String identifier, String definition) { 159 return (ContentField) findUniqueResult("select field from Layout layout inner join layout.contentFields field where layout.definition = ? and field.identifier = ?", new Object []{definition, identifier}); 160 } 161 162 165 public List findIndexedContentFieldsByLayoutDefinitionAndLocale(String identifier, String definition, String locale) { 166 String hql = new StringBuffer ("select distinct field from Layout layout inner join layout.contentFields field inner join field.contentFieldValues value ") 167 .append("where ") 168 .append("field.identifier like '") 169 .append(StringUtil.escape(identifier)).append("[%]' ") 170 .append("and layout.definition = ? ") 171 .append("and (value.contentLocale.identifier = ? or value.contentLocale.defaultInstance = 'T')") 172 .toString(); 173 return executeFind(hql, new Object []{definition, locale}); 174 } 175 176 179 public ContentField findContentFieldByPageUri(String identifier, String uri) { 180 return (ContentField) findUniqueResult("select field from Page page inner join page.contentFields field where page.uri = ? and field.identifier = ?", new Object []{uri, identifier}); 181 } 182 183 186 public List findIndexedContentFieldsByPageUriAndLocale(String identifier, String uri, String locale) { 187 String hql = new StringBuffer ("select distinct field from Page page inner join page.contentFields field inner join field.contentFieldValues value ") 188 .append("where ").append("field.identifier like '") 189 .append(StringUtil.escape(identifier)).append("[%]' ") 190 .append("and page.uri = ? ") 191 .append("and (value.contentLocale.identifier = ? or value.contentLocale.defaultInstance = 'T')") 192 .toString(); 193 return executeFind(hql, new Object []{uri, locale}); 194 } 195 196 199 public ContentField findContentFieldByLocalizable(String identifier, Localizable localizable) { 200 return (ContentField) findUniqueResult("from ContentField f where f.identifier = ? and f.owner = ?", new Object []{identifier, localizable}); 201 } 202 } 203 204 | Popular Tags |