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.Localizable; 21 import com.blandware.atleap.model.core.MenuItem; 22 import com.blandware.atleap.model.core.Role; 23 import com.blandware.atleap.persistence.core.MenuDAO; 24 25 import java.util.ArrayList ; 26 import java.util.List ; 27 28 36 public class MenuDAOHibernate extends LocalizableDAOHibernate implements MenuDAO { 37 38 41 public MenuDAOHibernate() { 42 } 43 44 46 49 public Long createMenuItem(MenuItem menuItem, MenuItem parent, Localizable owner) { 50 menuItem.setParentItem(parent); 51 menuItem.setOwner(owner); 52 return (Long ) getHibernateTemplate().save(menuItem); 53 } 54 55 58 public MenuItem retrieveMenuItem(Long menuItemId) { 59 return (MenuItem) getHibernateTemplate().get(MenuItem.class, menuItemId); 60 } 61 62 65 public void updateMenuItem(MenuItem menuItem, MenuItem parent, Localizable owner) { 66 menuItem.setParentItem(parent); 67 menuItem.setOwner(owner); 68 getHibernateTemplate().update(menuItem); 69 } 70 71 74 public void deleteMenuItem(MenuItem menuItem) { 75 getHibernateTemplate().delete(menuItem); 76 77 List roles = new ArrayList (menuItem.getRoles()); 79 for ( int i = 0; i < roles.size(); i++ ) { 80 Role role = (Role) roles.get(i); 81 menuItem.removeRole(role); 82 } 83 } 84 85 87 90 public PartialCollection listMenuItems(QueryInfo queryInfo) { 91 String whereClause = new String (); 92 String orderByClause = " order by item.position asc, item.identifier asc"; 93 if ( queryInfo != null ) { 94 whereClause = queryInfo.getWhereClause(); 95 if ( whereClause != null && whereClause.length() != 0 ) { 96 whereClause = " and " + whereClause; 97 } 98 } 99 100 List list = null; 101 Integer total = null; 102 Long parentItemId = null; 103 Long ownerId = null; 104 if ( queryInfo != null ) { 105 parentItemId = (Long ) queryInfo.getQueryParameters().get("parentItemId"); 106 ownerId = (Long ) queryInfo.getQueryParameters().get("ownerId"); 107 } 108 if ( log.isDebugEnabled() ) { 109 log.debug("Parent ID: " + parentItemId); 110 log.debug("Owner ID: " + ownerId); 111 } 112 String hql = "select distinct item from MenuItem as item left outer join item.roles as role where"; 113 114 ArrayList args = new ArrayList (); 115 116 if ( parentItemId != null ) { 117 hql += " item.parentItem.id = ?"; 118 args.add(parentItemId); 119 } else { 120 hql += " item.parentItem is null"; 121 } 122 123 if ( ownerId != null ) { 124 hql += " and item.owner.id = ?"; 125 args.add(ownerId); 126 } else { 127 hql += " and item.owner is null"; 128 } 129 130 hql += whereClause + orderByClause; 131 132 list = executeFind(hql, args.toArray()); 133 134 if ( list == null ) { 135 list = new ArrayList (); 136 } else { 137 total = new Integer (list.size()); 138 } 139 140 return new PartialCollection(list, total); 141 142 } 143 144 146 149 public MenuItem findMenuItemByIdentifierAndParentAndOwner(String identifier, Long parentItemId, Long ownerId) { 150 151 String hql = "from MenuItem item where item.identifier = ?"; 152 153 ArrayList args = new ArrayList (); 154 155 args.add(identifier); 156 157 String parentHqlPart = " and item.parentItem.id = ?"; 158 if ( parentItemId == null ) { 159 parentHqlPart = " and item.parentItem is null"; 160 } else { 161 args.add(parentItemId); 162 } 163 164 String ownerHqlPart = " and item.owner.id = ?"; 165 if ( ownerId == null ) { 166 ownerHqlPart = " and item.owner is null"; 167 } else { 168 args.add(ownerId); 169 } 170 171 hql += parentHqlPart + ownerHqlPart; 172 173 List menuItems = executeFind(hql, args.toArray()); 174 175 if ( menuItems != null && menuItems.size() > 0 ) { 176 return (MenuItem) menuItems.get(0); 177 } else { 178 return null; 179 } 180 } 181 182 185 public MenuItem findMenuItemByPositionAndParentAndOwner(Integer position, Long parentItemId, Long ownerId) { 186 187 String hql = "from MenuItem item where item.position = ?"; 188 ArrayList args = new ArrayList (); 189 190 args.add(position); 191 192 String parentHqlPart = " and item.parentItem.id = ?"; 193 if ( parentItemId == null ) { 194 parentHqlPart = " and item.parentItem is null"; 195 } else { 196 args.add(parentItemId); 197 } 198 199 String ownerHqlPart = " and item.owner.id = ?"; 200 if ( ownerId == null ) { 201 ownerHqlPart = " and item.owner is null"; 202 } else { 203 args.add(ownerId); 204 } 205 206 hql += parentHqlPart + ownerHqlPart; 207 208 return (MenuItem) findUniqueResult(hql, args.toArray()); 209 210 } 211 212 215 public List findMenuItemsByParentAndOwner(Long parentItemId, Long ownerId) { 216 217 String hql = "from MenuItem item where "; 218 ArrayList args = new ArrayList (); 219 220 221 String parentHqlPart = " item.parentItem.id = ?"; 222 if ( parentItemId == null ) { 223 parentHqlPart = " item.parentItem is null"; 224 } else { 225 args.add(parentItemId); 226 } 227 228 String ownerHqlPart = " and item.owner.id = ?"; 229 if ( ownerId == null ) { 230 ownerHqlPart = " and item.owner is null"; 231 } else { 232 args.add(ownerId); 233 } 234 235 hql += parentHqlPart + ownerHqlPart; 236 237 return executeFind(hql, args.toArray()); 238 } 239 240 241 244 public MenuItem findRedefinitionItem(Long origItemId, Long ownerId) { 245 246 String hql = "from MenuItem item where "; 247 ArrayList args = new ArrayList (); 248 249 String origItemHqlPart = "item.origItem.id = ?"; 250 if ( origItemId == null ) { 251 origItemHqlPart = "item.origItem is null"; 252 } else { 253 args.add(origItemId); 254 } 255 256 String ownerHqlPart = " and item.owner.id = ?"; 257 if ( ownerId == null ) { 258 ownerHqlPart = " and item.owner is null"; 259 } else { 260 args.add(ownerId); 261 } 262 263 hql += origItemHqlPart + ownerHqlPart; 264 265 return (MenuItem) findUniqueResult(hql, args.toArray()); 266 267 } 268 269 271 } 272 | Popular Tags |