| 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.Layout; 21 import com.blandware.atleap.persistence.core.LayoutDAO; 22 import com.blandware.atleap.persistence.exception.DeleteException; 23 import com.blandware.atleap.persistence.exception.UpdateException; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 29 36 public class LayoutDAOHibernate extends LocalizableDAOHibernate implements LayoutDAO { 37 38 41 public LayoutDAOHibernate() { 42 } 43 44 47 public Long createLayout(Layout layout) { 48 return (Long ) getHibernateTemplate().save(layout); 49 } 50 51 54 public Layout retrieveLayout(Long layoutId) { 55 Object o = getHibernateTemplate().get(Layout.class, layoutId); 56 if ( o instanceof Layout ) { 57 return (Layout) o; 58 } else { 59 return null; 60 } 61 } 62 63 66 public void updateLayout(Layout layout) throws UpdateException { 67 String cpDefinition = layout.getCpDefinition(); 68 if (cpDefinition == null || cpDefinition.length() == 0) { 69 String hql = "select p from Layout l, ContentPage p where l.id = ? and p.layout = l"; 70 List list = executeFind(hql, new Object []{layout.getId()}); 71 if ( list != null && list.size() > 0 ) { 72 String errorMessage = "Cannot update layout, because there is one or more content pages, based on it, so cpDefinition cannot be set to empty"; 73 if ( log.isErrorEnabled() ) { 74 log.error(errorMessage); 75 } 76 throw new UpdateException(errorMessage); 77 } 78 } 79 getHibernateTemplate().update(layout); 80 } 81 82 85 public void deleteLayout(Layout layout) throws DeleteException { 86 String hql = "select p from Layout l, ContentPage p where l.id = ? and p.layout = l"; 87 List list = executeFind(hql, new Object []{layout.getId()}); 88 if ( list != null && list.size() > 0 ) { 89 String errorMessage = "Cannot delete layout, because there is one or more content pages, based on it"; 90 if ( log.isErrorEnabled() ) { 91 log.error(errorMessage); 92 } 93 throw new DeleteException(errorMessage); 94 } 95 getHibernateTemplate().delete(layout); 96 } 97 98 101 public PartialCollection listLayouts(QueryInfo queryInfo) { 102 String whereClause = new String (); 103 String orderByClause = new String (); 104 if ( queryInfo != null ) { 105 whereClause = queryInfo.getWhereClause(); 106 orderByClause = queryInfo.getOrderByClause(); 107 if ( whereClause != null && whereClause.length() != 0 ) { 108 whereClause = " where " + whereClause; 109 } else { 110 whereClause = new String (); 111 } 112 if ( orderByClause != null && orderByClause.length() != 0 ) { 113 orderByClause = " order by " + orderByClause; 114 } else { 115 orderByClause = new String (); 116 } 117 } 118 119 List list = null; 120 Integer total = null; 121 String hqlPart = "from Layout l" + whereClause; 122 if ( queryInfo != null && (queryInfo.getLimit() != null || queryInfo.getOffset() != null) ) { 123 String hqlForTotal = "select count(l.id) " + hqlPart; 124 total = (Integer ) findUniqueResult(hqlForTotal); 125 if ( total == null ) { 126 total = new Integer (0); 127 } 128 } 129 if ( total == null || total.intValue() > 0 ) { 132 String hql = "select l " + hqlPart + orderByClause; 133 list = executeFind(hql, queryInfo); 134 if ( total == null ) { 135 total = new Integer (list.size()); 136 } 137 } else { 138 list = new ArrayList (); 139 } 140 return new PartialCollection(list, total); 141 } 142 143 146 public boolean hasDuplicates(Layout layout) { 147 148 ArrayList args = new ArrayList (); 149 args.add(layout.getName()); 150 args.add(layout.getDefinition()); 151 152 String hql = "select count(l.id) from Layout l where (l.name = ? or l.definition = ?)"; 153 if ( layout.getId() != null ) { 154 hql += " and l.id != ?"; 155 args.add(layout.getId()); 156 } 157 158 int count = ((Integer ) findUniqueResult(hql, args.toArray())).intValue(); 159 160 return count > 0; 161 } 162 163 166 public Layout findLayoutByContentPageUri(String contentPageUri) { 167 return (Layout) findUniqueResult("select p.layout from ContentPage p where p.uri = ?", new Object []{contentPageUri}); 168 } 169 170 173 public Layout findLayoutByDefinition(String definitionName) { 174 return (Layout) findUniqueResult("from Layout l where l.definition = ?", new Object []{definitionName}); 175 } 176 177 180 public Layout findLayoutByName(String name) { 181 return (Layout) findUniqueResult("from Layout l where l.name = ?", new Object []{name}); 182 } 183 } 184 | Popular Tags |